EditMode.cs
11.3 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
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
namespace UnityEditor.Timeline
{
static class EditMode
{
public enum EditType
{
None = -1,
Mix = 0,
Ripple = 1,
Replace = 2
}
interface ISubEditMode
{
IMoveItemMode moveItemMode { get; }
IMoveItemDrawer moveItemDrawer { get; }
ITrimItemMode trimItemMode { get; }
ITrimItemDrawer trimItemDrawer { get; }
IAddDeleteItemMode addDeleteItemMode { get; }
Color color { get; }
KeyCode clutchKey { get; }
void Reset();
}
class SubEditMode<TMoveMode, TTrimMode, TAddDeleteMode>: ISubEditMode
where TMoveMode : class, IMoveItemMode, IMoveItemDrawer, new()
where TTrimMode : class, ITrimItemMode, ITrimItemDrawer, new()
where TAddDeleteMode : class, IAddDeleteItemMode, new()
{
public SubEditMode(Color guiColor, KeyCode key)
{
color = guiColor;
clutchKey = key;
Reset();
}
public void Reset()
{
m_MoveItemMode = new TMoveMode();
m_TrimItemMode = new TTrimMode();
m_AddDeleteItemMode = new TAddDeleteMode();
}
TMoveMode m_MoveItemMode;
TTrimMode m_TrimItemMode;
TAddDeleteMode m_AddDeleteItemMode;
public IMoveItemMode moveItemMode { get { return m_MoveItemMode; } }
public IMoveItemDrawer moveItemDrawer { get { return m_MoveItemMode; } }
public ITrimItemMode trimItemMode { get { return m_TrimItemMode; } }
public ITrimItemDrawer trimItemDrawer { get { return m_TrimItemMode; } }
public IAddDeleteItemMode addDeleteItemMode { get { return m_AddDeleteItemMode; } }
public Color color { get; }
public KeyCode clutchKey { get; }
}
static readonly Dictionary<EditType, ISubEditMode> k_EditModes = new Dictionary<EditType, ISubEditMode>
{
{ EditType.Mix, new SubEditMode<MoveItemModeMix, TrimItemModeMix, AddDeleteItemModeMix>(DirectorStyles.kMixToolColor, KeyCode.Alpha1) },
{ EditType.Ripple, new SubEditMode<MoveItemModeRipple, TrimItemModeRipple, AddDeleteItemModeRipple>(DirectorStyles.kRippleToolColor, KeyCode.Alpha2) },
{ EditType.Replace, new SubEditMode<MoveItemModeReplace, TrimItemModeReplace, AddDeleteItemModeReplace>(DirectorStyles.kReplaceToolColor, KeyCode.Alpha3) }
};
static EditType s_CurrentEditType = EditType.Mix;
static EditType s_OverrideEditType = EditType.None;
static ITrimmable s_CurrentTrimItem;
static TrimEdge s_CurrentTrimDirection;
static MoveItemHandler s_CurrentMoveItemHandler;
static EditModeInputHandler s_InputHandler = new EditModeInputHandler();
static ITrimItemMode trimMode
{
get { return GetSubEditMode(editType).trimItemMode; }
}
static ITrimItemDrawer trimDrawer
{
get { return GetSubEditMode(editType).trimItemDrawer; }
}
static IMoveItemMode moveMode
{
get { return GetSubEditMode(editType).moveItemMode; }
}
static IMoveItemDrawer moveDrawer
{
get { return GetSubEditMode(editType).moveItemDrawer; }
}
static IAddDeleteItemMode addDeleteMode
{
get { return GetSubEditMode(editType).addDeleteItemMode; }
}
public static EditModeInputHandler inputHandler
{
get { return s_InputHandler; }
}
static Color modeColor
{
get { return GetSubEditMode(editType).color; }
}
public static EditType editType
{
get
{
if (s_OverrideEditType != EditType.None)
return s_OverrideEditType;
var window = TimelineWindow.instance;
if (window != null)
s_CurrentEditType = window.state.editType;
return s_CurrentEditType;
}
set
{
s_CurrentEditType = value;
var window = TimelineWindow.instance;
if (window != null)
window.state.editType = value;
s_OverrideEditType = EditType.None;
}
}
static ISubEditMode GetSubEditMode(EditType type)
{
var subEditMode = k_EditModes[type];
if (subEditMode != null)
return subEditMode;
Debug.LogError("Unsupported editmode type");
return null;
}
static EditType GetSubEditType(KeyCode key)
{
foreach (var subEditMode in k_EditModes)
{
if (subEditMode.Value.clutchKey == key)
return subEditMode.Key;
}
return EditType.None;
}
public static void ClearEditMode()
{
k_EditModes[editType].Reset();
}
public static void BeginTrim(ITimelineItem item, TrimEdge trimDirection)
{
var itemToTrim = item as ITrimmable;
if (itemToTrim == null) return;
s_CurrentTrimItem = itemToTrim;
s_CurrentTrimDirection = trimDirection;
trimMode.OnBeforeTrim(itemToTrim, trimDirection);
TimelineUndo.PushUndo(itemToTrim.parentTrack, "Trim Clip");
}
public static void TrimStart(ITimelineItem item, double time)
{
var itemToTrim = item as ITrimmable;
if (itemToTrim == null) return;
trimMode.TrimStart(itemToTrim, time);
}
public static void TrimEnd(ITimelineItem item, double time, bool affectTimeScale)
{
var itemToTrim = item as ITrimmable;
if (itemToTrim == null) return;
trimMode.TrimEnd(itemToTrim, time, affectTimeScale);
}
public static void DrawTrimGUI(WindowState state, TimelineItemGUI item, TrimEdge edge)
{
trimDrawer.DrawGUI(state, item.rect, modeColor, edge);
}
public static void FinishTrim()
{
s_CurrentTrimItem = null;
TimelineCursors.ClearCursor();
ClearEditMode();
TimelineEditor.Refresh(RefreshReason.ContentsModified);
}
public static void BeginMove(MoveItemHandler moveItemHandler)
{
s_CurrentMoveItemHandler = moveItemHandler;
moveMode.BeginMove(s_CurrentMoveItemHandler.movingItems);
}
public static void UpdateMove()
{
moveMode.UpdateMove(s_CurrentMoveItemHandler.movingItems);
}
public static void OnTrackDetach(IEnumerable<ItemsPerTrack> grabbedTrackItems)
{
moveMode.OnTrackDetach(grabbedTrackItems);
}
public static void HandleTrackSwitch(IEnumerable<ItemsPerTrack> grabbedTrackItems)
{
moveMode.HandleTrackSwitch(grabbedTrackItems);
}
public static bool AllowTrackSwitch()
{
return moveMode.AllowTrackSwitch();
}
public static double AdjustStartTime(WindowState state, ItemsPerTrack itemsGroup, double time)
{
return moveMode.AdjustStartTime(state, itemsGroup, time);
}
public static bool ValidateDrag(ItemsPerTrack itemsGroup)
{
return moveMode.ValidateMove(itemsGroup);
}
public static void DrawMoveGUI(WindowState state, IEnumerable<MovingItems> movingItems)
{
moveDrawer.DrawGUI(state, movingItems, modeColor);
}
public static void FinishMove()
{
var manipulatedItemsList = s_CurrentMoveItemHandler.movingItems;
moveMode.FinishMove(manipulatedItemsList);
foreach (var itemsGroup in manipulatedItemsList)
foreach (var item in itemsGroup.items)
item.parentTrack = itemsGroup.targetTrack;
s_CurrentMoveItemHandler = null;
TimelineCursors.ClearCursor();
ClearEditMode();
TimelineEditor.Refresh(RefreshReason.ContentsModified);
}
public static void FinalizeInsertItemsAtTime(IEnumerable<ItemsPerTrack> newItems, double requestedTime)
{
addDeleteMode.InsertItemsAtTime(newItems, requestedTime);
}
public static void PrepareItemsDelete(IEnumerable<ItemsPerTrack> newItems)
{
addDeleteMode.RemoveItems(newItems);
}
public static void HandleModeClutch()
{
if (Event.current.type == EventType.KeyDown && EditorGUI.IsEditingTextField())
return;
var prevType = editType;
if (Event.current.type == EventType.KeyDown)
{
var clutchEditType = GetSubEditType(Event.current.keyCode);
if (clutchEditType != EditType.None)
{
s_OverrideEditType = clutchEditType;
Event.current.Use();
}
}
else if (Event.current.type == EventType.KeyUp)
{
var clutchEditType = GetSubEditType(Event.current.keyCode);
if (clutchEditType == s_OverrideEditType)
{
s_OverrideEditType = EditType.None;
Event.current.Use();
}
}
if (prevType != editType)
{
if (s_CurrentTrimItem != null)
{
trimMode.OnBeforeTrim(s_CurrentTrimItem, s_CurrentTrimDirection);
}
else if (s_CurrentMoveItemHandler != null)
{
if (s_CurrentMoveItemHandler.movingItems == null)
{
s_CurrentMoveItemHandler = null;
return;
}
foreach (var movingItems in s_CurrentMoveItemHandler.movingItems)
{
if (movingItems != null && movingItems.HasAnyDetachedParents())
{
foreach (var items in movingItems.items)
{
items.parentTrack = movingItems.originalTrack;
}
}
}
var movingSelection = s_CurrentMoveItemHandler.movingItems;
// Handle clutch key transition if needed
GetSubEditMode(prevType).moveItemMode.OnModeClutchExit(movingSelection);
moveMode.OnModeClutchEnter(movingSelection);
moveMode.BeginMove(movingSelection);
moveMode.HandleTrackSwitch(movingSelection);
UpdateMove();
s_CurrentMoveItemHandler.RefreshPreviewItems();
TimelineWindow.instance.state.rebuildGraph = true; // TODO Rebuild only if parent changed
}
TimelineWindow.instance.Repaint(); // TODO Refresh the toolbar without doing a full repaint?
}
}
}
}