TimelineMarkerHeaderGUI.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Linq;
  3. using UnityEditor.Timeline.Actions;
  4. using UnityEngine;
  5. using UnityEngine.Timeline;
  6. using Object = UnityEngine.Object;
  7. namespace UnityEditor.Timeline
  8. {
  9. class TimelineMarkerHeaderGUI : IRowGUI, ILayerable
  10. {
  11. int m_TrackHash;
  12. TimelineAsset timeline { get; }
  13. WindowState state { get; }
  14. MarkersLayer m_Layer;
  15. LayerZOrder m_ZOrder = new LayerZOrder(Layer.MarkerHeaderTrack, 0);
  16. struct DrawData
  17. {
  18. public Rect headerRect;
  19. public Rect contentRect;
  20. public GUIStyle trackHeaderFont;
  21. public Color colorTrackFont;
  22. public bool showLockButton;
  23. public bool showMuteButton;
  24. }
  25. public TimelineMarkerHeaderGUI(TimelineAsset asset, WindowState state)
  26. {
  27. m_TrackHash = -1;
  28. timeline = asset;
  29. this.state = state;
  30. }
  31. public TrackAsset asset { get { return timeline.markerTrack; } }
  32. public Rect boundingRect { get; private set; }
  33. public bool locked { get { return !state.showMarkerHeader; } }
  34. public bool showMarkers
  35. {
  36. get { return state.showMarkerHeader; }
  37. }
  38. public bool muted
  39. {
  40. get { return timeline.markerTrack != null && timeline.markerTrack.muted; }
  41. }
  42. Rect IRowGUI.ToWindowSpace(Rect rect)
  43. {
  44. //header gui is already in global coordinates
  45. return rect;
  46. }
  47. public void Draw(Rect markerHeaderRect, Rect markerContentRect, WindowState state)
  48. {
  49. boundingRect = markerContentRect;
  50. var data = new DrawData()
  51. {
  52. headerRect = markerHeaderRect,
  53. contentRect = markerContentRect,
  54. trackHeaderFont = DirectorStyles.Instance.trackHeaderFont,
  55. colorTrackFont = DirectorStyles.Instance.customSkin.colorTrackFont,
  56. showLockButton = locked,
  57. showMuteButton = muted
  58. };
  59. if (state.showMarkerHeader)
  60. {
  61. DrawMarkerDrawer(data, state);
  62. if (Event.current.type == EventType.Repaint)
  63. state.spacePartitioner.AddBounds(this, boundingRect);
  64. }
  65. if (asset != null && Hash() != m_TrackHash)
  66. Rebuild();
  67. var rect = state.showMarkerHeader ? markerContentRect : state.timeAreaRect;
  68. using (new GUIViewportScope(rect))
  69. {
  70. if (m_Layer != null)
  71. m_Layer.Draw(rect, state);
  72. HandleDragAndDrop();
  73. }
  74. }
  75. public void Rebuild()
  76. {
  77. if (asset == null)
  78. return;
  79. m_Layer = new MarkersLayer(Layer.MarkersOnHeader, this);
  80. m_TrackHash = Hash();
  81. }
  82. void HandleDragAndDrop()
  83. {
  84. if (TimelineWindow.instance.state.editSequence.isReadOnly)
  85. return;
  86. if (Event.current == null || Event.current.type != EventType.DragUpdated &&
  87. Event.current.type != EventType.DragPerform && Event.current.type != EventType.DragExited)
  88. return;
  89. timeline.CreateMarkerTrack(); // Ensure Marker track is created.
  90. var objectsBeingDropped = DragAndDrop.objectReferences.OfType<Object>();
  91. var candidateTime = TimelineHelpers.GetCandidateTime(Event.current.mousePosition);
  92. var perform = Event.current.type == EventType.DragPerform;
  93. var director = state.editSequence != null ? state.editSequence.director : null;
  94. DragAndDrop.visualMode = TimelineDragging.HandleClipPaneObjectDragAndDrop(objectsBeingDropped, timeline.markerTrack, perform,
  95. timeline, null, director, candidateTime, TimelineDragging.ResolveType);
  96. if (perform && DragAndDrop.visualMode == DragAndDropVisualMode.Copy)
  97. {
  98. DragAndDrop.AcceptDrag();
  99. }
  100. }
  101. int Hash()
  102. {
  103. return timeline.markerTrack == null ? 0 : timeline.markerTrack.Hash();
  104. }
  105. static void DrawMarkerDrawer(DrawData data, WindowState state)
  106. {
  107. DrawMarkerDrawerHeaderBackground(data);
  108. DrawMarkerDrawerHeader(data, state);
  109. DrawMarkerDrawerContentBackground(data);
  110. }
  111. static void DrawMarkerDrawerHeaderBackground(DrawData data)
  112. {
  113. var backgroundColor = DirectorStyles.Instance.customSkin.markerHeaderDrawerBackgroundColor;
  114. var bgRect = data.headerRect;
  115. EditorGUI.DrawRect(bgRect, backgroundColor);
  116. }
  117. static void DrawMarkerDrawerHeader(DrawData data, WindowState state)
  118. {
  119. var textStyle = data.trackHeaderFont;
  120. textStyle.normal.textColor = data.colorTrackFont;
  121. var labelRect = data.headerRect;
  122. labelRect.x += DirectorStyles.kBaseIndent;
  123. EditorGUI.LabelField(labelRect, DirectorStyles.timelineMarkerTrackHeader);
  124. const float buttonSize = WindowConstants.trackHeaderButtonSize;
  125. const float padding = WindowConstants.trackHeaderButtonPadding;
  126. var x = data.headerRect.xMax - buttonSize - padding - 2f;
  127. var y = data.headerRect.y + (data.headerRect.height - buttonSize) / 2.0f;
  128. var buttonRect = new Rect(x, y, buttonSize, buttonSize);
  129. DrawTrackDropDownMenu(buttonRect, state);
  130. buttonRect.x -= 16.0f;
  131. if (data.showMuteButton)
  132. {
  133. DrawMuteButton(buttonRect);
  134. buttonRect.x -= 16.0f;
  135. }
  136. if (data.showLockButton)
  137. {
  138. DrawLockButton(buttonRect);
  139. }
  140. }
  141. static void DrawMarkerDrawerContentBackground(DrawData data)
  142. {
  143. var trackBackgroundColor = DirectorStyles.Instance.customSkin.markerDrawerBackgroundColor;
  144. EditorGUI.DrawRect(data.contentRect, trackBackgroundColor);
  145. }
  146. static void DrawLockButton(Rect rect)
  147. {
  148. if (GUI.Button(rect, GUIContent.none, TimelineWindow.styles.trackLockButton))
  149. Invoker.InvokeWithSelected<ToggleShowMarkersOnTimeline>();
  150. }
  151. static void DrawTrackDropDownMenu(Rect rect, WindowState state)
  152. {
  153. if (GUI.Button(rect, GUIContent.none, DirectorStyles.Instance.trackOptions))
  154. SequencerContextMenu.ShowMarkerHeaderContextMenu(null, state);
  155. }
  156. static void DrawMuteButton(Rect rect)
  157. {
  158. if (GUI.Button(rect, GUIContent.none, TimelineWindow.styles.markerHeaderMuteButton))
  159. Invoker.InvokeWithSelected<ToggleMuteMarkersOnTimeline>();
  160. }
  161. public LayerZOrder zOrder => m_ZOrder;
  162. }
  163. }