forked from ppy/osu-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyCombination.cs
693 lines (527 loc) · 23.7 KB
/
KeyCombination.cs
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using osu.Framework.Input.States;
using osuTK;
using osuTK.Input;
namespace osu.Framework.Input.Bindings
{
/// <summary>
/// Represent a combination of more than one <see cref="InputKey"/>s.
/// </summary>
public readonly struct KeyCombination : IEquatable<KeyCombination>
{
/// <summary>
/// The keys.
/// </summary>
public readonly ImmutableArray<InputKey> Keys;
private static readonly ImmutableArray<InputKey> none = ImmutableArray.Create(InputKey.None);
/// <summary>
/// Construct a new instance.
/// </summary>
/// <param name="keys">The keys.</param>
public KeyCombination(ICollection<InputKey>? keys)
{
if (keys == null || !keys.Any())
{
Keys = none;
return;
}
var keyBuilder = ImmutableArray.CreateBuilder<InputKey>(keys.Count);
bool hadDuplicates = false;
foreach (var key in keys)
{
if (keyBuilder.Contains(key))
{
// This changes the expected count meaning we can't use the optimised MoveToImmutable() method.
hadDuplicates = true;
continue;
}
keyBuilder.Add(key);
}
keyBuilder.Sort();
Keys = hadDuplicates ? keyBuilder.ToImmutableArray() : keyBuilder.MoveToImmutable();
}
/// <summary>
/// Construct a new instance.
/// </summary>
/// <param name="keys">The keys.</param>
/// <remarks>This constructor is not optimized. Hot paths are assumed to use <see cref="FromInputState(InputState, Vector2?)"/>.</remarks>
public KeyCombination(params InputKey[] keys)
: this((ICollection<InputKey>)keys)
{
}
/// <summary>
/// Construct a new instance from string representation provided by <see cref="ToString"/>.
/// </summary>
/// <param name="keys">A comma-separated (KeyCode in integer) string representation of the keys.</param>
/// <remarks>This constructor is not optimized. Hot paths are assumed to use <see cref="FromInputState(InputState, Vector2?)"/>.</remarks>
public KeyCombination(string keys)
: this(keys.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(s => (InputKey)int.Parse(s)).ToArray())
{
}
/// <summary>
/// Constructor optimized for known builder. The caller is responsible to sort it.
/// </summary>
/// <param name="keys">The already sorted <see cref="ImmutableArray{InputKey}"/>.</param>
private KeyCombination(ImmutableArray<InputKey> keys)
{
Keys = keys;
}
/// <summary>
/// Check whether the provided pressed keys are valid for this <see cref="KeyCombination"/>.
/// </summary>
/// <param name="pressedKeys">The potential pressed keys for this <see cref="KeyCombination"/>.</param>
/// <param name="matchingMode">The method for handling exact key matches.</param>
/// <returns>Whether the pressedKeys keys are valid.</returns>
public bool IsPressed(KeyCombination pressedKeys, KeyCombinationMatchingMode matchingMode)
{
Debug.Assert(!pressedKeys.Keys.Contains(InputKey.None)); // Having None in pressed keys will break IsPressed
if (Keys == pressedKeys.Keys) // Fast test for reference equality of underlying array
return true;
return ContainsAll(Keys, pressedKeys.Keys, matchingMode);
}
/// <summary>
/// Check whether the provided set of pressed keys matches the candidate binding.
/// </summary>
/// <param name="candidateKey">The candidate key binding to match against.</param>
/// <param name="pressedKey">The keys which have been pressed by a user.</param>
/// <param name="matchingMode">The matching mode to be used when checking.</param>
/// <returns>Whether this is a match.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool ContainsAll(ImmutableArray<InputKey> candidateKey, ImmutableArray<InputKey> pressedKey, KeyCombinationMatchingMode matchingMode)
{
// first, check that all the candidate keys are contained in the provided pressed keys.
// regardless of the matching mode, every key needs to at least be present (matching modes only change
// the behaviour of excess keys).
foreach (var key in candidateKey)
{
if (!ContainsKey(pressedKey, key))
return false;
}
switch (matchingMode)
{
case KeyCombinationMatchingMode.Exact:
foreach (var key in pressedKey)
{
// in exact matching mode, every pressed key needs to be in the candidate.
if (!ContainsKeyPermissive(candidateKey, key))
return false;
}
break;
case KeyCombinationMatchingMode.Modifiers:
foreach (var key in pressedKey)
{
// in modifiers match mode, the same check applies as exact but only for modifier keys.
if (IsModifierKey(key) && !ContainsKeyPermissive(candidateKey, key))
return false;
}
break;
case KeyCombinationMatchingMode.Any:
// any match mode needs no further checks.
break;
}
return true;
}
/// <summary>
/// Check whether the provided key is part of the candidate binding.
/// This will match bidirectionally for modifier keys (LShift and Shift being present in both of the two parameters in either order will return true).
/// </summary>
/// <param name="candidate">The candidate key binding to match against.</param>
/// <param name="key">The key which has been pressed by a user.</param>
/// <returns>Whether this is a match.</returns>
internal static bool ContainsKeyPermissive(ImmutableArray<InputKey> candidate, InputKey key)
{
switch (key)
{
case InputKey.LControl:
case InputKey.RControl:
if (candidate.Contains(InputKey.Control))
return true;
break;
case InputKey.LShift:
case InputKey.RShift:
if (candidate.Contains(InputKey.Shift))
return true;
break;
case InputKey.RAlt:
case InputKey.LAlt:
if (candidate.Contains(InputKey.Alt))
return true;
break;
case InputKey.LSuper:
case InputKey.RSuper:
if (candidate.Contains(InputKey.Super))
return true;
break;
}
return ContainsKey(candidate, key);
}
/// <summary>
/// Check whether a single key from a candidate binding is relevant to the currently pressed keys.
/// If the <paramref name="key"/> contains a left/right specific modifier, the <paramref name="candidate"/> must also for this to match.
/// </summary>
/// <param name="candidate">The candidate key binding to match against.</param>
/// <param name="key">The key which has been pressed by a user.</param>
/// <returns>Whether this is a match.</returns>
internal static bool ContainsKey(ImmutableArray<InputKey> candidate, InputKey key)
{
switch (key)
{
case InputKey.Control:
if (candidate.Contains(InputKey.LControl) || candidate.Contains(InputKey.RControl))
return true;
break;
case InputKey.Shift:
if (candidate.Contains(InputKey.LShift) || candidate.Contains(InputKey.RShift))
return true;
break;
case InputKey.Alt:
if (candidate.Contains(InputKey.LAlt) || candidate.Contains(InputKey.RAlt))
return true;
break;
case InputKey.Super:
if (candidate.Contains(InputKey.LSuper) || candidate.Contains(InputKey.RSuper))
return true;
break;
}
return candidate.Contains(key);
}
public bool Equals(KeyCombination other) => Keys.SequenceEqual(other.Keys);
public override bool Equals(object? obj) => obj is KeyCombination kc && Equals(kc);
public override int GetHashCode()
{
var hash = new HashCode();
foreach (var key in Keys)
hash.Add(key);
return hash.ToHashCode();
}
public static implicit operator KeyCombination(InputKey singleKey) => new KeyCombination(ImmutableArray.Create(singleKey));
public static implicit operator KeyCombination(string stringRepresentation) => new KeyCombination(stringRepresentation);
public static implicit operator KeyCombination(InputKey[] keys) => new KeyCombination(keys);
/// <summary>
/// Get a string representation can be used with <see cref="KeyCombination(string)"/>.
/// </summary>
/// <returns>The string representation.</returns>
public override string ToString() => string.Join(',', Keys.Select(k => (int)k));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsModifierKey(InputKey key)
{
switch (key)
{
case InputKey.LControl:
case InputKey.LShift:
case InputKey.LAlt:
case InputKey.LSuper:
case InputKey.RControl:
case InputKey.RShift:
case InputKey.RAlt:
case InputKey.RSuper:
case InputKey.Control:
case InputKey.Shift:
case InputKey.Alt:
case InputKey.Super:
return true;
}
return false;
}
private static string getReadableKey(InputKey key)
{
if (key >= InputKey.FirstTabletAuxiliaryButton)
return $"Tablet Aux {key - InputKey.FirstTabletAuxiliaryButton + 1}";
if (key >= InputKey.FirstTabletPenButton)
return $"Tablet Pen {key - InputKey.FirstTabletPenButton + 1}";
if (key >= InputKey.MidiA0)
return key.ToString().Substring("Midi".Length).Replace("Sharp", "#");
if (key >= InputKey.FirstJoystickHatRightButton)
return $"Joystick Hat {key - InputKey.FirstJoystickHatRightButton + 1} Right";
if (key >= InputKey.FirstJoystickHatLeftButton)
return $"Joystick Hat {key - InputKey.FirstJoystickHatLeftButton + 1} Left";
if (key >= InputKey.FirstJoystickHatDownButton)
return $"Joystick Hat {key - InputKey.FirstJoystickHatDownButton + 1} Down";
if (key >= InputKey.FirstJoystickHatUpButton)
return $"Joystick Hat {key - InputKey.FirstJoystickHatUpButton + 1} Up";
if (key >= InputKey.FirstJoystickAxisPositiveButton)
return $"Joystick Axis {key - InputKey.FirstJoystickAxisPositiveButton + 1} +";
if (key >= InputKey.FirstJoystickAxisNegativeButton)
return $"Joystick Axis {key - InputKey.FirstJoystickAxisNegativeButton + 1} -";
if (key >= InputKey.FirstJoystickButton)
return $"Joystick {key - InputKey.FirstJoystickButton + 1}";
switch (key)
{
case InputKey.None:
return string.Empty;
case InputKey.Shift:
return "Shift";
case InputKey.Control:
return "Ctrl";
case InputKey.Alt:
return "Alt";
case InputKey.Super:
return "Win";
case InputKey.LShift:
return "LShift";
case InputKey.LControl:
return "LCtrl";
case InputKey.LAlt:
return "LAlt";
case InputKey.LSuper:
return "LWin";
case InputKey.RShift:
return "RShift";
case InputKey.RControl:
return "RCtrl";
case InputKey.RAlt:
return "RAlt";
case InputKey.RSuper:
return "RWin";
case InputKey.Escape:
return "Esc";
case InputKey.BackSpace:
return "Backsp";
case InputKey.Insert:
return "Ins";
case InputKey.Delete:
return "Del";
case InputKey.PageUp:
return "PgUp";
case InputKey.PageDown:
return "PgDn";
case InputKey.CapsLock:
return "Caps";
case InputKey.Keypad0:
return "Numpad0";
case InputKey.Keypad1:
return "Numpad1";
case InputKey.Keypad2:
return "Numpad2";
case InputKey.Keypad3:
return "Numpad3";
case InputKey.Keypad4:
return "Numpad4";
case InputKey.Keypad5:
return "Numpad5";
case InputKey.Keypad6:
return "Numpad6";
case InputKey.Keypad7:
return "Numpad7";
case InputKey.Keypad8:
return "Numpad8";
case InputKey.Keypad9:
return "Numpad9";
case InputKey.KeypadDivide:
return "NumpadDivide";
case InputKey.KeypadMultiply:
return "NumpadMultiply";
case InputKey.KeypadMinus:
return "NumpadMinus";
case InputKey.KeypadPlus:
return "NumpadPlus";
case InputKey.KeypadDecimal:
return "NumpadDecimal";
case InputKey.KeypadEnter:
return "NumpadEnter";
case InputKey.Number0:
return "0";
case InputKey.Number1:
return "1";
case InputKey.Number2:
return "2";
case InputKey.Number3:
return "3";
case InputKey.Number4:
return "4";
case InputKey.Number5:
return "5";
case InputKey.Number6:
return "6";
case InputKey.Number7:
return "7";
case InputKey.Number8:
return "8";
case InputKey.Number9:
return "9";
case InputKey.Tilde:
return "~";
case InputKey.Minus:
return "Minus";
case InputKey.Plus:
return "Plus";
case InputKey.BracketLeft:
return "[";
case InputKey.BracketRight:
return "]";
case InputKey.Semicolon:
return ";";
case InputKey.Quote:
return "\"";
case InputKey.Comma:
return ",";
case InputKey.Period:
return ".";
case InputKey.Slash:
return "/";
case InputKey.BackSlash:
case InputKey.NonUSBackSlash:
return "\\";
case InputKey.Mute:
return "Mute";
case InputKey.VolumeDown:
return "Vol. Down";
case InputKey.VolumeUp:
return "Vol. Up";
case InputKey.Stop:
return "Media Stop";
case InputKey.PlayPause:
return "Media Play";
case InputKey.TrackNext:
return "Media Next";
case InputKey.TrackPrevious:
return "Media Previous";
case InputKey.MouseLeft:
return "M1";
case InputKey.MouseMiddle:
return "M3";
case InputKey.MouseRight:
return "M2";
case InputKey.ExtraMouseButton1:
return "M4";
case InputKey.ExtraMouseButton2:
return "M5";
case InputKey.ExtraMouseButton3:
return "M6";
case InputKey.ExtraMouseButton4:
return "M7";
case InputKey.ExtraMouseButton5:
return "M8";
case InputKey.ExtraMouseButton6:
return "M9";
case InputKey.ExtraMouseButton7:
return "M10";
case InputKey.ExtraMouseButton8:
return "M11";
case InputKey.ExtraMouseButton9:
return "M12";
case InputKey.MouseWheelDown:
return "Wheel Down";
case InputKey.MouseWheelUp:
return "Wheel Up";
case InputKey.MouseWheelLeft:
return "Wheel Left";
case InputKey.MouseWheelRight:
return "Wheel Right";
default:
return key.ToString();
}
}
public static InputKey FromKey(Key key)
{
switch (key)
{
case Key.LShift: return InputKey.LShift;
case Key.RShift: return InputKey.RShift;
case Key.LControl: return InputKey.LControl;
case Key.RControl: return InputKey.RControl;
case Key.LAlt: return InputKey.LAlt;
case Key.RAlt: return InputKey.RAlt;
case Key.LWin: return InputKey.LSuper;
case Key.RWin: return InputKey.RSuper;
}
return (InputKey)key;
}
public static InputKey FromMouseButton(MouseButton button) => (InputKey)((int)InputKey.FirstMouseButton + button);
public static InputKey FromJoystickButton(JoystickButton button)
{
if (button >= JoystickButton.FirstHatRight)
return InputKey.FirstJoystickHatRightButton + (button - JoystickButton.FirstHatRight);
if (button >= JoystickButton.FirstHatLeft)
return InputKey.FirstJoystickHatLeftButton + (button - JoystickButton.FirstHatLeft);
if (button >= JoystickButton.FirstHatDown)
return InputKey.FirstJoystickHatDownButton + (button - JoystickButton.FirstHatDown);
if (button >= JoystickButton.FirstHatUp)
return InputKey.FirstJoystickHatUpButton + (button - JoystickButton.FirstHatUp);
if (button >= JoystickButton.FirstAxisPositive)
return InputKey.FirstJoystickAxisPositiveButton + (button - JoystickButton.FirstAxisPositive);
if (button >= JoystickButton.FirstAxisNegative)
return InputKey.FirstJoystickAxisNegativeButton + (button - JoystickButton.FirstAxisNegative);
return InputKey.FirstJoystickButton + (button - JoystickButton.FirstButton);
}
public static IEnumerable<InputKey> FromScrollDelta(Vector2 scrollDelta)
{
if (scrollDelta.Y > 0)
yield return InputKey.MouseWheelUp;
if (scrollDelta.Y < 0)
yield return InputKey.MouseWheelDown;
if (scrollDelta.X > 0)
yield return InputKey.MouseWheelLeft;
if (scrollDelta.X < 0)
yield return InputKey.MouseWheelRight;
}
public static InputKey FromMidiKey(MidiKey key) => (InputKey)((int)InputKey.MidiA0 + key - MidiKey.A0);
public static InputKey FromTabletPenButton(TabletPenButton penButton) => (InputKey)((int)InputKey.FirstTabletPenButton + penButton);
public static InputKey FromTabletAuxiliaryButton(TabletAuxiliaryButton auxiliaryButton) => (InputKey)((int)InputKey.FirstTabletAuxiliaryButton + auxiliaryButton);
/// <summary>
/// Construct a new instance from input state.
/// </summary>
/// <param name="state">The input state object.</param>
/// <param name="scrollDelta">Delta of scroller's position.</param>
/// <returns>The new constructed <see cref="KeyCombination"/> instance.</returns>
/// <remarks>This factory method is optimized and should be used for hot paths.</remarks>
public static KeyCombination FromInputState(InputState state, Vector2? scrollDelta = null)
{
var keys = ImmutableArray.CreateBuilder<InputKey>();
if (state.Mouse != null)
{
foreach (var button in state.Mouse.Buttons)
keys.Add(FromMouseButton(button));
}
if (scrollDelta is Vector2 v && (v.X != 0 || v.Y != 0))
keys.AddRange(FromScrollDelta(v));
if (state.Keyboard != null)
{
foreach (var key in state.Keyboard.Keys)
{
var iKey = FromKey(key);
if (!keys.Contains(iKey))
keys.Add(iKey);
}
}
if (state.Joystick != null)
{
foreach (var joystickButton in state.Joystick.Buttons)
keys.Add(FromJoystickButton(joystickButton));
}
if (state.Midi != null)
keys.AddRange(state.Midi.Keys.Select(FromMidiKey));
if (state.Tablet != null)
{
keys.AddRange(state.Tablet.PenButtons.Select(FromTabletPenButton));
keys.AddRange(state.Tablet.AuxiliaryButtons.Select(FromTabletAuxiliaryButton));
}
Debug.Assert(!keys.Contains(InputKey.None)); // Having None in pressed keys will break IsPressed
keys.Sort();
// Can't use `MoveToImmutable` here as we don't provide accurate capacity.
return new KeyCombination(keys.ToImmutableList());
}
}
public enum KeyCombinationMatchingMode
{
/// <summary>
/// Matches a <see cref="KeyCombination"/> regardless of any additional key presses.
/// </summary>
Any,
/// <summary>
/// Matches a <see cref="KeyCombination"/> if there are no additional key presses.
/// </summary>
Exact,
/// <summary>
/// Matches a <see cref="KeyCombination"/> regardless of any additional key presses, however key modifiers must match exactly.
/// </summary>
Modifiers,
}
}