InlineCurveEditor.cs
11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using System.Linq;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Timeline;
namespace UnityEditor.Timeline
{
interface IClipCurveEditorOwner
{
ClipCurveEditor clipCurveEditor { get; }
bool inlineCurvesSelected { get; set; }
bool showLoops { get; }
TrackAsset owner { get; }
}
class InlineCurveResizeHandle : IBounds
{
public Rect boundingRect { get; private set; }
public TimelineTrackGUI trackGUI { get; }
public InlineCurveResizeHandle(TimelineTrackGUI trackGUI)
{
this.trackGUI = trackGUI;
}
public void Draw(Rect headerRect, Rect trackRect, WindowState state)
{
var rect = new Rect(headerRect.xMax + 4, headerRect.yMax - 5.0f, trackRect.width - 4, 5.0f);
var handleColor = Handles.color;
Handles.color = Color.black;
Handles.DrawAAPolyLine(1.0f,
new Vector3(rect.x, rect.yMax, 0.0f),
new Vector3(rect.xMax, rect.yMax, 0.0f));
Handles.color = handleColor;
EditorGUIUtility.AddCursorRect(rect, MouseCursor.SplitResizeUpDown);
boundingRect = trackGUI.ToWindowSpace(rect);
if (Event.current.type == EventType.Repaint)
{
state.spacePartitioner.AddBounds(this);
var dragStyle = new GUIStyle("RL DragHandle");
dragStyle.Draw(rect, GUIContent.none, false, false, false, false);
}
}
}
class InlineCurveEditor : IBounds
{
Rect m_TrackRect;
Rect m_HeaderRect;
readonly TimelineTrackGUI m_TrackGUI;
readonly InlineCurveResizeHandle m_ResizeHandle;
bool m_LastSelectionWasClip;
TimelineClipGUI m_LastSelectedClipGUI;
Rect IBounds.boundingRect { get { return m_TrackGUI.ToWindowSpace(m_TrackRect); } }
[UsedImplicitly] // Used in tests
public TimelineClipGUI currentClipGui
{
get { return m_LastSelectedClipGUI; }
}
public IClipCurveEditorOwner currentCurveEditor
{
get { return m_LastSelectionWasClip ? (IClipCurveEditorOwner)m_LastSelectedClipGUI : (IClipCurveEditorOwner)m_TrackGUI; }
}
public InlineCurveEditor(TimelineTrackGUI trackGUI)
{
m_TrackGUI = trackGUI;
m_ResizeHandle = new InlineCurveResizeHandle(trackGUI);
}
static bool MouseOverTrackArea(Rect curveRect, Rect trackRect)
{
curveRect.y = trackRect.y;
curveRect.height = trackRect.height;
// clamp the curve editor to the track. this allows the menu to scroll properly
curveRect.xMin = Mathf.Max(curveRect.xMin, trackRect.xMin);
curveRect.xMax = trackRect.xMax;
return curveRect.Contains(Event.current.mousePosition);
}
static bool MouseOverHeaderArea(Rect headerRect, Rect trackRect)
{
headerRect.y = trackRect.y;
headerRect.height = trackRect.height;
return headerRect.Contains(Event.current.mousePosition);
}
static void DrawCurveEditor(IClipCurveEditorOwner clipCurveEditorOwner, WindowState state, Rect headerRect, Rect trackRect, Vector2 activeRange, bool locked)
{
ClipCurveEditor clipCurveEditor = clipCurveEditorOwner.clipCurveEditor;
CurveDataSource dataSource = clipCurveEditor.dataSource;
Rect curveRect = dataSource.GetBackgroundRect(state);
bool newlySelected = false;
if (Event.current.type == EventType.MouseDown || Event.current.type == EventType.ContextClick)
newlySelected = MouseOverTrackArea(curveRect, trackRect) || MouseOverHeaderArea(headerRect, trackRect);
// make sure to not use any event before drawing the curve.
bool prevEnabledState = GUI.enabled;
GUI.enabled = true;
clipCurveEditorOwner.clipCurveEditor.DrawHeader(headerRect);
GUI.enabled = prevEnabledState;
bool displayAsSelected = !locked && (clipCurveEditorOwner.inlineCurvesSelected || newlySelected);
using (new EditorGUI.DisabledScope(locked))
clipCurveEditor.DrawCurveEditor(trackRect, state, activeRange, clipCurveEditorOwner.showLoops, displayAsSelected);
if (newlySelected && !locked)
{
clipCurveEditorOwner.inlineCurvesSelected = true;
HandleCurrentEvent();
}
}
public void Draw(Rect headerRect, Rect trackRect, WindowState state)
{
m_TrackRect = trackRect;
m_TrackRect.height -= 5.0f;
if (Event.current.type == EventType.Repaint)
state.spacePartitioner.AddBounds(this);
// Remove the indentation of this track to render it properly, otherwise every GUI elements will be offsetted.
headerRect.x -= DirectorStyles.kBaseIndent;
headerRect.width += DirectorStyles.kBaseIndent;
// Remove the width of the color swatch.
headerRect.x += 4.0f;
headerRect.width -= 4.0f;
m_HeaderRect = headerRect;
EditorGUI.DrawRect(m_HeaderRect, DirectorStyles.Instance.customSkin.colorAnimEditorBinding);
if (ShouldShowClipCurves(state))
{
DrawCurveEditorsForClipsOnTrack(m_HeaderRect, m_TrackRect, state);
}
else if (ShouldShowTrackCurves())
{
DrawCurveEditorForTrack(m_HeaderRect, m_TrackRect, state);
}
else
{
DrawCurvesEditorForNothingSelected(m_HeaderRect, m_TrackRect, state);
}
m_ResizeHandle.Draw(headerRect, trackRect, state);
// If MouseDown or ContextClick are not consumed by the curves, use the event to prevent it from going deeper into the treeview.
if (Event.current.type == EventType.ContextClick)
{
var r = Rect.MinMaxRect(m_HeaderRect.xMin, m_HeaderRect.yMin, m_TrackRect.xMax, m_TrackRect.yMax);
if (r.Contains(Event.current.mousePosition))
Event.current.Use();
}
UpdateViewModel();
}
public void Refresh()
{
if (m_LastSelectionWasClip)
RefreshInlineCurves(m_LastSelectedClipGUI);
else
RefreshInlineCurves(m_TrackGUI);
}
static void RefreshInlineCurves(IClipCurveEditorOwner guiItem)
{
if (guiItem.clipCurveEditor != null && guiItem.clipCurveEditor.dataSource != null)
guiItem.clipCurveEditor.dataSource.RebuildCurves();
}
void DrawCurveEditorForTrack(Rect headerRect, Rect trackRect, WindowState state)
{
if (m_TrackGUI.clipCurveEditor == null)
return;
var activeRange = new Vector2(state.TimeToPixel(0.0d), state.TimeToPixel(state.editSequence.duration));
DrawCurveEditor(m_TrackGUI, state, headerRect, trackRect, activeRange, m_TrackGUI.locked);
m_LastSelectionWasClip = false;
}
void DrawCurveEditorsForClipsOnTrack(Rect headerRect, Rect trackRect, WindowState state)
{
if (m_TrackGUI.clips.Count == 0)
return;
if (Event.current.type == EventType.Layout)
{
TimelineClipGUI selectedClip = SelectionManager.SelectedClipGUI().FirstOrDefault(x => x.parent == m_TrackGUI);
if (selectedClip != null)
{
m_LastSelectedClipGUI = selectedClip;
}
else if (state.recording && state.IsArmedForRecord(m_TrackGUI.track))
{
if (m_LastSelectedClipGUI == null || !m_TrackGUI.track.IsRecordingToClip(m_LastSelectedClipGUI.clip))
{
var clip = m_TrackGUI.clips.FirstOrDefault(x => m_TrackGUI.track.IsRecordingToClip(x.clip));
if (clip != null)
m_LastSelectedClipGUI = clip;
}
}
if (m_LastSelectedClipGUI == null)
m_LastSelectedClipGUI = m_TrackGUI.clips[0];
}
if (m_LastSelectedClipGUI == null || m_LastSelectedClipGUI.clipCurveEditor == null || m_LastSelectedClipGUI.isInvalid)
return;
var activeRange = new Vector2(state.TimeToPixel(m_LastSelectedClipGUI.clip.start), state.TimeToPixel(m_LastSelectedClipGUI.clip.end));
DrawCurveEditor(m_LastSelectedClipGUI, state, headerRect, trackRect, activeRange, m_TrackGUI.locked);
m_LastSelectionWasClip = true;
}
void DrawCurvesEditorForNothingSelected(Rect headerRect, Rect trackRect, WindowState state)
{
if (m_LastSelectionWasClip || !TrackHasCurvesToShow() && m_TrackGUI.clips.Count > 0)
{
DrawCurveEditorsForClipsOnTrack(headerRect, trackRect, state);
}
else
{
DrawCurveEditorForTrack(headerRect, trackRect, state);
}
}
bool ShouldShowClipCurves(WindowState state)
{
if (m_TrackGUI.clips.Count == 0)
return false;
// Is a clip selected or being recorded to?
return SelectionManager.SelectedClipGUI().FirstOrDefault(x => x.parent == m_TrackGUI) != null ||
state.recording && state.IsArmedForRecord(m_TrackGUI.track) && m_TrackGUI.clips.FirstOrDefault(x => m_TrackGUI.track.IsRecordingToClip(x.clip)) != null;
}
bool ShouldShowTrackCurves()
{
if (m_TrackGUI == null)
return false;
var isTrackSelected = SelectionManager.SelectedTrackGUI().FirstOrDefault(x => x == m_TrackGUI) != null;
if (!isTrackSelected)
return false;
return TrackHasCurvesToShow();
}
bool TrackHasCurvesToShow()
{
var animTrack = m_TrackGUI.track as AnimationTrack;
if (animTrack != null && !animTrack.inClipMode)
return true;
return m_TrackGUI.track.HasAnyAnimatableParameters();
}
void UpdateViewModel()
{
var curveEditor = currentCurveEditor.clipCurveEditor;
if (curveEditor == null || curveEditor.bindingHierarchy.treeViewController == null)
return;
var vm = TimelineWindowViewPrefs.GetTrackViewModelData(m_TrackGUI.track);
vm.inlineCurvesState = curveEditor.bindingHierarchy.treeViewController.state;
vm.inlineCurvesShownAreaInsideMargins = curveEditor.shownAreaInsideMargins;
vm.lastInlineCurveDataID = curveEditor.dataSource.id;
}
static void HandleCurrentEvent()
{
#if UNITY_EDITOR_OSX
Event.current.type = EventType.Ignore;
#else
Event.current.Use();
#endif
}
}
}