forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathJoystick.cs
435 lines (398 loc) · 14.8 KB
/
Joystick.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// AForge Controls Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © Andrew Kirillov, 2010
// andrew.kirillov@aforgenet.com
//
using System;
using System.Collections.Generic;
using AForge;
namespace AForge.Controls
{
/// <summary>
/// The class provides simple API for enumerating available joysticks and checking their
/// current status.
/// </summary>
///
/// <remarks><para>The class provides simple access to joysticks (game controllers) through using
/// Win32 API, which allows to enumerate available devices and query their status (state of all buttons,
/// axes, etc).</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // enumerate available devices
/// List<Joystick.DeviceInfo> devices = Joystick.GetAvailableDevices( );
///
/// foreach ( Joystick.DeviceInfo di in devices )
/// {
/// System.Diagnostics.Debug.WriteLine(
/// string.Format( "{0} : {1} ({2} axes, {3} buttons)",
/// di.ID, di.Name, di.Axes, di.Buttons ) );
/// }
///
///
/// // create new joystick and initialize it
/// Joystick joystick = new Joystick( 0 );
/// // get its current status
/// Joystick.Status status = joystick.GetCurrentStatus( );
/// // check if 1st button is pressed
/// if ( status.IsButtonPressed( Joystick.Buttons.Button1 ) )
/// {
/// // 1st button is pressed
/// }
/// </code>
/// </remarks>
///
public class Joystick
{
// information of the initialized joystick
private DeviceInfo info = null;
/// <summary>
/// Information about initialized joystick.
/// </summary>
///
/// <remarks><para>The property keeps information about joystick, which was
/// initialized using <see cref="Init"/> method. If no joystick was initialized,
/// then accessing this property will generate <see cref="ApplicationException"/>
/// exception.</para></remarks>
///
/// <exception cref="ApplicationException">Joystick was not initialized.</exception>
///
public DeviceInfo Info
{
get
{
if ( info == null )
throw new ApplicationException( "Joystick was not initialized." );
return info;
}
}
/// <summary>
/// Information about joystick connected to the system.
/// </summary>
///
public class DeviceInfo
{
/// <summary>
/// Joystick ID, [0..15].
/// </summary>
public readonly int ID;
internal readonly JoystickAPI.JOYCAPS capabilities;
/// <summary>
/// Joystick name.
/// </summary>
public string Name
{
get { return capabilities.name; }
}
/// <summary>
/// Number of joystick axes.
/// </summary>
public int Axes
{
get { return capabilities.axesNumber; }
}
/// <summary>
/// Number of joystick buttons.
/// </summary>
public int Buttons
{
get { return capabilities.buttonsNumber; }
}
internal DeviceInfo( int id, JoystickAPI.JOYCAPS joyCaps )
{
ID = id;
capabilities = joyCaps;
}
}
/// <summary>
/// Get list of available joysticks connected to the system.
/// </summary>
///
/// <returns>Returns list containing information about available joysticks connected to
/// the system.</returns>
///
public static List<DeviceInfo> GetAvailableDevices( )
{
List<DeviceInfo> devices = new List<DeviceInfo>( );
int joyCapsSize = System.Runtime.InteropServices.Marshal.SizeOf( typeof( JoystickAPI.JOYCAPS ) );
// get number of devices
int devicesCount = JoystickAPI.joyGetNumDevs( );
// check all devices
for ( int i = 0; i < devicesCount; i++ )
{
JoystickAPI.JOYCAPS joyCaps = new JoystickAPI.JOYCAPS( );
if ( JoystickAPI.joyGetDevCapsW( i, joyCaps, joyCapsSize ) == JoystickAPI.ResultCode.NoError )
{
devices.Add( new DeviceInfo( i, joyCaps ) );
}
}
return devices;
}
/// <summary>
/// Initializes a new instance of the <see cref="Joystick"/> class.
/// </summary>
///
/// <remarks><para>This constructor does not make initialization of any joystick
/// device, so <see cref="Init"/> method should be used before querying joystick
/// status or properties.</para></remarks>
///
public Joystick( ) { }
/// <summary>
/// Initializes a new instance of the <see cref="Joystick"/> class.
/// </summary>
///
/// <param name="id">Joystick ID to initialize, [0, 15].</param>
///
/// <remarks><para>This constructor initializes joystick with specified ID using
/// <see cref="Init"/> method, so the object becomes ready for querying joystick's
/// status.</para></remarks>
///
public Joystick( int id )
{
Init( id );
}
/// <summary>
/// Initialize joystick with the specified ID.
/// </summary>
///
/// <param name="id">Joystick's ID to initialize, [0, 15].</param>
///
/// <remarks><para></para></remarks>
///
/// <exception cref="ArgumentException">Invalid joystick ID was specified. It must be in [0, 15] range.</exception>
/// <exception cref="NotConnectedException">The requested joystick is not connected to the system.</exception>
///
public void Init( int id )
{
if ( ( id < 0 ) || ( id > 15 ) )
{
throw new ArgumentException( "Invalid joystick ID was specified." );
}
JoystickAPI.JOYCAPS joyCaps = new JoystickAPI.JOYCAPS( );
if ( JoystickAPI.joyGetDevCapsW( id, joyCaps,
System.Runtime.InteropServices.Marshal.SizeOf( joyCaps ) ) != JoystickAPI.ResultCode.NoError )
{
throw new NotConnectedException( "The requested joystick is not connected to the system." );
}
info = new DeviceInfo( id, joyCaps );
}
private static JoystickAPI.JoyPosFlags[] requestFlags = new JoystickAPI.JoyPosFlags[]
{
JoystickAPI.JoyPosFlags.ReturnPov | JoystickAPI.JoyPosFlags.ReturnButtons,
JoystickAPI.JoyPosFlags.ReturnPov | JoystickAPI.JoyPosFlags.ReturnButtons | JoystickAPI.JoyPosFlags.ReturnX,
JoystickAPI.JoyPosFlags.ReturnPov | JoystickAPI.JoyPosFlags.ReturnButtons | JoystickAPI.JoyPosFlags.ReturnXY,
JoystickAPI.JoyPosFlags.ReturnPov | JoystickAPI.JoyPosFlags.ReturnButtons | JoystickAPI.JoyPosFlags.ReturnXYZ,
JoystickAPI.JoyPosFlags.ReturnPov | JoystickAPI.JoyPosFlags.ReturnButtons | JoystickAPI.JoyPosFlags.ReturnXYZR,
JoystickAPI.JoyPosFlags.ReturnPov | JoystickAPI.JoyPosFlags.ReturnButtons | JoystickAPI.JoyPosFlags.ReturnXYZRU,
};
/// <summary>
/// Get joystick's status.
/// </summary>
///
/// <returns>Returns current status of initialized joystick, which provides information
/// about current state of all axes, buttons and point of view.</returns>
///
/// <remarks><para><note>Before using this method the joystick object needs to be initialized
/// using <see cref="Init"/> method or <see cref="Joystick(int)"/> constructor.</note></para></remarks>
///
/// <exception cref="NotConnectedException">The requested joystick is not connected to the system.</exception>
/// <exception cref="ApplicationException">Joystick was not initialized.</exception>
///
public Status GetCurrentStatus( )
{
JoystickAPI.JOYINFOEX ji = new JoystickAPI.JOYINFOEX( );
ji.size = System.Runtime.InteropServices.Marshal.SizeOf( ji );
ji.flags = ( Info.capabilities.axesNumber > 5 ) ? JoystickAPI.JoyPosFlags.ReturnAll :
requestFlags[Info.capabilities.axesNumber];
if ( JoystickAPI.joyGetPosEx( Info.ID, ji ) != JoystickAPI.ResultCode.NoError )
{
throw new NotConnectedException( "The requested joystick is not connected to the system." );
}
return new Status( ji, Info.capabilities );
}
/// <summary>
/// Class describing current joystick's status.
/// </summary>
///
/// <remarks><para><note>All joystick axes' positions are measured in [-1, 1] range, where
/// 0 corresponds to center position - axis is not deflected (directed) to any side.</note></para></remarks>
///
public class Status
{
private JoystickAPI.JOYINFOEX status;
private JoystickAPI.JOYCAPS capabilities;
internal Status( JoystickAPI.JOYINFOEX status, JoystickAPI.JOYCAPS capabilities )
{
this.status = status;
this.capabilities = capabilities;
}
/// <summary>
/// Position of X axis, [-1, 1].
/// </summary>
public float XAxis
{
get
{
return ( ( ( status.flags & JoystickAPI.JoyPosFlags.ReturnX ) == 0 ) ? 0 :
(float) ( status.xPos - capabilities.xMin ) / capabilities.xMax * 2 - 1 );
}
}
/// <summary>
/// Position of Y axis, [-1, 1].
/// </summary>
public float YAxis
{
get
{
return ( ( ( status.flags & JoystickAPI.JoyPosFlags.ReturnY ) == 0 ) ? 0 :
(float) ( status.yPos - capabilities.yMin ) / capabilities.yMax * 2 - 1 );
}
}
/// <summary>
/// Position of Z axis, [-1, 1].
/// </summary>
public float ZAxis
{
get
{
return ( ( ( status.flags & JoystickAPI.JoyPosFlags.ReturnZ ) == 0 ) ? 0 :
(float) ( status.zPos - capabilities.zMin ) / capabilities.zMax * 2 - 1 );
}
}
/// <summary>
/// Position of R axis - 4th joystick's axes, [-1, 1].
/// </summary>
public float RAxis
{
get
{
return ( ( ( status.flags & JoystickAPI.JoyPosFlags.ReturnR ) == 0 ) ? 0 :
(float) ( status.rPos - capabilities.rMin ) / capabilities.rMax * 2 - 1 );
}
}
/// <summary>
/// Position of U axis - 5th joystick's axes, [-1, 1].
/// </summary>
public float UAxis
{
get
{
return ( ( ( status.flags & JoystickAPI.JoyPosFlags.ReturnU ) == 0 ) ? 0 :
(float) ( status.uPos - capabilities.uMin ) / capabilities.uMax * 2 - 1 );
}
}
/// <summary>
/// Position of V axis - 6th joystick's axes, [-1, 1].
/// </summary>
public float VAxis
{
get
{
return ( ( ( status.flags & JoystickAPI.JoyPosFlags.ReturnV ) == 0 ) ? 0 :
(float) ( status.vPos - capabilities.vMin ) / capabilities.vMax * 2 - 1 );
}
}
/// <summary>
/// Joystick buttons' state.
/// </summary>
public Buttons Buttons
{
get { return (Buttons) status.buttons; }
}
/// <summary>
/// Current point of view state, [0, 359].
/// </summary>
///
public float PointOfView
{
get { return ( status.pov > 35900) ? -1 : (float) status.pov / 100 ; }
}
/// <summary>
/// Check if certain button (or combination of buttons) is pressed.
/// </summary>
///
/// <param name="button">Button to check state of.</param>
///
/// <returns>Returns <see langword="true"/> if the specified button is pressed or
/// <see langword="false"/> otherwise.</returns>
///
public bool IsButtonPressed( Buttons button )
{
return ( ( ( (Buttons) status.buttons ) & button ) != 0 );
}
}
/// <summary>
/// Flags enumeration of joystick buttons.
/// </summary>
[Flags]
public enum Buttons
{
/// <summary>
/// 1st button.
/// </summary>
Button1 = 0x0001,
/// <summary>
/// 2nd button.
/// </summary>
Button2 = 0x0002,
/// <summary>
/// 3rd button.
/// </summary>
Button3 = 0x0004,
/// <summary>
/// 4th button.
/// </summary>
Button4 = 0x0008,
/// <summary>
/// 5th button.
/// </summary>
Button5 = 0x0010,
/// <summary>
/// 6th button.
/// </summary>
Button6 = 0x0020,
/// <summary>
/// 7th button.
/// </summary>
Button7 = 0x0040,
/// <summary>
/// 8th button.
/// </summary>
Button8 = 0x0080,
/// <summary>
/// 9th button.
/// </summary>
Button9 = 0x0100,
/// <summary>
/// 10th button.
/// </summary>
Button10 = 0x0200,
/// <summary>
/// 11th button.
/// </summary>
Button11 = 0x0400,
/// <summary>
/// 12th button.
/// </summary>
Button12 = 0x0800,
/// <summary>
/// 13th button.
/// </summary>
Button13 = 0x1000,
/// <summary>
/// 14th button.
/// </summary>
Button14 = 0x2000,
/// <summary>
/// 15th button.
/// </summary>
Button15 = 0x4000,
/// <summary>
/// 16th button.
/// </summary>
Button16 = 0x8000,
}
}
}