TimelineWindow_HeaderGui.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System.Linq;
  2. using UnityEngine;
  3. using UnityEngine.Timeline;
  4. namespace UnityEditor.Timeline
  5. {
  6. partial class TimelineWindow
  7. {
  8. static readonly GUIContent[] k_TimeReferenceGUIContents =
  9. {
  10. EditorGUIUtility.TrTextContent("Local", "Display time based on the current timeline."),
  11. EditorGUIUtility.TrTextContent("Global", "Display time based on the master timeline.")
  12. };
  13. TimelineMarkerHeaderGUI m_MarkerHeaderGUI;
  14. void MarkerHeaderGUI()
  15. {
  16. var timelineAsset = state.editSequence.asset;
  17. if (timelineAsset == null)
  18. return;
  19. if (m_MarkerHeaderGUI == null)
  20. m_MarkerHeaderGUI = new TimelineMarkerHeaderGUI(timelineAsset, state);
  21. m_MarkerHeaderGUI.Draw(markerHeaderRect, markerContentRect, state);
  22. }
  23. void DrawTransportToolbar()
  24. {
  25. using (new EditorGUI.DisabledScope(currentMode.PreviewState(state) == TimelineModeGUIState.Disabled))
  26. {
  27. PreviewModeButtonGUI();
  28. }
  29. using (new EditorGUI.DisabledScope(currentMode.ToolbarState(state) == TimelineModeGUIState.Disabled))
  30. {
  31. GotoBeginingSequenceGUI();
  32. PreviousEventButtonGUI();
  33. PlayButtonGUI();
  34. NextEventButtonGUI();
  35. GotoEndSequenceGUI();
  36. PlayRangeButtonGUI();
  37. TimeCodeGUI();
  38. ReferenceTimeGUI();
  39. }
  40. }
  41. void PreviewModeButtonGUI()
  42. {
  43. if (state.ignorePreview && !Application.isPlaying)
  44. {
  45. GUILayout.Label(DirectorStyles.previewDisabledContent, DirectorStyles.Instance.previewButtonDisabled);
  46. return;
  47. }
  48. EditorGUI.BeginChangeCheck();
  49. var enabled = state.previewMode;
  50. enabled = GUILayout.Toggle(enabled, DirectorStyles.previewContent, EditorStyles.toolbarButton);
  51. if (EditorGUI.EndChangeCheck())
  52. {
  53. // turn off auto play as well, so it doesn't auto reenable
  54. if (!enabled)
  55. {
  56. state.SetPlaying(false);
  57. state.recording = false;
  58. }
  59. state.previewMode = enabled;
  60. // if we are successfully enabled, rebuild the graph so initial states work correctly
  61. // Note: testing both values because previewMode setter can "fail"
  62. if (enabled && state.previewMode)
  63. state.rebuildGraph = true;
  64. }
  65. }
  66. void GotoBeginingSequenceGUI()
  67. {
  68. if (GUILayout.Button(DirectorStyles.gotoBeginingContent, EditorStyles.toolbarButton))
  69. {
  70. state.editSequence.time = 0;
  71. state.EnsurePlayHeadIsVisible();
  72. }
  73. }
  74. // in the editor the play button starts/stops simulation
  75. void PlayButtonGUIEditor()
  76. {
  77. EditorGUI.BeginChangeCheck();
  78. var isPlaying = GUILayout.Toggle(state.playing, DirectorStyles.playContent, EditorStyles.toolbarButton);
  79. if (EditorGUI.EndChangeCheck())
  80. {
  81. state.SetPlaying(isPlaying);
  82. }
  83. }
  84. // in playmode the button reflects the playing state.
  85. // needs to disabled if playing is not possible
  86. void PlayButtonGUIPlayMode()
  87. {
  88. bool buttonEnabled = state.masterSequence.director != null &&
  89. state.masterSequence.director.isActiveAndEnabled;
  90. using (new EditorGUI.DisabledScope(!buttonEnabled))
  91. {
  92. PlayButtonGUIEditor();
  93. }
  94. }
  95. void PlayButtonGUI()
  96. {
  97. if (!Application.isPlaying)
  98. PlayButtonGUIEditor();
  99. else
  100. PlayButtonGUIPlayMode();
  101. }
  102. void NextEventButtonGUI()
  103. {
  104. if (GUILayout.Button(DirectorStyles.nextFrameContent, EditorStyles.toolbarButton))
  105. {
  106. state.referenceSequence.frame += 1;
  107. }
  108. }
  109. void PreviousEventButtonGUI()
  110. {
  111. if (GUILayout.Button(DirectorStyles.previousFrameContent, EditorStyles.toolbarButton))
  112. {
  113. state.referenceSequence.frame -= 1;
  114. }
  115. }
  116. void GotoEndSequenceGUI()
  117. {
  118. if (GUILayout.Button(DirectorStyles.gotoEndContent, EditorStyles.toolbarButton))
  119. {
  120. state.editSequence.time = state.editSequence.asset.duration;
  121. state.EnsurePlayHeadIsVisible();
  122. }
  123. }
  124. void PlayRangeButtonGUI()
  125. {
  126. using (new EditorGUI.DisabledScope(state.ignorePreview || state.IsEditingASubTimeline()))
  127. {
  128. state.playRangeEnabled = GUILayout.Toggle(state.playRangeEnabled, DirectorStyles.Instance.playrangeContent, EditorStyles.toolbarButton);
  129. }
  130. }
  131. void AddButtonGUI()
  132. {
  133. if (currentMode.trackOptionsState.newButton == TimelineModeGUIState.Hidden)
  134. return;
  135. using (new EditorGUI.DisabledScope(currentMode.trackOptionsState.newButton == TimelineModeGUIState.Disabled))
  136. {
  137. if (EditorGUILayout.DropdownButton(DirectorStyles.newContent, FocusType.Passive, EditorStyles.toolbarPopup))
  138. {
  139. // if there is 1 and only 1 track selected, AND it's a group, add to that group
  140. var groupTracks = SelectionManager.SelectedTracks().ToList();
  141. if (groupTracks.Any(x => x.GetType() != typeof(GroupTrack) || x.lockedInHierarchy))
  142. groupTracks = null;
  143. SequencerContextMenu.ShowNewTracksContextMenu(groupTracks, state, EditorGUILayout.s_LastRect);
  144. }
  145. }
  146. }
  147. void ShowMarkersButton()
  148. {
  149. var asset = state.editSequence.asset;
  150. if (asset == null)
  151. return;
  152. var content = state.showMarkerHeader ? DirectorStyles.showMarkersOn : DirectorStyles.showMarkersOff;
  153. SetShowMarkerHeader(GUILayout.Toggle(state.showMarkerHeader, content, DirectorStyles.Instance.showMarkersBtn));
  154. }
  155. internal void SetShowMarkerHeader(bool newValue)
  156. {
  157. if (state.showMarkerHeader == newValue)
  158. return;
  159. TimelineUndo.PushUndo(state.editSequence.viewModel, "Toggle Show Markers");
  160. state.editSequence.viewModel.showMarkerHeader = newValue;
  161. if (!newValue)
  162. {
  163. var asset = state.editSequence.asset;
  164. if (asset != null && asset.markerTrack != null)
  165. {
  166. SelectionManager.Remove(asset.markerTrack);
  167. foreach (var marker in asset.markerTrack.GetMarkers())
  168. {
  169. SelectionManager.Remove(marker);
  170. }
  171. }
  172. }
  173. }
  174. static void EditModeToolbarGUI(TimelineMode mode)
  175. {
  176. using (new EditorGUI.DisabledScope(mode.EditModeButtonsState(instance.state) == TimelineModeGUIState.Disabled))
  177. {
  178. var editType = EditMode.editType;
  179. EditorGUI.BeginChangeCheck();
  180. var mixIcon = editType == EditMode.EditType.Mix ? DirectorStyles.mixOn : DirectorStyles.mixOff;
  181. GUILayout.Toggle(editType == EditMode.EditType.Mix, mixIcon, DirectorStyles.Instance.editModeBtn);
  182. if (EditorGUI.EndChangeCheck())
  183. EditMode.editType = EditMode.EditType.Mix;
  184. EditorGUI.BeginChangeCheck();
  185. var rippleIcon = editType == EditMode.EditType.Ripple ? DirectorStyles.rippleOn : DirectorStyles.rippleOff;
  186. GUILayout.Toggle(editType == EditMode.EditType.Ripple, rippleIcon, DirectorStyles.Instance.editModeBtn);
  187. if (EditorGUI.EndChangeCheck())
  188. EditMode.editType = EditMode.EditType.Ripple;
  189. EditorGUI.BeginChangeCheck();
  190. var replaceIcon = editType == EditMode.EditType.Replace ? DirectorStyles.replaceOn : DirectorStyles.replaceOff;
  191. GUILayout.Toggle(editType == EditMode.EditType.Replace, replaceIcon, DirectorStyles.Instance.editModeBtn);
  192. if (EditorGUI.EndChangeCheck())
  193. EditMode.editType = EditMode.EditType.Replace;
  194. }
  195. }
  196. // Draws the box to enter the time field
  197. void TimeCodeGUI()
  198. {
  199. const string timeFieldHint = "TimelineWindow-TimeCodeGUI";
  200. EditorGUI.BeginChangeCheck();
  201. var currentTime = state.editSequence.asset != null ? TimeReferenceUtility.ToTimeString(state.editSequence.time, "F1") : "0";
  202. var r = EditorGUILayout.GetControlRect(false, EditorGUI.kSingleLineHeight, EditorStyles.toolbarTextField, GUILayout.Width(WindowConstants.timeCodeWidth));
  203. var id = GUIUtility.GetControlID(timeFieldHint.GetHashCode(), FocusType.Keyboard, r);
  204. var newCurrentTime = EditorGUI.DelayedTextFieldInternal(r, id, GUIContent.none, currentTime, null, EditorStyles.toolbarTextField);
  205. if (EditorGUI.EndChangeCheck())
  206. state.editSequence.time = TimeReferenceUtility.FromTimeString(newCurrentTime);
  207. }
  208. void ReferenceTimeGUI()
  209. {
  210. if (!state.IsEditingASubTimeline())
  211. return;
  212. EditorGUI.BeginChangeCheck();
  213. state.timeReferenceMode = (TimeReferenceMode)EditorGUILayout.CycleButton((int)state.timeReferenceMode, k_TimeReferenceGUIContents, DirectorStyles.Instance.timeReferenceButton);
  214. if (EditorGUI.EndChangeCheck())
  215. OnTimeReferenceModeChanged();
  216. }
  217. void OnTimeReferenceModeChanged()
  218. {
  219. m_TimeAreaDirty = true;
  220. InitTimeAreaFrameRate();
  221. SyncTimeAreaShownRange();
  222. foreach (var inspector in InspectorWindow.GetAllInspectorWindows())
  223. {
  224. inspector.Repaint();
  225. }
  226. }
  227. void DrawHeaderEditButtons()
  228. {
  229. if (state.editSequence.asset == null)
  230. return;
  231. using (new GUILayout.HorizontalScope(EditorStyles.toolbar, GUILayout.Width(sequenceHeaderRect.width)))
  232. {
  233. GUILayout.Space(DirectorStyles.kBaseIndent);
  234. AddButtonGUI();
  235. GUILayout.FlexibleSpace();
  236. EditModeToolbarGUI(currentMode);
  237. ShowMarkersButton();
  238. EditorGUILayout.Space();
  239. }
  240. }
  241. }
  242. }