No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimelineMarkerHeaderGUI.cs 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.Timeline.Actions;
  5. using UnityEngine;
  6. using UnityEngine.Timeline;
  7. using Object = UnityEngine.Object;
  8. namespace UnityEditor.Timeline
  9. {
  10. class TimelineMarkerHeaderGUI : IRowGUI, ILayerable
  11. {
  12. static readonly GUIContent k_Muted = L10n.TextContent("Muted");
  13. int m_TrackHash;
  14. TimelineAsset timeline { get; }
  15. WindowState state { get; }
  16. MarkersLayer m_Layer;
  17. LayerZOrder m_ZOrder = new LayerZOrder(Layer.MarkerHeaderTrack, 0);
  18. struct DrawData
  19. {
  20. public Rect headerRect;
  21. public Rect contentRect;
  22. public GUIStyle trackHeaderFont;
  23. public Color colorTrackFont;
  24. public bool isMuted;
  25. public bool isSelected;
  26. }
  27. public TimelineMarkerHeaderGUI(TimelineAsset asset, WindowState state)
  28. {
  29. m_TrackHash = -1;
  30. timeline = asset;
  31. this.state = state;
  32. }
  33. public TrackAsset asset => timeline.markerTrack;
  34. public Rect boundingRect { get; private set; }
  35. public bool showMarkers => state.showMarkerHeader;
  36. public bool muted => timeline.markerTrack != null && timeline.markerTrack.muted;
  37. public bool locked => timeline.markerTrack.locked;
  38. public LayerZOrder zOrder => m_ZOrder;
  39. Rect IRowGUI.ToWindowSpace(Rect rect)
  40. {
  41. //header gui is already in global coordinates
  42. return rect;
  43. }
  44. public void Draw(Rect markerHeaderRect, Rect markerContentRect, WindowState state)
  45. {
  46. boundingRect = markerContentRect;
  47. var data = new DrawData
  48. {
  49. headerRect = markerHeaderRect,
  50. contentRect = markerContentRect,
  51. trackHeaderFont = DirectorStyles.Instance.trackHeaderFont,
  52. colorTrackFont = DirectorStyles.Instance.customSkin.colorTrackFont,
  53. isMuted = muted,
  54. isSelected = IsSelected()
  55. };
  56. if (state.showMarkerHeader)
  57. {
  58. DrawMarkerDrawer(data);
  59. if (Event.current.type == EventType.Repaint)
  60. state.spacePartitioner.AddBounds(this, boundingRect);
  61. }
  62. if (asset != null && Hash() != m_TrackHash)
  63. Rebuild();
  64. Rect rect = state.showMarkerHeader ? markerContentRect : state.timeAreaRect;
  65. using (new GUIViewportScope(rect))
  66. {
  67. if (m_Layer != null)
  68. m_Layer.Draw(rect, state);
  69. HandleDragAndDrop();
  70. }
  71. if (state.showMarkerHeader && data.isMuted)
  72. DrawMuteOverlay(data);
  73. }
  74. public void Rebuild()
  75. {
  76. if (asset == null)
  77. return;
  78. m_Layer = new MarkersLayer(Layer.MarkersOnHeader, this);
  79. m_TrackHash = Hash();
  80. }
  81. void HandleDragAndDrop()
  82. {
  83. if (state.editSequence.isReadOnly || !state.showMarkerHeader)
  84. return;
  85. if (Event.current == null || Event.current.type != EventType.DragUpdated &&
  86. Event.current.type != EventType.DragPerform && Event.current.type != EventType.DragExited)
  87. return;
  88. var objectsBeingDropped = DragAndDrop.objectReferences.OfType<Object>();
  89. var candidateTime = TimelineHelpers.GetCandidateTime(Event.current.mousePosition);
  90. var perform = Event.current.type == EventType.DragPerform;
  91. var director = state.editSequence != null ? state.editSequence.director : null;
  92. DragAndDrop.visualMode = TimelineDragging.HandleClipPaneObjectDragAndDrop(objectsBeingDropped, timeline.markerTrack, perform,
  93. timeline, null, director, candidateTime, ResolveType);
  94. if (perform && DragAndDrop.visualMode == DragAndDropVisualMode.Copy)
  95. {
  96. DragAndDrop.AcceptDrag();
  97. }
  98. }
  99. static bool ResolveType(IEnumerable<Type> types, Action<Type> onComplete, string formatString)
  100. {
  101. void CreateMarkerTrackOnComplete(Type type)
  102. {
  103. WindowState state = TimelineWindow.instance.state;
  104. state.editSequence.asset.CreateMarkerTrack();
  105. state.showMarkerHeader = true;
  106. onComplete(type);
  107. }
  108. return TimelineDragging.ResolveType(types, CreateMarkerTrackOnComplete, formatString);
  109. }
  110. int Hash()
  111. {
  112. return timeline.markerTrack == null ? 0 : timeline.markerTrack.Hash();
  113. }
  114. static void DrawMarkerDrawer(DrawData data)
  115. {
  116. DrawMarkerDrawerHeaderBackground(data);
  117. DrawMarkerDrawerHeader(data);
  118. DrawMarkerDrawerContentBackground(data);
  119. }
  120. static void DrawMarkerDrawerHeaderBackground(DrawData data)
  121. {
  122. Color backgroundColor = data.isSelected
  123. ? DirectorStyles.Instance.customSkin.colorSelection
  124. : DirectorStyles.Instance.customSkin.markerHeaderDrawerBackgroundColor;
  125. EditorGUI.DrawRect(data.headerRect, backgroundColor);
  126. }
  127. static void DrawMarkerDrawerHeader(DrawData data)
  128. {
  129. var textStyle = data.trackHeaderFont;
  130. textStyle.normal.textColor = data.colorTrackFont;
  131. var labelRect = data.headerRect;
  132. labelRect.x += DirectorStyles.kBaseIndent;
  133. EditorGUI.LabelField(labelRect, DirectorStyles.timelineMarkerTrackHeader);
  134. const float buttonSize = WindowConstants.trackHeaderButtonSize;
  135. const float padding = WindowConstants.trackHeaderButtonPadding;
  136. var x = data.headerRect.xMax - buttonSize - padding - 2f;
  137. var y = data.headerRect.y + (data.headerRect.height - buttonSize) / 2.0f;
  138. var buttonRect = new Rect(x, y, buttonSize, buttonSize);
  139. DrawTrackDropDownMenu(buttonRect);
  140. buttonRect.x -= 21.0f;
  141. DrawMuteButton(buttonRect, data);
  142. }
  143. static void DrawMarkerDrawerContentBackground(DrawData data)
  144. {
  145. Color trackBackgroundColor = DirectorStyles.Instance.customSkin.markerDrawerBackgroundColor;
  146. if (data.isSelected)
  147. trackBackgroundColor = DirectorStyles.Instance.customSkin.colorTrackBackgroundSelected;
  148. EditorGUI.DrawRect(data.contentRect, trackBackgroundColor);
  149. }
  150. static void DrawMuteOverlay(DrawData data)
  151. {
  152. DirectorStyles styles = TimelineWindow.styles;
  153. var colorOverlay = OverlayDrawer.CreateColorOverlay(GUIClip.Unclip(data.contentRect), styles.customSkin.colorTrackDarken);
  154. colorOverlay.Draw();
  155. Rect textRect = Graphics.CalculateTextBoxSize(data.contentRect, styles.fontClip, k_Muted, WindowConstants.overlayTextPadding);
  156. var boxOverlay = OverlayDrawer.CreateTextBoxOverlay(
  157. GUIClip.Unclip(textRect),
  158. k_Muted.text,
  159. styles.fontClip,
  160. Color.white,
  161. styles.customSkin.colorLockTextBG,
  162. styles.displayBackground);
  163. boxOverlay.Draw();
  164. }
  165. static void DrawTrackDropDownMenu(Rect rect)
  166. {
  167. if (GUI.Button(rect, GUIContent.none, DirectorStyles.Instance.trackOptions))
  168. {
  169. SelectionManager.SelectOnly(TimelineEditor.inspectedAsset.markerTrack);
  170. SequencerContextMenu.ShowTrackContextMenu(null);
  171. }
  172. }
  173. static void DrawMuteButton(Rect rect, DrawData data)
  174. {
  175. bool muted = GUI.Toggle(rect, data.isMuted, string.Empty, TimelineWindow.styles.trackMuteButton);
  176. if (muted != data.isMuted)
  177. new[] { TimelineEditor.inspectedAsset.markerTrack }.Invoke<MuteTrack>();
  178. }
  179. bool IsSelected()
  180. {
  181. return SelectionManager.Contains(asset);
  182. }
  183. }
  184. }