Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1452 chris 1
#region License
2
//
3
// The Open Toolkit Library License
4
//
5
// Copyright (c) 2006 - 2008 the Open Toolkit library, except where noted.
6
//
7
// Permission is hereby granted, free of charge, to any person obtaining a copy
8
// of this software and associated documentation files (the "Software"), to deal
9
// in the Software without restriction, including without limitation the rights to 
10
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11
// the Software, and to permit persons to whom the Software is furnished to do
12
// so, subject to the following conditions:
13
//
14
// The above copyright notice and this permission notice shall be included in all
15
// copies or substantial portions of the Software.
16
//
17
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
// OTHER DEALINGS IN THE SOFTWARE.
25
//
26
#endregion
27
 
28
using System;
29
using System.Collections.Generic;
30
 
31
namespace OpenTK.Input
32
{
33
    /// <summary>
34
    /// Represents a joystick device and provides methods to query its status.
35
    /// </summary>
36
    public abstract class JoystickDevice : IInputDevice
37
    {
38
        #region Fields
39
 
40
        int id;
41
        string description;
42
        JoystickAxisCollection axis_collection;
43
        JoystickButtonCollection button_collection;
44
        JoystickMoveEventArgs move_args = new JoystickMoveEventArgs(0, 0, 0);
45
        JoystickButtonEventArgs button_args = new JoystickButtonEventArgs(0, false);
46
 
47
        #endregion
48
 
49
        #region Constructors
50
 
51
        internal JoystickDevice(int id, int axes, int buttons)
52
        {
53
            if (axes < 0)
54
                throw new ArgumentOutOfRangeException("axes");
55
 
56
            if (buttons < 0)
57
                throw new ArgumentOutOfRangeException("buttons");
58
 
59
            Id = id;
60
            axis_collection = new JoystickAxisCollection(axes);
61
            button_collection = new JoystickButtonCollection(buttons);
62
        }
63
 
64
        #endregion
65
 
66
        #region Public Members
67
 
68
        /// <summary>
69
        /// Gets a JoystickAxisCollection containing the state of each axis on this instance. Values are normalized in the [-1, 1] range.
70
        /// </summary>
71
        public JoystickAxisCollection Axis { get { return axis_collection; } }
72
 
73
        /// <summary>
74
        /// Gets JoystickButtonCollection containing the state of each button on this instance. True indicates that the button is pressed.
75
        /// </summary>
76
        public JoystickButtonCollection Button { get { return button_collection; } }
77
 
78
        #endregion
79
 
80
        #region IInputDevice Members
81
 
82
        /// <summary>
83
        /// Gets a System.String containing a unique description for this instance.
84
        /// </summary>
85
        public string Description
86
        {
87
            get { return description; }
88
            internal set { description = value; }
89
        }
90
 
91
        /// <summary>
92
        /// Gets a value indicating the InputDeviceType of this InputDevice. 
93
        /// </summary>
94
        public InputDeviceType DeviceType
95
        {
96
            get { return InputDeviceType.Hid; }
97
        }
98
 
99
        #endregion
100
 
101
        #region Events
102
 
103
        /// <summary>
104
        /// Occurs when an axis of this JoystickDevice instance is moved.
105
        /// </summary>
106
        public EventHandler<JoystickMoveEventArgs> Move =
107
            delegate(object sender, JoystickMoveEventArgs e) { };
108
 
109
        /// <summary>
110
        /// Occurs when a button of this JoystickDevice instance is pressed.
111
        /// </summary>
112
        public EventHandler<JoystickButtonEventArgs> ButtonDown =
113
            delegate(object sender, JoystickButtonEventArgs e) { };
114
 
115
        /// <summary>
116
        /// Occurs when a button of this JoystickDevice is released.
117
        /// </summary>
118
        public EventHandler<JoystickButtonEventArgs> ButtonUp =
119
            delegate(object sender, JoystickButtonEventArgs e) { };
120
 
121
        #endregion
122
 
123
        #region Internal Members
124
 
125
        internal int Id
126
        {
127
            get { return id; }
128
            set { id = value; }
129
        }
130
 
131
        internal void SetAxis(JoystickAxis axis, float @value)
132
        {
133
            move_args.Axis = axis;
134
            move_args.Delta = move_args.Value - @value;
135
            axis_collection[axis] = move_args.Value = @value;
136
            Move(this, move_args);
137
        }
138
 
139
        internal void SetButton(JoystickButton button, bool @value)
140
        {
141
            if (button_collection[button] != @value)
142
            {
143
                button_args.Button = button;
144
                button_collection[button] = button_args.Pressed = @value;
145
                if (@value)
146
                    ButtonDown(this, button_args);
147
                else
148
                    ButtonUp(this, button_args);
149
            }
150
        }
151
 
152
        #endregion
153
    }
154
 
155
    #region JoystickDevice<TDetail> : JoystickDevice
156
 
157
    // Provides platform-specific information about the relevant JoystickDevice.
158
    internal sealed class JoystickDevice<TDetail> : JoystickDevice
159
    {
160
        internal JoystickDevice(int id, int axes, int buttons)
161
            : base(id, axes, buttons)
162
        { }
163
 
164
        internal TDetail Details;
165
    }
166
 
167
    #endregion
168
 
169
    #region Event Arguments
170
 
171
    /// <summary>
172
    /// The base class for JoystickDevice event arguments.
173
    /// </summary>
174
    public class JoystickEventArgs : EventArgs
175
    {
176
    }
177
 
178
    /// <summary>
179
    /// Provides data for the <see cref="JoystickDevice.ButtonDown"/> and <see cref="JoystickDevice.ButtonUp"/> events.
180
    /// This class is cached for performance reasons - avoid storing references outside the scope of the event.
181
    /// </summary>
182
    public class JoystickButtonEventArgs : EventArgs
183
    {
184
        #region Fields
185
 
186
        JoystickButton button;
187
        bool pressed;
188
 
189
        #endregion
190
 
191
        #region Constructors
192
 
193
        /// <summary>
194
        /// Initializes a new instance of the <see cref="JoystickButtonEventArgs"/> class.
195
        /// </summary>
196
        /// <param name="button">The index of the joystick button for the event.</param>
197
        /// <param name="pressed">The current state of the button.</param>
198
        internal JoystickButtonEventArgs(JoystickButton button, bool pressed)
199
        {
200
            this.button = button;
201
            this.pressed = pressed;
202
        }
203
 
204
        #endregion
205
 
206
        #region Public Members
207
 
208
        /// <summary>
209
        /// The index of the joystick button for the event.
210
        /// </summary>
211
        public JoystickButton Button { get { return this.button; } internal set { this.button = value; } }
212
 
213
        /// <summary>
214
        /// Gets a System.Boolean representing the state of the button for the event.
215
        /// </summary>
216
        public bool Pressed { get { return pressed; } internal set { this.pressed = value; } }
217
 
218
        #endregion
219
    }
220
 
221
    /// <summary>
222
    /// Provides data for the <see cref="JoystickDevice.Move"/> event.
223
    /// This class is cached for performance reasons - avoid storing references outside the scope of the event.
224
    /// </summary>
225
    public class JoystickMoveEventArgs : JoystickEventArgs
226
    {
227
        #region Fields
228
 
229
        JoystickAxis axis;
230
        float value;
231
        float delta;
232
 
233
        #endregion
234
 
235
        #region Constructors
236
 
237
        /// <summary>
238
        /// Initializes a new instance of the <see cref="JoystickMoveEventArgs"/> class.
239
        /// </summary>
240
        /// <param name="axis">The index of the joystick axis that was moved.</param>
241
        /// <param name="value">The absolute value of the joystick axis.</param>
242
        /// <param name="delta">The relative change in value of the joystick axis.</param>
243
        public JoystickMoveEventArgs(JoystickAxis axis, float value, float delta)
244
        {
245
            this.axis = axis;
246
            this.value = value;
247
            this.delta = delta;
248
        }
249
 
250
        #endregion
251
 
252
        #region Public Members
253
 
254
        /// <summary>
255
        /// Gets a System.Int32 representing the index of the axis that was moved.
256
        /// </summary>
257
        public JoystickAxis Axis { get { return axis; } internal set { this.axis = value; } }
258
 
259
        /// <summary>
260
        /// Gets a System.Single representing the absolute position of the axis.
261
        /// </summary>
262
        public float Value { get { return value; } internal set { this.value = value; } }
263
 
264
        /// <summary>
265
        /// Gets a System.Single representing the relative change in the position of the axis.
266
        /// </summary>
267
        public float Delta { get { return delta; } internal set { this.delta = value; } }
268
 
269
        #endregion
270
    }
271
 
272
    #endregion
273
 
274
    #region JoystickButton
275
 
276
    /// <summary>
277
    /// Defines available JoystickDevice buttons.
278
    /// </summary>
279
    public enum JoystickButton
280
    {
281
        /// <summary>The first button of the JoystickDevice.</summary>
282
        Button0 = 0,
283
        /// <summary>The second button of the JoystickDevice.</summary>
284
        Button1,
285
        /// <summary>The third button of the JoystickDevice.</summary>
286
        Button2,
287
        /// <summary>The fourth button of the JoystickDevice.</summary>
288
        Button3,
289
        /// <summary>The fifth button of the JoystickDevice.</summary>
290
        Button4,
291
        /// <summary>The sixth button of the JoystickDevice.</summary>
292
        Button5,
293
        /// <summary>The seventh button of the JoystickDevice.</summary>
294
        Button6,
295
        /// <summary>The eighth button of the JoystickDevice.</summary>
296
        Button7,
297
        /// <summary>The ninth button of the JoystickDevice.</summary>
298
        Button8,
299
        /// <summary>The tenth button of the JoystickDevice.</summary>
300
        Button9,
301
        /// <summary>The eleventh button of the JoystickDevice.</summary>
302
        Button10,
303
        /// <summary>The twelfth button of the JoystickDevice.</summary>
304
        Button11,
305
        /// <summary>The thirteenth button of the JoystickDevice.</summary>
306
        Button12,
307
        /// <summary>The fourteenth button of the JoystickDevice.</summary>
308
        Button13,
309
        /// <summary>The fifteenth button of the JoystickDevice.</summary>
310
        Button14,
311
        /// <summary>The sixteenth button of the JoystickDevice.</summary>
312
        Button15,
313
    }
314
 
315
    #endregion
316
 
317
    #region JoystickButtonCollection
318
 
319
    /// <summary>
320
    /// Defines a collection of JoystickButtons.
321
    /// </summary>
322
    public sealed class JoystickButtonCollection
323
    {
324
        #region Fields
325
 
326
        bool[] button_state;
327
 
328
        #endregion
329
 
330
        #region Constructors
331
 
332
        internal JoystickButtonCollection(int numButtons)
333
        {
334
            if (numButtons < 0)
335
                throw new ArgumentOutOfRangeException("numButtons");
336
 
337
            button_state = new bool[numButtons];
338
        }
339
 
340
        #endregion
341
 
342
        #region Public Members
343
 
344
        /// <summary>
345
        /// Gets a System.Boolean indicating whether the JoystickButton with the specified index is pressed.
346
        /// </summary>
347
        /// <param name="index">The index of the JoystickButton to check.</param>
348
        /// <returns>True if the JoystickButton is pressed; false otherwise.</returns>
349
        public bool this[int index]
350
        {
351
            get { return button_state[index]; }
352
            internal set { button_state[index] = value; }
353
        }
354
 
355
        /// <summary>
356
        /// Gets a System.Boolean indicating whether the specified JoystickButton is pressed.
357
        /// </summary>
358
        /// <param name="button">The JoystickButton to check.</param>
359
        /// <returns>True if the JoystickButton is pressed; false otherwise.</returns>
360
        public bool this[JoystickButton button]
361
        {
362
            get { return button_state[(int)button]; }
363
            internal set { button_state[(int)button] = value; }
364
        }
365
 
366
        /// <summary>
367
        /// Gets a System.Int32 indicating the available amount of JoystickButtons.
368
        /// </summary>
369
        public int Count
370
        {
371
            get { return button_state.Length; }
372
        }
373
 
374
        #endregion
375
    }
376
 
377
    #endregion
378
 
379
    #region JoystickAxis
380
 
381
    /// <summary>
382
    /// Defines available JoystickDevice axes.
383
    /// </summary>
384
    public enum JoystickAxis
385
    {
386
        /// <summary>The first axis of the JoystickDevice.</summary>
387
        Axis0 = 0,
388
        /// <summary>The second axis of the JoystickDevice.</summary>
389
        Axis1,
390
        /// <summary>The third axis of the JoystickDevice.</summary>
391
        Axis2,
392
        /// <summary>The fourth axis of the JoystickDevice.</summary>
393
        Axis3,
394
        /// <summary>The fifth axis of the JoystickDevice.</summary>
395
        Axis4,
396
        /// <summary>The sixth axis of the JoystickDevice.</summary>
397
        Axis5,
398
        /// <summary>The seventh axis of the JoystickDevice.</summary>
399
        Axis6,
400
        /// <summary>The eighth axis of the JoystickDevice.</summary>
401
        Axis7,
402
        /// <summary>The ninth axis of the JoystickDevice.</summary>
403
        Axis8,
404
        /// <summary>The tenth axis of the JoystickDevice.</summary>
405
        Axis9,
406
    }
407
 
408
    #endregion
409
 
410
    #region JoystickAxisCollection
411
 
412
    /// <summary>
413
    /// Defines a collection of JoystickAxes.
414
    /// </summary>
415
    public sealed class JoystickAxisCollection
416
    {
417
        #region Fields
418
 
419
        float[] axis_state;
420
 
421
        #endregion
422
 
423
        #region Constructors
424
 
425
        internal JoystickAxisCollection(int numAxes)
426
        {
427
            if (numAxes < 0)
428
                throw new ArgumentOutOfRangeException("numAxes");
429
 
430
            axis_state = new float[numAxes];
431
        }
432
 
433
        #endregion
434
 
435
        #region Public Members
436
 
437
        /// <summary>
438
        /// Gets a System.Single indicating the absolute position of the JoystickAxis with the specified index.
439
        /// </summary>
440
        /// <param name="index">The index of the JoystickAxis to check.</param>
441
        /// <returns>A System.Single in the range [-1, 1].</returns>
442
        public float this[int index]
443
        {
444
            get { return axis_state[index]; }
445
            internal set { axis_state[index] = value; }
446
        }
447
 
448
        /// <summary>
449
        /// Gets a System.Single indicating the absolute position of the JoystickAxis.
450
        /// </summary>
451
        /// <param name="axis">The JoystickAxis to check.</param>
452
        /// <returns>A System.Single in the range [-1, 1].</returns>
453
        public float this[JoystickAxis axis]
454
        {
455
            get { return axis_state[(int)axis]; }
456
            internal set { axis_state[(int)axis] = value; }
457
        }
458
 
459
        /// <summary>
460
        /// Gets a System.Int32 indicating the available amount of JoystickAxes.
461
        /// </summary>
462
        public int Count
463
        {
464
            get { return axis_state.Length; }
465
        }
466
 
467
        #endregion
468
    }
469
 
470
    #endregion
471
}