-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputManager.cs
More file actions
338 lines (297 loc) · 12.5 KB
/
InputManager.cs
File metadata and controls
338 lines (297 loc) · 12.5 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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace Liquish;
public class InputManager {
public KeyboardState PKS;
public KeyboardState CKS;
public MouseState PMS;
public MouseState CMS;
public int HoldTime;
private Dictionary<Keys, int> keyHoldDurations = new Dictionary<Keys, int>();
private class TapInfo {
public long LastTapFrame;
public int TapCount;
public bool WasReleased;
public bool JustTapped;
public int CustomInterval;
}
private Dictionary<Keys, TapInfo> keyTapHistory = new Dictionary<Keys, TapInfo>();
private long currentFrame = 0;
private const int DEFAULT_MAX_INTERVAL = 20;
private readonly Dictionary<Keys, char> KeyChars = new(){
{Keys.A, 'a'}, {Keys.B, 'b'}, {Keys.C, 'c'}, {Keys.D, 'd'}, {Keys.E, 'e'},
{Keys.F, 'f'}, {Keys.G, 'g'}, {Keys.H, 'h'}, {Keys.I, 'i'}, {Keys.J, 'j'},
{Keys.K, 'k'}, {Keys.L, 'l'}, {Keys.M, 'm'}, {Keys.N, 'n'}, {Keys.O, 'o'},
{Keys.P, 'p'}, {Keys.Q, 'q'}, {Keys.R, 'r'}, {Keys.S, 's'}, {Keys.T, 't'},
{Keys.U, 'u'}, {Keys.V, 'v'}, {Keys.W, 'w'}, {Keys.X, 'x'}, {Keys.Y, 'y'},
{Keys.Z, 'z'},
{Keys.D0, '0'}, {Keys.D1, '1'}, {Keys.D2, '2'}, {Keys.D3, '3'}, {Keys.D4, '4'},
{Keys.D5, '5'}, {Keys.D6, '6'}, {Keys.D7, '7'}, {Keys.D8, '8'}, {Keys.D9, '9'},
{Keys.NumPad0, '0'}, {Keys.NumPad1, '1'}, {Keys.NumPad2, '2'}, {Keys.NumPad3, '3'},
{Keys.NumPad4, '4'}, {Keys.NumPad5, '5'}, {Keys.NumPad6, '6'}, {Keys.NumPad7, '7'},
{Keys.NumPad8, '8'}, {Keys.NumPad9, '9'},
{Keys.Space, ' '}, {Keys.OemPeriod, '.'}, {Keys.OemComma, ','},
{Keys.OemMinus, '-'}, {Keys.OemPlus, '+'}, {Keys.OemQuestion, '?'},
{Keys.OemQuotes, '\''}, {Keys.OemSemicolon, ';' }, {Keys.OemPipe, '\\'},
{Keys.OemBackslash, '\\'},
};
private readonly Dictionary<Keys, char> ShiftedKeyChars = new()
{
{Keys.D1, '!'}, {Keys.D2, '@'}, {Keys.D3, '#'}, {Keys.D4, '$'}, {Keys.D5, '%'},
{Keys.D6, '^'}, {Keys.D7, '&'}, {Keys.D8, '*'}, {Keys.D9, '('}, {Keys.D0, ')'},
{Keys.OemPeriod, '>'}, {Keys.OemComma, '<'}, {Keys.OemMinus, '_'},
{Keys.OemPlus, '+'}, {Keys.OemQuestion, '?'}, {Keys.OemQuotes, '"'},
{Keys.OemSemicolon, ':'},{Keys.OemPipe, '|'},{Keys.OemBackslash, '|'},
};
public InputManager() {
CKS = Keyboard.GetState();
PKS = CKS;
CMS = Mouse.GetState();
PMS = CMS;
}
public void Update() {
PKS = CKS;
CKS = Keyboard.GetState();
PMS = CMS;
CMS = Mouse.GetState();
currentFrame++;
if (CKS.GetPressedKeyCount() > 0 && !OnlyAModIsPressed())
HoldTime++;
else
HoldTime = 0;
UpdateKeyHoldDurations();
UpdateTapTracking();
}
private void UpdateKeyHoldDurations() {
Keys[] pressedKeys = CKS.GetPressedKeys();
foreach (Keys key in pressedKeys) {
if (keyHoldDurations.ContainsKey(key)) {
keyHoldDurations[key]++;
}
else {
keyHoldDurations[key] = 1;
}
}
List<Keys> keysToReset = new List<Keys>();
foreach (Keys key in keyHoldDurations.Keys) {
if (!CKS.IsKeyDown(key)) {
keysToReset.Add(key);
}
}
foreach (Keys key in keysToReset) {
keyHoldDurations.Remove(key);
}
}
private void UpdateTapTracking() {
foreach (var key in keyTapHistory.Keys) {
keyTapHistory[key].JustTapped = false;
}
// Process all keys
foreach (Keys key in CKS.GetPressedKeys()) {
if (!keyTapHistory.ContainsKey(key)) {
keyTapHistory[key] = new TapInfo {
LastTapFrame = -DEFAULT_MAX_INTERVAL,
TapCount = 0,
WasReleased = true,
JustTapped = false,
CustomInterval = DEFAULT_MAX_INTERVAL
};
}
TapInfo info = keyTapHistory[key];
if (!PKS.IsKeyDown(key) && CKS.IsKeyDown(key) && info.WasReleased) {
if (currentFrame - info.LastTapFrame <= info.CustomInterval) {
}
else {
info.TapCount = 1;
}
info.LastTapFrame = currentFrame;
info.WasReleased = false;
info.JustTapped = true;
}
}
foreach (Keys key in Enum.GetValues(typeof(Keys))) {
if (keyTapHistory.ContainsKey(key)) {
if (PKS.IsKeyDown(key) && !CKS.IsKeyDown(key)) {
keyTapHistory[key].WasReleased = true;
}
TapInfo info = keyTapHistory[key];
if (info.TapCount > 0 &&
currentFrame - info.LastTapFrame > info.CustomInterval &&
info.WasReleased) {
info.TapCount = 0;
}
}
}
}
private bool ModifiersPressed(bool requireShift, bool requireCtrl, bool requireAlt) {
bool shift = CKS.IsKeyDown(Keys.LeftShift) || CKS.IsKeyDown(Keys.RightShift);
bool ctrl = CKS.IsKeyDown(Keys.LeftControl) || CKS.IsKeyDown(Keys.RightControl);
bool alt = CKS.IsKeyDown(Keys.LeftAlt) || CKS.IsKeyDown(Keys.RightAlt);
return (shift == requireShift) &&
(ctrl == requireCtrl) &&
(alt == requireAlt);
}
public bool KeyTapped(Keys key, bool Mod = false, bool Ctrl = false, bool Alt = false) {
return !PKS.IsKeyDown(key) && CKS.IsKeyDown(key) && ModifiersPressed(Mod, Ctrl, Alt);
}
public bool KeyReleased(Keys key, bool Mod = false, bool Ctrl = false, bool Alt = false) {
return PKS.IsKeyDown(key) && !CKS.IsKeyDown(key) && ModifiersPressed(Mod, Ctrl, Alt);
}
public bool KeyHeld(Keys key, bool Mod = false, bool Ctrl = false, bool Alt = false) {
return CKS.IsKeyDown(key) && ModifiersPressed(Mod, Ctrl, Alt);
}
public bool MBTapped(MouseButton button, bool Mod = false, bool Ctrl = false, bool Alt = false) {
return (button switch {
MouseButton.Left => !PMS.LeftButton.Equals(ButtonState.Pressed) && CMS.LeftButton.Equals(ButtonState.Pressed),
MouseButton.Right => !PMS.RightButton.Equals(ButtonState.Pressed) && CMS.RightButton.Equals(ButtonState.Pressed),
MouseButton.Middle => !PMS.MiddleButton.Equals(ButtonState.Pressed) && CMS.MiddleButton.Equals(ButtonState.Pressed),
_ => false,
}) && ModifiersPressed(Mod, Ctrl, Alt);
}
public bool MBReleased(MouseButton button, bool Mod = false, bool Ctrl = false, bool Alt = false) {
return (button switch {
MouseButton.Left => PMS.LeftButton.Equals(ButtonState.Pressed) && !CMS.LeftButton.Equals(ButtonState.Pressed),
MouseButton.Right => PMS.RightButton.Equals(ButtonState.Pressed) && !CMS.RightButton.Equals(ButtonState.Pressed),
MouseButton.Middle => PMS.MiddleButton.Equals(ButtonState.Pressed) && !CMS.MiddleButton.Equals(ButtonState.Pressed),
_ => false,
}) && ModifiersPressed(Mod, Ctrl, Alt);
}
public bool MBHeld(MouseButton button, bool Mod = false, bool Ctrl = false, bool Alt = false) {
return (button switch {
MouseButton.Left => CMS.LeftButton.Equals(ButtonState.Pressed),
MouseButton.Right => CMS.RightButton.Equals(ButtonState.Pressed),
MouseButton.Middle => CMS.MiddleButton.Equals(ButtonState.Pressed),
_ => false,
}) && ModifiersPressed(Mod, Ctrl, Alt);
}
public bool SomeKeyIsTapped() {
return PKS.GetPressedKeyCount() < CKS.GetPressedKeyCount();
}
public bool SomeKeyIsHeld() {
return CKS.GetPressedKeyCount() > 0;
}
public bool SomeKeyIsReleased() {
return PKS.GetPressedKeyCount() > CKS.GetPressedKeyCount();
}
public bool OneKeyIsTapped(Keys key) {
return (PKS.GetPressedKeyCount() == 0) && (CKS.GetPressedKeyCount() == 1) && KeyTapped(key);
}
public bool OneKeyIsHeld(Keys key) {
return (CKS.GetPressedKeyCount() == 1) && KeyHeld(key);
}
public bool OneKeyIsReleased(Keys key) {
return (PKS.GetPressedKeyCount() == 1) && (CKS.GetPressedKeyCount() == 0) && KeyReleased(key);
}
public bool OnlyAModIsPressed() {
int C = 0;
if (CKS.IsKeyDown(Keys.LeftShift) || CKS.IsKeyDown(Keys.RightShift))
C++;
if (CKS.IsKeyDown(Keys.LeftControl) || CKS.IsKeyDown(Keys.RightControl))
C++;
if (CKS.IsKeyDown(Keys.LeftAlt) || CKS.IsKeyDown(Keys.RightAlt))
C++;
return C > 0 && CKS.GetPressedKeyCount() == C;
}
public bool AModIsPressed() {
int C = 0;
if (CKS.IsKeyDown(Keys.LeftShift) || CKS.IsKeyDown(Keys.RightShift))
C++;
if (CKS.IsKeyDown(Keys.LeftControl) || CKS.IsKeyDown(Keys.RightControl))
C++;
if (CKS.IsKeyDown(Keys.LeftAlt) || CKS.IsKeyDown(Keys.RightAlt))
C++;
return C > 0;
}
public Point CMousePos() {
return CMS.Position;
}
public Point PMousePos() {
return PMS.Position;
}
public bool MWithinRect(Rectangle rect) {
return rect.Contains(CMS.X, CMS.Y);
}
public bool MouseMoves() {
return PMS.Position != CMS.Position;
}
public Point MouseMotionDir() {
return new(Math.Sign(CMS.X - PMS.X), Math.Sign(CMS.Y - PMS.Y));
}
public Point MouseMotionSpeed() {
return CMS.Position - PMS.Position;
}
public float MWheelValue() {
return Math.Sign(CMS.ScrollWheelValue - PMS.ScrollWheelValue);
}
public char? GetPressedChar() {
foreach (Keys key in CKS.GetPressedKeys()) {
bool shift = CKS.IsKeyDown(Keys.LeftShift) || CKS.IsKeyDown(Keys.RightShift);
if (KeyChars.ContainsKey(key) && key >= Keys.A && key <= Keys.Z) {
return shift ? char.ToUpper(KeyChars[key]) : KeyChars[key];
}
if (shift && ShiftedKeyChars.ContainsKey(key)) {
return ShiftedKeyChars[key];
}
if (!shift && KeyChars.ContainsKey(key)) {
return KeyChars[key];
}
}
return null;
}
public bool KeyHeldForDuration(Keys key, int duration, bool Mod = false, bool Ctrl = false, bool Alt = false) {
if (!KeyHeld(key, Mod, Ctrl, Alt)) {
return false;
}
return keyHoldDurations.ContainsKey(key) && keyHoldDurations[key] >= duration;
}
public bool KeyTappedAndHeldForDuration(Keys key, int duration, bool Mod = false, bool Ctrl = false, bool Alt = false) {
if (!KeyHeld(key, Mod, Ctrl, Alt)) {
return false;
}
return (keyHoldDurations.ContainsKey(key) && keyHoldDurations[key] >= duration) || KeyTapped(key, Mod, Ctrl, Alt);
}
public bool KeyHeldForExactDuration(Keys key, int duration, bool Mod = false, bool Ctrl = false, bool Alt = false) {
if (!KeyHeld(key, Mod, Ctrl, Alt)) {
return false;
}
return keyHoldDurations.ContainsKey(key) && keyHoldDurations[key] == duration;
}
public int GetKeyHoldDuration(Keys key) {
return keyHoldDurations.ContainsKey(key) ? keyHoldDurations[key] : 0;
}
public bool KeyMultiTapped(Keys key, int tapCount, int maxInterval = DEFAULT_MAX_INTERVAL, bool Mod = false, bool Ctrl = false, bool Alt = false) {
if (!ModifiersPressed(Mod, Ctrl, Alt))
return false;
if (keyTapHistory.ContainsKey(key)) {
keyTapHistory[key].CustomInterval = maxInterval;
}
else {
keyTapHistory[key] = new TapInfo {
LastTapFrame = -maxInterval,
TapCount = 0,
WasReleased = true,
JustTapped = false,
CustomInterval = maxInterval
};
}
TapInfo info = keyTapHistory[key];
if (info.TapCount == tapCount && info.JustTapped) {
info.TapCount = 0;
return true;
}
return false;
}
public bool HasKeyTapCount(Keys key, int tapCount) {
if (keyTapHistory.TryGetValue(key, out TapInfo info)) {
return info.TapCount == tapCount;
}
return false;
}
}
public enum MouseButton {
Left,
Right,
Middle
}