-
Notifications
You must be signed in to change notification settings - Fork 424
/
Copy pathInputManager.cs
1020 lines (823 loc) · 43.2 KB
/
InputManager.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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
#nullable disable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Extensions.ListExtensions;
using osu.Framework.Extensions.TypeExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Framework.Input.Handlers;
using osu.Framework.Input.StateChanges;
using osu.Framework.Input.StateChanges.Events;
using osu.Framework.Input.States;
using osu.Framework.Lists;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Statistics;
using osuTK;
using osuTK.Input;
using JoystickState = osu.Framework.Input.States.JoystickState;
using KeyboardState = osu.Framework.Input.States.KeyboardState;
using MouseState = osu.Framework.Input.States.MouseState;
namespace osu.Framework.Input
{
public abstract class InputManager : Container, IInputStateChangeHandler
{
/// <summary>
/// The initial delay before key repeat begins.
/// </summary>
private const int repeat_initial_delay = 250;
/// <summary>
/// The delay between key repeats after the initial repeat.
/// </summary>
private const int repeat_tick_rate = 70;
[Resolved(CanBeNull = true)]
protected GameHost Host { get; private set; }
/// <summary>
/// The currently focused <see cref="Drawable"/>. Null if there is no current focus.
/// </summary>
public Drawable FocusedDrawable { get; internal set; }
protected abstract ImmutableArray<InputHandler> InputHandlers { get; }
private double keyboardRepeatTime;
private Key? keyboardRepeatKey;
/// <summary>
/// The initial input state. <see cref="CurrentState"/> is always equal (as a reference) to the value returned from this.
/// </summary>
protected virtual InputState CreateInitialState() => new InputState(
new MouseState { IsPositionValid = false },
new KeyboardState(),
new TouchState(),
new JoystickState(),
new MidiState(),
new TabletState()
);
/// <summary>
/// The last processed state.
/// </summary>
public readonly InputState CurrentState;
/// <summary>
/// The <see cref="Drawable"/> which is currently being dragged. null if none is.
/// </summary>
public Drawable DraggedDrawable
{
get
{
mouseButtonEventManagers.TryGetValue(MouseButton.Left, out var manager);
return manager?.DraggedDrawable;
}
}
/// <summary>
/// Contains the previously hovered <see cref="Drawable"/>s prior to when
/// <see cref="hoveredDrawables"/> got updated.
/// </summary>
private readonly List<Drawable> lastHoveredDrawables = new List<Drawable>();
/// <summary>
/// Contains all hovered <see cref="Drawable"/>s in top-down order up to the first
/// which returned true in its <see cref="Drawable.OnHover"/> method.
/// Top-down in this case means reverse draw order, i.e. the front-most visible
/// <see cref="Drawable"/> first, and <see cref="Container"/>s after their children.
/// </summary>
private readonly List<Drawable> hoveredDrawables = new List<Drawable>();
/// <summary>
/// The <see cref="Drawable"/> which returned true in its
/// <see cref="Drawable.OnHover"/> method, or null if none did so.
/// </summary>
private Drawable hoverHandledDrawable;
/// <summary>
/// Contains all hovered <see cref="Drawable"/>s in top-down order up to the first
/// which returned true in its <see cref="Drawable.OnHover"/> method.
/// Top-down in this case means reverse draw order, i.e. the front-most visible
/// <see cref="Drawable"/> first, and <see cref="Container"/>s after their children.
/// </summary>
public SlimReadOnlyListWrapper<Drawable> HoveredDrawables => hoveredDrawables.AsSlimReadOnly();
/// <summary>
/// Contains all <see cref="Drawable"/>s in top-down order which are considered
/// for positional input. This list is the same as <see cref="HoveredDrawables"/>, only
/// that the return value of <see cref="Drawable.OnHover"/> is not taken
/// into account.
/// </summary>
/// <remarks>
/// This collection should not be retained as a reference. The contents is not stable outside of local usage.
/// </remarks>
public SlimReadOnlyListWrapper<Drawable> PositionalInputQueue => buildPositionalInputQueue(CurrentState.Mouse.Position);
/// <summary>
/// Contains all <see cref="Drawable"/>s in top-down order which are considered
/// for non-positional input.
/// </summary>
/// <remarks>
/// This collection should not be retained as a reference. The contents is not stable outside of local usage.
/// </remarks>
public SlimReadOnlyListWrapper<Drawable> NonPositionalInputQueue => buildNonPositionalInputQueue();
private readonly Dictionary<MouseButton, MouseButtonEventManager> mouseButtonEventManagers = new Dictionary<MouseButton, MouseButtonEventManager>();
private readonly Dictionary<Key, KeyEventManager> keyButtonEventManagers = new Dictionary<Key, KeyEventManager>();
private readonly Dictionary<TouchSource, TouchEventManager> touchEventManagers = new Dictionary<TouchSource, TouchEventManager>();
private readonly Dictionary<TabletPenButton, TabletPenButtonEventManager> tabletPenButtonEventManagers = new Dictionary<TabletPenButton, TabletPenButtonEventManager>();
private readonly Dictionary<TabletAuxiliaryButton, TabletAuxiliaryButtonEventManager> tabletAuxiliaryButtonEventManagers = new Dictionary<TabletAuxiliaryButton, TabletAuxiliaryButtonEventManager>();
private readonly Dictionary<JoystickButton, JoystickButtonEventManager> joystickButtonEventManagers = new Dictionary<JoystickButton, JoystickButtonEventManager>();
private readonly Dictionary<MidiKey, MidiKeyEventManager> midiKeyEventManagers = new Dictionary<MidiKey, MidiKeyEventManager>();
private readonly Dictionary<JoystickAxisSource, JoystickAxisEventManager> joystickAxisEventManagers = new Dictionary<JoystickAxisSource, JoystickAxisEventManager>();
/// <summary>
/// Whether to produce mouse input on any touch input from latest source.
/// </summary>
protected virtual bool MapMouseToLatestTouch => true;
protected InputManager()
{
CurrentState = CreateInitialState();
RelativeSizeAxes = Axes.Both;
foreach (var button in Enum.GetValues(typeof(MouseButton)).Cast<MouseButton>())
{
var manager = CreateButtonEventManagerFor(button);
manager.RequestFocus = ChangeFocusFromClick;
manager.GetInputQueue = () => PositionalInputQueue;
manager.GetCurrentTime = () => Time.Current;
mouseButtonEventManagers.Add(button, manager);
}
}
protected override void LoadComplete()
{
base.LoadComplete();
// Set mouse position to zero in input manager local space instead of screen space zero.
// This ensures initial mouse position is non-negative in nested input managers whose origin is not (0, 0).
CurrentState.Mouse.Position = ToScreenSpace(Vector2.Zero);
}
/// <summary>
/// Create a <see cref="MouseButtonEventManager"/> for a specified mouse button.
/// </summary>
/// <param name="button">The button to be handled by the returned manager.</param>
/// <returns>The <see cref="MouseButtonEventManager"/>.</returns>
protected virtual MouseButtonEventManager CreateButtonEventManagerFor(MouseButton button)
{
switch (button)
{
case MouseButton.Left:
return new MouseLeftButtonEventManager(button);
default:
return new MouseMinorButtonEventManager(button);
}
}
/// <summary>
/// Get the <see cref="MouseButtonEventManager"/> responsible for a specified mouse button.
/// </summary>
/// <param name="button">The button to find the manager for.</param>
/// <returns>The <see cref="MouseButtonEventManager"/>.</returns>
public MouseButtonEventManager GetButtonEventManagerFor(MouseButton button) =>
mouseButtonEventManagers.TryGetValue(button, out var manager) ? manager : null;
/// <summary>
/// Create a <see cref="KeyEventManager"/> for a specified key.
/// </summary>
/// <param name="key">The key to be handled by the returned manager.</param>
/// <returns>The <see cref="KeyEventManager"/>.</returns>
protected virtual KeyEventManager CreateButtonEventManagerFor(Key key) => new KeyEventManager(key);
/// <summary>
/// Get the <see cref="KeyEventManager"/> responsible for a specified key.
/// </summary>
/// <param name="key">The key to find the manager for.</param>
/// <returns>The <see cref="KeyEventManager"/>.</returns>
public KeyEventManager GetButtonEventManagerFor(Key key)
{
if (keyButtonEventManagers.TryGetValue(key, out var existing))
return existing;
var manager = CreateButtonEventManagerFor(key);
manager.GetInputQueue = () => NonPositionalInputQueue;
return keyButtonEventManagers[key] = manager;
}
/// <summary>
/// Create a <see cref="TouchEventManager"/> for a specified touch source.
/// </summary>
/// <param name="source">The touch source to be handled by the returned manager.</param>
/// <returns>The <see cref="TouchEventManager"/>.</returns>
protected virtual TouchEventManager CreateButtonEventManagerFor(TouchSource source) => new TouchEventManager(source);
/// <summary>
/// Get the <see cref="TouchEventManager"/> responsible for a specified touch source.
/// </summary>
/// <param name="source">The touch source to find the manager for.</param>
/// <returns>The <see cref="TouchEventManager"/>.</returns>
public TouchEventManager GetButtonEventManagerFor(TouchSource source)
{
if (touchEventManagers.TryGetValue(source, out var existing))
return existing;
var manager = CreateButtonEventManagerFor(source);
manager.GetInputQueue = () => buildPositionalInputQueue(CurrentState.Touch.TouchPositions[(int)source]);
return touchEventManagers[source] = manager;
}
/// <summary>
/// Create a <see cref="TabletPenButtonEventManager"/> for a specified tablet pen button.
/// </summary>
/// <param name="button">The button to be handled by the returned manager.</param>
/// <returns>The <see cref="TabletPenButtonEventManager"/>.</returns>
protected virtual TabletPenButtonEventManager CreateButtonEventManagerFor(TabletPenButton button) => new TabletPenButtonEventManager(button);
/// <summary>
/// Get the <see cref="TabletPenButtonEventManager"/> responsible for a specified tablet pen button.
/// </summary>
/// <param name="button">The button to find the manager for.</param>
/// <returns>The <see cref="TabletPenButtonEventManager"/>.</returns>
public TabletPenButtonEventManager GetButtonEventManagerFor(TabletPenButton button)
{
if (tabletPenButtonEventManagers.TryGetValue(button, out var existing))
return existing;
var manager = CreateButtonEventManagerFor(button);
manager.GetInputQueue = () => PositionalInputQueue;
return tabletPenButtonEventManagers[button] = manager;
}
/// <summary>
/// Create a <see cref="TabletAuxiliaryButtonEventManager"/> for a specified tablet auxiliary button.
/// </summary>
/// <param name="button">The button to be handled by the returned manager.</param>
/// <returns>The <see cref="TabletAuxiliaryButtonEventManager"/>.</returns>
protected virtual TabletAuxiliaryButtonEventManager CreateButtonEventManagerFor(TabletAuxiliaryButton button) => new TabletAuxiliaryButtonEventManager(button);
/// <summary>
/// Get the <see cref="TabletAuxiliaryButtonEventManager"/> responsible for a specified tablet auxiliary button.
/// </summary>
/// <param name="button">The button to find the manager for.</param>
/// <returns>The <see cref="TabletAuxiliaryButtonEventManager"/>.</returns>
public TabletAuxiliaryButtonEventManager GetButtonEventManagerFor(TabletAuxiliaryButton button)
{
if (tabletAuxiliaryButtonEventManagers.TryGetValue(button, out var existing))
return existing;
var manager = CreateButtonEventManagerFor(button);
manager.GetInputQueue = () => NonPositionalInputQueue;
return tabletAuxiliaryButtonEventManagers[button] = manager;
}
/// <summary>
/// Create a <see cref="JoystickButtonEventManager"/> for a specified joystick button.
/// </summary>
/// <param name="button">The button to be handled by the returned manager.</param>
/// <returns>The <see cref="JoystickButtonEventManager"/>.</returns>
protected virtual JoystickButtonEventManager CreateButtonEventManagerFor(JoystickButton button) => new JoystickButtonEventManager(button);
/// <summary>
/// Get the <see cref="JoystickButtonEventManager"/> responsible for a specified joystick button.
/// </summary>
/// <param name="button">The button to find the manager for.</param>
/// <returns>The <see cref="JoystickButtonEventManager"/>.</returns>
public JoystickButtonEventManager GetButtonEventManagerFor(JoystickButton button)
{
if (joystickButtonEventManagers.TryGetValue(button, out var existing))
return existing;
var manager = CreateButtonEventManagerFor(button);
manager.GetInputQueue = () => NonPositionalInputQueue;
return joystickButtonEventManagers[button] = manager;
}
/// <summary>
/// Create a <see cref="MidiKeyEventManager"/> for a specified midi key.
/// </summary>
/// <param name="key">The key to be handled by the returned manager.</param>
/// <returns>The <see cref="MidiKeyEventManager"/>.</returns>
protected virtual MidiKeyEventManager CreateButtonEventManagerFor(MidiKey key) => new MidiKeyEventManager(key);
/// <summary>
/// Get the <see cref="MidiKeyEventManager"/> responsible for a specified midi key.
/// </summary>
/// <param name="key">The key to find the manager for.</param>
/// <returns>The <see cref="MidiKeyEventManager"/>.</returns>
public MidiKeyEventManager GetButtonEventManagerFor(MidiKey key)
{
if (midiKeyEventManagers.TryGetValue(key, out var existing))
return existing;
var manager = CreateButtonEventManagerFor(key);
manager.GetInputQueue = () => NonPositionalInputQueue;
return midiKeyEventManagers[key] = manager;
}
/// <summary>
/// Create a <see cref="JoystickAxisEventManager"/> for a specified joystick axis.
/// </summary>
/// <param name="source">The axis to be handled by the returned manager.</param>
/// <returns>The <see cref="JoystickAxisEventManager"/>.</returns>
protected virtual JoystickAxisEventManager CreateJoystickAxisEventManagerFor(JoystickAxisSource source) => new JoystickAxisEventManager(source);
/// <summary>
/// Get the <see cref="JoystickAxisEventManager"/> responsible for a specified joystick axis.
/// </summary>
/// <param name="source">The axis to find the manager for.</param>
/// <returns>The <see cref="JoystickAxisEventManager"/>.</returns>
public JoystickAxisEventManager GetJoystickAxisEventManagerFor(JoystickAxisSource source)
{
if (joystickAxisEventManagers.TryGetValue(source, out var existing))
return existing;
var manager = CreateJoystickAxisEventManagerFor(source);
manager.GetInputQueue = () => NonPositionalInputQueue;
return joystickAxisEventManagers[source] = manager;
}
/// <summary>
/// Reset current focused drawable to the top-most drawable which is <see cref="Drawable.RequestsFocus"/>.
/// </summary>
/// <param name="triggerSource">The source which triggered this event.</param>
public void TriggerFocusContention(Drawable triggerSource)
{
if (FocusedDrawable == null) return;
Logger.Log($"Focus contention triggered by {triggerSource}.");
ChangeFocus(null);
}
/// <summary>
/// Changes the currently-focused drawable. First checks that <paramref name="potentialFocusTarget"/> is in a valid state to receive focus,
/// then unfocuses the current <see cref="FocusedDrawable"/> and focuses <paramref name="potentialFocusTarget"/>.
/// <paramref name="potentialFocusTarget"/> can be null to reset focus.
/// If the given drawable is already focused, nothing happens and no events are fired.
/// </summary>
/// <param name="potentialFocusTarget">The drawable to become focused.</param>
/// <returns>True if the given drawable is now focused (or focus is dropped in the case of a null target).</returns>
public bool ChangeFocus(Drawable potentialFocusTarget) => ChangeFocus(potentialFocusTarget, CurrentState);
/// <summary>
/// Changes the currently-focused drawable. First checks that <paramref name="potentialFocusTarget"/> is in a valid state to receive focus,
/// then unfocuses the current <see cref="FocusedDrawable"/> and focuses <paramref name="potentialFocusTarget"/>.
/// <paramref name="potentialFocusTarget"/> can be null to reset focus.
/// If the given drawable is already focused, nothing happens and no events are fired.
/// </summary>
/// <param name="potentialFocusTarget">The drawable to become focused.</param>
/// <param name="state">The <see cref="InputState"/> associated with the focusing event.</param>
/// <returns>True if the given drawable is now focused (or focus is dropped in the case of a null target).</returns>
protected bool ChangeFocus(Drawable potentialFocusTarget, InputState state)
{
if (potentialFocusTarget == FocusedDrawable)
return true;
if (potentialFocusTarget != null && (!isDrawableValidForFocus(potentialFocusTarget) || !potentialFocusTarget.AcceptsFocus))
return false;
var previousFocus = FocusedDrawable;
FocusedDrawable = null;
if (previousFocus != null)
{
previousFocus.HasFocus = false;
previousFocus.TriggerEvent(new FocusLostEvent(state, potentialFocusTarget));
if (FocusedDrawable != null) throw new InvalidOperationException($"Focus cannot be changed inside {nameof(OnFocusLost)}");
}
FocusedDrawable = potentialFocusTarget;
Logger.Log($"Focus changed from {previousFocus?.ToString() ?? "nothing"} to {FocusedDrawable?.ToString() ?? "nothing"}.", LoggingTarget.Runtime, LogLevel.Debug);
if (FocusedDrawable != null)
{
FocusedDrawable.HasFocus = true;
FocusedDrawable.TriggerEvent(new FocusEvent(state, previousFocus));
}
return true;
}
internal override bool BuildNonPositionalInputQueue(List<Drawable> queue, bool allowBlocking = true)
{
if (!allowBlocking)
base.BuildNonPositionalInputQueue(queue, false);
return false;
}
internal override bool BuildPositionalInputQueue(Vector2 screenSpacePos, List<Drawable> queue) => false;
private bool hoverEventsUpdated;
private readonly List<Drawable> highFrequencyDrawables = new List<Drawable>();
private MouseMoveEvent highFrequencyMoveEvent;
protected override void Update()
{
unfocusIfNoLongerValid();
// aggressively clear to avoid holding references.
inputQueue.Clear();
positionalInputQueue.Clear();
hoverEventsUpdated = false;
var pendingInputs = GetPendingInputs();
foreach (var result in pendingInputs)
{
result.Apply(CurrentState, this);
}
if (CurrentState.Mouse.IsPositionValid)
{
Debug.Assert(highFrequencyDrawables.Count == 0);
foreach (var d in PositionalInputQueue)
{
if (d is IRequireHighFrequencyMousePosition)
highFrequencyDrawables.Add(d);
}
if (highFrequencyDrawables.Count > 0)
{
// conditional avoid allocs of MouseMoveEvent when state is guaranteed to not have been mutated.
// can be removed if we pool/change UIEvent allocation to be more efficient.
if (highFrequencyMoveEvent == null || pendingInputs.Count > 0)
highFrequencyMoveEvent = new MouseMoveEvent(CurrentState);
PropagateBlockableEvent(highFrequencyDrawables.AsSlimReadOnly(), highFrequencyMoveEvent);
}
highFrequencyDrawables.Clear();
}
updateKeyRepeat(CurrentState);
// Other inputs or drawable changes may affect hover even if
// there were no mouse movements, so it must be updated every frame.
if (!hoverEventsUpdated)
updateHoverEvents(CurrentState);
if (FocusedDrawable == null)
focusTopMostRequestingDrawable();
base.Update();
}
private void updateKeyRepeat(InputState state)
{
if (!(keyboardRepeatKey is Key key)) return;
keyboardRepeatTime -= Time.Elapsed;
while (keyboardRepeatTime < 0)
{
GetButtonEventManagerFor(key).HandleRepeat(state);
keyboardRepeatTime += repeat_tick_rate;
}
}
private readonly List<IInput> inputs = new List<IInput>();
private readonly List<IInput> dequeuedInputs = new List<IInput>();
private InputHandler mouseSource;
private double mouseSourceDebounceTimeRemaining;
private double lastPendingInputRetrievalTime;
/// <summary>
/// The length in time after which a lower priority input handler is allowed to take over mouse control from a high priority handler that is no longer reporting.
/// It's safe to assume that all input devices are reporting at higher than the debounce time specified here (20hz).
/// If the delay seen between device swaps is ever considered to be too slow, this can likely be further increased up to 100hz.
/// </summary>
private const int mouse_source_debounce_time = 50;
protected virtual List<IInput> GetPendingInputs()
{
double now = Clock.CurrentTime;
double elapsed = now - lastPendingInputRetrievalTime;
lastPendingInputRetrievalTime = now;
inputs.Clear();
bool reachedPreviousMouseSource = false;
foreach (var h in InputHandlers)
{
if (!h.IsActive)
continue;
dequeuedInputs.Clear();
h.CollectPendingInputs(dequeuedInputs);
foreach (var i in dequeuedInputs)
{
// To avoid the same device reporting via two channels (and causing feedback), only one handler should be allowed to
// report mouse position data at a time. Handlers are given priority based on their constructed order.
// Devices which higher priority are allowed to take over control immediately, after which a delay is enforced (on every subsequent positional report)
// before a lower priority device can obtain control.
if (i is MousePositionAbsoluteInput || i is MousePositionRelativeInput)
{
if (mouseSource == null // a new device taking control when no existing preference is present.
|| mouseSource == h // if this is the device which currently has control, renew the debounce delay.
|| !reachedPreviousMouseSource // we have not reached the previous mouse source, so a higher priority device can take over control.
|| mouseSourceDebounceTimeRemaining <= 0) // a lower priority device taking over control if the debounce delay has elapsed.
{
mouseSource = h;
mouseSourceDebounceTimeRemaining = mouse_source_debounce_time;
}
else
{
// drop positional input if we did not meet the criteria to be the current reporting handler.
continue;
}
}
inputs.Add(i);
}
// track whether we have passed the handler which is currently in control of positional handling.
// importantly, this is updated regardless of whether the handler has reported any new inputs.
if (mouseSource == h)
reachedPreviousMouseSource = true;
}
mouseSourceDebounceTimeRemaining -= elapsed;
return inputs;
}
private readonly List<Drawable> inputQueue = new List<Drawable>();
private SlimReadOnlyListWrapper<Drawable> buildNonPositionalInputQueue()
{
inputQueue.Clear();
if (this is UserInputManager)
FrameStatistics.Increment(StatisticsCounterType.InputQueue);
var children = AliveInternalChildren;
for (int i = 0; i < children.Count; i++)
children[i].BuildNonPositionalInputQueue(inputQueue);
if (!unfocusIfNoLongerValid())
{
inputQueue.Remove(FocusedDrawable);
inputQueue.Add(FocusedDrawable);
}
// queues were created in back-to-front order.
// We want input to first reach front-most drawables, so the queues
// need to be reversed.
inputQueue.Reverse();
return inputQueue.AsSlimReadOnly();
}
private readonly List<Drawable> positionalInputQueue = new List<Drawable>();
private SlimReadOnlyListWrapper<Drawable> buildPositionalInputQueue(Vector2 screenSpacePos)
{
positionalInputQueue.Clear();
if (this is UserInputManager)
FrameStatistics.Increment(StatisticsCounterType.PositionalIQ);
var children = AliveInternalChildren;
for (int i = 0; i < children.Count; i++)
children[i].BuildPositionalInputQueue(screenSpacePos, positionalInputQueue);
positionalInputQueue.Reverse();
return positionalInputQueue.AsSlimReadOnly();
}
/// <summary>
/// Whether this input manager is in a state it should handle hover events.
/// This could for instance be set to false when the window/target does not have input focus.
/// </summary>
public virtual bool HandleHoverEvents => true;
private void updateHoverEvents(InputState state)
{
Drawable lastHoverHandledDrawable = hoverHandledDrawable;
hoverHandledDrawable = null;
lastHoveredDrawables.Clear();
lastHoveredDrawables.AddRange(hoveredDrawables);
hoveredDrawables.Clear();
// New drawables shouldn't be hovered if the cursor isn't in the window
if (HandleHoverEvents)
{
// First, we need to construct hoveredDrawables for the current frame
foreach (Drawable d in PositionalInputQueue)
{
hoveredDrawables.Add(d);
lastHoveredDrawables.Remove(d);
// Don't need to re-hover those that are already hovered
if (d.IsHovered)
{
// Check if this drawable previously handled hover, and assume it would once more
if (d == lastHoverHandledDrawable)
{
hoverHandledDrawable = lastHoverHandledDrawable;
break;
}
continue;
}
d.IsHovered = true;
if (d.TriggerEvent(new HoverEvent(state)))
{
hoverHandledDrawable = d;
break;
}
}
}
// Unhover all previously hovered drawables which are no longer hovered.
foreach (Drawable d in lastHoveredDrawables)
{
d.IsHovered = false;
d.TriggerEvent(new HoverLostEvent(state));
}
hoverEventsUpdated = true;
}
private bool isModifierKey(Key k) =>
k == Key.LControl || k == Key.RControl
|| k == Key.LAlt || k == Key.RAlt
|| k == Key.LShift || k == Key.RShift
|| k == Key.LWin || k == Key.RWin;
protected virtual void HandleKeyboardKeyStateChange(ButtonStateChangeEvent<Key> keyboardKeyStateChange)
{
var state = keyboardKeyStateChange.State;
var key = keyboardKeyStateChange.Button;
var kind = keyboardKeyStateChange.Kind;
GetButtonEventManagerFor(key).HandleButtonStateChange(state, kind);
if (kind == ButtonStateChangeKind.Pressed)
{
if (!isModifierKey(key))
{
keyboardRepeatKey = key;
keyboardRepeatTime = repeat_initial_delay;
}
}
else
{
if (key == keyboardRepeatKey)
{
keyboardRepeatKey = null;
keyboardRepeatTime = 0;
}
}
}
protected virtual void HandleTouchStateChange(TouchStateChangeEvent e)
{
Debug.Assert(e.LastPosition != null || e.IsActive != null, $"A {nameof(TouchStateChangeEvent)} provided with no changes information.");
var manager = GetButtonEventManagerFor(e.Touch.Source);
if (e.LastPosition is Vector2 lastPosition)
manager.HandlePositionChange(e.State, lastPosition);
if (e.IsActive is bool active)
manager.HandleButtonStateChange(e.State, active ? ButtonStateChangeKind.Pressed : ButtonStateChangeKind.Released);
}
/// <summary>
/// The number of touches which are currently active, causing a single cumulative "mouse down" state.
/// </summary>
private readonly HashSet<TouchSource> mouseMappedTouchesDown = new HashSet<TouchSource>();
/// <summary>
/// Handles latest activated touch state change event to produce mouse input from.
/// </summary>
/// <param name="e">The latest activated touch state change event.</param>
/// <returns>Whether mouse input has been performed accordingly.</returns>
protected virtual bool HandleMouseTouchStateChange(TouchStateChangeEvent e)
{
if (!MapMouseToLatestTouch)
return false;
if (e.IsActive == true || e.LastPosition != null)
{
new MousePositionAbsoluteInputFromTouch(e)
{
Position = e.Touch.Position
}.Apply(CurrentState, this);
}
switch (e.IsActive)
{
case true:
mouseMappedTouchesDown.Add(e.Touch.Source);
break;
case false:
mouseMappedTouchesDown.Remove(e.Touch.Source);
break;
}
new MouseButtonInputFromTouch(MouseButton.Left, mouseMappedTouchesDown.Count > 0, e).Apply(CurrentState, this);
return true;
}
protected virtual void HandleTabletPenButtonStateChange(ButtonStateChangeEvent<TabletPenButton> tabletPenButtonStateChange)
=> GetButtonEventManagerFor(tabletPenButtonStateChange.Button).HandleButtonStateChange(tabletPenButtonStateChange.State, tabletPenButtonStateChange.Kind);
protected virtual void HandleTabletAuxiliaryButtonStateChange(ButtonStateChangeEvent<TabletAuxiliaryButton> tabletAuxiliaryButtonStateChange)
=> GetButtonEventManagerFor(tabletAuxiliaryButtonStateChange.Button).HandleButtonStateChange(tabletAuxiliaryButtonStateChange.State, tabletAuxiliaryButtonStateChange.Kind);
protected virtual void HandleJoystickButtonStateChange(ButtonStateChangeEvent<JoystickButton> joystickButtonStateChange)
=> GetButtonEventManagerFor(joystickButtonStateChange.Button).HandleButtonStateChange(joystickButtonStateChange.State, joystickButtonStateChange.Kind);
protected virtual void HandleMidiKeyStateChange(ButtonStateChangeEvent<MidiKey> midiKeyStateChange)
=> GetButtonEventManagerFor(midiKeyStateChange.Button).HandleButtonStateChange(midiKeyStateChange.State, midiKeyStateChange.Kind);
public virtual void HandleInputStateChange(InputStateChangeEvent inputStateChange)
{
switch (inputStateChange)
{
case MousePositionChangeEvent mousePositionChange:
HandleMousePositionChange(mousePositionChange);
return;
case MouseScrollChangeEvent mouseScrollChange:
HandleMouseScrollChange(mouseScrollChange);
return;
case ButtonStateChangeEvent<MouseButton> mouseButtonStateChange:
HandleMouseButtonStateChange(mouseButtonStateChange);
return;
case ButtonStateChangeEvent<Key> keyboardKeyStateChange:
HandleKeyboardKeyStateChange(keyboardKeyStateChange);
return;
case TouchStateChangeEvent touchChange:
var manager = GetButtonEventManagerFor(touchChange.Touch.Source);
bool touchWasHandled = manager.HeldDrawable != null;
HandleTouchStateChange(touchChange);
bool touchIsHandled = manager.HeldDrawable != null;
// Produce mouse input if no drawable in the input queue has handled this touch event.
// Done for compatibility with components that do not handle touch input directly.
if (!touchWasHandled && !touchIsHandled)
HandleMouseTouchStateChange(touchChange);
return;
case ButtonStateChangeEvent<TabletPenButton> tabletPenButtonStateChange:
HandleTabletPenButtonStateChange(tabletPenButtonStateChange);
return;
case ButtonStateChangeEvent<TabletAuxiliaryButton> tabletAuxiliaryButtonStateChange:
HandleTabletAuxiliaryButtonStateChange(tabletAuxiliaryButtonStateChange);
return;
case ButtonStateChangeEvent<JoystickButton> joystickButtonStateChange:
HandleJoystickButtonStateChange(joystickButtonStateChange);
return;
case JoystickAxisChangeEvent joystickAxisChangeEvent:
HandleJoystickAxisChange(joystickAxisChangeEvent);
return;
case ButtonStateChangeEvent<MidiKey> midiKeyStateChange:
HandleMidiKeyStateChange(midiKeyStateChange);
return;
}
}
protected virtual void HandleJoystickAxisChange(JoystickAxisChangeEvent e)
=> GetJoystickAxisEventManagerFor(e.Axis.Source).HandleAxisChange(e.State, e.Axis.Value, e.LastValue);
protected virtual void HandleMousePositionChange(MousePositionChangeEvent e)
{
var state = e.State;
var mouse = state.Mouse;
foreach (var h in InputHandlers)
{
if (h.Enabled.Value && h is INeedsMousePositionFeedback handler)
handler.FeedbackMousePositionChange(mouse.Position, h == mouseSource);
}
handleMouseMove(state, e.LastPosition);
foreach (var manager in mouseButtonEventManagers.Values)
manager.HandlePositionChange(state, e.LastPosition);
updateHoverEvents(state);
}
protected virtual void HandleMouseScrollChange(MouseScrollChangeEvent e)
{
handleScroll(e.State, e.LastScroll, e.IsPrecise);
}
protected virtual void HandleMouseButtonStateChange(ButtonStateChangeEvent<MouseButton> e)
{
if (mouseButtonEventManagers.TryGetValue(e.Button, out var manager))
manager.HandleButtonStateChange(e.State, e.Kind);
}
private bool handleMouseMove(InputState state, Vector2 lastPosition) => PropagateBlockableEvent(PositionalInputQueue, new MouseMoveEvent(state, lastPosition));
private bool handleScroll(InputState state, Vector2 lastScroll, bool isPrecise) => PropagateBlockableEvent(PositionalInputQueue, new ScrollEvent(state, state.Mouse.Scroll - lastScroll, isPrecise));
/// <summary>
/// Triggers events on drawables in <paramref name="drawables"/> until it is handled.
/// </summary>
/// <param name="drawables">The drawables in the queue.</param>
/// <param name="e">The event.</param>
/// <returns>Whether the event was handled.</returns>
protected virtual bool PropagateBlockableEvent(SlimReadOnlyListWrapper<Drawable> drawables, UIEvent e)
{
foreach (var d in drawables)
{
if (!d.TriggerEvent(e)) continue;
if (shouldLog(e))
{
string detail = d is ISuppressKeyEventLogging ? e.GetType().ReadableName() : e.ToString();
Logger.Log($"{detail} handled by {d}.", LoggingTarget.Runtime, LogLevel.Debug);
}
return true;
}
return false;
}
private bool shouldLog(UIEvent eventType)
{
switch (eventType)
{
case KeyDownEvent k:
return !k.Repeat;
case DragEvent:
case ScrollEvent:
case MouseMoveEvent:
return false;
default:
return true;
}
}
/// <summary>
/// Unfocus the current focused drawable if it is no longer in a valid state.
/// </summary>
/// <returns>true if there is no longer a focus.</returns>
private bool unfocusIfNoLongerValid()
{
if (FocusedDrawable == null) return true;
if (isDrawableValidForFocus(FocusedDrawable))
return false;
Logger.Log($"Focus on \"{FocusedDrawable}\" no longer valid as a result of {nameof(unfocusIfNoLongerValid)}.", LoggingTarget.Runtime, LogLevel.Debug);
ChangeFocus(null);
return true;
}
private bool isDrawableValidForFocus(Drawable drawable)
{
bool valid = drawable.IsAlive && drawable.IsPresent && drawable.Parent != null;
if (valid)
{
//ensure we are visible
CompositeDrawable d = drawable.Parent;
while (d != null)
{
if (!d.IsPresent || !d.IsAlive)
{
valid = false;
break;
}
d = d.Parent;
}
}
return valid;
}
protected virtual void ChangeFocusFromClick(Drawable clickedDrawable)
{
Drawable focusTarget = null;
if (clickedDrawable != null)
{
focusTarget = clickedDrawable;
if (!focusTarget.AcceptsFocus)
{
// search upwards from the clicked drawable until we find something to handle focus.
Drawable previousFocused = FocusedDrawable;
while (focusTarget?.AcceptsFocus == false)
focusTarget = focusTarget.Parent;
if (focusTarget != null && previousFocused != null)
{
// we found a focusable target above us.
// now search upwards from previousFocused to check whether focusTarget is a common parent.
Drawable search = previousFocused;
while (search != null && search != focusTarget)
search = search.Parent;
if (focusTarget == search)
// we have a common parent, so let's keep focus on the previously focused target.
focusTarget = previousFocused;
}
}
}
ChangeFocus(focusTarget);
}
private void focusTopMostRequestingDrawable()
{
// todo: don't rebuild input queue every frame
foreach (var d in NonPositionalInputQueue)
{
if (d.RequestsFocus)
{
ChangeFocus(d);
return;
}
}
ChangeFocus(null);
}
private class MouseLeftButtonEventManager : MouseButtonEventManager
{
public MouseLeftButtonEventManager(MouseButton button)
: base(button)
{
}
public override bool EnableDrag => true;