-
Notifications
You must be signed in to change notification settings - Fork 45
/
InputManager.cs
132 lines (110 loc) · 4.44 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
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using DirectX12GameEngine.Games;
namespace DirectX12GameEngine.Input
{
public class InputManager : GameSystemBase
{
private readonly List<IGamepadInputSource> gamepadSources = new List<IGamepadInputSource>();
private readonly List<IKeyboardInputSource> keyboardSources = new List<IKeyboardInputSource>();
private readonly List<IPointerInputSource> pointerSources = new List<IPointerInputSource>();
private readonly HashSet<VirtualKey> noKeys = new HashSet<VirtualKey>();
public ObservableCollection<IInputSource> Sources { get; } = new ObservableCollection<IInputSource>();
public IGamepadInputSource? Gamepad => gamepadSources.FirstOrDefault();
public IKeyboardInputSource? Keyboard => keyboardSources.FirstOrDefault();
public IPointerInputSource? Pointer => pointerSources.FirstOrDefault();
public IReadOnlyList<IGamepadInputSource> GamepadSources => gamepadSources.AsReadOnly();
public IReadOnlyList<IKeyboardInputSource> KeyboardSources => keyboardSources.AsReadOnly();
public IReadOnlyList<IPointerInputSource> PointerSources => pointerSources.AsReadOnly();
public InputManager()
{
Sources.CollectionChanged += OnSourcesCollectionChanged;
}
public InputManager(IInputSourceConfiguration configuration) : this()
{
foreach (IInputSource inputSource in configuration.Sources)
{
Sources.Add(inputSource);
}
}
public bool IsKeyDown(VirtualKey key)
{
return Keyboard?.DownKeys.Contains(key) ?? false;
}
public bool IsKeyPressed(VirtualKey key)
{
return Keyboard?.PressedKeys.Contains(key) ?? false;
}
public bool IsKeyReleased(VirtualKey key)
{
return Keyboard?.ReleasedKeys.Contains(key) ?? false;
}
public ISet<VirtualKey> DownKeys => Keyboard is null ? noKeys : Keyboard.DownKeys;
public ISet<VirtualKey> PressedKeys => Keyboard is null ? noKeys : Keyboard.PressedKeys;
public ISet<VirtualKey> ReleasedKeys => Keyboard is null ? noKeys : Keyboard.ReleasedKeys;
public void Scan()
{
foreach (IInputSource source in Sources)
{
source.Scan();
}
}
public override void Update(GameTime gameTime)
{
foreach (IInputSource source in Sources)
{
source.Update();
}
}
private void OnInputSourceAdded(IInputSource source)
{
switch (source)
{
case IKeyboardInputSource keyboardSource:
keyboardSources.Add(keyboardSource);
break;
case IPointerInputSource pointerSource:
pointerSources.Add(pointerSource);
break;
case IGamepadInputSource gamepadSource:
gamepadSources.Add(gamepadSource);
break;
}
}
private void OnInputSourceRemoved(IInputSource source)
{
switch (source)
{
case IKeyboardInputSource keyboardDevice:
keyboardSources.Remove(keyboardDevice);
break;
case IPointerInputSource pointerSource:
pointerSources.Remove(pointerSource);
break;
case IGamepadInputSource gamepadSource:
gamepadSources.Remove(gamepadSource);
break;
}
}
private void OnSourcesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (IInputSource source in e.NewItems.Cast<IInputSource>())
{
OnInputSourceAdded(source);
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (IInputSource source in e.OldItems.Cast<IInputSource>())
{
OnInputSourceRemoved(source);
}
break;
}
}
}
}