TimelineSelection.cs
9.37 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Timeline;
namespace UnityEditor.Timeline
{
static class SelectionManager
{
static IClipCurveEditorOwner currentInlineEditorCurve;
public static void AddObject(Object obj)
{
if (obj == null)
return;
currentInlineEditorCurve = null;
if (Selection.Contains(obj))
return;
Selection.Add(obj);
}
public static void Add(TrackAsset obj)
{
// this can indicate a deleted track (Unity 'null')
if (obj == null)
return;
currentInlineEditorCurve = null;
if (!Selection.Contains(obj))
{
WindowState state = null;
if (TimelineWindow.instance != null)
state = TimelineWindow.instance.state;
if (!Selection.instanceIDs.Any() && state != null && state.editSequence.director != null)
Selection.SetActiveObjectWithContext(obj, TimelineWindow.instance.state.editSequence.director);
else
Selection.Add(obj);
}
}
public static void Add(TimelineClip item)
{
AddObject(EditorClipFactory.GetEditorClip(item));
}
public static void Add(IMarker marker)
{
var markerAsObject = marker as Object;
if (markerAsObject != null)
{
if (!Selection.Contains(markerAsObject))
{
currentInlineEditorCurve = null;
WindowState state = null;
if (TimelineWindow.instance != null)
state = TimelineWindow.instance.state;
if (!Selection.instanceIDs.Any() && state != null && state.editSequence.director != null)
Selection.SetActiveObjectWithContext(markerAsObject, TimelineWindow.instance.state.editSequence.director);
else
Selection.Add(markerAsObject);
}
}
}
public static void Add(ITimelineItem item)
{
var clipItem = item as ClipItem;
if (clipItem != null)
{
Add(clipItem.clip);
}
else
{
var markerItem = item as MarkerItem;
if (markerItem != null)
{
Add(markerItem.marker);
}
}
}
public static void SelectOnly(TrackAsset track)
{
RemoveTimelineSelection();
Add(track);
}
public static void SelectOnly(TimelineClip clip)
{
RemoveTimelineSelection();
Add(clip);
}
public static void SelectOnly(IMarker item)
{
RemoveTimelineSelection();
Add(item);
}
public static void SelectOnly(ITimelineItem item)
{
var clipItem = item as ClipItem;
if (clipItem != null)
{
SelectOnly(clipItem.clip);
}
else
{
var markerItem = item as MarkerItem;
if (markerItem != null)
{
SelectOnly(markerItem.marker);
}
}
}
public static void SelectInlineCurveEditor(IClipCurveEditorOwner selection)
{
// case 1030681 - selecting an inline curve should remove clips and
// tracks to prevent shortcuts being forwarded incorrectly
if (selection != null && selection != currentInlineEditorCurve)
{
var track = selection.owner;
var selectedTracks = SelectedTracks().ToArray();
var selectedClips = SelectedClips().ToArray();
foreach (var t in selectedTracks)
{
if (t != track)
Remove(t);
}
foreach (var c in selectedClips)
{
if (c.parentTrack != track)
Remove(c);
}
}
currentInlineEditorCurve = selection;
}
public static IClipCurveEditorOwner GetCurrentInlineEditorCurve()
{
return currentInlineEditorCurve;
}
public static bool IsCurveEditorFocused(IClipCurveEditorOwner selection)
{
return selection == currentInlineEditorCurve;
}
public static bool Contains(TrackAsset item)
{
if (item == null)
return false;
return Selection.Contains(item);
}
public static bool Contains(TimelineClip item)
{
if (item == null)
return false;
return Selection.Contains(EditorClipFactory.GetEditorClip(item));
}
public static bool Contains(Object obj)
{
return Selection.Contains(obj);
}
public static bool Contains(IMarker marker)
{
var markerAsObject = marker as Object;
return Selection.Contains(markerAsObject);
}
public static bool Contains(ITimelineItem item)
{
var clipItem = item as ClipItem;
if (clipItem != null)
{
return Contains(clipItem.clip);
}
var markerItem = item as MarkerItem;
return markerItem != null && Contains(markerItem.marker);
}
public static void Clear()
{
currentInlineEditorCurve = null;
Selection.activeObject = null;
}
public static void UnSelectTracks()
{
var newSelection = Selection.objects.Where(x => !(x is TrackAsset));
Selection.objects = newSelection.ToArray();
}
public static void Remove(TimelineClip item)
{
Remove(EditorClipFactory.GetEditorClip(item));
}
public static void Remove(IMarker marker)
{
var o = marker as Object;
if (o != null)
{
Remove(o);
}
}
static void Remove(Object item)
{
Selection.Remove(item);
}
public static void Remove(TrackAsset item)
{
Selection.Remove(item);
}
public static void Remove(ITimelineItem item)
{
var clipItem = item as ClipItem;
if (clipItem != null)
{
Remove(clipItem.clip);
}
else
{
var markerItem = item as MarkerItem;
if (markerItem != null)
{
Remove(markerItem.marker);
}
}
}
public static void RemoveTimelineSelection()
{
Selection.objects = Selection.objects.Where(s => !IsTimelineType(s)).ToArray();
}
public static void RemoveAllClips()
{
Selection.objects = Selection.objects.Where(s => !(s is EditorClip)).ToArray();
}
public static int Count()
{
return Selection.objects.Count(IsTimelineType);
}
public static IEnumerable<TimelineClipGUI> SelectedClipGUI()
{
foreach (var clip in SelectedClips())
{
var gui = ItemToItemGui.GetGuiForClip(clip);
if (gui != null)
yield return gui;
}
}
public static IEnumerable<TimelineClip> SelectedClips()
{
return Selection.objects.OfType<EditorClip>().Select(x => x.clip);
}
public static IEnumerable<IMarker> SelectedMarkers()
{
return Selection.objects.OfType<IMarker>();
}
public static IEnumerable<TrackAsset> SelectedTracks()
{
return Selection.objects.OfType<TrackAsset>();
}
public static IEnumerable<T> SelectedItemOfType<T>()
{
if (typeof(T) == typeof(TimelineClip))
return SelectedClips().Cast<T>();
if (typeof(T).IsAssignableFrom(typeof(IMarker)))
return SelectedMarkers().Cast<T>();
return Enumerable.Empty<T>();
}
public static IEnumerable<TimelineTrackBaseGUI> SelectedTrackGUI()
{
var tracks = SelectedTracks();
return TimelineWindow.instance.allTracks.Where(x => tracks.Contains(x.track));
}
static bool IsTimelineType(Object o)
{
return o is TrackAsset || o is EditorClip || o is IMarker;
}
public static IEnumerable<ITimelineItem> SelectedItems()
{
var list = new List<ITimelineItem>();
foreach (var obj in Selection.objects)
{
var editorClip = obj as EditorClip;
if (editorClip != null)
{
list.Add(new ClipItem(editorClip.clip));
continue;
}
var marker = obj as IMarker;
if (marker != null)
{
list.Add(new MarkerItem(marker));
}
}
return list;
}
}
}