-
Notifications
You must be signed in to change notification settings - Fork 33
/
DirectXController.cs
272 lines (226 loc) · 7.69 KB
/
DirectXController.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
using System;
using System.Collections.Generic;
using System.Linq;
//using SharpDX.DirectInput;
namespace KSPAdvancedFlyByWire
{
enum DirectXAxis
{
X = 0,
Y = 1,
Z = 2,
Rx = 3,
Ry = 4,
Rz = 5,
Ax = 6,
Ay = 7,
Az = 8,
Tx = 9,
Ty = 10,
Tz = 11,
Slider0 = 12,
Slider1 = 13,
}
enum DirectXHatAxes
{
Centered = -1,
Up = 0,
Right = 9000,
Down = 18000,
Left = 27000,
RightUp = 4500,
RightDown = 13500,
LeftUp = 31500,
LeftDown = 22500
}
/* public class DirectXController : IController
{
private int m_AxesCount = 0;
private int m_ButtonsCount = 0;
private int m_HatsCount = 0;
int m_ControllerIndex = 0;
private Joystick m_Joystick = null;
private JoystickState m_State = null;
private static DirectInput m_DirectInput = null;
private static void InitializeDirectInput()
{
if (m_DirectInput != null)
{
return;
}
m_DirectInput = new DirectInput();
}
public DirectXController(int controllerIndex)
{
m_ControllerIndex = controllerIndex;
InitializeDirectInput();
var devices = m_DirectInput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AllDevices);
if (devices.Count < m_ControllerIndex)
{
return;
}
m_Joystick = new Joystick(m_DirectInput, devices[m_ControllerIndex].InstanceGuid);
m_Joystick.Properties.AxisMode = DeviceAxisMode.Absolute;
m_Joystick.Acquire();
m_AxesCount = m_Joystick.Capabilities.AxeCount;
m_ButtonsCount = m_Joystick.Capabilities.ButtonCount;
m_HatsCount = m_Joystick.Capabilities.PovCount;
int buttonsCount = m_ButtonsCount + m_HatsCount * 8;
int axesCount = Enum.GetNames(typeof(DirectXAxis)).Count();
InitializeStateArrays(buttonsCount, axesCount);
for (int i = 0; i < buttonsCount; i++)
{
buttonStates[i] = false;
}
for (int i = 0; i < axesCount; i++)
{
axisStates[i].m_NegativeDeadZone = float.MaxValue;
axisStates[i].m_PositiveDeadZone = float.MaxValue;
axisStates[i].m_Left = -1.0f;
axisStates[i].m_Identity = 0.0f;
axisStates[i].m_Right = 1.0f;
}
}
public override string GetControllerName()
{
var devices = m_DirectInput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AllDevices);
if (devices.Count < m_ControllerIndex)
{
return "";
}
return devices[m_ControllerIndex].InstanceName + " (DX)";
}
bool IsConnected()
{
var devices = m_DirectInput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AllDevices);
if (devices.Count < m_ControllerIndex)
{
return false;
}
return m_DirectInput.IsDeviceAttached(devices[m_ControllerIndex].InstanceGuid);
}
public static List<KeyValuePair<int, string>> EnumerateControllers()
{
InitializeDirectInput();
List<KeyValuePair<int, string>> controllers = new List<KeyValuePair<int, string>>();
int index = 0;
foreach (
DeviceInstance instance in
m_DirectInput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AllDevices))
{
controllers.Add(new KeyValuePair<int, string>(index, instance.InstanceName + " (DX)"));
index++;
}
return controllers;
}
public static bool IsControllerConnected(int id)
{
var devices = m_DirectInput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AllDevices);
if (devices.Count < id)
{
return false;
}
return m_DirectInput.IsDeviceAttached(devices[id].InstanceGuid);
}
void Deinitialize()
{
m_Joystick.Unacquire();
m_Joystick.Dispose();
m_Joystick = null;
}
public override void Update(FlightCtrlState state)
{
try
{
m_Joystick.Poll();
m_State = m_Joystick.GetCurrentState();
}
catch (Exception) {}
base.Update(state);
}
public override int GetButtonsCount()
{
return m_ButtonsCount + m_HatsCount * 8;
}
public override string GetButtonName(int id)
{
if (id < m_ButtonsCount)
{
return String.Format("Button #{0}", id);
}
int hatId = (id - m_ButtonsCount) / 8;
if (hatId < m_HatsCount)
{
int buttonId = (id - m_ButtonsCount) % 8;
return String.Format("Hat #{0} Button {1}", hatId, buttonId);
}
return "unknown";
}
public override int GetAxesCount()
{
return m_AxesCount;
}
public override string GetAxisName(int id)
{
if (id < m_AxesCount)
{
return String.Format("Axis #{0}", id);
}
return "unknown";
}
public override bool GetButtonState(int button)
{
if (button < m_ButtonsCount)
{
return m_State.Buttons[button];
}
int hatId = (button - m_ButtonsCount) / 8;
if (hatId < m_HatsCount)
{
int buttonId = (button - m_ButtonsCount) % 8;
return (DirectXHatAxes)Enum.GetValues(typeof (DirectXHatAxes)).GetValue(buttonId) ==
(DirectXHatAxes) m_State.PointOfViewControllers[hatId];
}
return false;
}
public override float GetRawAxisState(int analogInput)
{
if (analogInput < m_AxesCount)
{
switch ((DirectXAxis)analogInput)
{
case DirectXAxis.X:
return m_State.X;
case DirectXAxis.Y:
return m_State.Y;
case DirectXAxis.Z:
return m_State.Z;
case DirectXAxis.Rx:
return m_State.RotationX;
case DirectXAxis.Ry:
return m_State.RotationY;
case DirectXAxis.Rz:
return m_State.RotationZ;
case DirectXAxis.Ax:
return m_State.AccelerationX;
case DirectXAxis.Ay:
return m_State.AccelerationY;
case DirectXAxis.Az:
return m_State.AccelerationZ;
case DirectXAxis.Tx:
return m_State.TorqueX;
case DirectXAxis.Ty:
return m_State.TorqueY;
case DirectXAxis.Tz:
return m_State.TorqueZ;
case DirectXAxis.Slider0:
return m_State.Sliders[0];
case DirectXAxis.Slider1:
return m_State.Sliders[1];
}
}
return 0.0f;
}
}
*/
}