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