DirectorStyles.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using UnityEditor.Experimental;
  2. using UnityEditor.StyleSheets;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. class DirectorStyles
  8. {
  9. const string k_Elipsis = "…";
  10. const string k_ImagePath = "Packages/com.unity.timeline/Editor/StyleSheets/Images/Icons/{0}.png";
  11. public const string resourcesPath = "Packages/com.unity.timeline/Editor/StyleSheets/res/";
  12. //Timeline resources
  13. public static readonly GUIContent referenceTrackLabel = TrTextContent("R", "This track references an external asset");
  14. public static readonly GUIContent recordingLabel = TrTextContent("Recording...");
  15. public static readonly GUIContent noTimelineAssetSelected = TrTextContent("To start creating a timeline, select a GameObject");
  16. public static readonly GUIContent createTimelineOnSelection = TrTextContent("To begin a new timeline with {0}, create {1}");
  17. public static readonly GUIContent noTimelinesInScene = TrTextContent("No timeline found in the scene");
  18. public static readonly GUIContent createNewTimelineText = TrTextContent("Create a new Timeline and Director Component for Game Object");
  19. public static readonly GUIContent previewContent = TrTextContent("Preview", "Enable/disable scene preview mode");
  20. public static readonly GUIContent mixOff = TrIconContent("TimelineEditModeMixOFF", "Mix Mode (1)");
  21. public static readonly GUIContent mixOn = TrIconContent("TimelineEditModeMixON", "Mix Mode (1)");
  22. public static readonly GUIContent rippleOff = TrIconContent("TimelineEditModeRippleOFF", "Ripple Mode (2)");
  23. public static readonly GUIContent rippleOn = TrIconContent("TimelineEditModeRippleON", "Ripple Mode (2)");
  24. public static readonly GUIContent replaceOff = TrIconContent("TimelineEditModeReplaceOFF", "Replace Mode (3)");
  25. public static readonly GUIContent replaceOn = TrIconContent("TimelineEditModeReplaceON", "Replace Mode (3)");
  26. public static readonly GUIContent showMarkersOn = TrIconContent("TimelineMarkerAreaButtonEnabled", "Show / Hide Timeline Markers");
  27. public static readonly GUIContent showMarkersOff = TrIconContent("TimelineMarkerAreaButtonDisabled", "Show / Hide Timeline Markers");
  28. public static readonly GUIContent showMarkersOnTimeline = TrTextContent("Show markers");
  29. public static readonly GUIContent timelineMarkerTrackHeader = TrTextContentWithIcon("Markers", string.Empty, "TimelineHeaderMarkerIcon");
  30. public static readonly GUIContent markerCollapseButton = TrTextContent(string.Empty, "Expand / Collapse Track Markers");
  31. public static readonly GUIContent signalTrackIcon = IconContent("TimelineSignal");
  32. //Unity Default Resources
  33. public static readonly GUIContent playContent = EditorGUIUtility.TrIconContent("Animation.Play", "Play the timeline (Space)");
  34. public static readonly GUIContent gotoBeginingContent = EditorGUIUtility.TrIconContent("Animation.FirstKey", "Go to the beginning of the timeline (Shift+<)");
  35. public static readonly GUIContent gotoEndContent = EditorGUIUtility.TrIconContent("Animation.LastKey", "Go to the end of the timeline (Shift+>)");
  36. public static readonly GUIContent nextFrameContent = EditorGUIUtility.TrIconContent("Animation.NextKey", "Go to the next frame");
  37. public static readonly GUIContent previousFrameContent = EditorGUIUtility.TrIconContent("Animation.PrevKey", "Go to the previous frame");
  38. public static readonly GUIContent newContent = EditorGUIUtility.IconContent("CreateAddNew", "Add new tracks.");
  39. public static readonly GUIContent optionsCogIcon = EditorGUIUtility.TrIconContent("_Popup", "Options");
  40. public static readonly GUIContent animationTrackIcon = EditorGUIUtility.IconContent("AnimationClip Icon");
  41. public static readonly GUIContent audioTrackIcon = EditorGUIUtility.IconContent("AudioSource Icon");
  42. public static readonly GUIContent playableTrackIcon = EditorGUIUtility.IconContent("cs Script Icon");
  43. public static readonly GUIContent timelineSelectorArrow = EditorGUIUtility.TrIconContent("icon dropdown", "Timeline Selector");
  44. public GUIContent playrangeContent;
  45. public static readonly float kBaseIndent = 15.0f;
  46. public static readonly float kDurationGuiThickness = 5.0f;
  47. // matches dark skin warning color.
  48. public static readonly Color kClipErrorColor = new Color(0.957f, 0.737f, 0.008f, 1f);
  49. // TODO: Make skinnable? If we do, we should probably also make the associated cursors skinnable...
  50. public static readonly Color kMixToolColor = Color.white;
  51. public static readonly Color kRippleToolColor = new Color(255f / 255f, 210f / 255f, 51f / 255f);
  52. public static readonly Color kReplaceToolColor = new Color(165f / 255f, 30f / 255f, 30f / 255f);
  53. public const string markerDefaultStyle = "MarkerItem";
  54. public GUIStyle groupBackground;
  55. public GUIStyle displayBackground;
  56. public GUIStyle fontClip;
  57. public GUIStyle fontClipLoop;
  58. public GUIStyle trackHeaderFont;
  59. public GUIStyle trackGroupAddButton;
  60. public GUIStyle groupFont;
  61. public GUIStyle timeCursor;
  62. public GUIStyle endmarker;
  63. public GUIStyle tinyFont;
  64. public GUIStyle foldout;
  65. public GUIStyle mute;
  66. public GUIStyle locked;
  67. public GUIStyle autoKey;
  68. public GUIStyle playTimeRangeStart;
  69. public GUIStyle playTimeRangeEnd;
  70. public GUIStyle selectedStyle;
  71. public GUIStyle trackSwatchStyle;
  72. public GUIStyle connector;
  73. public GUIStyle keyframe;
  74. public GUIStyle warning;
  75. public GUIStyle extrapolationHold;
  76. public GUIStyle extrapolationLoop;
  77. public GUIStyle extrapolationPingPong;
  78. public GUIStyle extrapolationContinue;
  79. public GUIStyle collapseMarkers;
  80. public GUIStyle markerMultiOverlay;
  81. public GUIStyle bottomShadow;
  82. public GUIStyle trackOptions;
  83. public GUIStyle infiniteTrack;
  84. public GUIStyle clipOut;
  85. public GUIStyle clipIn;
  86. public GUIStyle curves;
  87. public GUIStyle lockedBG;
  88. public GUIStyle activation;
  89. public GUIStyle playrange;
  90. public GUIStyle lockButton;
  91. public GUIStyle avatarMaskOn;
  92. public GUIStyle avatarMaskOff;
  93. public GUIStyle markerWarning;
  94. public GUIStyle editModeBtn;
  95. public GUIStyle showMarkersBtn;
  96. public GUIStyle sequenceSwitcher;
  97. static internal DirectorStyles s_Instance;
  98. DirectorNamedColor m_DarkSkinColors;
  99. DirectorNamedColor m_LightSkinColors;
  100. DirectorNamedColor m_DefaultSkinColors;
  101. const string k_DarkSkinPath = resourcesPath + "Timeline_DarkSkin.txt";
  102. const string k_LightSkinPath = resourcesPath + "Timeline_LightSkin.txt";
  103. static readonly GUIContent s_TempContent = new GUIContent();
  104. public static bool IsInitialized
  105. {
  106. get { return s_Instance != null; }
  107. }
  108. public static DirectorStyles Instance
  109. {
  110. get
  111. {
  112. if (s_Instance == null)
  113. {
  114. s_Instance = new DirectorStyles();
  115. s_Instance.Initialize();
  116. }
  117. return s_Instance;
  118. }
  119. }
  120. public static void ReloadStylesIfNeeded()
  121. {
  122. if (Instance.ShouldLoadStyles())
  123. {
  124. Instance.LoadStyles();
  125. if (!Instance.ShouldLoadStyles())
  126. Instance.Initialize();
  127. }
  128. }
  129. public DirectorNamedColor customSkin
  130. {
  131. get { return EditorGUIUtility.isProSkin ? m_DarkSkinColors : m_LightSkinColors; }
  132. internal set
  133. {
  134. if (EditorGUIUtility.isProSkin)
  135. m_DarkSkinColors = value;
  136. else
  137. m_LightSkinColors = value;
  138. }
  139. }
  140. DirectorNamedColor LoadColorSkin(string path)
  141. {
  142. var asset = EditorGUIUtility.LoadRequired(path) as TextAsset;
  143. if (asset != null && !string.IsNullOrEmpty(asset.text))
  144. {
  145. return DirectorNamedColor.CreateAndLoadFromText(asset.text);
  146. }
  147. return m_DefaultSkinColors;
  148. }
  149. static DirectorNamedColor CreateDefaultSkin()
  150. {
  151. var nc = ScriptableObject.CreateInstance<DirectorNamedColor>();
  152. nc.SetDefault();
  153. return nc;
  154. }
  155. public void ExportSkinToFile()
  156. {
  157. if (customSkin == m_DarkSkinColors)
  158. customSkin.ToText(k_DarkSkinPath);
  159. if (customSkin == m_LightSkinColors)
  160. customSkin.ToText(k_LightSkinPath);
  161. }
  162. public void ReloadSkin()
  163. {
  164. if (customSkin == m_DarkSkinColors)
  165. {
  166. m_DarkSkinColors = LoadColorSkin(k_DarkSkinPath);
  167. }
  168. else if (customSkin == m_LightSkinColors)
  169. {
  170. m_LightSkinColors = LoadColorSkin(k_LightSkinPath);
  171. }
  172. }
  173. public void Initialize()
  174. {
  175. m_DefaultSkinColors = CreateDefaultSkin();
  176. m_DarkSkinColors = LoadColorSkin(k_DarkSkinPath);
  177. m_LightSkinColors = LoadColorSkin(k_LightSkinPath);
  178. // add the built in colors (control track uses attribute)
  179. TrackResourceCache.ClearTrackColorCache();
  180. TrackResourceCache.SetTrackColor<AnimationTrack>(customSkin.colorAnimation);
  181. TrackResourceCache.SetTrackColor<PlayableTrack>(Color.white);
  182. TrackResourceCache.SetTrackColor<AudioTrack>(customSkin.colorAudio);
  183. TrackResourceCache.SetTrackColor<ActivationTrack>(customSkin.colorActivation);
  184. TrackResourceCache.SetTrackColor<GroupTrack>(customSkin.colorGroup);
  185. TrackResourceCache.SetTrackColor<ControlTrack>(customSkin.colorControl);
  186. // add default icons
  187. TrackResourceCache.ClearTrackIconCache();
  188. TrackResourceCache.SetTrackIcon<AnimationTrack>(animationTrackIcon);
  189. TrackResourceCache.SetTrackIcon<AudioTrack>(audioTrackIcon);
  190. TrackResourceCache.SetTrackIcon<PlayableTrack>(playableTrackIcon);
  191. TrackResourceCache.SetTrackIcon<ActivationTrack>(new GUIContent(GetBackgroundImage(activation)));
  192. TrackResourceCache.SetTrackIcon<SignalTrack>(signalTrackIcon);
  193. }
  194. DirectorStyles()
  195. {
  196. LoadStyles();
  197. }
  198. bool ShouldLoadStyles()
  199. {
  200. return endmarker == null ||
  201. endmarker.name == GUISkin.error.name;
  202. }
  203. void LoadStyles()
  204. {
  205. endmarker = GetGUIStyle("Icon-Endmarker");
  206. groupBackground = GetGUIStyle("groupBackground");
  207. displayBackground = GetGUIStyle("sequenceClip");
  208. fontClip = GetGUIStyle("Font-Clip");
  209. trackHeaderFont = GetGUIStyle("sequenceTrackHeaderFont");
  210. trackGroupAddButton = GetGUIStyle("sequenceTrackGroupAddButton");
  211. groupFont = GetGUIStyle("sequenceGroupFont");
  212. timeCursor = GetGUIStyle("Icon-TimeCursor");
  213. tinyFont = GetGUIStyle("tinyFont");
  214. foldout = GetGUIStyle("Icon-Foldout");
  215. mute = GetGUIStyle("Icon-Mute");
  216. locked = GetGUIStyle("Icon-Locked");
  217. autoKey = GetGUIStyle("Icon-AutoKey");
  218. playTimeRangeStart = GetGUIStyle("Icon-PlayAreaStart");
  219. playTimeRangeEnd = GetGUIStyle("Icon-PlayAreaEnd");
  220. selectedStyle = GetGUIStyle("Color-Selected");
  221. trackSwatchStyle = GetGUIStyle("Icon-TrackHeaderSwatch");
  222. connector = GetGUIStyle("Icon-Connector");
  223. keyframe = GetGUIStyle("Icon-Keyframe");
  224. warning = GetGUIStyle("Icon-Warning");
  225. extrapolationHold = GetGUIStyle("Icon-ExtrapolationHold");
  226. extrapolationLoop = GetGUIStyle("Icon-ExtrapolationLoop");
  227. extrapolationPingPong = GetGUIStyle("Icon-ExtrapolationPingPong");
  228. extrapolationContinue = GetGUIStyle("Icon-ExtrapolationContinue");
  229. bottomShadow = GetGUIStyle("Icon-Shadow");
  230. trackOptions = GetGUIStyle("Icon-TrackOptions");
  231. infiniteTrack = GetGUIStyle("Icon-InfiniteTrack");
  232. clipOut = GetGUIStyle("Icon-ClipOut");
  233. clipIn = GetGUIStyle("Icon-ClipIn");
  234. curves = GetGUIStyle("Icon-Curves");
  235. lockedBG = GetGUIStyle("Icon-LockedBG");
  236. activation = GetGUIStyle("Icon-Activation");
  237. playrange = GetGUIStyle("Icon-Playrange");
  238. lockButton = GetGUIStyle("IN LockButton");
  239. avatarMaskOn = GetGUIStyle("Icon-AvatarMaskOn");
  240. avatarMaskOff = GetGUIStyle("Icon-AvatarMaskOff");
  241. collapseMarkers = GetGUIStyle("TrackCollapseMarkerButton");
  242. markerMultiOverlay = GetGUIStyle("MarkerMultiOverlay");
  243. editModeBtn = GetGUIStyle("editModeBtn");
  244. showMarkersBtn = GetGUIStyle("showMarkerBtn");
  245. markerWarning = GetGUIStyle("markerWarningOverlay");
  246. sequenceSwitcher = GetGUIStyle("sequenceSwitcher");
  247. playrangeContent = new GUIContent(GetBackgroundImage(playrange)) { tooltip = "Toggle play range markers." };
  248. fontClipLoop = new GUIStyle(fontClip) { fontStyle = FontStyle.Bold };
  249. }
  250. public static GUIStyle GetGUIStyle(string s)
  251. {
  252. return EditorStyles.FromUSS(s);
  253. }
  254. public static GUIContent TrIconContent(string iconName, string tooltip = null)
  255. {
  256. return EditorGUIUtility.TrIconContent(iconName == null ? null : ResolveIcon(iconName), tooltip);
  257. }
  258. public static GUIContent IconContent(string iconName)
  259. {
  260. return EditorGUIUtility.IconContent(iconName == null ? null : ResolveIcon(iconName));
  261. }
  262. public static GUIContent TrTextContentWithIcon(string text, string tooltip, string iconName)
  263. {
  264. return EditorGUIUtility.TrTextContentWithIcon(text, tooltip, iconName == null ? null : ResolveIcon(iconName));
  265. }
  266. public static GUIContent TrTextContent(string text, string tooltip = null)
  267. {
  268. return EditorGUIUtility.TrTextContent(text, tooltip);
  269. }
  270. public static Texture2D LoadIcon(string iconName)
  271. {
  272. return EditorGUIUtility.LoadIconRequired(iconName == null ? null : ResolveIcon(iconName));
  273. }
  274. static string ResolveIcon(string icon)
  275. {
  276. return string.Format(k_ImagePath, icon);
  277. }
  278. public static string Elipsify(string label, Rect rect, GUIStyle style)
  279. {
  280. var ret = label;
  281. if (label.Length == 0)
  282. return ret;
  283. s_TempContent.text = label;
  284. float neededWidth = style.CalcSize(s_TempContent).x;
  285. return Elipsify(label, rect.width, neededWidth);
  286. }
  287. public static string Elipsify(string label, float destinationWidth, float neededWidth)
  288. {
  289. var ret = label;
  290. if (label.Length == 0)
  291. return ret;
  292. if (destinationWidth < neededWidth)
  293. {
  294. float averageWidthOfOneChar = neededWidth / label.Length;
  295. int floor = Mathf.Max((int)Mathf.Floor(destinationWidth / averageWidthOfOneChar), 0);
  296. if (floor < k_Elipsis.Length)
  297. ret = string.Empty;
  298. else if (floor == k_Elipsis.Length)
  299. ret = k_Elipsis;
  300. else if (floor < label.Length)
  301. ret = label.Substring(0, floor - k_Elipsis.Length) + k_Elipsis;
  302. }
  303. return ret;
  304. }
  305. public static Texture2D GetBackgroundImage(GUIStyle style, StyleState state = StyleState.normal)
  306. {
  307. var blockName = GUIStyleExtensions.StyleNameToBlockName(style.name, false);
  308. var styleBlock = EditorResources.GetStyle(blockName, state);
  309. return styleBlock.GetTexture(StyleCatalogKeyword.backgroundImage);
  310. }
  311. }
  312. }