-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathXInputManager.cs
221 lines (203 loc) · 7.98 KB
/
XInputManager.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
using SharpDX.XInput;
using System;
using System.Collections.Generic;
using GTA.Math;
namespace Benjamin94
{
namespace Input
{
/// <summary>
/// This class will be the InputManager for all XInput devices
/// </summary>
class XInputManager : InputManager
{
/// <summary>
/// The XInput Controller which will be managed by this InputManager
/// </summary>
private Controller device;
public XInputManager(Controller device)
{
this.device = device;
DeviceState state = GetState();
X_CENTER_L = state.LeftThumbStick.X;
Y_CENTER_L = state.LeftThumbStick.Y;
X_CENTER_R = state.RightThumbStick.X;
Y_CENTER_R = state.RightThumbStick.Y;
}
public override string DeviceName
{
get
{
return "XInput controller: " + device.UserIndex;
}
}
public override string DeviceGuid
{
get
{
return DeviceName;
}
}
/// <summary>
/// Helper method to get all connected XInput devices
/// </summary>
/// <returns>A List with connected Controllers</returns>
public static List<Controller> GetDevices()
{
List<Controller> controllers = new List<Controller>();
foreach (UserIndex index in Enum.GetValues(typeof(UserIndex)))
{
if (index != UserIndex.Any)
{
Controller controller = new Controller(index);
if (controller.IsConnected)
{
controllers.Add(controller);
}
}
}
return controllers;
}
public override DeviceState GetState()
{
try
{
Gamepad pad = device.GetState().Gamepad;
DeviceState state = new DeviceState();
state.LeftThumbStick = NormalizeThumbStick(pad.LeftThumbX, pad.LeftThumbY, Gamepad.LeftThumbDeadZone);
state.RightThumbStick = NormalizeThumbStick(pad.RightThumbX, pad.RightThumbY, Gamepad.RightThumbDeadZone);
if (pad.LeftTrigger > Gamepad.TriggerThreshold)
{
state.Buttons.Add(DeviceButton.LeftTrigger);
}
if (pad.RightTrigger > Gamepad.TriggerThreshold)
{
state.Buttons.Add(DeviceButton.RightTrigger);
}
if (pad.Buttons != GamepadButtonFlags.None)
{
if ((pad.Buttons & GamepadButtonFlags.DPadUp) != 0)
{
state.Buttons.Add(DeviceButton.DPadUp);
}
if ((pad.Buttons & GamepadButtonFlags.DPadLeft) != 0)
{
state.Buttons.Add(DeviceButton.DPadLeft);
}
if ((pad.Buttons & GamepadButtonFlags.DPadRight) != 0)
{
state.Buttons.Add(DeviceButton.DPadRight);
}
if ((pad.Buttons & GamepadButtonFlags.DPadDown) != 0)
{
state.Buttons.Add(DeviceButton.DPadDown);
}
if ((pad.Buttons & GamepadButtonFlags.A) != 0)
{
state.Buttons.Add(DeviceButton.A);
}
if ((pad.Buttons & GamepadButtonFlags.B) != 0)
{
state.Buttons.Add(DeviceButton.B);
}
if ((pad.Buttons & GamepadButtonFlags.X) != 0)
{
state.Buttons.Add(DeviceButton.X);
}
if ((pad.Buttons & GamepadButtonFlags.Y) != 0)
{
state.Buttons.Add(DeviceButton.Y);
}
if ((pad.Buttons & GamepadButtonFlags.Start) != 0)
{
state.Buttons.Add(DeviceButton.Start);
}
if ((pad.Buttons & GamepadButtonFlags.Back) != 0)
{
state.Buttons.Add(DeviceButton.Back);
}
if ((pad.Buttons & GamepadButtonFlags.LeftShoulder) != 0)
{
state.Buttons.Add(DeviceButton.LeftShoulder);
}
if ((pad.Buttons & GamepadButtonFlags.RightShoulder) != 0)
{
state.Buttons.Add(DeviceButton.RightShoulder);
}
if ((pad.Buttons & GamepadButtonFlags.LeftThumb) != 0)
{
state.Buttons.Add(DeviceButton.LeftStick);
}
if ((pad.Buttons & GamepadButtonFlags.RightThumb) != 0)
{
state.Buttons.Add(DeviceButton.RightStick);
}
}
return state;
}
catch (Exception)
{
return new DeviceState();
}
}
/// <summary>
/// Removes deadzone from the input X and Y
/// </summary>
/// <param name="x">input X</param>
/// <param name="y">input Y</param>
/// <param name="deadZone">the deadzone to remove</param>
/// <returns></returns>
private static Vector2 NormalizeThumbStick(short x, short y, int deadZone)
{
int fx = x;
int fy = y;
if (fx * fx < deadZone * deadZone)
x = 0;
if (fy * fy < deadZone * deadZone)
y = 0;
return new Vector2(x < 0 ? -((float)x / (float)short.MinValue) : (float)x / (float)short.MaxValue,
y < 0 ? -((float)y / (float)short.MinValue) : (float)y / (float)short.MaxValue);
}
protected override Direction GetDirection(float X, float Y, float xCenter, float yCenter)
{
if (X < xCenter && Y > yCenter)
{
return Direction.ForwardLeft;
}
if (X > xCenter && Y > yCenter)
{
return Direction.ForwardRight;
}
if (X < xCenter && Y == yCenter)
{
return Direction.Left;
}
if (X == xCenter && Y > yCenter)
{
return Direction.Forward;
}
if (X == xCenter && Y < yCenter)
{
return Direction.Backward;
}
if (X < xCenter && Y < yCenter)
{
return Direction.BackwardLeft;
}
if (X > xCenter && Y < yCenter)
{
return Direction.BackwardRight;
}
if (X > xCenter && Y == yCenter)
{
return Direction.Right;
}
return Direction.None;
}
public override void Cleanup()
{
// nothing to do here
}
}
}
}