MovingItems.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. class MovingItems : ItemsPerTrack
  8. {
  9. TrackAsset m_ReferenceTrack;
  10. readonly bool m_AllowTrackSwitch;
  11. readonly Rect[] m_ItemsBoundsOnTrack;
  12. readonly Vector2[] m_ItemsMouseOffsets;
  13. static readonly Rect s_InvisibleBounds = new Rect(float.MaxValue, float.MaxValue, 0.0f, 0.0f);
  14. public TrackAsset originalTrack { get; }
  15. public override TrackAsset targetTrack
  16. {
  17. get
  18. {
  19. if (m_AllowTrackSwitch)
  20. return m_ReferenceTrack;
  21. return originalTrack;
  22. }
  23. }
  24. public bool canDrop;
  25. public double start
  26. {
  27. get { return m_ItemsGroup.start; }
  28. set { m_ItemsGroup.start = value; }
  29. }
  30. public double end
  31. {
  32. get { return m_ItemsGroup.end; }
  33. }
  34. public Rect[] onTrackItemsBounds
  35. {
  36. get { return m_ItemsBoundsOnTrack; }
  37. }
  38. public MovingItems(WindowState state, TrackAsset parentTrack, ITimelineItem[] items, TrackAsset referenceTrack, Vector2 mousePosition, bool allowTrackSwitch)
  39. : base(parentTrack, items)
  40. {
  41. originalTrack = parentTrack;
  42. m_ReferenceTrack = referenceTrack;
  43. m_AllowTrackSwitch = allowTrackSwitch;
  44. m_ItemsBoundsOnTrack = new Rect[items.Length];
  45. m_ItemsMouseOffsets = new Vector2[items.Length];
  46. for (int i = 0; i < items.Length; ++i)
  47. {
  48. var itemGUi = items[i].gui;
  49. if (itemGUi != null)
  50. {
  51. m_ItemsBoundsOnTrack[i] = itemGUi.rect;
  52. m_ItemsMouseOffsets[i] = mousePosition - m_ItemsBoundsOnTrack[i].position;
  53. }
  54. }
  55. canDrop = true;
  56. }
  57. public void SetReferenceTrack(TrackAsset track)
  58. {
  59. m_ReferenceTrack = track;
  60. }
  61. public bool HasAnyDetachedParents()
  62. {
  63. return m_ItemsGroup.items.Any(x => x.parentTrack == null);
  64. }
  65. public void RefreshBounds(WindowState state, Vector2 mousePosition)
  66. {
  67. for (int i = 0; i < m_ItemsGroup.items.Length; ++i)
  68. {
  69. var item = m_ItemsGroup.items[i];
  70. var itemGUI = item.gui;
  71. if (item.parentTrack != null)
  72. {
  73. m_ItemsBoundsOnTrack[i] = itemGUI.visible ? itemGUI.rect : s_InvisibleBounds;
  74. }
  75. else
  76. {
  77. if (targetTrack != null)
  78. {
  79. var trackGUI = (TimelineTrackGUI)TimelineWindow.instance.allTracks.FirstOrDefault(t => t.track == targetTrack);
  80. if (trackGUI == null) return;
  81. var trackRect = trackGUI.boundingRect;
  82. m_ItemsBoundsOnTrack[i] = itemGUI.RectToTimeline(trackRect, state);
  83. }
  84. else
  85. {
  86. m_ItemsBoundsOnTrack[i].position = mousePosition - m_ItemsMouseOffsets[i];
  87. }
  88. }
  89. }
  90. }
  91. public void Draw(bool isValid)
  92. {
  93. for (int i = 0; i < m_ItemsBoundsOnTrack.Length; ++i)
  94. {
  95. var rect = m_ItemsBoundsOnTrack[i];
  96. DrawItemInternal(m_ItemsGroup.items[i], rect, isValid);
  97. }
  98. }
  99. static void DrawItemInternal(ITimelineItem item, Rect rect, bool isValid)
  100. {
  101. var clipGUI = item.gui as TimelineClipGUI;
  102. if (clipGUI != null)
  103. {
  104. if (isValid)
  105. {
  106. clipGUI.DrawGhostClip(rect);
  107. }
  108. else
  109. {
  110. clipGUI.DrawInvalidClip(rect);
  111. }
  112. }
  113. }
  114. }
  115. }