TimelineTrackBaseGUI.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System.Linq;
  2. using UnityEditor.IMGUI.Controls;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. abstract class TimelineTrackBaseGUI : TreeViewItem, IBounds
  8. {
  9. static class Styles
  10. {
  11. public static readonly GUIContent s_LockedAndMuted = EditorGUIUtility.TrTextContent("Locked / Muted");
  12. public static readonly GUIContent s_LockedAndPartiallyMuted = EditorGUIUtility.TrTextContent("Locked / Partially Muted");
  13. public static readonly GUIContent s_Locked = EditorGUIUtility.TrTextContent("Locked");
  14. public static readonly GUIContent s_Muted = EditorGUIUtility.TrTextContent("Muted");
  15. public static readonly GUIContent s_PartiallyMuted = EditorGUIUtility.TrTextContent("Partially Muted");
  16. public static readonly Texture2D lockBg = DirectorStyles.GetBackgroundImage(DirectorStyles.Instance.lockedBG);
  17. }
  18. protected bool m_IsRoot = false;
  19. protected const float k_ButtonSize = 16.0f;
  20. readonly TimelineTreeViewGUI m_TreeViewGUI;
  21. readonly TrackDrawer m_Drawer;
  22. public Vector2 treeViewToWindowTransformation { get; set; }
  23. public bool isExpanded { get; set; }
  24. public bool isDropTarget { protected get; set; }
  25. public TrackAsset track { get; }
  26. TreeViewController treeView { get; }
  27. public TimelineWindow TimelineWindow
  28. {
  29. get
  30. {
  31. if (m_TreeViewGUI == null)
  32. return null;
  33. return m_TreeViewGUI.TimelineWindow;
  34. }
  35. }
  36. public TrackDrawer drawer
  37. {
  38. get { return m_Drawer; }
  39. }
  40. public virtual float GetVerticalSpacingBetweenTracks()
  41. {
  42. return 3.0f;
  43. }
  44. public bool visibleRow { get; set; } // is the header row visible
  45. public bool visibleExpanded { get; set; } // is the expanded area (group) visible
  46. public bool drawInsertionMarkerBefore { get; set; }
  47. public bool drawInsertionMarkerAfter { get; set; }
  48. public abstract Rect boundingRect { get; }
  49. public abstract bool expandable { get; }
  50. public abstract void Draw(Rect headerRect, Rect contentRect, WindowState state);
  51. public abstract void OnGraphRebuilt(); // callback when the corresponding graph is rebuilt. This can happen, but not have the GUI rebuilt.
  52. protected TimelineTrackBaseGUI(int id, int depth, TreeViewItem parent, string displayName, TrackAsset trackAsset, TreeViewController tv, TimelineTreeViewGUI tvgui)
  53. : base(id, depth, parent, displayName)
  54. {
  55. m_Drawer = TrackDrawer.CreateInstance(trackAsset);
  56. m_Drawer.sequencerState = tvgui.TimelineWindow.state;
  57. isExpanded = false;
  58. isDropTarget = false;
  59. track = trackAsset;
  60. treeView = tv;
  61. m_TreeViewGUI = tvgui;
  62. }
  63. public static TimelineTrackBaseGUI FindGUITrack(TrackAsset track)
  64. {
  65. var allTracks = TimelineWindow.instance.allTracks;
  66. return allTracks.Find(x => x.track == track);
  67. }
  68. protected void DrawTrackState(Rect trackRect, Rect expandedRect, TrackAsset track)
  69. {
  70. if (Event.current.type == EventType.Layout)
  71. {
  72. bool needStateBox = false;
  73. //Mute
  74. if (track.muted && !TimelineUtility.IsParentMuted(track))
  75. {
  76. Rect bgRect = expandedRect;
  77. TimelineWindow.instance.OverlayDrawData.Add(TimelineWindow.OverlayData.CreateColorOverlay(GUIClip.Unclip(bgRect), DirectorStyles.Instance.customSkin.colorTrackDarken));
  78. needStateBox = true;
  79. }
  80. //Lock
  81. if (!needStateBox && track.locked && !TimelineUtility.IsLockedFromGroup(track))
  82. {
  83. Rect bgRect = expandedRect;
  84. TimelineWindow.instance.OverlayDrawData.Add(TimelineWindow.OverlayData.CreateTextureOverlay(GUIClip.Unclip(bgRect), Styles.lockBg));
  85. needStateBox = true;
  86. }
  87. if (needStateBox)
  88. {
  89. DrawTrackStateBox(trackRect, track);
  90. }
  91. }
  92. }
  93. void DrawTrackStateBox(Rect trackRect, TrackAsset track)
  94. {
  95. const float k_LockTextPadding = 40f;
  96. var styles = DirectorStyles.Instance;
  97. bool locked = track.locked && !TimelineUtility.IsLockedFromGroup(track);
  98. bool muted = track.muted && !TimelineUtility.IsParentMuted(track);
  99. bool allSubTrackMuted = TimelineUtility.IsAllSubTrackMuted(track);
  100. GUIContent content = null;
  101. if (locked && muted)
  102. {
  103. content = Styles.s_LockedAndMuted;
  104. if (!allSubTrackMuted)
  105. content = Styles.s_LockedAndPartiallyMuted;
  106. }
  107. else if (locked) content = Styles.s_Locked;
  108. else if (muted)
  109. {
  110. content = Styles.s_Muted;
  111. if (!allSubTrackMuted)
  112. content = Styles.s_PartiallyMuted;
  113. }
  114. // the track could be locked, but we only show the 'locked portion' on the upper most track
  115. // that is causing the lock
  116. if (content == null)
  117. return;
  118. var textRect = trackRect;
  119. textRect.width = styles.fontClip.CalcSize(content).x + k_LockTextPadding;
  120. textRect.x += (trackRect.width - textRect.width) / 2f;
  121. textRect.height -= 4f;
  122. textRect.y += 2f;
  123. TimelineWindow.instance.OverlayDrawData.Add(TimelineWindow.OverlayData.CreateTextBoxOverlay(GUIClip.Unclip(textRect), content.text, styles.fontClip, Color.white, styles.customSkin.colorLockTextBG, styles.displayBackground));
  124. }
  125. protected float DrawMuteButton(Rect rect, WindowState state)
  126. {
  127. if (track.mutedInHierarchy)
  128. {
  129. using (new EditorGUI.DisabledScope(TimelineUtility.IsParentMuted(track)))
  130. {
  131. if (GUI.Button(rect, GUIContent.none, TimelineWindow.styles.mute))
  132. {
  133. MuteTrack.Mute(state, new[] { track }, false);
  134. }
  135. }
  136. return WindowConstants.trackHeaderButtonSize;
  137. }
  138. return 0.0f;
  139. }
  140. protected float DrawLockButton(Rect rect, WindowState state)
  141. {
  142. if (track.lockedInHierarchy)
  143. {
  144. // if the parent is locked, show it the lock disabled
  145. using (new EditorGUI.DisabledScope(TimelineUtility.IsLockedFromGroup(track)))
  146. {
  147. if (GUI.Button(rect, GUIContent.none, TimelineWindow.styles.locked))
  148. {
  149. LockTrack.SetLockState(new[] { track }, !track.locked, state);
  150. }
  151. }
  152. return WindowConstants.trackHeaderButtonSize;
  153. }
  154. return 0.0f;
  155. }
  156. public void DrawInsertionMarkers(Rect rowRectWithIndent)
  157. {
  158. const float insertionHeight = WindowConstants.trackInsertionMarkerHeight;
  159. if (Event.current.type == EventType.Repaint && (drawInsertionMarkerAfter || drawInsertionMarkerBefore))
  160. {
  161. if (drawInsertionMarkerBefore)
  162. {
  163. var rect = new Rect(rowRectWithIndent.x, rowRectWithIndent.y - insertionHeight * 0.5f - 2.0f, rowRectWithIndent.width, insertionHeight);
  164. EditorGUI.DrawRect(rect, Color.white);
  165. }
  166. if (drawInsertionMarkerAfter)
  167. {
  168. var rect = new Rect(rowRectWithIndent.x, rowRectWithIndent.y + rowRectWithIndent.height - insertionHeight * 0.5f + 1.0f, rowRectWithIndent.width, insertionHeight);
  169. EditorGUI.DrawRect(rect, Color.white);
  170. }
  171. }
  172. }
  173. public void ClearDrawFlags()
  174. {
  175. if (Event.current.type == EventType.Repaint)
  176. {
  177. isDropTarget = false;
  178. drawInsertionMarkerAfter = false;
  179. drawInsertionMarkerBefore = false;
  180. }
  181. }
  182. }
  183. }