-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMotionControllerController.cs
More file actions
346 lines (317 loc) · 13 KB
/
Copy pathMotionControllerController.cs
File metadata and controls
346 lines (317 loc) · 13 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
using System;
using System.Collections.Generic;
using Microsoft.DirectX.DirectInput;
namespace MotionControllerManager
{
/* Motion Controller Control Class
*
* This class enables the easy detection, enumeration, and reading of Motion Controllers attached to the system.
* It requires Managed DirectInput and DirectX to run.
*
* http://www.actionxl.com
*
* (c) ActionXL 2007
* Written by Benjamin Kalb
* bkalb@actionxl.com
*
* Edited to allow any joystick - GACJ Aug09
*
*/
public class MotionControllerManager
{
List<Device> applicationDevices;
List<JoystickState> currentStates, lastStates;
List<MotionDetector> detectors;
public List<Device> ApplicationDevices
{
get
{
return applicationDevices;
}
}
/// <summary>
/// The number of attached and detected Motion Controllers
/// </summary>
public int Count
{
get { return applicationDevices.Count; }
}
/// <summary>
/// A Framework for using ActionXL Motion Controllers
/// </summary>
public MotionControllerManager()
{
applicationDevices = new List<Device>();
currentStates = new List<JoystickState>();
lastStates = new List<JoystickState>();
detectors = new List<MotionDetector>();
}
/// <summary>
/// Clear all currently detected joysticks and re-detect all joysticks attached to the system,
/// enumerating all attached Motion Controllers.
/// </summary>
/// <param name="initDetecors">Whether or not to also initialize a motion detector for each joystick.</param>
public void initialize(bool initDetecors)
{
//Reset stored Joysticks
applicationDevices.Clear();
currentStates.Clear();
lastStates.Clear();
//Initialize DirectInput
// Enumerate Joysticks in the system.
foreach (DeviceInstance instance in Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly))
{
//DELETE // Create the device. Accept only Motion Controllers
//DELETE if (instance.InstanceName == "Motion Controller")
//DELETE {
applicationDevices.Add(new Device(instance.InstanceGuid));
currentStates.Add(new JoystickState());
lastStates.Add(new JoystickState());
//DELETE }
}
/*
if (applicationDevices.Count == 0)
{
applicationDevices.Add(null);
currentStates.Add(new JoystickState());
lastStates.Add(new JoystickState());
}
*/
if (applicationDevices.Count == 0)
{
//fail
throw new NoMotionControllerFoundException("No Motion Controller Found");
}
//loop through all detected controllers and set axis parameters.
foreach (Device applicationDevice in applicationDevices)
{
// Set the data format to the c_dfDIJoystick pre-defined format.
applicationDevice.SetDataFormat(DeviceDataFormat.Joystick);
// Enumerate all the objects on the device.
foreach (DeviceObjectInstance d in applicationDevice.Objects)
{
// For axes that are returned, set the DIPROP_RANGE property for the
// enumerated axis in order to scale min/max values.
if ((0 != (d.ObjectId & (int)DeviceObjectTypeFlags.Axis)))
{
// Set the range for the axis. The Motion Controller has 12 bits of precision (0-4095),
//so we use that range and center it about zero.
applicationDevice.Properties.SetRange(ParameterHow.ById, d.ObjectId, new InputRange(-2048, +2047));
}
}
}
}
/// <summary>
/// Activates or resets the MotionDetectors attached to the Motion Controllers
/// </summary>
public void initDetectors()
{
if (detectors.Count == 0)
{
for (int i = 0; i < applicationDevices.Count; i++)
{
detectors.Add(new MotionDetector());
}
}
else
{
foreach (MotionDetector detector in detectors)
{
detector.Clear();
}
}
}
/// <summary>
/// Removes all MotionDetectors
/// </summary>
public void releaseDetectors()
{
detectors.Clear();
}
/// <summary>
/// Poll each motion controller and get its state. This function should be called every frame.
/// </summary>
/// <param name="seconds">The number of seconds that have passed since the last update.</param>
public void update(double seconds)
{
if (applicationDevices.Count == 0)
return;
//set the last states (to check for button presses, etc.)
for (int i = 0; i < currentStates.Count; i++)
{
lastStates[i] = currentStates[i];
}
//Loop through all the Motion Controllers
foreach (Device applicationDevice in applicationDevices)
{
try
{
// Poll the device for info.
applicationDevice.Poll();
}
catch (InputException inputex)
{
if ((inputex is NotAcquiredException) || (inputex is InputLostException))
{
// Check to see if either the app
// needs to acquire the device, or
// if the app lost the device to another
// process.
try
{
// Acquire the device.
applicationDevice.Acquire();
}
catch (InputException)
{
// Failed to acquire the device.
// This could be because the app
// doesn't have focus.
return;
}
}
} //catch(InputException inputex)
// Get the state of the device.
try { currentStates[applicationDevices.IndexOf(applicationDevice)] = applicationDevice.CurrentJoystickState; }
// Catch any exceptions. None will be handled here,
// any device re-aquisition will be handled above.
catch (InputException)
{
return;
}
}
//update detectors if initialized
if (detectors.Count > 0)
{
for (int i = 0; i < currentStates.Count; i++)
{
detectors[i].feedData(currentStates[i].X, currentStates[i].Y, currentStates[i].Z, seconds);
}
}
}
/// <summary>
/// Get the value of an axis on Motion Controller 0
/// </summary>
/// <param name="axis">The axis to check (X=0, Y=1, Z=2)</param>
/// <returns>The value of the axis from -2048 to +2047</returns>
public int getAxis(int axis)
{
return getAxis(axis, 0);
}
/// <summary>
/// Get the value of an axis on a Motion Controller
/// </summary>
/// <param name="axis">The axis to check (X=0, Y=1, Z=2)</param>
/// <param name="joyStick">The zero-based index of the Motion Controller to read</param>
/// <returns>The value of the axis from -2048 to +2047</returns>
public int getAxis(int axis, int joyStick)
{
if (joyStick >= applicationDevices.Count)
{
throw new InsufficientControllersAttachedException("There are fewer than " + (joyStick + 1) + " Motion Controllers connected");
}
switch (axis)
{
case 0:
return currentStates[joyStick].X;
case 1:
return currentStates[joyStick].Y;
case 2:
return currentStates[joyStick].Z;
case 3:
return currentStates[joyStick].Rx;
case 4:
return currentStates[joyStick].Ry;
case 5:
return currentStates[joyStick].Rz;
case 6:
var slider6 = currentStates[joyStick].GetSlider();
return slider6.Length > 0 ? slider6[0] : 0;
case 7:
var slider7 = currentStates[joyStick].GetSlider();
return slider7.Length > 1 ? slider7[1] : 0;
case 8:
var slider8 = currentStates[joyStick].GetSlider();
return slider8.Length > 2 ? slider8[2] : 0;
default:
return 0;
}
}
public MotionDetector getDetector(int joyStick)
{
if (joyStick >= applicationDevices.Count)
throw new InsufficientControllersAttachedException("There are fewer than " + (joyStick + 1) + " Motion Controllers connected");
else if (detectors.Count > 0)
return detectors[joyStick];
else
throw new DetectorsNotInitializedException("You must call initDetectors before getting them.");
}
/// <summary>
/// Check the status of a button on Motion Controller 0
/// </summary>
/// <param name="button">The zero-based index of the button to read</param>
/// <returns>True if the button is down</returns>
public bool getButton(int button)
{
return getButton(button, 0);
}
/// <summary>
/// Check the status of a button on a Motion Controller
/// </summary>
/// <param name="button">The zero-based index of the button to read</param>
/// <param name="joyStick">The zero-based index of the Motion Controller to read</param>
/// <returns>True if the button is down</returns>
public bool getButton(int button, int joyStick)
{
if (joyStick >= applicationDevices.Count)
{
throw new InsufficientControllersAttachedException("There are fewer than " + (joyStick + 1) + " Motion Controllers connected");
}
return currentStates[joyStick].GetButtons()[button] > 0;
}
/// <summary>
/// Check whether a button was pressed this frame (getButton() was false last update() and is true this update()) on Motion Controller 0
/// </summary>
/// <param name="button">The zero-based index of the button to read</param>
/// <returns>True if the button was just pressed</returns>
public bool getButtonPress(int button)
{
return getButtonPress(button, 0);
}
/// <summary>
/// Check whether a button was pressed this frame (getButton() was false last update() and is true this update()) on a Motion Controller
/// </summary>
/// <param name="button">The zero-based index of the button to read</param>
/// <param name="joyStick">The zero-based index of the Motion Controller to read</param>
/// <returns>True if the button was just pressed</returns>
public bool getButtonPress(int button, int joyStick)
{
if (joyStick >= applicationDevices.Count)
{
throw new InsufficientControllersAttachedException("There are fewer than " + (joyStick + 1) + " Motion Controllers connected");
}
return currentStates[joyStick].GetButtons()[button] > 0 && lastStates[joyStick].GetButtons()[button] == 0;
}
}
class DetectorsNotInitializedException : Exception
{
public DetectorsNotInitializedException(string message)
: base(message)
{
}
}
class InsufficientControllersAttachedException : Exception
{
public InsufficientControllersAttachedException(string message)
: base(message)
{
}
}
class NoMotionControllerFoundException : Exception
{
public NoMotionControllerFoundException(string message)
: base(message)
{
}
}
}