00001 #region License
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #endregion
00027
00028 using System;
00029 using System.Collections.Generic;
00030
00031 namespace OpenTK.Input
00032 {
00036 public abstract class JoystickDevice : IInputDevice
00037 {
00038 #region Fields
00039
00040 int id;
00041 string description;
00042 JoystickAxisCollection axis_collection;
00043 JoystickButtonCollection button_collection;
00044 JoystickMoveEventArgs move_args = new JoystickMoveEventArgs(0, 0, 0);
00045 JoystickButtonEventArgs button_args = new JoystickButtonEventArgs(0, false);
00046
00047 #endregion
00048
00049 #region Constructors
00050
00051 internal JoystickDevice(int id, int axes, int buttons)
00052 {
00053 if (axes < 0)
00054 throw new ArgumentOutOfRangeException("axes");
00055
00056 if (buttons < 0)
00057 throw new ArgumentOutOfRangeException("buttons");
00058
00059 Id = id;
00060 axis_collection = new JoystickAxisCollection(axes);
00061 button_collection = new JoystickButtonCollection(buttons);
00062 }
00063
00064 #endregion
00065
00066 #region Public Members
00067
00071 public JoystickAxisCollection Axis { get { return axis_collection; } }
00072
00076 public JoystickButtonCollection Button { get { return button_collection; } }
00077
00078 #endregion
00079
00080 #region IInputDevice Members
00081
00085 public string Description
00086 {
00087 get { return description; }
00088 internal set { description = value; }
00089 }
00090
00094 public InputDeviceType DeviceType
00095 {
00096 get { return InputDeviceType.Hid; }
00097 }
00098
00099 #endregion
00100
00101 #region Events
00102
00106 public EventHandler<JoystickMoveEventArgs> Move =
00107 delegate(object sender, JoystickMoveEventArgs e) { };
00108
00112 public EventHandler<JoystickButtonEventArgs> ButtonDown =
00113 delegate(object sender, JoystickButtonEventArgs e) { };
00114
00118 public EventHandler<JoystickButtonEventArgs> ButtonUp =
00119 delegate(object sender, JoystickButtonEventArgs e) { };
00120
00121 #endregion
00122
00123 #region Internal Members
00124
00125 internal int Id
00126 {
00127 get { return id; }
00128 set { id = value; }
00129 }
00130
00131 internal void SetAxis(JoystickAxis axis, float @value)
00132 {
00133 move_args.Axis = axis;
00134 move_args.Delta = move_args.Value - @value;
00135 axis_collection[axis] = move_args.Value = @value;
00136 Move(this, move_args);
00137 }
00138
00139 internal void SetButton(JoystickButton button, bool @value)
00140 {
00141 if (button_collection[button] != @value)
00142 {
00143 button_args.Button = button;
00144 button_collection[button] = button_args.Pressed = @value;
00145 if (@value)
00146 ButtonDown(this, button_args);
00147 else
00148 ButtonUp(this, button_args);
00149 }
00150 }
00151
00152 #endregion
00153 }
00154
00155 #region JoystickDevice<TDetail> : JoystickDevice
00156
00157
00158 internal sealed class JoystickDevice<TDetail> : JoystickDevice
00159 {
00160 internal JoystickDevice(int id, int axes, int buttons)
00161 : base(id, axes, buttons)
00162 { }
00163
00164 internal TDetail Details;
00165 }
00166
00167 #endregion
00168
00169 #region Event Arguments
00170
00174 public class JoystickEventArgs : EventArgs
00175 {
00176 }
00177
00182 public class JoystickButtonEventArgs : EventArgs
00183 {
00184 #region Fields
00185
00186 JoystickButton button;
00187 bool pressed;
00188
00189 #endregion
00190
00191 #region Constructors
00192
00198 internal JoystickButtonEventArgs(JoystickButton button, bool pressed)
00199 {
00200 this.button = button;
00201 this.pressed = pressed;
00202 }
00203
00204 #endregion
00205
00206 #region Public Members
00207
00211 public JoystickButton Button { get { return this.button; } internal set { this.button = value; } }
00212
00216 public bool Pressed { get { return pressed; } internal set { this.pressed = value; } }
00217
00218 #endregion
00219 }
00220
00225 public class JoystickMoveEventArgs : JoystickEventArgs
00226 {
00227 #region Fields
00228
00229 JoystickAxis axis;
00230 float value;
00231 float delta;
00232
00233 #endregion
00234
00235 #region Constructors
00236
00243 public JoystickMoveEventArgs(JoystickAxis axis, float value, float delta)
00244 {
00245 this.axis = axis;
00246 this.value = value;
00247 this.delta = delta;
00248 }
00249
00250 #endregion
00251
00252 #region Public Members
00253
00257 public JoystickAxis Axis { get { return axis; } internal set { this.axis = value; } }
00258
00262 public float Value { get { return value; } internal set { this.value = value; } }
00263
00267 public float Delta { get { return delta; } internal set { this.delta = value; } }
00268
00269 #endregion
00270 }
00271
00272 #endregion
00273
00274 #region JoystickButton
00275
00279 public enum JoystickButton
00280 {
00282 Button0 = 0,
00284 Button1,
00286 Button2,
00288 Button3,
00290 Button4,
00292 Button5,
00294 Button6,
00296 Button7,
00298 Button8,
00300 Button9,
00302 Button10,
00304 Button11,
00306 Button12,
00308 Button13,
00310 Button14,
00312 Button15,
00313 }
00314
00315 #endregion
00316
00317 #region JoystickButtonCollection
00318
00322 public sealed class JoystickButtonCollection
00323 {
00324 #region Fields
00325
00326 bool[] button_state;
00327
00328 #endregion
00329
00330 #region Constructors
00331
00332 internal JoystickButtonCollection(int numButtons)
00333 {
00334 if (numButtons < 0)
00335 throw new ArgumentOutOfRangeException("numButtons");
00336
00337 button_state = new bool[numButtons];
00338 }
00339
00340 #endregion
00341
00342 #region Public Members
00343
00349 public bool this[int index]
00350 {
00351 get { return button_state[index]; }
00352 internal set { button_state[index] = value; }
00353 }
00354
00360 public bool this[JoystickButton button]
00361 {
00362 get { return button_state[(int)button]; }
00363 internal set { button_state[(int)button] = value; }
00364 }
00365
00369 public int Count
00370 {
00371 get { return button_state.Length; }
00372 }
00373
00374 #endregion
00375 }
00376
00377 #endregion
00378
00379 #region JoystickAxis
00380
00384 public enum JoystickAxis
00385 {
00387 Axis0 = 0,
00389 Axis1,
00391 Axis2,
00393 Axis3,
00395 Axis4,
00397 Axis5,
00399 Axis6,
00401 Axis7,
00403 Axis8,
00405 Axis9,
00406 }
00407
00408 #endregion
00409
00410 #region JoystickAxisCollection
00411
00415 public sealed class JoystickAxisCollection
00416 {
00417 #region Fields
00418
00419 float[] axis_state;
00420
00421 #endregion
00422
00423 #region Constructors
00424
00425 internal JoystickAxisCollection(int numAxes)
00426 {
00427 if (numAxes < 0)
00428 throw new ArgumentOutOfRangeException("numAxes");
00429
00430 axis_state = new float[numAxes];
00431 }
00432
00433 #endregion
00434
00435 #region Public Members
00436
00442 public float this[int index]
00443 {
00444 get { return axis_state[index]; }
00445 internal set { axis_state[index] = value; }
00446 }
00447
00453 public float this[JoystickAxis axis]
00454 {
00455 get { return axis_state[(int)axis]; }
00456 internal set { axis_state[(int)axis] = value; }
00457 }
00458
00462 public int Count
00463 {
00464 get { return axis_state.Length; }
00465 }
00466
00467 #endregion
00468 }
00469
00470 #endregion
00471 }