Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TimelineWindow_Gui.cs 17KB

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