ClipsActions.cs
14.1 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables;
using ClipAction = UnityEditor.Timeline.ItemAction<UnityEngine.Timeline.TimelineClip>;
namespace UnityEditor.Timeline
{
[MenuEntry("Edit in Animation Window", MenuOrder.ClipEditAction.EditInAnimationWindow), UsedImplicitly]
class EditClipInAnimationWindow : ClipAction
{
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
if (clips.Length == 1 && clips[0].animationClip != null)
return MenuActionDisplayState.Visible;
return MenuActionDisplayState.Hidden;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
var clip = clips[0];
if (clip.curves != null || clip.animationClip != null)
{
var clipToEdit = clip.animationClip != null ? clip.animationClip : clip.curves;
if (clipToEdit == null)
return false;
var gameObject = state.GetSceneReference(clip.parentTrack);
var timeController = TimelineAnimationUtilities.CreateTimeController(state, clip);
TimelineAnimationUtilities.EditAnimationClipWithTimeController(
clipToEdit, timeController, clip.animationClip != null ? gameObject : null);
return true;
}
return false;
}
}
[MenuEntry("Edit Sub-Timeline", MenuOrder.ClipEditAction.EditSubTimeline), UsedImplicitly]
class EditSubTimeline : ClipAction
{
private static readonly string MultiItemPrefix = "Edit Sub-Timelines/";
private static readonly string SingleItemPrefix = "Edit ";
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
return IsValid(state, clips) ? MenuActionDisplayState.Visible : MenuActionDisplayState.Hidden;
}
bool IsValid(WindowState state, TimelineClip[] clips)
{
if (clips.Length != 1 || state == null || state.editSequence.director == null) return false;
var clip = clips[0];
var directors = TimelineUtility.GetSubTimelines(clip, state.editSequence.director);
return directors.Any(x => x != null);
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
if (!IsValid(state, clips)) return false;
var clip = clips[0];
var directors = TimelineUtility.GetSubTimelines(clip, state.editSequence.director);
ExecuteInternal(state, directors, 0, clip);
return true;
}
static void ExecuteInternal(WindowState state, IList<PlayableDirector> directors, int directorIndex, TimelineClip clip)
{
SelectionManager.Clear();
state.GetWindow().SetCurrentTimeline(directors[directorIndex], clip);
}
protected override void AddMenuItem(WindowState state, TimelineClip[] items, List<MenuActionItem> menuItems)
{
if (items == null || items.Length != 1)
return;
var mode = TimelineWindow.instance.currentMode.mode;
var menuItem = new MenuActionItem()
{
category = category,
entryName = GetDisplayName(items),
shortCut = string.Empty,
isChecked = false,
isActiveInMode = IsActionActiveInMode(this, mode),
priority = priority,
state = GetDisplayState(state, items),
callback = null
};
var subDirectors = TimelineUtility.GetSubTimelines(items[0], state.editSequence.director);
if (subDirectors.Count == 1)
{
menuItem.entryName = SingleItemPrefix + DisplayNameHelper.GetDisplayName(subDirectors[0]);
menuItem.callback = () => Execute(state, items);
menuItems.Add(menuItem);
}
else
{
for (int i = 0; i < subDirectors.Count; i++)
{
var index = i;
menuItem.category = MultiItemPrefix;
menuItem.entryName = DisplayNameHelper.GetDisplayName(subDirectors[i]);
menuItem.callback = () => ExecuteInternal(state, subDirectors, index, items[0]);
menuItems.Add(menuItem);
}
}
}
}
[MenuEntry("Editing/Trim Start", MenuOrder.ClipAction.TrimStart)]
[Shortcut(Shortcuts.Clip.trimStart), UsedImplicitly]
class TrimStart : ItemAction<TimelineClip>
{
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
return clips.All(x => state.editSequence.time <= x.start || state.editSequence.time >= x.start + x.duration) ?
MenuActionDisplayState.Disabled : MenuActionDisplayState.Visible;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
return ClipModifier.TrimStart(clips, state.editSequence.time);
}
}
[MenuEntry("Editing/Trim End", MenuOrder.ClipAction.TrimEnd), UsedImplicitly]
[Shortcut(Shortcuts.Clip.trimEnd)]
class TrimEnd : ItemAction<TimelineClip>
{
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
return clips.All(x => state.editSequence.time <= x.start || state.editSequence.time >= x.start + x.duration) ?
MenuActionDisplayState.Disabled : MenuActionDisplayState.Visible;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
return ClipModifier.TrimEnd(clips, state.editSequence.time);
}
}
[Shortcut(Shortcuts.Clip.split), MenuEntry("Editing/Split", MenuOrder.ClipAction.Split), UsedImplicitly]
class Split : ClipAction
{
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
return clips.All(x => state.editSequence.time <= x.start || state.editSequence.time >= x.start + x.duration) ?
MenuActionDisplayState.Disabled : MenuActionDisplayState.Visible;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
bool success = ClipModifier.Split(clips, state.editSequence.time, state.editSequence.director);
if (success)
state.Refresh();
return success;
}
}
[MenuEntry("Editing/Complete Last Loop", MenuOrder.ClipAction.CompleteLastLoop), UsedImplicitly]
class CompleteLastLoop : ClipAction
{
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
bool canDisplay = clips.Any(TimelineHelpers.HasUsableAssetDuration);
return canDisplay ? MenuActionDisplayState.Visible : MenuActionDisplayState.Disabled;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
return ClipModifier.CompleteLastLoop(clips);
}
}
[MenuEntry("Editing/Trim Last Loop", MenuOrder.ClipAction.TrimLastLoop), UsedImplicitly]
class TrimLastLoop : ClipAction
{
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
bool canDisplay = clips.Any(TimelineHelpers.HasUsableAssetDuration);
return canDisplay ? MenuActionDisplayState.Visible : MenuActionDisplayState.Disabled;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
return ClipModifier.TrimLastLoop(clips);
}
}
[MenuEntry("Editing/Match Duration", MenuOrder.ClipAction.MatchDuration), UsedImplicitly]
class MatchDuration : ClipAction
{
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
return clips.Length > 1 ? MenuActionDisplayState.Visible : MenuActionDisplayState.Disabled;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
return ClipModifier.MatchDuration(clips);
}
}
[MenuEntry("Editing/Double Speed", MenuOrder.ClipAction.DoubleSpeed), UsedImplicitly]
class DoubleSpeed : ClipAction
{
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
bool canDisplay = clips.All(x => x.SupportsSpeedMultiplier());
return canDisplay ? MenuActionDisplayState.Visible : MenuActionDisplayState.Disabled;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
return ClipModifier.DoubleSpeed(clips);
}
}
[MenuEntry("Editing/Half Speed", MenuOrder.ClipAction.HalfSpeed), UsedImplicitly]
class HalfSpeed : ClipAction
{
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
bool canDisplay = clips.All(x => x.SupportsSpeedMultiplier());
return canDisplay ? MenuActionDisplayState.Visible : MenuActionDisplayState.Disabled;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
return ClipModifier.HalfSpeed(clips);
}
}
[MenuEntry("Editing/Reset Duration", MenuOrder.ClipAction.ResetDuration), UsedImplicitly]
class ResetDuration : ClipAction
{
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
bool canDisplay = clips.Any(TimelineHelpers.HasUsableAssetDuration);
return canDisplay ? MenuActionDisplayState.Visible : MenuActionDisplayState.Disabled;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
return ClipModifier.ResetEditing(clips);
}
}
[MenuEntry("Editing/Reset Speed", MenuOrder.ClipAction.ResetSpeed), UsedImplicitly]
class ResetSpeed : ClipAction
{
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
bool canDisplay = clips.All(x => x.SupportsSpeedMultiplier());
return canDisplay ? MenuActionDisplayState.Visible : MenuActionDisplayState.Disabled;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
return ClipModifier.ResetSpeed(clips);
}
}
[MenuEntry("Editing/Reset All", MenuOrder.ClipAction.ResetAll), UsedImplicitly]
class ResetAll : ClipAction
{
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
bool canDisplay = clips.Any(TimelineHelpers.HasUsableAssetDuration) ||
clips.All(x => x.SupportsSpeedMultiplier());
return canDisplay ? MenuActionDisplayState.Visible : MenuActionDisplayState.Disabled;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
var speedResult = ClipModifier.ResetSpeed(clips);
var editResult = ClipModifier.ResetEditing(clips);
return speedResult || editResult;
}
}
[MenuEntry("Tile", MenuOrder.ClipAction.Tile), UsedImplicitly]
class Tile : ClipAction
{
protected override MenuActionDisplayState GetDisplayState(WindowState state, TimelineClip[] clips)
{
return clips.Length > 1 ? MenuActionDisplayState.Visible : MenuActionDisplayState.Disabled;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
return ClipModifier.Tile(clips);
}
}
[MenuEntry("Find Source Asset", MenuOrder.ClipAction.FindSourceAsset), UsedImplicitly]
[ActiveInMode(TimelineModes.Default | TimelineModes.ReadOnly)]
class FindSourceAsset : ClipAction
{
protected override MenuActionDisplayState GetDisplayState(WindowState state,
TimelineClip[] clips)
{
if (clips.Length > 1)
return MenuActionDisplayState.Disabled;
if (GetUnderlyingAsset(state, clips[0]) == null)
return MenuActionDisplayState.Disabled;
return MenuActionDisplayState.Visible;
}
public override bool Execute(WindowState state, TimelineClip[] clips)
{
EditorGUIUtility.PingObject(GetUnderlyingAsset(state, clips[0]));
return true;
}
private static UnityEngine.Object GetExternalPlayableAsset(TimelineClip clip)
{
if (clip.asset == null)
return null;
if ((clip.asset.hideFlags & HideFlags.HideInHierarchy) != 0)
return null;
return clip.asset;
}
private static UnityEngine.Object GetUnderlyingAsset(WindowState state, TimelineClip clip)
{
var asset = clip.asset as ScriptableObject;
if (asset == null)
return null;
var fields = ObjectReferenceField.FindObjectReferences(asset.GetType());
if (fields.Length == 0)
return GetExternalPlayableAsset(clip);
// Find the first non-null field
foreach (var field in fields)
{
// skip scene refs in asset mode
if (state.editSequence.director == null && field.isSceneReference)
continue;
var obj = field.Find(asset, state.editSequence.director);
if (obj != null)
return obj;
}
return GetExternalPlayableAsset(clip);
}
}
class CopyClipsToClipboard : ClipAction
{
public override bool Execute(WindowState state, TimelineClip[] clips)
{
TimelineEditor.clipboard.CopyItems(clips.ToItems());
return true;
}
}
}