TimelineTrackErrorGUI.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using UnityEditor.IMGUI.Controls;
  3. using UnityEngine;
  4. using UnityEngine.Playables;
  5. namespace UnityEditor.Timeline
  6. {
  7. class TimelineTrackErrorGUI : TimelineTrackBaseGUI
  8. {
  9. static class Styles
  10. {
  11. public static readonly GUIContent ErrorText = EditorGUIUtility.TrTextContent("Track cannot be loaded.", "Please fix any compile errors in the script for this track");
  12. public static readonly Texture2D IconWarn = EditorGUIUtility.LoadIconRequired("console.warnicon.inactive.sml");
  13. public static readonly GUIContent RemoveTrack = EditorGUIUtility.TrTextContent("Delete");
  14. public static readonly Color WarningBoxBackgroundColor = new Color(115.0f / 255.0f, 115.0f / 255.0f, 115.0f / 255.0f); // approved for both skins
  15. public static readonly Color WarningBoxHighlightColor = new Color(229 / 255.0f, 208 / 255.0f, 54 / 255.0f); // brigher than standard warning color for contrast
  16. }
  17. Rect m_TrackRect;
  18. ScriptableObject m_ScriptableObject;
  19. PlayableAsset m_Owner;
  20. static GUIContent s_GUIContent = new GUIContent();
  21. public TimelineTrackErrorGUI(TreeViewController treeview, TimelineTreeViewGUI treeviewGUI, int id, int depth, TreeViewItem parent, string displayName, ScriptableObject track, PlayableAsset owner)
  22. : base(id, depth, parent, displayName, null, treeview, treeviewGUI)
  23. {
  24. m_ScriptableObject = track;
  25. m_Owner = owner;
  26. }
  27. public override Rect boundingRect
  28. {
  29. get { return m_TrackRect; }
  30. }
  31. public override bool expandable
  32. {
  33. get { return false; }
  34. }
  35. public override void Draw(Rect headerRect, Rect contentRect, WindowState state)
  36. {
  37. m_TrackRect = contentRect;
  38. DrawMissingTrackHeader(headerRect, state);
  39. DrawMissingTrackBody(contentRect);
  40. }
  41. void DrawMissingTrackHeader(Rect headerRect, WindowState state)
  42. {
  43. var styles = DirectorStyles.Instance;
  44. // Draw a header
  45. Color backgroundColor = styles.customSkin.colorTrackHeaderBackground;
  46. var bgRect = headerRect;
  47. bgRect.x += styles.trackSwatchStyle.fixedWidth;
  48. bgRect.width -= styles.trackSwatchStyle.fixedWidth;
  49. EditorGUI.DrawRect(bgRect, backgroundColor);
  50. // draw the warning icon
  51. var errorIcon = Styles.IconWarn;
  52. Rect iconRect = new Rect(headerRect.xMin + styles.trackSwatchStyle.fixedWidth, headerRect.yMin + 0.5f * (headerRect.height - errorIcon.height), errorIcon.width, errorIcon.height);
  53. if (iconRect.width > 0 && iconRect.height > 0)
  54. {
  55. GUI.DrawTexture(iconRect, errorIcon, ScaleMode.ScaleAndCrop, true, 0, DirectorStyles.kClipErrorColor, 0, 0);
  56. }
  57. // Draw the name
  58. // m_ScriptableObject == null will return true because the script can't be loaded. so this checks
  59. // to make sure it is actually not null so we can grab the name
  60. object o = m_ScriptableObject;
  61. if (o != null)
  62. {
  63. s_GUIContent.text = m_ScriptableObject.name;
  64. var textStyle = styles.trackHeaderFont;
  65. textStyle.normal.textColor = styles.customSkin.colorTrackFont; // TODO -- we shouldn't modify the style like this. track header does it though :(
  66. Rect textRect = headerRect;
  67. textRect.xMin = iconRect.xMax + 1;
  68. textRect.xMax = Math.Min(textRect.xMin + styles.trackHeaderFont.CalcSize(s_GUIContent).x, headerRect.xMax - 1);
  69. EditorGUI.LabelField(textRect, s_GUIContent, textStyle);
  70. }
  71. // Draw the color swatch to the left of the track, darkened by the mute
  72. var color = Color.Lerp(DirectorStyles.kClipErrorColor, styles.customSkin.colorTrackDarken, styles.customSkin.colorTrackDarken.a);
  73. color.a = 1;
  74. using (new GUIColorOverride(color))
  75. {
  76. var colorSwatchRect = headerRect;
  77. colorSwatchRect.width = styles.trackSwatchStyle.fixedWidth;
  78. GUI.Label(colorSwatchRect, GUIContent.none, styles.trackSwatchStyle);
  79. }
  80. // draw darken overlay
  81. EditorGUI.DrawRect(bgRect, styles.customSkin.colorTrackDarken);
  82. DrawRemoveMenu(headerRect, state);
  83. }
  84. void DrawRemoveMenu(Rect headerRect, WindowState state)
  85. {
  86. const float pad = 3;
  87. const float buttonSize = 16;
  88. var buttonRect = new Rect(headerRect.xMax - buttonSize - pad, headerRect.y + ((headerRect.height - buttonSize) / 2f) + 2, buttonSize, buttonSize);
  89. if (GUI.Button(buttonRect, GUIContent.none, DirectorStyles.Instance.trackOptions))
  90. {
  91. GenericMenu menu = new GenericMenu();
  92. var owner = m_Owner;
  93. var scriptableObject = m_ScriptableObject;
  94. menu.AddItem(Styles.RemoveTrack, false, () =>
  95. {
  96. if (TrackExtensions.RemoveBrokenTrack(owner, scriptableObject))
  97. state.Refresh();
  98. }
  99. );
  100. menu.ShowAsContext();
  101. }
  102. }
  103. static void DrawMissingTrackBody(Rect contentRect)
  104. {
  105. if (contentRect.width < 0)
  106. return;
  107. var styles = DirectorStyles.Instance;
  108. // draw a track rectangle
  109. EditorGUI.DrawRect(contentRect, styles.customSkin.colorTrackDarken);
  110. // draw the warning box
  111. DrawScriptWarningBox(contentRect, Styles.ErrorText);
  112. }
  113. static void DrawScriptWarningBox(Rect trackRect, GUIContent content)
  114. {
  115. var styles = DirectorStyles.Instance;
  116. const float kTextPadding = 52f;
  117. var errorIcon = Styles.IconWarn;
  118. float textWidth = styles.fontClip.CalcSize(content).x;
  119. var outerRect = trackRect;
  120. outerRect.width = textWidth + kTextPadding + errorIcon.width;
  121. outerRect.x += (trackRect.width - outerRect.width) / 2f;
  122. outerRect.height -= 4f;
  123. outerRect.y += 1f;
  124. bool drawText = true;
  125. if (outerRect.width > trackRect.width)
  126. {
  127. outerRect.x = trackRect.x;
  128. outerRect.width = trackRect.width;
  129. drawText = false;
  130. }
  131. var innerRect = new Rect(outerRect.x + 2, outerRect.y + 2, outerRect.width - 4, outerRect.height - 4);
  132. using (new GUIColorOverride(Styles.WarningBoxHighlightColor))
  133. GUI.Box(outerRect, GUIContent.none, styles.displayBackground);
  134. using (new GUIColorOverride(Styles.WarningBoxBackgroundColor))
  135. GUI.Box(innerRect, GUIContent.none, styles.displayBackground);
  136. if (drawText)
  137. {
  138. var iconRect = new Rect(outerRect.x + kTextPadding / 2.0f - 4.0f, outerRect.y + (outerRect.height - errorIcon.height) / 2.0f, errorIcon.width, errorIcon.height);
  139. var textRect = new Rect(iconRect.xMax + 4.0f, outerRect.y, textWidth, outerRect.height);
  140. GUI.DrawTexture(iconRect, errorIcon, ScaleMode.ScaleAndCrop, true, 0, Styles.WarningBoxHighlightColor, 0, 0);
  141. Graphics.ShadowLabel(textRect, content, styles.fontClip, Color.white, Color.black);
  142. }
  143. else if (errorIcon.width > innerRect.width)
  144. {
  145. var iconRect = new Rect(outerRect.x + (outerRect.width - errorIcon.width) / 2.0f, outerRect.y + (outerRect.height - errorIcon.height) / 2.0f, errorIcon.width, errorIcon.height);
  146. GUI.DrawTexture(iconRect, errorIcon, ScaleMode.ScaleAndCrop, true, 0, Styles.WarningBoxHighlightColor, 0, 0);
  147. }
  148. }
  149. public override void OnGraphRebuilt() {}
  150. }
  151. }