This repository has been archived by the owner on Nov 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
InputManager.cs
346 lines (245 loc) · 13.9 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
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Aragas.Core.Wrappers;
namespace MineLib.PGL
{
public enum AnalogStickDirection { None, Up, UpRight, Right, DownRight, Down, DownLeft, Left, UpLeft }
public static class InputManager
{
public static bool TouchEnabled = true;
public static bool MouseEnabled = true;
public static bool KeyboardEnabled = true;
public static bool GamepadEnabled = true;
#region Fields
public static Rectangle TouchCameraRectangle;
public static Vector2 TouchCameraDelta;
public static Rectangle TouchMoveRectangle;
public static Vector2 TouchMoveDelta;
public static Rectangle UpArrwow, DownArrow, LeftArrow, RightArrow, Jump, Crouch;
private static MouseState CurrentMouseState;
private static KeyboardState CurrentKeyboardState;
private static GamePadState CurrentGamePadState;
public static TouchCollection CurrentTouchCollection;
public static List<Keys> CurrentOtherKeys;
private static MouseState LastMouseState;
private static KeyboardState LastKeyboardState;
private static GamePadState LastGamePadState;
public static TouchCollection LastTouchCollection;
public static List<Keys> LastOtherKeys;
#endregion Fields
#region Properties
public static List<GestureSample> TouchGestures = new List<GestureSample>();
public static Point MousePosition => CurrentMouseState.Position;
public static bool MouseLeftClicked => CurrentMouseState.LeftButton == ButtonState.Pressed && LastMouseState.LeftButton == ButtonState.Released;
public static bool MouseRightClicked => CurrentMouseState.RightButton == ButtonState.Pressed && LastMouseState.RightButton == ButtonState.Released;
public static bool MoveForward { get { return IsCurrentKeyPressed(Keys.W) || IsCurrentButtonPressed(Buttons.LeftThumbstickUp) || moveForwardTouch; } }
private static bool moveForwardTouch;
public static bool MoveBackward { get { return IsCurrentKeyPressed(Keys.S) || IsCurrentButtonPressed(Buttons.LeftThumbstickDown) || moveBackwardTouch; } }
private static bool moveBackwardTouch;
public static bool MoveLeft { get { return IsCurrentKeyPressed(Keys.A) || IsCurrentButtonPressed(Buttons.LeftThumbstickLeft) || moveLeftTouch; } }
private static bool moveLeftTouch;
public static bool MoveRight { get { return IsCurrentKeyPressed(Keys.D) || IsCurrentButtonPressed(Buttons.LeftThumbstickRight) || moveRightTouch; } }
private static bool moveRightTouch;
public static bool MoveJump { get { return IsCurrentKeyPressed(Keys.Space) || IsCurrentButtonPressed(Buttons.B) || moveJumpTouch; } }
private static bool moveJumpTouch;
public static bool MoveCrouch { get { return IsCurrentKeyPressed(Keys.LeftShift) || IsCurrentButtonPressed(Buttons.A) || moveCrouchTouch; } }
private static bool moveCrouchTouch;
public static bool ButtonCameraUp => IsCurrentKeyPressed(Keys.Up) || IsCurrentButtonPressed(Buttons.RightThumbstickUp);
public static bool ButtonCameraDown => IsCurrentKeyPressed(Keys.Down) || IsCurrentButtonPressed(Buttons.RightThumbstickDown);
public static bool ButtonCameraLeft => IsCurrentKeyPressed(Keys.Left) || IsCurrentButtonPressed(Buttons.RightThumbstickLeft);
public static bool ButtonCameraRight => IsCurrentKeyPressed(Keys.Right) || IsCurrentButtonPressed(Buttons.RightThumbstickRight);
public static bool ButtonCameraSwitch { get { return IsOncePressed(Keys.O); } }
public static bool MouseScrollUp => CurrentMouseState.ScrollWheelValue > LastMouseState.ScrollWheelValue;
public static bool MouseScrollDown => CurrentMouseState.ScrollWheelValue < LastMouseState.ScrollWheelValue;
public static bool MenuUIUp => (CurrentGamePadState.DPad.Up == ButtonState.Pressed && LastGamePadState.DPad.Up == ButtonState.Released && CurrentGamePadState.IsButtonUp(Buttons.LeftTrigger)) || IsOncePressed(Keys.Up);
public static bool MenuUIDown => (CurrentGamePadState.DPad.Down == ButtonState.Pressed && LastGamePadState.DPad.Down == ButtonState.Released && CurrentGamePadState.IsButtonUp(Buttons.LeftTrigger)) || IsOncePressed(Keys.Down);
public static bool MenuUIPressed => IsOncePressed(Buttons.A) || IsOncePressed(Keys.Enter);
public static bool GameUILeft => CurrentGamePadState.Buttons.LeftShoulder == ButtonState.Pressed && LastGamePadState.Buttons.LeftShoulder == ButtonState.Released;
public static bool GameUIRight => CurrentGamePadState.Buttons.RightShoulder == ButtonState.Pressed && LastGamePadState.Buttons.RightShoulder == ButtonState.Released;
public static Keys[] CurrentKeys { get { var list = new List<Keys>(CurrentOtherKeys); list.AddRange(CurrentKeyboardState.GetPressedKeys()); return list.ToArray(); } }
public static Keys[] LastKeys { get { var list = new List<Keys>(LastOtherKeys); list.AddRange(LastKeyboardState.GetPressedKeys()); return list.ToArray(); } }
#endregion Properties
static InputManager()
{
CurrentOtherKeys = new List<Keys>();
LastOtherKeys = new List<Keys>();
TouchPanel.EnabledGestures = GestureType.FreeDrag | GestureType.Hold | GestureType.DoubleTap | GestureType.Tap;
//TouchPanel.EnableMouseGestures = true;
//TouchPanel.EnableMouseTouchPoint = true;
InputWrapper.OnKey += KeyOnKey;
}
private static void KeyOnKey(object sender, KeyPressedEventArgs e)
{
// CurrentKeys.Add((Keys)e.Key);
}
public static void Update(GameTime time)
{
LastOtherKeys = new List<Keys>(CurrentOtherKeys);
CurrentOtherKeys.Clear();
LastMouseState = CurrentMouseState;
CurrentMouseState = Mouse.GetState();
LastKeyboardState = CurrentKeyboardState;
CurrentKeyboardState = Keyboard.GetState();
LastGamePadState = CurrentGamePadState;
CurrentGamePadState = GamePad.GetState(PlayerIndex.One);
LastTouchCollection = CurrentTouchCollection;
CurrentTouchCollection = TouchPanel.GetState();
if (TouchEnabled)
{
TouchGestures.Clear();
while (TouchPanel.IsGestureAvailable)
TouchGestures.Add(TouchPanel.ReadGesture());
TouchCameraDelta = Vector2.Zero;
foreach (var gesture in TouchGestures)
{
switch (gesture.GestureType)
{
case GestureType.Hold:
break;
case GestureType.Tap:
break;
case GestureType.DoubleTap:
break;
case GestureType.FreeDrag:
if (TouchCameraRectangle.Contains(gesture.Position))
TouchCameraDelta = gesture.Delta;
if (TouchMoveRectangle.Contains(gesture.Position))
TouchMoveDelta = gesture.Delta;
break;
}
}
}
}
#region Methods
public static bool IsCurrentKeyPressed(Keys key) { return CurrentKeys.Any(pressedKey => pressedKey == key); }
public static bool IsLastKeyPressed(Keys key) { return LastKeys.Any(pressedKey => pressedKey == key); }
public static bool IsCurrentButtonPressed(Buttons button) { return CurrentGamePadState.IsButtonDown(button); }
public static bool IsOncePressed(Keys key) { return IsCurrentKeyPressed(key) && !IsLastKeyPressed(key); }
public static bool IsOncePressed(Buttons button) { return CurrentGamePadState.IsButtonDown(button) && LastGamePadState.IsButtonUp(button); }
public static bool IsOncePressed(Buttons firstButton, Buttons secondButton)
{
if (LastGamePadState.IsButtonUp(firstButton) && LastGamePadState.IsButtonUp(secondButton))
return false;
return LastGamePadState.IsButtonDown(firstButton) && (LastGamePadState.IsButtonDown(secondButton) && CurrentGamePadState.IsButtonUp(secondButton));
}
public static void ShowKeyboard()
{
InputWrapper.ShowKeyboard();
}
public static void HideKeyboard()
{
InputWrapper.HideKeyboard();
}
#endregion
#region AnalogThumbStick stuff
const float PI = (float) Math.PI;
// You could make this user-configurable.
const float Deadzone = 0.8f;
const float AliveAngle = PI / 4 - (PI / 16);
// Remember PI = 180 degrees
const float AliveAngle1Start = PI * 0;
const float AliveAngle1End = PI * 0.5f - AliveAngle;
const float AliveAngle2Start = PI * 0.5f + AliveAngle;
const float AliveAngle2End = PI * 1f - AliveAngle;
const float AliveAngle3Start = PI * 1f + AliveAngle;
const float AliveAngle3End = PI * 1.5f - AliveAngle;
const float AliveAngle4Start = PI * 1.5f + AliveAngle;
const float AliveAngle4End = PI * 2f - AliveAngle;
public static AnalogStickDirection GetAnalogStickDirection(Vector2 gamepadThumbStick)
{
// Get the length and prevent something from happening
// if it's in our deadzone.
var length = gamepadThumbStick.Length();
if (length < Deadzone)
return AnalogStickDirection.None;
// Find the angle that the stick is at. see: http://en.wikipedia.org/wiki/File:Atan2_60.svg
var angle = (float) Math.Atan2(gamepadThumbStick.Y, gamepadThumbStick.X);
if (angle < 0)
angle += PI * 2; // Simpify our checks.
if (angle > AliveAngle4End)
return AnalogStickDirection.Right;
if (angle > AliveAngle4Start)
return AnalogStickDirection.DownRight;
if (angle > AliveAngle3End)
return AnalogStickDirection.Down;
if (angle > AliveAngle3Start)
return AnalogStickDirection.DownLeft;
if (angle > AliveAngle2End)
return AnalogStickDirection.Left;
if (angle > AliveAngle2Start)
return AnalogStickDirection.UpLeft;
if (angle > AliveAngle1End)
return AnalogStickDirection.Up;
if (angle > AliveAngle1Start)
return AnalogStickDirection.UpRight;
return AnalogStickDirection.Right;
}
public static AnalogStickDirection GetAnalogStickLeftDirection()
{
var gamepadThumbStick = CurrentGamePadState.ThumbSticks.Left;
// Get the length and prevent something from happening
// if it's in our deadzone.
var length = gamepadThumbStick.Length();
if (length < Deadzone)
return AnalogStickDirection.None;
// Find the angle that the stick is at. see: http://en.wikipedia.org/wiki/File:Atan2_60.svg
var angle = (float)Math.Atan2(gamepadThumbStick.Y, gamepadThumbStick.X);
if (angle < 0)
angle += PI * 2; // Simpify our checks.
if (angle > AliveAngle4End)
return AnalogStickDirection.Right;
if (angle > AliveAngle4Start)
return AnalogStickDirection.DownRight;
if (angle > AliveAngle3End)
return AnalogStickDirection.Down;
if (angle > AliveAngle3Start)
return AnalogStickDirection.DownLeft;
if (angle > AliveAngle2End)
return AnalogStickDirection.Left;
if (angle > AliveAngle2Start)
return AnalogStickDirection.UpLeft;
if (angle > AliveAngle1End)
return AnalogStickDirection.Up;
if (angle > AliveAngle1Start)
return AnalogStickDirection.UpRight;
return AnalogStickDirection.Right;
}
public static AnalogStickDirection GetAnalogStickRightDirection()
{
var gamepadThumbStick = CurrentGamePadState.ThumbSticks.Right;
// Get the length and prevent something from happening
// if it's in our deadzone.
var length = gamepadThumbStick.Length();
if (length < Deadzone)
return AnalogStickDirection.None;
// Find the angle that the stick is at. see: http://en.wikipedia.org/wiki/File:Atan2_60.svg
var angle = (float)Math.Atan2(gamepadThumbStick.Y, gamepadThumbStick.X);
if (angle < 0)
angle += PI * 2; // Simpify our checks.
if (angle > AliveAngle4End)
return AnalogStickDirection.Right;
if (angle > AliveAngle4Start)
return AnalogStickDirection.DownRight;
if (angle > AliveAngle3End)
return AnalogStickDirection.Down;
if (angle > AliveAngle3Start)
return AnalogStickDirection.DownLeft;
if (angle > AliveAngle2End)
return AnalogStickDirection.Left;
if (angle > AliveAngle2Start)
return AnalogStickDirection.UpLeft;
if (angle > AliveAngle1End)
return AnalogStickDirection.Up;
if (angle > AliveAngle1Start)
return AnalogStickDirection.UpRight;
return AnalogStickDirection.Right;
}
public static Vector2 AnalogStickLeft => CurrentGamePadState.ThumbSticks.Left;
public static Vector2 AnalogStickRight => CurrentGamePadState.ThumbSticks.Right;
#endregion
}
}