TimelineWindow_Gui.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using System;
  2. using System.Collections.Generic;
  3. #if UNITY_2021_2_OR_NEWER
  4. using UnityEditor.SceneManagement;
  5. #else
  6. using UnityEditor.Experimental.SceneManagement;
  7. #endif
  8. using UnityEngine;
  9. using UnityEngine.Timeline;
  10. namespace UnityEditor.Timeline
  11. {
  12. partial class TimelineWindow
  13. {
  14. struct MarkerOverlay
  15. {
  16. public IMarker marker;
  17. public Rect rect;
  18. public bool isSelected;
  19. public bool isCollapsed;
  20. public MarkerEditor editor;
  21. }
  22. enum TimelineItemArea
  23. {
  24. Header,
  25. Lines
  26. }
  27. [SerializeField] float m_HierarchySplitterPerc = WindowConstants.hierarchySplitterDefaultPercentage;
  28. static internal readonly TimelineMode s_ActiveMode = new TimelineActiveMode();
  29. static internal readonly TimelineMode s_EditAssetMode = new TimelineAssetEditionMode();
  30. static internal readonly TimelineMode s_InactiveMode = new TimelineInactiveMode();
  31. static internal readonly TimelineMode s_DisabledMode = new TimelineDisabledMode();
  32. static internal readonly TimelineMode s_PrefabOutOfContextMode = new TimelineAssetEditionMode();
  33. static internal readonly TimelineMode s_ReadonlyMode = new TimelineReadOnlyMode();
  34. int m_SplitterCaptured;
  35. float m_VerticalScrollBarSize, m_HorizontalScrollBarSize;
  36. List<MarkerOverlay> m_OverlayQueue = new List<MarkerOverlay>(100);
  37. float headerHeight
  38. {
  39. get
  40. {
  41. return WindowConstants.markerRowYPosition + (state.showMarkerHeader ? WindowConstants.markerRowHeight : 0.0f);
  42. }
  43. }
  44. public Rect markerHeaderRect
  45. {
  46. get { return new Rect(0.0f, WindowConstants.markerRowYPosition, state.sequencerHeaderWidth, WindowConstants.markerRowHeight); }
  47. }
  48. public Rect markerContentRect
  49. {
  50. get { return Rect.MinMaxRect(state.sequencerHeaderWidth, WindowConstants.markerRowYPosition, position.width, WindowConstants.markerRowYPosition + WindowConstants.markerRowHeight); }
  51. }
  52. Rect trackRect
  53. {
  54. get
  55. {
  56. var yMinHeight = headerHeight;
  57. return new Rect(0, yMinHeight, position.width, position.height - yMinHeight - horizontalScrollbarHeight);
  58. }
  59. }
  60. public Rect sequenceRect
  61. {
  62. get { return new Rect(0.0f, WindowConstants.markerRowYPosition, position.width - WindowConstants.sliderWidth, position.height - WindowConstants.timeAreaYPosition); }
  63. }
  64. public Rect sequenceHeaderRect
  65. {
  66. get { return new Rect(0.0f, WindowConstants.markerRowYPosition, state.sequencerHeaderWidth, position.height - WindowConstants.timeAreaYPosition); }
  67. }
  68. public Rect sequenceContentRect
  69. {
  70. get
  71. {
  72. return new Rect(
  73. state.sequencerHeaderWidth,
  74. WindowConstants.markerRowYPosition,
  75. position.width - state.sequencerHeaderWidth - (treeView != null && treeView.showingVerticalScrollBar ? WindowConstants.sliderWidth : 0),
  76. position.height - WindowConstants.markerRowYPosition - horizontalScrollbarHeight);
  77. }
  78. }
  79. public float verticalScrollbarWidth
  80. {
  81. get
  82. {
  83. return m_VerticalScrollBarSize;
  84. }
  85. }
  86. public float horizontalScrollbarHeight
  87. {
  88. get { return m_HorizontalScrollBarSize; }
  89. }
  90. internal TimelineMode currentMode
  91. {
  92. get
  93. {
  94. if (state == null || state.editSequence.asset == null)
  95. return s_InactiveMode;
  96. if (state.editSequence.isReadOnly)
  97. return s_ReadonlyMode;
  98. if (state.editSequence.director == null || state.masterSequence.director == null)
  99. return s_EditAssetMode;
  100. if (PrefabUtility.IsPartOfPrefabAsset(state.editSequence.director))
  101. {
  102. var stage = PrefabStageUtility.GetCurrentPrefabStage();
  103. if (stage == null || !stage.IsPartOfPrefabContents(state.editSequence.director.gameObject))
  104. return s_PrefabOutOfContextMode;
  105. }
  106. if (!state.masterSequence.director.isActiveAndEnabled)
  107. return s_DisabledMode;
  108. return s_ActiveMode;
  109. }
  110. }
  111. void DoLayout()
  112. {
  113. var rawType = Event.current.rawType; // TODO: rawType seems to be broken after calling Use(), use this Hack and remove it once it's fixed.
  114. var mousePosition = Event.current.mousePosition; // mousePosition is also affected by this bug and does not reflect the original position after a Use()
  115. Initialize();
  116. HandleSplitterResize();
  117. var processManipulators = Event.current.type != EventType.Repaint && Event.current.type != EventType.Layout;
  118. if (processManipulators)
  119. {
  120. // Update what's under mouse the cursor
  121. PickerUtils.DoPick(state, mousePosition);
  122. if (state.editSequence.asset != null)
  123. m_PreTreeViewControl.HandleManipulatorsEvents(state);
  124. }
  125. SequencerGUI();
  126. if (processManipulators)
  127. {
  128. if (state.editSequence.asset != null)
  129. m_PostTreeViewControl.HandleManipulatorsEvents(state);
  130. }
  131. m_RectangleSelect.OnGUI(state, rawType, mousePosition);
  132. m_RectangleZoom.OnGUI(state, rawType, mousePosition);
  133. }
  134. void SplitterGUI()
  135. {
  136. if (!state.IsEditingAnEmptyTimeline())
  137. {
  138. var splitterLineRect = new Rect(state.sequencerHeaderWidth - 1.0f, WindowConstants.timeAreaYPosition + 2.0f, 2.0f, clientArea.height);
  139. EditorGUI.DrawRect(splitterLineRect, DirectorStyles.Instance.customSkin.colorTopOutline3);
  140. }
  141. }
  142. void TrackViewsGUI()
  143. {
  144. using (new GUIViewportScope(trackRect))
  145. {
  146. TracksGUI(trackRect, state, currentMode.TrackState(state));
  147. }
  148. }
  149. void UserOverlaysGUI()
  150. {
  151. if (Event.current.type != EventType.Repaint)
  152. return;
  153. // the rect containing the time area plus the time ruler
  154. var screenRect = new Rect(
  155. state.sequencerHeaderWidth,
  156. WindowConstants.timeAreaYPosition,
  157. position.width - state.sequencerHeaderWidth - (treeView != null && treeView.showingVerticalScrollBar ? WindowConstants.sliderWidth : 0),
  158. position.height - WindowConstants.timeAreaYPosition - horizontalScrollbarHeight);
  159. var startTime = state.PixelToTime(screenRect.xMin);
  160. var endTime = state.PixelToTime(screenRect.xMax);
  161. using (new GUIViewportScope(screenRect))
  162. {
  163. foreach (var entry in m_OverlayQueue)
  164. {
  165. var uiState = MarkerUIStates.None;
  166. if (entry.isCollapsed)
  167. uiState |= MarkerUIStates.Collapsed;
  168. if (entry.isSelected)
  169. uiState |= MarkerUIStates.Selected;
  170. var region = new MarkerOverlayRegion(GUIClip.Clip(entry.rect), screenRect, startTime, endTime);
  171. try
  172. {
  173. entry.editor.DrawOverlay(entry.marker, uiState, region);
  174. }
  175. catch (Exception e)
  176. {
  177. Debug.LogException(e);
  178. }
  179. }
  180. }
  181. m_OverlayQueue.Clear();
  182. }
  183. void DrawHeaderBackground()
  184. {
  185. var rect = state.timeAreaRect;
  186. rect.xMin = 0.0f;
  187. EditorGUI.DrawRect(rect, DirectorStyles.Instance.customSkin.colorTimelineBackground);
  188. }
  189. void HandleBottomFillerDragAndDrop(Rect rect)
  190. {
  191. if (Event.current.type != EventType.DragUpdated &&
  192. Event.current.type != EventType.DragExited &&
  193. Event.current.type != EventType.DragPerform)
  194. return;
  195. if (instance.treeView == null || instance.treeView.timelineDragging == null)
  196. return;
  197. if (!rect.Contains(Event.current.mousePosition))
  198. return;
  199. instance.treeView.timelineDragging.DragElement(null, new Rect(), -1);
  200. }
  201. void DrawHeaderBackgroundBottomFiller()
  202. {
  203. var rect = sequenceRect;
  204. rect.yMin = rect.yMax;
  205. rect.yMax = rect.yMax + WindowConstants.sliderWidth;
  206. if (state.editSequence.asset != null && !state.IsEditingAnEmptyTimeline())
  207. {
  208. rect.width = state.sequencerHeaderWidth;
  209. }
  210. using (new GUIViewportScope(rect))
  211. {
  212. Graphics.DrawBackgroundRect(state, rect);
  213. }
  214. HandleBottomFillerDragAndDrop(rect);
  215. }
  216. void SequencerGUI()
  217. {
  218. var duration = state.editSequence.duration;
  219. DrawHeaderBackground();
  220. DurationGUI(TimelineItemArea.Header, duration);
  221. DrawToolbar();
  222. TrackViewsGUI();
  223. MarkerHeaderGUI();
  224. UserOverlaysGUI();
  225. DurationGUI(TimelineItemArea.Lines, duration);
  226. PlayRangeGUI(TimelineItemArea.Lines);
  227. TimeCursorGUI(TimelineItemArea.Lines);
  228. DrawHeaderBackgroundBottomFiller();
  229. SubTimelineRangeGUI();
  230. PlayRangeGUI(TimelineItemArea.Header);
  231. TimeCursorGUI(TimelineItemArea.Header);
  232. SplitterGUI();
  233. }
  234. void DrawToolbar()
  235. {
  236. DrawOptions();
  237. using (new GUILayout.VerticalScope())
  238. {
  239. using (new GUILayout.HorizontalScope(EditorStyles.toolbar))
  240. {
  241. using (new GUILayout.HorizontalScope())
  242. {
  243. DrawTransportToolbar();
  244. }
  245. DrawSequenceSelector();
  246. DrawBreadcrumbs();
  247. GUILayout.FlexibleSpace();
  248. DrawOptions();
  249. }
  250. using (new GUILayout.HorizontalScope())
  251. {
  252. DrawHeaderEditButtons();
  253. DrawTimelineRuler();
  254. }
  255. }
  256. }
  257. void SubTimelineRangeGUI()
  258. {
  259. if (!state.IsEditingASubTimeline() || state.IsEditingAnEmptyTimeline()) return;
  260. var subTimelineOverlayColor = DirectorStyles.Instance.customSkin.colorSubSequenceOverlay;
  261. var range = state.editSequence.GetEvaluableRange();
  262. var area = new Vector2(state.TimeToPixel(range.start), state.TimeToPixel(range.end));
  263. var fullRect = sequenceContentRect;
  264. fullRect.yMin = WindowConstants.timeAreaYPosition + 1.0f;
  265. if (fullRect.xMin < area.x)
  266. {
  267. var before = fullRect;
  268. before.xMin = fullRect.xMin;
  269. before.xMax = Mathf.Min(area.x, fullRect.xMax);
  270. EditorGUI.DrawRect(before, subTimelineOverlayColor);
  271. }
  272. if (fullRect.xMax > area.y)
  273. {
  274. var after = fullRect;
  275. after.xMin = Mathf.Max(area.y, fullRect.xMin);
  276. after.xMax = fullRect.xMax;
  277. EditorGUI.DrawRect(after, subTimelineOverlayColor);
  278. // Space above the vertical scrollbar
  279. after.xMin = after.xMax;
  280. after.width = verticalScrollbarWidth;
  281. after.yMax = state.timeAreaRect.y + state.timeAreaRect.height + (state.showMarkerHeader ? WindowConstants.markerRowHeight : 0.0f);
  282. EditorGUI.DrawRect(after, subTimelineOverlayColor);
  283. }
  284. }
  285. void HandleSplitterResize()
  286. {
  287. state.mainAreaWidth = position.width;
  288. if (state.editSequence.asset == null)
  289. return;
  290. // Sequencer Header Splitter : The splitter has 6 pixels wide,center it around m_State.sequencerHeaderWidth. That's why there's this -3.
  291. Rect sequencerHeaderSplitterRect = new Rect(state.sequencerHeaderWidth - 3.0f, 0.0f, 6.0f, clientArea.height);
  292. EditorGUIUtility.AddCursorRect(sequencerHeaderSplitterRect, MouseCursor.SplitResizeLeftRight);
  293. if (Event.current.type == EventType.MouseDown)
  294. {
  295. if (sequencerHeaderSplitterRect.Contains(Event.current.mousePosition))
  296. m_SplitterCaptured = 1;
  297. }
  298. if (m_SplitterCaptured > 0)
  299. {
  300. if (Event.current.type == EventType.MouseUp)
  301. {
  302. m_SplitterCaptured = 0;
  303. Event.current.Use();
  304. }
  305. if (Event.current.type == EventType.MouseDrag)
  306. {
  307. if (m_SplitterCaptured == 1)
  308. {
  309. var percInc = Event.current.delta.x / position.width;
  310. m_HierarchySplitterPerc = Mathf.Clamp(m_HierarchySplitterPerc + percInc, WindowConstants.minHierarchySplitter, WindowConstants.maxHierarchySplitter);
  311. state.sequencerHeaderWidth += Event.current.delta.x;
  312. }
  313. Event.current.Use();
  314. }
  315. }
  316. }
  317. void DrawOptions()
  318. {
  319. if (currentMode.headerState.options == TimelineModeGUIState.Hidden || state.editSequence.asset == null)
  320. return;
  321. using (new EditorGUI.DisabledScope(currentMode.headerState.options == TimelineModeGUIState.Disabled))
  322. {
  323. var rect = new Rect(position.width - WindowConstants.cogButtonWidth, 0, WindowConstants.cogButtonWidth, WindowConstants.timeAreaYPosition);
  324. if (EditorGUI.DropdownButton(rect, DirectorStyles.optionsCogIcon, FocusType.Keyboard, EditorStyles.toolbarButton))
  325. {
  326. GenericMenu menu = new GenericMenu();
  327. menu.AddItem(EditorGUIUtility.TrTextContent("Preferences Page..."), false, () => SettingsWindow.Show(SettingsScope.User, "Preferences/Timeline"));
  328. menu.AddSeparator("");
  329. menu.AddItem(EditorGUIUtility.TrTextContent("Seconds"), !state.timeInFrames, ChangeTimeCode, "seconds");
  330. menu.AddItem(EditorGUIUtility.TrTextContent("Frames"), state.timeInFrames, ChangeTimeCode, "frames");
  331. menu.AddSeparator("");
  332. TimeAreaContextMenu.AddTimeAreaMenuItems(menu, state);
  333. menu.AddSeparator("");
  334. bool standardFrameRate = false;
  335. for (int i = 0; i < TimelineProjectSettings.framerateValues.Length; i++)
  336. {
  337. standardFrameRate |= AddStandardFrameRateMenu(menu, "Frame Rate/" + TimelineProjectSettings.framerateLabels[i], TimelineProjectSettings.framerateValues[i]);
  338. }
  339. if (standardFrameRate)
  340. menu.AddDisabledItem(EditorGUIUtility.TrTextContent("Frame Rate/Custom"));
  341. else
  342. menu.AddItem(EditorGUIUtility.TrTextContent("Frame Rate/Custom (" + state.editSequence.frameRate + ")"), true, () => {});
  343. menu.AddSeparator("");
  344. if (state.playRangeEnabled)
  345. {
  346. menu.AddItem(EditorGUIUtility.TrTextContent("Play Range Mode/Loop"), state.playRangeLoopMode, () => state.playRangeLoopMode = true);
  347. menu.AddItem(EditorGUIUtility.TrTextContent("Play Range Mode/Once"), !state.playRangeLoopMode, () => state.playRangeLoopMode = false);
  348. }
  349. else
  350. {
  351. menu.AddDisabledItem(EditorGUIUtility.TrTextContent("Play Range Mode"));
  352. }
  353. if (Unsupported.IsDeveloperMode())
  354. {
  355. menu.AddSeparator("");
  356. menu.AddItem(EditorGUIUtility.TrTextContent("Show Snapping Debug"), SnapEngine.displayDebugLayout,
  357. () => SnapEngine.displayDebugLayout = !SnapEngine.displayDebugLayout);
  358. menu.AddItem(EditorGUIUtility.TrTextContent("Debug TimeArea"), false,
  359. () =>
  360. Debug.LogFormat("translation: {0} scale: {1} rect: {2} shownRange: {3}", m_TimeArea.translation, m_TimeArea.scale, m_TimeArea.rect, m_TimeArea.shownArea));
  361. menu.AddItem(EditorGUIUtility.TrTextContent("Edit Skin"), false, () => Selection.activeObject = DirectorStyles.Instance.customSkin);
  362. menu.AddItem(EditorGUIUtility.TrTextContent("Show QuadTree Debugger"), state.showQuadTree,
  363. () => state.showQuadTree = !state.showQuadTree);
  364. }
  365. menu.DropDown(rect);
  366. }
  367. }
  368. }
  369. bool AddStandardFrameRateMenu(GenericMenu menu, string name, float value)
  370. {
  371. bool on = state.editSequence.frameRate.Equals(value);
  372. if (state.editSequence.isReadOnly)
  373. {
  374. menu.AddDisabledItem(EditorGUIUtility.TextContent(name), on);
  375. }
  376. else
  377. {
  378. menu.AddItem(EditorGUIUtility.TextContent(name), on, r =>
  379. {
  380. state.editSequence.frameRate = value;
  381. }, value);
  382. }
  383. return on;
  384. }
  385. void ChangeTimeCode(object obj)
  386. {
  387. string format = obj.ToString();
  388. if (format == "frames")
  389. {
  390. state.timeInFrames = true;
  391. }
  392. else
  393. {
  394. state.timeInFrames = false;
  395. }
  396. }
  397. public void AddUserOverlay(IMarker marker, Rect rect, MarkerEditor editor, bool collapsed, bool selected)
  398. {
  399. if (marker == null)
  400. throw new ArgumentNullException("marker");
  401. if (editor == null)
  402. throw new ArgumentNullException("editor");
  403. m_OverlayQueue.Add(new MarkerOverlay()
  404. {
  405. isCollapsed = collapsed,
  406. isSelected = selected,
  407. marker = marker,
  408. rect = rect,
  409. editor = editor
  410. }
  411. );
  412. }
  413. }
  414. }