forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrigger.cs
More file actions
651 lines (544 loc) · 26.3 KB
/
Trigger.cs
File metadata and controls
651 lines (544 loc) · 26.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using UnityEngine;
namespace UnityEditor.ShortcutManagement
{
class Trigger
{
IDirectory m_Directory;
IConflictResolver m_ConflictResolver;
IContextManager m_CurrentContextManager;
KeyCombination m_CurrentActiveClutch;
// the current set of keys used for querying shortcut entry matches against active contexts. cleared frequently.
// this is a list because chord support was planned for, but ultimately not implemented. in practice, this is
// only ever 1 entry long (key + modifier).
List<KeyCombination> m_KeyCombinationSequence = new List<KeyCombination>();
List<ShortcutEntry> m_Entries = new List<ShortcutEntry>();
// separate lists of action and clutch entries are used to store candidate shortcut entries when a mouse down
// is not yet determined to be a clutch or action. clicking, or dragging or invoking a clutch dependent context
// shortcut will determine if the mouse event is an action or clutch (respectively).
List<ShortcutEntry> m_MouseActionEntries = new List<ShortcutEntry>();
List<ShortcutEntry> m_MouseClutchEntries = new List<ShortcutEntry>();
// this is a frequently used temporary list kept as a property to avoid gc
List<KeyCode> m_ContextClutchKeyRemoveQueue = new List<KeyCode>();
// key down events is used to keep track of mismatched key down/up events. ex, external code calls Event.Use()
// on key down but not up.
List<KeyCode> m_KeyDownEvents = new List<KeyCode>();
internal List<ShortcutEntry> activeEntries => m_Entries;
internal List<ShortcutEntry> activeMouseActionEntries => m_MouseActionEntries;
internal List<ShortcutEntry> activeMouseClutchEntries => m_MouseClutchEntries;
// accessed by tests
internal Dictionary<KeyCode, ClutchShortcutContext> m_ClutchActivatedContexts = new();
static readonly Event s_QueuedMouseClutchEvent = new Event(), s_CachedCurrentEvent = new Event();
struct ActiveClutch
{
public ShortcutEntry entry;
public object context;
public ClutchShortcutContext clutchContext;
}
// There can be more than one active clutch at a time.
readonly Dictionary<KeyCode, ActiveClutch> m_ActiveClutches = new();
(KeyCode keyCode, ShortcutEntry entry, object context) m_ActiveMouseActionEntry;
static readonly List<EventType> k_ShortcutEventFilter = new List<EventType>
{
EventType.KeyDown,
EventType.KeyUp,
EventType.MouseDown,
EventType.MouseUp,
EventType.MouseDrag,
EventType.ScrollWheel,
};
Vector2 m_StartPosition;
const float k_DragOffset = 6f;
// Used only in tests
bool m_ShowConflictsWindow = true;
internal bool ShowConflictsWindow
{
get => m_ShowConflictsWindow;
set => m_ShowConflictsWindow = value;
}
public event Action<ShortcutEntry, ShortcutArguments> beforeShortcutInvoked;
public Trigger(IDirectory directory, IConflictResolver conflictResolver)
{
m_Directory = directory;
m_ConflictResolver = conflictResolver;
}
public void ResetShortcutState(EventType type, KeyCode keyCode)
{
switch (type)
{
case EventType.MouseDown:
if (m_ActiveMouseActionEntry.entry != null && m_ActiveMouseActionEntry.keyCode == keyCode)
ResetMouseShortcuts();
m_ActiveClutches.Remove(keyCode);
break;
case EventType.KeyDown:
if (m_KeyDownEvents.Contains(keyCode))
return;
m_KeyDownEvents.Add(keyCode);
m_ActiveClutches.Remove(keyCode);
break;
case EventType.KeyUp:
m_KeyDownEvents.Remove(keyCode);
break;
}
}
public void HandleKeyEvent(Event evt, IContextManager contextManager)
{
if (evt == null || evt.keyCode == KeyCode.None)
return;
if (!k_ShortcutEventFilter.Contains(evt.type))
return;
var isKeyUpOrMouseUpEvent = evt.type == EventType.KeyUp || evt.type == EventType.MouseUp;
if (isKeyUpOrMouseUpEvent)
{
// mouse shortcuts can be both action and clutch. if this is the case, m_ActiveMouseActionEntry will be
// valid when mouse delta is less than a drag threshold
if (m_ActiveMouseActionEntry.entry != null && m_ActiveMouseActionEntry.keyCode == evt.keyCode)
{
var shortcutEntry = m_ActiveMouseActionEntry.entry;
var context = m_ActiveMouseActionEntry.context;
ExecuteShortcut(shortcutEntry, ShortcutStage.End, context, true);
evt.Use();
return;
}
if (m_ActiveClutches.TryGetValue(evt.keyCode, out var active))
{
var shortcutEntry = active.entry;
var context = active.context;
DeactivateClutchContext(active.clutchContext);
m_ActiveClutches.Remove(evt.keyCode);
ExecuteShortcut(shortcutEntry, ShortcutStage.End, context, true);
evt.Use();
return;
}
}
var newModifier = EventModifiers.None;
var isModifierKey = KeyCombination.k_KeyCodeToEventModifiers.TryGetValue(evt.keyCode, out newModifier);
var isKeyEvent = evt.type == EventType.KeyDown || evt.type == EventType.KeyUp;
var modifierKeysChanged = isKeyEvent && isModifierKey;
// Won't handle the case where there are multiple active clutches at once.
if (modifierKeysChanged && m_ActiveClutches != null && m_ActiveClutches.Count == 1)
{
var enumerator = m_ActiveClutches.GetEnumerator();
enumerator.MoveNext();
var activeClutch = enumerator.Current;
if (ModifierKeysChangedWasHandled(activeClutch.Value.entry, evt, newModifier, contextManager))
return;
}
else if (!modifierKeysChanged && isKeyUpOrMouseUpEvent)
{
return;
}
// Use the event and return if the key is currently used in an active clutch
if (m_ActiveClutches.ContainsKey(evt.keyCode))
{
evt.Use();
return;
}
var keyCodeCombination = KeyCombination.FromInput(evt);
m_KeyCombinationSequence.Add(keyCodeCombination);
// Ignore event if sequence is empty
if (m_KeyCombinationSequence.Count == 0)
return;
List<ShortcutEntry> entries = new List<ShortcutEntry>();
switch (evt.type)
{
case EventType.MouseDrag when m_MouseClutchEntries.Count > 1:
entries = m_MouseClutchEntries;
break;
case EventType.MouseDrag when m_MouseClutchEntries.Count == 1:
{
if ((evt.mousePosition - m_StartPosition).magnitude <= k_DragOffset)
{
ResetKeyCombo();
return;
}
// in some cases the drag can change ShortcutContext.active (ex, mouse down very close to window
// border, then drag outside exceeds k_DragOffset)
var entry = m_MouseClutchEntries[0];
var key = entry.combinations[0].keyCode;
if (contextManager.HasActiveContextOfType(entry.context))
{
entries.Add(entry);
}
else if (m_ClutchActivatedContexts.TryGetValue(key, out var clutchCtx))
{
DeactivateClutchContext(clutchCtx);
m_ClutchActivatedContexts.Remove(key);
}
ResetActiveMouseActionEntry();
m_MouseClutchEntries.Clear();
break;
}
case EventType.MouseDrag:
ResetKeyCombo();
return;
case EventType.MouseDown:
case EventType.KeyDown:
case EventType.ScrollWheel:
m_Directory.FindShortcutEntries(m_KeyCombinationSequence, contextManager, m_Entries);
entries = m_Entries;
SetClutchContextActive(contextManager, evt.keyCode, entries);
break;
}
if (entries.Count > 1)
{
// Deal ONLY with prioritycontext
if (contextManager.HasAnyPriorityContext())
{
m_CurrentContextManager = contextManager;
entries = m_Entries.FindAll(CurrentContextManagerHasPriorityContextFor);
if (entries.Count == 0)
entries = m_Entries;
}
if (evt.type == EventType.MouseDown)
{
ResetMouseShortcuts();
foreach (var entry in entries)
{
if (entry.type == ShortcutType.Action)
m_MouseActionEntries.Add(entry);
else if (entry.type == ShortcutType.Clutch)
m_MouseClutchEntries.Add(entry);
}
s_QueuedMouseClutchEvent.CopyFrom(evt);
SetClutchContextActive(contextManager, evt.keyCode, m_MouseClutchEntries);
if (m_MouseActionEntries.Count > 1)
{
entries = m_MouseActionEntries;
}
else if (m_MouseActionEntries.Count == 1)
{
m_StartPosition = evt.mousePosition;
ShortcutArguments args = new ShortcutArguments();
if (m_ActiveMouseActionEntry.entry == null || m_ActiveMouseActionEntry.keyCode != evt.keyCode)
{
args.context = contextManager.GetContextInstanceOfType(m_MouseActionEntries[0].context, true);
m_ActiveMouseActionEntry = (evt.keyCode, m_MouseActionEntries[0], args.context);
}
ResetKeyCombo();
return;
}
}
}
switch (entries.Count)
{
case 0:
ResetKeyCombo();
break;
case 1:
var shortcutEntry = entries[0];
if (ShortcutFullyMatchesKeyCombination(shortcutEntry, m_KeyCombinationSequence))
{
if (evt.keyCode != m_KeyCombinationSequence[^1].keyCode)
break;
var context = contextManager.GetContextInstanceOfType(shortcutEntry.context, true);
if (context == null)
{
ResetKeyCombo();
break;
}
// When a clutch key that belongs to a dependent context is invoked while we are waiting to
// determine whether a mouse button is a click or drag, consider that key to be an indication
// that this is a "drag" (clutch).
if(m_MouseActionEntries.Count > 0 && m_MouseClutchEntries.Count == 1)
{
foreach (var kvp in m_ClutchActivatedContexts)
{
if (kvp.Value != context)
continue;
// invoke the queued mouse clutch shortcut first, then allow the current event to be invoked
var queued = m_MouseClutchEntries[0];
var key = queued.combinations[0].keyCode;
if (!m_ActiveClutches.ContainsKey(key)
&& contextManager.GetContextInstanceOfType(queued.context, true) is var mouseCtx)
{
m_ClutchActivatedContexts.Remove(kvp.Key);
// null check because when HandleKeyEvent is invoked from tests the current event is
// not used.
if (Event.current != null)
{
s_CachedCurrentEvent.CopyFrom(Event.current);
Event.current.CopyFrom(s_QueuedMouseClutchEvent);
ExecuteShortcut(queued, ShortcutStage.Begin, mouseCtx, true);
Event.current.CopyFrom(s_CachedCurrentEvent);
}
else
{
ExecuteShortcut(queued, ShortcutStage.Begin, mouseCtx, true);
}
m_ActiveClutches.Add(key, new ActiveClutch()
{
entry = queued,
context = mouseCtx,
clutchContext = context as ClutchShortcutContext
});
break;
}
}
}
switch (shortcutEntry.type)
{
case ShortcutType.Menu:
case ShortcutType.Action:
ExecuteShortcut(shortcutEntry, ShortcutStage.End, context, true);
evt.Use();
break;
case ShortcutType.Clutch:
if (!m_ActiveClutches.ContainsKey(evt.keyCode))
{
var evtKeyCode = evt.keyCode;
m_ClutchActivatedContexts.Remove(evtKeyCode);
var args = ExecuteShortcut(shortcutEntry, ShortcutStage.Begin, context, true);
m_ActiveClutches.Add(evtKeyCode, new ActiveClutch()
{
entry = shortcutEntry,
context = args.context,
clutchContext = shortcutEntry.clutchContext == null
? null
: contextManager.GetContextInstanceOfType(shortcutEntry.clutchContext, true) as
ClutchShortcutContext
});
evt.Use();
}
break;
}
}
break;
default:
if (HasConflicts(entries, m_KeyCombinationSequence))
{
if (ShowConflictsWindow)
m_ConflictResolver.ResolveConflict(m_KeyCombinationSequence, entries);
evt.Use();
ResetKeyCombo();
}
break;
}
}
void SetClutchContextActive(IContextManager context, KeyCode key, IEnumerable<ShortcutEntry> shortcuts)
{
foreach (var entry in shortcuts)
{
if (entry.type != ShortcutType.Clutch || entry.clutchContext == null)
continue;
if (context.GetContextInstanceOfType(entry.clutchContext, false) is ClutchShortcutContext ctx)
{
m_ClutchActivatedContexts[key] = ctx;
ctx.active = true;
}
}
}
bool ModifierKeysChangedWasHandled(ShortcutEntry activeClutchShortcutEntry, Event evt, EventModifiers modifier, IContextManager contextManager)
{
// Assume that there is only one key combination per shortcut entry.
var previousActiveClutchKeyCombination = activeClutchShortcutEntry.combinations[0];
if (!m_CurrentActiveClutch.Equals(default) && !m_CurrentActiveClutch.Equals(previousActiveClutchKeyCombination))
previousActiveClutchKeyCombination = m_CurrentActiveClutch;
KeyCombination newActiveClutchKeyCombination = default;
if (evt.type == EventType.KeyDown)
{
// Add the new modifier key (the event key code) to the shortcut's key combination.
newActiveClutchKeyCombination = KeyCombination.FromKeyboardInputAndAddEventModifiers(previousActiveClutchKeyCombination, modifier | evt.modifiers);
}
else if (evt.type == EventType.KeyUp)
{
if (evt.modifiers != EventModifiers.None)
{
// In the case that the event has a modifier, add it to the shortcut's key combination
// and remove the modifier key (the event key code) from the shortcut's key combination.
newActiveClutchKeyCombination = KeyCombination.FromKeyboardInputAndAddEventModifiers(previousActiveClutchKeyCombination, evt.modifiers);
newActiveClutchKeyCombination = KeyCombination.FromKeyboardInputAndRemoveEventModifiers(newActiveClutchKeyCombination, modifier);
}
else
{
// Remove the modifier key (the event key code) from the shortcut's key combination.
newActiveClutchKeyCombination = KeyCombination.FromKeyboardInputAndRemoveEventModifiers(previousActiveClutchKeyCombination, modifier);
}
}
m_CurrentActiveClutch = newActiveClutchKeyCombination;
var keyCombinationSequence = new List<KeyCombination>();
keyCombinationSequence.Add(newActiveClutchKeyCombination);
if (ShortcutFullyMatchesKeyCombination(activeClutchShortcutEntry, keyCombinationSequence))
return false;
var newEntries = new List<ShortcutEntry>();
m_Directory.FindShortcutEntries(keyCombinationSequence, contextManager, newEntries);
if (newEntries.Count == 0)
return false;
// Check for conflicts.
if (newEntries.Count > 1)
{
// Deal ONLY with prioritycontext
if (contextManager.HasAnyPriorityContext())
{
m_CurrentContextManager = contextManager;
var priorityEntries = newEntries.FindAll(CurrentContextManagerHasPriorityContextFor);
if (priorityEntries.Count != 0)
newEntries = priorityEntries;
}
// Only retrieve the clutch shortcut entries.
var clutchShortcuts = new List<ShortcutEntry>();
foreach (var entry in newEntries)
{
if (entry.type == ShortcutType.Clutch)
clutchShortcuts.Add(entry);
}
newEntries = clutchShortcuts;
}
else
{
if (newEntries[0].type == ShortcutType.Action)
return false;
}
switch (newEntries.Count)
{
case 0:
return false;
case 1:
var newShortcutEntry = newEntries[0];
var newContext = contextManager.GetContextInstanceOfType(newShortcutEntry.context, true);
var newContextType = newContext.GetType();
var previousContext = activeClutchShortcutEntry.context;
if (previousContext != newContextType)
return false;
ResetActiveClutches();
m_ClutchActivatedContexts.Remove(newActiveClutchKeyCombination.keyCode);
var newArgs = ExecuteShortcut(newShortcutEntry, ShortcutStage.Begin, newContext, true);
m_ActiveClutches.Add(newActiveClutchKeyCombination.keyCode, new ActiveClutch()
{
entry = newShortcutEntry,
context = newArgs.context,
clutchContext = contextManager.GetContextInstanceOfType(newShortcutEntry.clutchContext, true) as ClutchShortcutContext
});
evt.Use();
return true;
default:
if (HasConflicts(newEntries, keyCombinationSequence))
{
if (ShowConflictsWindow)
m_ConflictResolver.ResolveConflict(keyCombinationSequence, newEntries);
evt.Use();
ResetKeyCombo();
return true;
}
return false;
}
}
public void ResetActiveClutches()
{
// this is done in two passes to ensure that shortcuts enabled by a clutch context are ended before the
// parent clutch shortcut ends. it is necessary because the child clutch can be dependent on state managed
// by the parent (see scene view fps camera navigation).
foreach (var shortcut in m_ActiveClutches.Values)
{
if (shortcut.clutchContext == null)
ExecuteShortcut(shortcut.entry, ShortcutStage.End, shortcut.context);
}
foreach (var shortcut in m_ActiveClutches.Values)
{
if (shortcut.clutchContext == null)
continue;
ExecuteShortcut(shortcut.entry, ShortcutStage.End, shortcut.context);
shortcut.clutchContext.active = false;
}
m_ActiveClutches.Clear();
}
public bool HasAnyEntries()
{
return m_Entries.Count > 0;
}
// filtered entries are expected to all be in the same context and/or null context and they all are known to share the prefix
bool HasConflicts(List<ShortcutEntry> filteredEntries, List<KeyCombination> prefix)
{
var hasEntryFullyMatchesPrefix = false;
foreach (var entry in filteredEntries)
{
if (entry.FullyMatches(prefix))
{
hasEntryFullyMatchesPrefix = true;
break;
}
}
if (hasEntryFullyMatchesPrefix)
return filteredEntries.Count > 1;
return false;
}
bool ShortcutFullyMatchesKeyCombination(ShortcutEntry shortcutEntry, List<KeyCombination> keyCombinationSequence)
{
return shortcutEntry.FullyMatches(keyCombinationSequence);
}
bool CurrentContextManagerHasPriorityContextFor(ShortcutEntry entry)
{
return m_CurrentContextManager.HasPriorityContextOfType(entry.context);
}
ShortcutArguments ExecuteShortcut(ShortcutEntry entry, ShortcutStage stage, object context, bool reset = false)
{
var args = new ShortcutArguments
{
context = context,
stage = stage
};
try
{
// it is imperative that exception does not derail execution here. after execution active clutches
// need to be cleared, otherwise trigger state can be irreparably corrupted.
beforeShortcutInvoked?.Invoke(entry, args);
entry.action(args);
}
catch (Exception e)
{
Debug.LogException(e);
}
finally
{
if (reset)
{
ResetKeyCombo();
ResetMouseShortcuts();
}
}
return args;
}
void DeactivateClutchContext(ClutchShortcutContext context)
{
if (context == null)
return;
m_ContextClutchKeyRemoveQueue.Clear();
// end any active clutches that are subject to this context
foreach (var active in m_ActiveClutches)
{
if (active.Value.context != context)
continue;
m_ContextClutchKeyRemoveQueue.Add(active.Key);
ExecuteShortcut(active.Value.entry, ShortcutStage.End, context);
}
foreach (var key in m_ContextClutchKeyRemoveQueue)
m_ActiveClutches.Remove(key);
context.active = false;
}
void ResetKeyCombo()
{
m_KeyCombinationSequence.Clear();
}
void ResetMouseShortcuts()
{
foreach (var clutch in m_ClutchActivatedContexts)
DeactivateClutchContext(clutch.Value);
m_ClutchActivatedContexts.Clear();
ResetActiveMouseActionEntry();
m_MouseClutchEntries.Clear();
m_MouseActionEntries.Clear();
m_CurrentActiveClutch = default;
}
void ResetActiveMouseActionEntry()
{
m_ActiveMouseActionEntry.keyCode = KeyCode.None;
m_ActiveMouseActionEntry.context = null;
m_ActiveMouseActionEntry.entry = null;
}
}
}