TimelineWindow_TrackGui.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Playables;
  6. namespace UnityEditor.Timeline
  7. {
  8. partial class TimelineWindow
  9. {
  10. public TimelineTreeViewGUI treeView { get; private set; }
  11. void TracksGUI(Rect clientRect, WindowState state, TimelineModeGUIState trackState)
  12. {
  13. if (Event.current.type == EventType.Repaint && treeView != null)
  14. {
  15. state.headerSpacePartitioner.Clear();
  16. state.spacePartitioner.Clear();
  17. }
  18. if (state.IsEditingASubTimeline() && !state.IsEditingAnEmptyTimeline())
  19. {
  20. var headerRect = clientRect;
  21. headerRect.width = state.sequencerHeaderWidth;
  22. Graphics.DrawBackgroundRect(state, headerRect);
  23. var clipRect = clientRect;
  24. clipRect.xMin = headerRect.xMax;
  25. Graphics.DrawBackgroundRect(state, clipRect, subSequenceMode: true);
  26. }
  27. else
  28. {
  29. Graphics.DrawBackgroundRect(state, clientRect);
  30. }
  31. if (!state.IsEditingAnEmptyTimeline())
  32. m_TimeArea.DrawMajorTicks(sequenceContentRect, state.referenceSequence.frameRate);
  33. GUILayout.BeginVertical();
  34. {
  35. GUILayout.Space(5.0f);
  36. GUILayout.BeginHorizontal();
  37. if (this.state.editSequence.asset == null)
  38. DrawNoSequenceGUI(state);
  39. else
  40. DrawTracksGUI(clientRect, trackState);
  41. GUILayout.EndHorizontal();
  42. }
  43. GUILayout.EndVertical();
  44. Graphics.DrawShadow(clientRect);
  45. }
  46. void DrawNoSequenceGUI(WindowState windowState)
  47. {
  48. bool showCreateButton = false;
  49. var currentlySelectedGo = UnityEditor.Selection.activeObject != null ? UnityEditor.Selection.activeObject as GameObject : null;
  50. var textContent = DirectorStyles.noTimelineAssetSelected;
  51. var existingDirector = currentlySelectedGo != null ? currentlySelectedGo.GetComponent<PlayableDirector>() : null;
  52. var existingAsset = existingDirector != null ? existingDirector.playableAsset : null;
  53. if (currentlySelectedGo != null && !TimelineUtility.IsPrefabOrAsset(currentlySelectedGo) && existingAsset == null)
  54. {
  55. showCreateButton = true;
  56. textContent = new GUIContent(String.Format(DirectorStyles.createTimelineOnSelection.text, currentlySelectedGo.name, "a Director component and a Timeline asset"));
  57. }
  58. GUILayout.FlexibleSpace();
  59. GUILayout.BeginVertical();
  60. GUILayout.FlexibleSpace();
  61. GUILayout.Label(textContent);
  62. if (showCreateButton)
  63. {
  64. GUILayout.BeginHorizontal();
  65. var textSize = GUI.skin.label.CalcSize(textContent);
  66. GUILayout.Space((textSize.x / 2.0f) - (WindowConstants.createButtonWidth / 2.0f));
  67. if (GUILayout.Button("Create", GUILayout.Width(WindowConstants.createButtonWidth)))
  68. {
  69. var message = DirectorStyles.createNewTimelineText.text + " '" + currentlySelectedGo.name + "'";
  70. var defaultName = currentlySelectedGo.name.EndsWith(DirectorStyles.newTimelineDefaultNameSuffix, StringComparison.OrdinalIgnoreCase)
  71. ? currentlySelectedGo.name
  72. : currentlySelectedGo.name + DirectorStyles.newTimelineDefaultNameSuffix;
  73. // Use the project window path by default only if it's under the asset folder.
  74. // Otherwise the saveFilePanel will reject the save (case 1289923)
  75. var defaultPath = ProjectWindowUtil.GetActiveFolderPath();
  76. if (!defaultPath.StartsWith("Assets/", StringComparison.OrdinalIgnoreCase))
  77. defaultPath = "Assets";
  78. string newSequencePath = EditorUtility.SaveFilePanelInProject(DirectorStyles.createNewTimelineText.text, defaultName, "playable", message, defaultPath);
  79. if (!string.IsNullOrEmpty(newSequencePath))
  80. {
  81. var newAsset = TimelineUtility.CreateAndSaveTimelineAsset(newSequencePath);
  82. Undo.IncrementCurrentGroup();
  83. if (existingDirector == null)
  84. {
  85. existingDirector = Undo.AddComponent<PlayableDirector>(currentlySelectedGo);
  86. }
  87. existingDirector.playableAsset = newAsset;
  88. SetCurrentTimeline(existingDirector);
  89. windowState.previewMode = false;
  90. }
  91. // If we reach this point, the state of the panel has changed; skip the rest of this GUI phase
  92. // Fixes: case 955831 - [OSX] NullReferenceException when creating a timeline on a selected object
  93. GUIUtility.ExitGUI();
  94. }
  95. GUILayout.EndHorizontal();
  96. }
  97. GUILayout.FlexibleSpace();
  98. GUILayout.EndVertical();
  99. GUILayout.FlexibleSpace();
  100. }
  101. public enum OverlayDataTypes
  102. {
  103. None,
  104. BackgroundColor,
  105. BackgroundTexture,
  106. TextBox
  107. }
  108. public struct OverlayData
  109. {
  110. public OverlayDataTypes types { get; private set; }
  111. public Rect rect { get; internal set; }
  112. public string text { get; private set; }
  113. public Texture2D texture { get; private set; }
  114. public Color color { get; private set; }
  115. public GUIStyle backgroundTextStyle { get; private set; }
  116. public GUIStyle textStyle { get; private set; }
  117. public static OverlayData CreateColorOverlay(Rect rectangle, Color backgroundColor)
  118. {
  119. OverlayData data = new OverlayData();
  120. data.rect = rectangle;
  121. data.color = backgroundColor;
  122. data.types = OverlayDataTypes.BackgroundColor;
  123. return data;
  124. }
  125. public static OverlayData CreateTextureOverlay(Rect rectangle, Texture2D backTexture)
  126. {
  127. OverlayData data = new OverlayData();
  128. data.rect = rectangle;
  129. data.texture = backTexture;
  130. data.types = OverlayDataTypes.BackgroundTexture;
  131. return data;
  132. }
  133. public static OverlayData CreateTextBoxOverlay(Rect rectangle, string msg, GUIStyle textstyle, Color textcolor, Color bgTextColor, GUIStyle bgTextStyle)
  134. {
  135. OverlayData data = new OverlayData();
  136. data.rect = rectangle;
  137. data.text = msg;
  138. data.textStyle = textstyle;
  139. data.textStyle.normal.textColor = textcolor;
  140. data.backgroundTextStyle = bgTextStyle;
  141. data.backgroundTextStyle.normal.textColor = bgTextColor;
  142. data.types = OverlayDataTypes.TextBox;
  143. return data;
  144. }
  145. }
  146. internal List<OverlayData> OverlayDrawData = new List<OverlayData>();
  147. void DrawTracksGUI(Rect clientRect, TimelineModeGUIState trackState)
  148. {
  149. GUILayout.BeginVertical(GUILayout.Height(clientRect.height));
  150. if (treeView != null)
  151. {
  152. if (Event.current.type == EventType.Layout)
  153. {
  154. OverlayDrawData.Clear();
  155. }
  156. treeView.OnGUI(clientRect);
  157. if (Event.current.type == EventType.Repaint)
  158. {
  159. foreach (var overlayData in OverlayDrawData)
  160. {
  161. using (new GUIViewportScope(sequenceContentRect))
  162. DrawOverlay(overlayData);
  163. }
  164. }
  165. }
  166. GUILayout.EndVertical();
  167. }
  168. void DrawOverlay(OverlayData overlayData)
  169. {
  170. Rect overlayRect = GUIClip.Clip(overlayData.rect);
  171. if (overlayData.types == OverlayDataTypes.BackgroundColor)
  172. {
  173. EditorGUI.DrawRect(overlayRect, overlayData.color);
  174. }
  175. else if (overlayData.types == OverlayDataTypes.BackgroundTexture)
  176. {
  177. Graphics.DrawTextureRepeated(overlayRect, overlayData.texture);
  178. }
  179. else if (overlayData.types == OverlayDataTypes.TextBox)
  180. {
  181. using (new GUIColorOverride(overlayData.backgroundTextStyle.normal.textColor))
  182. GUI.Box(overlayRect, GUIContent.none, overlayData.backgroundTextStyle);
  183. Graphics.ShadowLabel(overlayRect, GUIContent.Temp(overlayData.text), overlayData.textStyle, overlayData.textStyle.normal.textColor, Color.black);
  184. }
  185. }
  186. }
  187. }