-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathMonoInput.cs
More file actions
548 lines (457 loc) · 20.1 KB
/
Copy pathMonoInput.cs
File metadata and controls
548 lines (457 loc) · 20.1 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
using System.Diagnostics;
using Intersect.Client.Core.Controls;
using Intersect.Client.Framework.GenericClasses;
using Intersect.Client.Framework.Gwen.Control;
using Intersect.Client.Framework.Gwen.Input;
using Intersect.Client.Framework.Input;
using Intersect.Client.General;
using Intersect.Client.MonoGame.Graphics;
using Intersect.Client.ThirdParty;
using Intersect.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Keys = Intersect.Client.Framework.GenericClasses.Keys;
using Rectangle = Intersect.Client.Framework.GenericClasses.Rectangle;
namespace Intersect.Client.MonoGame.Input;
using MonoGameKeys = Microsoft.Xna.Framework.Input.Keys;
public partial class MonoInput : GameInput
{
private readonly Dictionary<Keys, MonoGameKeys> _intersectToMonoGameKeyMap;
public override InputDeviceType CursorMovementDevice { get; set; } = InputDeviceType.Mouse;
private GamePadState _currentGamePadState;
private GamePadState _previousGamePadState;
private KeyboardState _currentKeyboardState;
private KeyboardState _previousKeyboardState;
private MouseState _currentMouseState;
private MouseState _previousMouseState;
private int mMouseX;
private int mMouseY;
private int mMouseVScroll;
private int mMouseHScroll;
private Game mMyGame;
private readonly GamePadCapabilities[] _gamePadCapabilities =
new GamePadCapabilities[GamePad.MaximumGamePadCount];
private int _activeGamePad;
private bool _keyboardOpened;
private IControlSet _controlSet;
public MonoInput(Game myGame)
{
myGame.Window.TextInput += Window_TextInput;
mMyGame = myGame;
_intersectToMonoGameKeyMap = [];
foreach (var intersectKey in Enum.GetValues<Keys>())
{
#pragma warning disable CA1864
if (!_intersectToMonoGameKeyMap.ContainsKey(intersectKey))
#pragma warning restore CA1864
{
foreach (var monoGameKey in Enum.GetValues<MonoGameKeys>())
{
if (intersectKey == Keys.Shift)
{
_intersectToMonoGameKeyMap.Add(intersectKey, MonoGameKeys.LeftShift);
break;
}
if (intersectKey is Keys.Control or Keys.LControlKey)
{
_intersectToMonoGameKeyMap.Add(intersectKey, MonoGameKeys.LeftControl);
break;
}
if (intersectKey == Keys.RControlKey)
{
_intersectToMonoGameKeyMap.Add(intersectKey, MonoGameKeys.RightControl);
break;
}
if (intersectKey is Keys.Alt or Keys.LMenu)
{
_intersectToMonoGameKeyMap.Add(intersectKey, MonoGameKeys.LeftAlt);
break;
}
if (intersectKey == Keys.RMenu)
{
_intersectToMonoGameKeyMap.Add(intersectKey, MonoGameKeys.RightAlt);
break;
}
if (intersectKey == Keys.Return)
{
_intersectToMonoGameKeyMap.Add(intersectKey, MonoGameKeys.Enter);
break;
}
if (intersectKey.ToString() != monoGameKey.ToString())
{
continue;
}
_intersectToMonoGameKeyMap.Add(intersectKey, monoGameKey);
break;
}
}
if (!_intersectToMonoGameKeyMap.ContainsKey(intersectKey))
{
ApplicationContext.Context.Value?.Logger.LogDebug(
"No matching MonoGame key for {IntersectKey}",
intersectKey
);
}
}
_controlSet = new Controls();
InputHandler.FocusChanged += InputHandlerOnFocusChanged;
}
public override IControlSet ControlSet
{
get => _controlSet;
set => _controlSet = value;
}
public override bool MouseHitInterface => Interface.Interface.DoesMouseHitInterface();
public override bool IsMouseInBounds => Interface.Interface.IsInBounds(_currentMouseState.X, _currentMouseState.Y);
private void InputHandlerOnFocusChanged(Base? control, FocusSource focusSource)
{
if (control == default)
{
return;
}
if (focusSource == FocusSource.Mouse)
{
return;
}
switch (CursorMovementDevice)
{
case InputDeviceType.Mouse:
return;
}
Vector2 center = new(
(control.GlobalBounds.Left + control.GlobalBounds.Right) / 2f,
(control.GlobalBounds.Bottom + control.GlobalBounds.Top) / 2f
);
Mouse.SetPosition((int)center.X, (int)center.Y);
var mouseState = Mouse.GetState();
Interface.Interface.GwenInput.ProcessMessage(
new GwenInputMessage(IntersectInput.InputEvent.MouseMove, new System.Numerics.Vector2(mouseState.X, mouseState.Y), MouseButton.None, Keys.Alt)
);
}
private void Window_TextInput(object sender, TextInputEventArgs e)
{
Interface.Interface.GwenInput.ProcessMessage(
new GwenInputMessage(
IntersectInput.InputEvent.TextEntered, GetMousePosition(), MouseButton.None, Keys.Alt, false,
false, false, e.Character.ToString()
)
);
}
private bool MouseButtonDown(MouseState mouseState, MouseButton mb)
{
switch (mb)
{
case MouseButton.Left:
return mouseState.LeftButton == ButtonState.Pressed;
case MouseButton.Right:
return mouseState.RightButton == ButtonState.Pressed;
case MouseButton.Middle:
return mouseState.MiddleButton == ButtonState.Pressed;
case MouseButton.X1:
return mouseState.XButton1 == ButtonState.Pressed;
case MouseButton.X2:
return mouseState.XButton2 == ButtonState.Pressed;
default:
throw new ArgumentOutOfRangeException(nameof(mb), mb, null);
}
}
public override bool IsMouseButtonDown(MouseButton mb) => MouseButtonDown(_currentMouseState, mb);
public override bool WasMouseButtonDown(MouseButton mb) => MouseButtonDown(_previousMouseState, mb);
public override bool IsKeyDown(Keys key) =>
_intersectToMonoGameKeyMap.TryGetValue(key, out var mappedKey) && _currentKeyboardState.IsKeyDown(mappedKey);
public override bool WasKeyDown(Keys key) =>
_intersectToMonoGameKeyMap.TryGetValue(key, out var mappedKey) && _previousKeyboardState.IsKeyDown(mappedKey);
public override System.Numerics.Vector2 GetMousePosition()
{
return new System.Numerics.Vector2(mMouseX, mMouseY);
}
private void CheckMouseButton(Keys modifier, ButtonState bs, MouseButton mb)
{
if (Globals.GameState == GameStates.Intro)
{
return; //No mouse input allowed while showing intro slides
}
if (bs == ButtonState.Pressed && !IsMouseButtonDown(mb))
{
if (Core.Input.TestInterceptMouse(modifier, mb, down: true))
{
return;
}
Interface.Interface.GwenInput.ProcessMessage(
new GwenInputMessage(IntersectInput.InputEvent.MouseDown, GetMousePosition(), mb, Keys.Alt)
);
Core.Input.OnMouseDown(modifier, mb);
}
else if (bs == ButtonState.Released && IsMouseButtonDown(mb))
{
if (Core.Input.TestInterceptMouse(modifier, mb, down: false))
{
return;
}
Interface.Interface.GwenInput.ProcessMessage(
new GwenInputMessage(IntersectInput.InputEvent.MouseUp, GetMousePosition(), mb, Keys.Alt)
);
Core.Input.OnMouseUp(modifier, mb);
}
}
private static readonly TimeSpan ZoomDelay = TimeSpan.FromMilliseconds(300);
private DateTime _lastZoomTime = DateTime.MinValue;
private void CheckMouseScrollWheel(int scrlVValue, int scrlHValue)
{
System.Numerics.Vector2 p = default;
if (scrlVValue != mMouseVScroll || scrlHValue != mMouseHScroll)
{
p = new System.Numerics.Vector2(scrlHValue - mMouseHScroll, scrlVValue - mMouseVScroll);
Interface.Interface.GwenInput.ProcessMessage(
new GwenInputMessage(IntersectInput.InputEvent.MouseScroll, p, MouseButton.Middle, Keys.Alt)
);
var deltaV = Math.Sign(mMouseVScroll - scrlVValue);
var now = DateTime.Now;
if (now - _lastZoomTime >= ZoomDelay)
{
if ((InputHandler.HoveredControl == null
|| !InputHandler.HoveredControl.IsVisibleInTree
|| (InputHandler.HoveredControl is Canvas canvas && canvas == canvas.Canvas))
&& Globals.GameState == GameStates.InGame
&& Globals.Database.EnableScrollingWorldZoom)
{
if (deltaV < 0)
{
Core.Input.HandleZoomIn(false);
}
else
{
Core.Input.HandleZoomOut(false);
}
_lastZoomTime = now;
}
}
mMouseVScroll = scrlVValue;
mMouseHScroll = scrlHValue;
}
}
public override void Update(TimeSpan elapsed)
{
if (mMyGame.IsActive)
{
if (_keyboardOpened)
{
Steam.PumpEvents();
}
var gamePadCapabilities = Enumerable.Range(0, GamePad.MaximumGamePadCount)
.Select(GamePad.GetCapabilities)
.ToArray();
Array.Copy(
gamePadCapabilities,
_gamePadCapabilities,
Math.Min(gamePadCapabilities.Length, _gamePadCapabilities.Length)
);
var gamePadState = Enumerable
.Range(0, GamePad.MaximumGamePadCount)
.Select(GamePad.GetState)
.Skip(_activeGamePad)
.FirstOrDefault(gamePad => gamePad.IsConnected);
if (gamePadState.IsConnected)
{
const float DEADZONE = 0.15f; // deadzone threshold
var rightStickX = Math.Abs(gamePadState.ThumbSticks.Right.X) > DEADZONE ? gamePadState.ThumbSticks.Right.X : 0f;
var rightStickY = Math.Abs(gamePadState.ThumbSticks.Right.Y) > DEADZONE ? gamePadState.ThumbSticks.Right.Y : 0f;
var deltaX = (int)(rightStickX * elapsed.TotalSeconds * 1000);
var deltaY = (int)(-rightStickY * elapsed.TotalSeconds * 1000);
if (deltaX != 0 || deltaY != 0)
{
CursorMovementDevice = InputDeviceType.Controller;
}
var temporaryMouseState = Mouse.GetState();
Mouse.SetPosition(temporaryMouseState.X + deltaX, temporaryMouseState.Y + deltaY);
}
var keyboardState = Keyboard.GetState();
var mouseState = Mouse.GetState();
if (mouseState.X != mMouseX || mouseState.Y != mMouseY)
{
mMouseX = (int)(mouseState.X * ((MonoRenderer)Core.Graphics.Renderer).GetMouseOffset().X);
mMouseY = (int)(mouseState.Y * ((MonoRenderer)Core.Graphics.Renderer).GetMouseOffset().Y);
CursorMovementDevice = InputDeviceType.Mouse;
Interface.Interface.GwenInput.ProcessMessage(
new GwenInputMessage(
IntersectInput.InputEvent.MouseMove, GetMousePosition(), MouseButton.None, Keys.Alt
)
);
}
if (gamePadState.IsConnected)
{
var leftMouseButton = gamePadState.Buttons.A == ButtonState.Released
? mouseState.LeftButton
: ButtonState.Pressed;
var rightMouseButton = gamePadState.Buttons.B == ButtonState.Released
? mouseState.RightButton
: ButtonState.Pressed;
mouseState = new MouseState(
mouseState.X,
mouseState.Y,
mouseState.ScrollWheelValue,
leftMouseButton,
mouseState.MiddleButton,
rightMouseButton,
mouseState.XButton1,
mouseState.XButton2,
mouseState.HorizontalScrollWheelValue
);
var gamePadKeys = Enum.GetValues<Buttons>()
.Where(gamePadState.IsButtonDown)
.Select(
button => button switch
{
Buttons.None => MonoGameKeys.None,
Buttons.DPadUp => MonoGameKeys.Up,
Buttons.DPadDown => MonoGameKeys.Down,
Buttons.DPadLeft => MonoGameKeys.Left,
Buttons.DPadRight => MonoGameKeys.Right,
Buttons.Start => MonoGameKeys.Escape,
Buttons.Back => MonoGameKeys.None,
Buttons.LeftStick => MonoGameKeys.None,
Buttons.RightStick => MonoGameKeys.None,
Buttons.LeftShoulder => MonoGameKeys.Back,
Buttons.RightShoulder => MonoGameKeys.Tab,
Buttons.BigButton => MonoGameKeys.None,
Buttons.A => MonoGameKeys.Enter,
Buttons.B => MonoGameKeys.Back,
Buttons.X => MonoGameKeys.E,
Buttons.Y => MonoGameKeys.None,
Buttons.LeftThumbstickLeft => MonoGameKeys.None,
Buttons.RightTrigger => MonoGameKeys.None,
Buttons.LeftTrigger => MonoGameKeys.None,
Buttons.RightThumbstickUp => MonoGameKeys.None,
Buttons.RightThumbstickDown => MonoGameKeys.None,
Buttons.RightThumbstickRight => MonoGameKeys.None,
Buttons.RightThumbstickLeft => MonoGameKeys.None,
Buttons.LeftThumbstickUp => MonoGameKeys.None,
Buttons.LeftThumbstickDown => MonoGameKeys.None,
Buttons.LeftThumbstickRight => MonoGameKeys.None,
_ => throw new ArgumentOutOfRangeException(nameof(button), button, null)
}
);
keyboardState = new KeyboardState(
keyboardState.GetPressedKeys().Concat(gamePadKeys).ToArray(),
keyboardState.CapsLock,
keyboardState.NumLock
);
}
// Get what modifier key we're currently pressing.
var modifier = GetPressedModifier(keyboardState);
//Check for state changes in the left mouse button
if (Interface.Interface.IsInBounds(mouseState.X, mouseState.Y))
{
CheckMouseButton(modifier, mouseState.LeftButton, MouseButton.Left);
CheckMouseButton(modifier, mouseState.RightButton, MouseButton.Right);
CheckMouseButton(modifier, mouseState.MiddleButton, MouseButton.Middle);
CheckMouseButton(modifier, mouseState.XButton1, MouseButton.X1);
CheckMouseButton(modifier, mouseState.XButton2, MouseButton.X2);
CheckMouseScrollWheel(mouseState.ScrollWheelValue, mouseState.HorizontalScrollWheelValue);
}
foreach (var key in _intersectToMonoGameKeyMap)
{
if (keyboardState.IsKeyDown(key.Value) && !_currentKeyboardState.IsKeyDown(key.Value))
{
ApplicationContext.Context.Value?.Logger.LogTrace("{0} -> {1}", key.Key, key.Value);
Interface.Interface.GwenInput.ProcessMessage(
new GwenInputMessage(
IntersectInput.InputEvent.KeyDown, GetMousePosition(), MouseButton.None, key.Key
)
);
Core.Input.OnKeyPressed(modifier, key.Key);
}
else if (!keyboardState.IsKeyDown(key.Value) && _currentKeyboardState.IsKeyDown(key.Value))
{
Interface.Interface.GwenInput.ProcessMessage(
new GwenInputMessage(
IntersectInput.InputEvent.KeyUp, GetMousePosition(), MouseButton.None, key.Key
)
);
Core.Input.OnKeyReleased(modifier, key.Key);
}
}
(_previousGamePadState, _currentGamePadState) = (_currentGamePadState, gamePadState);
(_previousKeyboardState, _currentKeyboardState) = (_currentKeyboardState, keyboardState);
(_previousMouseState, _currentMouseState) = (_currentMouseState, mouseState);
}
else
{
var modifier = GetPressedModifier(_currentKeyboardState);
foreach (var key in _intersectToMonoGameKeyMap)
{
if (_currentKeyboardState.IsKeyDown(key.Value))
{
Interface.Interface.GwenInput.ProcessMessage(
new GwenInputMessage(
IntersectInput.InputEvent.KeyUp, GetMousePosition(), MouseButton.None, key.Key
)
);
Core.Input.OnKeyReleased(modifier, key.Key);
}
}
CheckMouseButton(modifier, ButtonState.Released, MouseButton.Left);
CheckMouseButton(modifier, ButtonState.Released, MouseButton.Right);
CheckMouseButton(modifier, ButtonState.Released, MouseButton.Middle);
CheckMouseButton(modifier, ButtonState.Released, MouseButton.X1);
CheckMouseButton(modifier, ButtonState.Released, MouseButton.X2);
_currentGamePadState = new GamePadState();
_previousGamePadState = new GamePadState();
_currentKeyboardState = new KeyboardState();
_previousKeyboardState = new KeyboardState();
_currentMouseState = new MouseState();
_previousMouseState = new MouseState();
}
}
public Keys GetPressedModifier(KeyboardState state)
{
var modifier = Keys.None;
if (state.IsKeyDown(MonoGameKeys.LeftControl) || state.IsKeyDown(MonoGameKeys.RightControl))
{
modifier = Keys.Control;
}
if (state.IsKeyDown(MonoGameKeys.LeftShift) || state.IsKeyDown(MonoGameKeys.RightShift))
{
modifier = Keys.Shift;
}
if (state.IsKeyDown(MonoGameKeys.LeftAlt) || state.IsKeyDown(MonoGameKeys.RightAlt))
{
modifier = Keys.Alt;
}
return modifier;
}
public override void OpenKeyboard(
KeyboardType type,
string text,
bool autoCorrection,
bool multiLine,
bool secure
)
{
return; //no on screen keyboard for pc clients
}
public override void OpenKeyboard(
KeyboardType keyboardType,
Action<string?> inputHandler,
string description,
string text,
bool multiline = false,
uint maxLength = 1024,
Rectangle? inputBounds = default
)
{
if (!Steam.SteamDeck)
{
return;
}
_keyboardOpened = Steam.ShowKeyboard(
inputHandler,
description,
existingInput: text,
keyboardType == KeyboardType.Password,
maxLength,
inputBounds
);
}
}