TimelineMarkerGUI.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. using UnityObject = UnityEngine.Object;
  6. namespace UnityEditor.Timeline
  7. {
  8. class TimelineMarkerGUI : TimelineItemGUI, ISnappable, IAttractable
  9. {
  10. public event Action onStartDrag;
  11. int m_ProjectedClipHash;
  12. int m_MarkerHash;
  13. bool m_Selectable;
  14. MarkerDrawOptions m_MarkerDrawOptions;
  15. MarkerEditor m_Editor;
  16. IMarker marker { get; }
  17. bool selectable
  18. {
  19. get { return m_Selectable; }
  20. }
  21. public double time
  22. {
  23. get { return marker.time; }
  24. }
  25. public override double start
  26. {
  27. get { return time; }
  28. }
  29. public override double end
  30. {
  31. get { return time; }
  32. }
  33. public override void Select()
  34. {
  35. zOrder = zOrderProvider.Next();
  36. SelectionManager.Add(marker);
  37. TimelineWindowViewPrefs.GetTrackViewModelData(parent.asset).markerTimeStamps[m_MarkerHash] = DateTime.UtcNow.Ticks;
  38. }
  39. public override bool IsSelected()
  40. {
  41. return SelectionManager.Contains(marker);
  42. }
  43. public override void Deselect()
  44. {
  45. SelectionManager.Remove(marker);
  46. }
  47. public override ITimelineItem item
  48. {
  49. get { return ItemsUtils.ToItem(marker); }
  50. }
  51. IZOrderProvider zOrderProvider { get; }
  52. public TimelineMarkerGUI(IMarker theMarker, IRowGUI parent, IZOrderProvider provider) : base(parent)
  53. {
  54. marker = theMarker;
  55. m_Selectable = marker.GetType().IsSubclassOf(typeof(UnityObject));
  56. m_MarkerHash = 0;
  57. var o = marker as object;
  58. if (!o.Equals(null))
  59. m_MarkerHash = o.GetHashCode();
  60. zOrderProvider = provider;
  61. zOrder = zOrderProvider.Next();
  62. ItemToItemGui.Add(marker, this);
  63. m_Editor = CustomTimelineEditorCache.GetMarkerEditor(theMarker);
  64. }
  65. int ComputeDirtyHash()
  66. {
  67. return time.GetHashCode();
  68. }
  69. static void DrawMarker(Rect drawRect, Type type, bool isSelected, bool isCollapsed, MarkerDrawOptions options)
  70. {
  71. if (Event.current.type == EventType.Repaint)
  72. {
  73. bool hasError = !string.IsNullOrEmpty(options.errorText);
  74. var style = StyleManager.UssStyleForType(type);
  75. style.Draw(drawRect, GUIContent.none, false, false, !isCollapsed, isSelected);
  76. // case1141836: Use of GUI.Box instead of GUI.Label causes desync in UI controlID
  77. if (hasError)
  78. GUI.Label(drawRect, String.Empty, DirectorStyles.Instance.markerWarning);
  79. var tooltip = hasError ? options.errorText : options.tooltip;
  80. if (!string.IsNullOrEmpty(tooltip) && drawRect.Contains(Event.current.mousePosition))
  81. {
  82. GUIStyle.SetMouseTooltip(tooltip, drawRect);
  83. }
  84. }
  85. }
  86. void UpdateDrawData()
  87. {
  88. if (Event.current.type == EventType.Layout)
  89. {
  90. try
  91. {
  92. m_MarkerDrawOptions = m_Editor.GetMarkerOptions(marker);
  93. }
  94. catch (Exception e)
  95. {
  96. Debug.LogException(e);
  97. m_MarkerDrawOptions = CustomTimelineEditorCache.GetDefaultMarkerEditor().GetMarkerOptions(marker);
  98. }
  99. }
  100. }
  101. public override void Draw(Rect trackRect, bool trackRectChanged, WindowState state)
  102. {
  103. UpdateDrawData();
  104. // compute marker hash
  105. var currentMarkerHash = ComputeDirtyHash();
  106. // update the clip projected rectangle on the timeline
  107. CalculateClipRectangle(trackRect, state, currentMarkerHash, trackRectChanged);
  108. var isSelected = selectable && SelectionManager.Contains(marker);
  109. var showMarkers = parent.showMarkers;
  110. QueueOverlay(treeViewRect, isSelected, !showMarkers);
  111. DrawMarker(treeViewRect, marker.GetType(), isSelected, !showMarkers, m_MarkerDrawOptions);
  112. if (Event.current.type == EventType.Repaint && showMarkers && !parent.locked)
  113. state.spacePartitioner.AddBounds(this, rect);
  114. }
  115. public void QueueOverlay(Rect rect, bool isSelected, bool isCollapsed)
  116. {
  117. if (Event.current.type == EventType.Repaint && m_Editor.supportsDrawOverlay)
  118. {
  119. rect = GUIClip.Unclip(rect);
  120. TimelineWindow.instance.AddUserOverlay(marker, rect, m_Editor, isCollapsed, isSelected);
  121. }
  122. }
  123. public override void StartDrag()
  124. {
  125. if (onStartDrag != null)
  126. onStartDrag.Invoke();
  127. }
  128. void CalculateClipRectangle(Rect trackRect, WindowState state, int projectedClipHash, bool trackRectChanged)
  129. {
  130. if (m_ProjectedClipHash == projectedClipHash && !trackRectChanged)
  131. return;
  132. m_ProjectedClipHash = projectedClipHash;
  133. treeViewRect = RectToTimeline(trackRect, state);
  134. }
  135. public override Rect RectToTimeline(Rect trackRect, WindowState state)
  136. {
  137. var style = StyleManager.UssStyleForType(marker.GetType());
  138. var width = style.fixedWidth;
  139. var height = style.fixedHeight;
  140. var x = ((float)marker.time * state.timeAreaScale.x) + state.timeAreaTranslation.x + trackRect.xMin;
  141. x -= 0.5f * width;
  142. return new Rect(x, trackRect.y, width, height);
  143. }
  144. public IEnumerable<Edge> SnappableEdgesFor(IAttractable attractable, ManipulateEdges manipulateEdges)
  145. {
  146. var edges = new List<Edge>();
  147. var attractableGUI = attractable as TimelineMarkerGUI;
  148. var canAddEdges = !(attractableGUI != null && attractableGUI.parent == parent);
  149. if (canAddEdges)
  150. edges.Add(new Edge(time));
  151. return edges;
  152. }
  153. public bool ShouldSnapTo(ISnappable snappable)
  154. {
  155. return snappable != this;
  156. }
  157. }
  158. }