From 44e4971a38740f9db9aff4d5e4783ad2605c3fb0 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Sun, 1 Mar 2009 01:17:24 +0000 Subject: [PATCH] Added JoystickAxis and JoystickButton enumerations, as well as their respective collections. Implemented necessary Poll() functions. --- Source/OpenTK/Input/JoystickDevice.cs | 249 ++++++++++++++++-- Source/OpenTK/Platform/Windows/WMInput.cs | 2 +- .../OpenTK/Platform/Windows/WinMMJoystick.cs | 17 +- Source/OpenTK/Platform/X11/X11Input.cs | 14 +- Source/OpenTK/Platform/X11/X11Joystick.cs | 13 +- 5 files changed, 243 insertions(+), 52 deletions(-) diff --git a/Source/OpenTK/Input/JoystickDevice.cs b/Source/OpenTK/Input/JoystickDevice.cs index e12e9bba..4611f11d 100644 --- a/Source/OpenTK/Input/JoystickDevice.cs +++ b/Source/OpenTK/Input/JoystickDevice.cs @@ -39,10 +39,8 @@ namespace OpenTK.Input int id; string description; - List axis = new List(); - List button = new List(); - IList axis_readonly; - IList button_readonly; + JoystickAxisCollection axis_collection; + JoystickButtonCollection button_collection; JoystickMoveEventArgs move_args = new JoystickMoveEventArgs(0, 0, 0); JoystickButtonEventArgs button_args = new JoystickButtonEventArgs(0, false); @@ -59,11 +57,8 @@ namespace OpenTK.Input throw new ArgumentOutOfRangeException("buttons"); Id = id; - axis.AddRange(new float[axes]); - button.AddRange(new bool[buttons]); - - axis_readonly = axis.AsReadOnly(); - button_readonly = button.AsReadOnly(); + axis_collection = new JoystickAxisCollection(axes); + button_collection = new JoystickButtonCollection(buttons); } #endregion @@ -71,14 +66,14 @@ namespace OpenTK.Input #region Public Members /// - /// Gets a readonly IList containing the state of each axis on this instance. Values are normalized in the [-1, 1] range. + /// Gets a JoystickAxisCollection containing the state of each axis on this instance. Values are normalized in the [-1, 1] range. /// - public IList Axis { get { return axis_readonly; } } + public JoystickAxisCollection Axis { get { return axis_collection; } } /// - /// Gets a readonly IList containing the state of each button on this instance. True indicates that the button is pressed. + /// Gets JoystickButtonCollection containing the state of each button on this instance. True indicates that the button is pressed. /// - public IList Button { get { return button_readonly; } } + public JoystickButtonCollection Button { get { return button_collection; } } #endregion @@ -133,20 +128,20 @@ namespace OpenTK.Input set { id = value; } } - internal void SetAxis(int num, float @value) + internal void SetAxis(JoystickAxis axis, float @value) { - move_args.Index = num; + move_args.Axis = axis; move_args.Delta = move_args.Value - @value; - axis[num] = move_args.Value = @value; + axis_collection[axis] = move_args.Value = @value; Move(this, move_args); } - internal void SetButton(int num, bool @value) + internal void SetButton(JoystickButton button, bool @value) { - if (button[num] != @value) + if (button_collection[button] != @value) { - button_args.Button = num; - button[num] = button_args.Pressed = @value; + button_args.Button = button; + button_collection[button] = button_args.Pressed = @value; if (@value) ButtonDown(this, button_args); else @@ -190,7 +185,7 @@ namespace OpenTK.Input { #region Fields - int button; + JoystickButton button; bool pressed; #endregion @@ -202,7 +197,7 @@ namespace OpenTK.Input /// /// The index of the joystick button for the event. /// The current state of the button. - internal JoystickButtonEventArgs(int button, bool pressed) + internal JoystickButtonEventArgs(JoystickButton button, bool pressed) { this.button = button; this.pressed = pressed; @@ -215,7 +210,7 @@ namespace OpenTK.Input /// /// The index of the joystick button for the event. /// - public int Button { get { return this.button; } internal set { this.button = value; } } + public JoystickButton Button { get { return this.button; } internal set { this.button = value; } } /// /// Gets a System.Boolean representing the state of the button for the event. @@ -233,7 +228,7 @@ namespace OpenTK.Input { #region Fields - int index; + JoystickAxis axis; float value; float delta; @@ -247,9 +242,9 @@ namespace OpenTK.Input /// The index of the joystick axis that was moved. /// The absolute value of the joystick axis. /// The relative change in value of the joystick axis. - public JoystickMoveEventArgs(int index, float value, float delta) + public JoystickMoveEventArgs(JoystickAxis axis, float value, float delta) { - this.index = index; + this.axis = axis; this.value = value; this.delta = delta; } @@ -261,7 +256,7 @@ namespace OpenTK.Input /// /// Gets a System.Int32 representing the index of the axis that was moved. /// - public int Index { get { return index; } internal set { this.index = value; } } + public JoystickAxis Axis { get { return axis; } internal set { this.axis = value; } } /// /// Gets a System.Single representing the absolute position of the axis. @@ -271,7 +266,205 @@ namespace OpenTK.Input /// /// Gets a System.Single representing the relative change in the position of the axis. /// - public float Delta { get { return delta; } internal set { this.delta = delta; } } + public float Delta { get { return delta; } internal set { this.delta = value; } } + + #endregion + } + + #endregion + + #region JoystickButton + + /// + /// Defines available JoystickDevice buttons. + /// + public enum JoystickButton + { + /// The first button of the JoystickDevice. + Button0 = 0, + /// The second button of the JoystickDevice. + Button1, + /// The third button of the JoystickDevice. + Button2, + /// The fourth button of the JoystickDevice. + Button3, + /// The fifth button of the JoystickDevice. + Button4, + /// The sixth button of the JoystickDevice. + Button5, + /// The seventh button of the JoystickDevice. + Button6, + /// The eighth button of the JoystickDevice. + Button7, + /// The ninth button of the JoystickDevice. + Button8, + /// The tenth button of the JoystickDevice. + Button9, + /// The eleventh button of the JoystickDevice. + Button10, + /// The twelfth button of the JoystickDevice. + Button11, + /// The thirteenth button of the JoystickDevice. + Button12, + /// The fourteenth button of the JoystickDevice. + Button13, + /// The fifteenth button of the JoystickDevice. + Button14, + /// The sixteenth button of the JoystickDevice. + Button15, + } + + #endregion + + #region JoystickButtonCollection + + /// + /// Defines a collection of JoystickButtons. + /// + public sealed class JoystickButtonCollection + { + #region Fields + + bool[] button_state; + + #endregion + + #region Constructors + + internal JoystickButtonCollection(int numButtons) + { + if (numButtons <= 0) + throw new ArgumentOutOfRangeException("numButtons"); + + button_state = new bool[numButtons]; + } + + #endregion + + #region Public Members + + /// + /// Gets a System.Boolean indicating whether the JoystickButton with the specified index is pressed. + /// + /// The index of the JoystickButton to check. + /// True if the JoystickButton is pressed; false otherwise. + public bool this[int index] + { + get { return button_state[index]; } + internal set { button_state[index] = value; } + } + + /// + /// Gets a System.Boolean indicating whether the specified JoystickButton is pressed. + /// + /// The JoystickButton to check. + /// True if the JoystickButton is pressed; false otherwise. + public bool this[JoystickButton button] + { + get { return button_state[(int)button]; } + internal set { button_state[(int)button] = value; } + } + + /// + /// Gets a System.Int32 indicating the available amount of JoystickButtons. + /// + public int Count + { + get { return button_state.Length; } + } + + #endregion + } + + #endregion + + #region JoystickAxis + + /// + /// Defines available JoystickDevice axes. + /// + public enum JoystickAxis + { + /// The first axis of the JoystickDevice. + Axis0 = 0, + /// The second axis of the JoystickDevice. + Axis1, + /// The third axis of the JoystickDevice. + Axis2, + /// The fourth axis of the JoystickDevice. + Axis3, + /// The fifth axis of the JoystickDevice. + Axis4, + /// The sixth axis of the JoystickDevice. + Axis5, + /// The seventh axis of the JoystickDevice. + Axis6, + /// The eighth axis of the JoystickDevice. + Axis7, + /// The ninth axis of the JoystickDevice. + Axis8, + /// The tenth axis of the JoystickDevice. + Axis9, + } + + #endregion + + #region JoystickAxisCollection + + /// + /// Defines a collection of JoystickAxes. + /// + public sealed class JoystickAxisCollection + { + #region Fields + + float[] axis_state; + + #endregion + + #region Constructors + + internal JoystickAxisCollection(int numAxes) + { + if (numAxes <= 0) + throw new ArgumentOutOfRangeException("numAxes"); + + axis_state = new float[numAxes]; + } + + #endregion + + #region Public Members + + /// + /// Gets a System.Single indicating the absolute position of the JoystickAxis with the specified index. + /// + /// The index of the JoystickAxis to check. + /// A System.Single in the range [-1, 1]. + public float this[int index] + { + get { return axis_state[index]; } + internal set { axis_state[index] = value; } + } + + /// + /// Gets a System.Single indicating the absolute position of the JoystickAxis. + /// + /// The JoystickAxis to check. + /// A System.Single in the range [-1, 1]. + public float this[JoystickAxis axis] + { + get { return axis_state[(int)axis]; } + internal set { axis_state[(int)axis] = value; } + } + + /// + /// Gets a System.Int32 indicating the available amount of JoystickAxes. + /// + public int Count + { + get { return axis_state.Length; } + } #endregion } diff --git a/Source/OpenTK/Platform/Windows/WMInput.cs b/Source/OpenTK/Platform/Windows/WMInput.cs index ec6b3a05..bd806882 100644 --- a/Source/OpenTK/Platform/Windows/WMInput.cs +++ b/Source/OpenTK/Platform/Windows/WMInput.cs @@ -233,7 +233,7 @@ namespace OpenTK.Platform.Windows public void Poll() { - // No polling needed. + joystick_driver.Poll(); } #endregion diff --git a/Source/OpenTK/Platform/Windows/WinMMJoystick.cs b/Source/OpenTK/Platform/Windows/WinMMJoystick.cs index 23b45771..4487d352 100644 --- a/Source/OpenTK/Platform/Windows/WinMMJoystick.cs +++ b/Source/OpenTK/Platform/Windows/WinMMJoystick.cs @@ -55,7 +55,10 @@ namespace OpenTK.Platform.Windows { JoystickDevice stick = OpenJoystick(number++); if (stick != null) + { + stick.Description = String.Format("USB Joystick {0} ({1} axes, {2} buttons)", number, stick.Axis.Count, stick.Button.Count); sticks.Add(stick); + } } } @@ -116,22 +119,22 @@ namespace OpenTK.Platform.Windows int axis = 0; if (axis < js.Axis.Count) - { js.SetAxis(axis, js.Details.CalculateOffset((float)info.XPos, axis)); axis++; } + { js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.XPos, axis)); axis++; } if (axis < js.Axis.Count) - { js.SetAxis(axis, js.Details.CalculateOffset((float)info.YPos, axis)); axis++; } + { js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.YPos, axis)); axis++; } if (axis < js.Axis.Count) - { js.SetAxis(axis, js.Details.CalculateOffset((float)info.ZPos, axis)); axis++; } + { js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.ZPos, axis)); axis++; } if (axis < js.Axis.Count) - { js.SetAxis(axis, js.Details.CalculateOffset((float)info.RPos, axis)); axis++; } + { js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.RPos, axis)); axis++; } if (axis < js.Axis.Count) - { js.SetAxis(axis, js.Details.CalculateOffset((float)info.UPos, axis)); axis++; } + { js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.UPos, axis)); axis++; } if (axis < js.Axis.Count) - { js.SetAxis(axis, js.Details.CalculateOffset((float)info.VPos, axis)); axis++; } + { js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.VPos, axis)); axis++; } int button = 0; while (button < js.Button.Count) { - js.SetButton(button, (info.Buttons & (1 << button)) != 0); + js.SetButton((JoystickButton)button, (info.Buttons & (1 << button)) != 0); button++; } } diff --git a/Source/OpenTK/Platform/X11/X11Input.cs b/Source/OpenTK/Platform/X11/X11Input.cs index 2924a4ad..8ee60446 100644 --- a/Source/OpenTK/Platform/X11/X11Input.cs +++ b/Source/OpenTK/Platform/X11/X11Input.cs @@ -20,6 +20,7 @@ namespace OpenTK.Platform.X11 /// internal sealed class X11Input : IInputDriver { + X11Joystick joystick_driver = new X11Joystick(); //X11WindowInfo window; KeyboardDevice keyboard = new KeyboardDevice(); MouseDevice mouse = new MouseDevice(); @@ -199,15 +200,6 @@ namespace OpenTK.Platform.X11 #region --- IInputDriver Members --- - #region public IList InputDevices - - public IList InputDevices - { - get { throw new Exception("The method or operation is not implemented."); } - } - - #endregion - #region public IList Keyboard public IList Keyboard @@ -230,7 +222,7 @@ namespace OpenTK.Platform.X11 public IList Joysticks { - get { throw new NotImplementedException(); } + get { return joystick_driver.Joysticks; } } #endregion @@ -242,7 +234,7 @@ namespace OpenTK.Platform.X11 /// public void Poll() { - //mouseDriver.Poll(); + joystick_driver.Poll(); } #endregion diff --git a/Source/OpenTK/Platform/X11/X11Joystick.cs b/Source/OpenTK/Platform/X11/X11Joystick.cs index 12a96a69..9ae5fa85 100644 --- a/Source/OpenTK/Platform/X11/X11Joystick.cs +++ b/Source/OpenTK/Platform/X11/X11Joystick.cs @@ -35,7 +35,7 @@ namespace OpenTK.Platform.X11 { struct X11JoyDetails { } - sealed class X11JoystickDriver : IJoystickDriver + sealed class X11Joystick : IJoystickDriver { #region Fields @@ -48,7 +48,7 @@ namespace OpenTK.Platform.X11 #region Constructors - public X11JoystickDriver() + public X11Joystick() { sticks_readonly = sticks.AsReadOnly(); @@ -57,7 +57,10 @@ namespace OpenTK.Platform.X11 { JoystickDevice stick = OpenJoystick(JoystickPath, number++); if (stick != null) + { + stick.Description = String.Format("USB Joystick {0} ({1} axes, {2} buttons)", number, stick.Axis.Count, stick.Button.Count); sticks.Add(stick); + } } number = 0; @@ -98,11 +101,11 @@ namespace OpenTK.Platform.X11 switch (e.Type) { case JoystickEventType.Axis: - js.SetAxis(e.Number, e.Value / 32767.0f); + js.SetAxis((JoystickAxis)e.Number, e.Value / 32767.0f); break; case JoystickEventType.Button: - js.SetButton(e.Number, e.Value != 0); + js.SetButton((JoystickButton)e.Number, e.Value != 0); break; } } @@ -233,7 +236,7 @@ namespace OpenTK.Platform.X11 } } - ~X11JoystickDriver() + ~X11Joystick() { Dispose(false); }