forked from ToadsworthLP/desktoptale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputManager.cs
89 lines (70 loc) · 3.74 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
using Desktoptale.Messages;
using Desktoptale.Messaging;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace Desktoptale
{
public class InputManager
{
public Vector2 DirectionalInput { get; private set; }
public Vector2 RawDirectionalInput { get; private set; }
public Point PointerPosition { get; private set; }
public Point VirtualScreenPointerPosition { get; private set; }
public bool LeftClickPressed { get; private set; }
public bool LeftClickJustPressed { get; private set; }
public bool RightClickPressed { get; private set; }
public bool RightClickJustPressed { get; private set; }
public bool RunButtonPressed { get; private set; }
public bool ShiftPressed { get; private set; }
public bool CtrlPressed { get; private set; }
public bool ActionButtonPressed { get; private set; }
public bool InteractButtonPressed { get; private set; }
public bool InteractButtonJustPressed { get; private set; }
private bool previousFrameLeftClick, previousFrameRightClick, previousFrameInteractButtonPressed;
private MonitorManager monitorManager;
public InputManager(MonitorManager monitorManager)
{
this.monitorManager = monitorManager;
}
public void Update()
{
UpdateKeyboardInput();
UpdateMouseInput();
}
private void UpdateKeyboardInput()
{
KeyboardState keyboardState = Keyboard.GetState();
Vector2 input = Vector2.Zero;
if (keyboardState.IsKeyDown(Keys.W)) input.Y -= 1f;
if (keyboardState.IsKeyDown(Keys.S)) input.Y += 1f;
if (keyboardState.IsKeyDown(Keys.A)) input.X -= 1f;
if (keyboardState.IsKeyDown(Keys.D)) input.X += 1f;
if (keyboardState.IsKeyDown(Keys.Up)) input.Y -= 1f;
if (keyboardState.IsKeyDown(Keys.Down)) input.Y += 1f;
if (keyboardState.IsKeyDown(Keys.Left)) input.X -= 1f;
if (keyboardState.IsKeyDown(Keys.Right)) input.X += 1f;
RawDirectionalInput = new Vector2(input.X, input.Y);
if(input.LengthSquared() > float.Epsilon) input.Normalize();
DirectionalInput = input;
RunButtonPressed = keyboardState.IsKeyDown(Keys.X) || keyboardState.IsKeyDown(Keys.LeftShift);
CtrlPressed = keyboardState.IsKeyDown(Keys.LeftControl) || keyboardState.IsKeyDown(Keys.RightControl);
ShiftPressed = keyboardState.IsKeyDown(Keys.LeftShift) || keyboardState.IsKeyDown(Keys.RightShift);
ActionButtonPressed = keyboardState.IsKeyDown(Keys.C);
previousFrameInteractButtonPressed = InteractButtonPressed;
InteractButtonPressed = keyboardState.IsKeyDown(Keys.Z) || keyboardState.IsKeyDown(Keys.Space);
InteractButtonJustPressed = InteractButtonPressed && !previousFrameInteractButtonPressed;
}
private void UpdateMouseInput()
{
previousFrameLeftClick = LeftClickPressed;
previousFrameRightClick = RightClickPressed;
MouseState mouseState = Mouse.GetState();
LeftClickPressed = mouseState.LeftButton == ButtonState.Pressed;
LeftClickJustPressed = LeftClickPressed && !previousFrameLeftClick;
RightClickPressed = mouseState.RightButton == ButtonState.Pressed;
RightClickJustPressed = RightClickPressed && !previousFrameRightClick;
PointerPosition = mouseState.Position;
VirtualScreenPointerPosition = monitorManager.ToVirtualScreenCoordinates(PointerPosition.ToVector2()).ToPoint();
}
}
}