From 5c73a3ea74f72cc618192fd73cbc890204e1321c Mon Sep 17 00:00:00 2001 From: "Stefanos A." Date: Tue, 24 Dec 2013 14:21:37 +0100 Subject: [PATCH] Implements JoystickState and Capabilities setters --- Source/OpenTK/Input/JoystickCapabilities.cs | 34 ++++++++--- Source/OpenTK/Input/JoystickState.cs | 67 +++++++++++++++++++-- 2 files changed, 86 insertions(+), 15 deletions(-) diff --git a/Source/OpenTK/Input/JoystickCapabilities.cs b/Source/OpenTK/Input/JoystickCapabilities.cs index 5265d209..b25fa69b 100644 --- a/Source/OpenTK/Input/JoystickCapabilities.cs +++ b/Source/OpenTK/Input/JoystickCapabilities.cs @@ -38,20 +38,35 @@ namespace OpenTK.Input byte axis_count; byte button_count; byte dpad_count; - byte trackball_count; + bool is_connected; + + #region Constructors + + public JoystickCapabilities(int axis_count, int button_count, bool is_connected) + { + if (axis_count < 0 || axis_count >= JoystickState.MaxAxes) + throw new ArgumentOutOfRangeException("axis_count"); + if (button_count < 0 || button_count >= JoystickState.MaxButtons) + throw new ArgumentOutOfRangeException("axis_count"); + + this.axis_count = (byte)axis_count; + this.button_count = (byte)button_count; + this.dpad_count = 0; // Todo: either remove dpad_count or add it as a parameter + this.is_connected = is_connected; + } + + #endregion #region Public Members public int AxisCount { get { return axis_count; } - set { axis_count = (byte)value; } } public int ButtonCount { get { return button_count; } - set { button_count = (byte)value; } } #endregion @@ -61,15 +76,14 @@ namespace OpenTK.Input int DPadCount { get { return dpad_count; } - set { dpad_count = (byte)value; } - } - - int TrackballCount - { - get { return trackball_count; } - set { trackball_count = (byte)value; } } #endregion + + public bool IsConnected + { + get { return is_connected; } + } + } } diff --git a/Source/OpenTK/Input/JoystickState.cs b/Source/OpenTK/Input/JoystickState.cs index 28775e69..8a233df6 100644 --- a/Source/OpenTK/Input/JoystickState.cs +++ b/Source/OpenTK/Input/JoystickState.cs @@ -36,10 +36,18 @@ namespace OpenTK.Input { public struct JoystickState { - const int MaxAxes = 10; // JoystickAxis defines 10 axes - const float ConversionFactor = 1.0f / short.MaxValue; + // If we ever add more values to JoystickAxis or JoystickButton + // then we'll need to increase these limits. + internal const int MaxAxes = 10; + internal const int MaxButtons = 32; + + const float ConversionFactor = 1.0f / (short.MaxValue + 1); + unsafe fixed short axes[MaxAxes]; - JoystickButton buttons; + int buttons; + bool is_connected; + + #region Public Members public float GetAxis(JoystickAxis axis) { @@ -66,14 +74,63 @@ namespace OpenTK.Input return value; } + public ButtonState GetButton(JoystickButton button) + { + return (buttons & (1 << (int)button)) != 0 ? ButtonState.Pressed : ButtonState.Released; + } + public bool IsButtonDown(JoystickButton button) { - return (buttons & button) != 0; + return (buttons & (1 << (int)button)) != 0; } public bool IsButtonUp(JoystickButton button) { - return (buttons & button) == 0; + return (buttons & (1 << (int)button)) == 0; } + + public bool IsConnected + { + get { return is_connected; } + } + + #endregion + + #region Internal Members + + internal void SetAxis(JoystickAxis axis, short value) + { + int index = (int)axis; + if (index < 0 || index >= MaxAxes) + throw new ArgumentOutOfRangeException("axis"); + + unsafe + { + fixed (short* paxes = axes) + { + *(paxes + index) = value; + } + } + } + + internal void SetButton(JoystickButton button, bool value) + { + int index = 1 << (int)button; + if (value) + { + buttons |= index; + } + else + { + buttons &= ~index; + } + } + + internal void SetIsConnected(bool value) + { + is_connected = value; + } + + #endregion } }