Permalink
Browse files

[Input] Added hat state to JoystickState

  • Loading branch information...
thefiddler committed Jan 31, 2014
1 parent 0cacdf6 commit 801d6ead04869715cad12e9c5532851be52ac7ee
Showing with 49 additions and 2 deletions.
  1. +49 −2 Source/OpenTK/Input/JoystickState.cs
@@ -42,12 +42,17 @@ public struct JoystickState : IEquatable<JoystickState>
// then we'll need to increase these limits.
internal const int MaxAxes = (int)JoystickAxis.Last + 1;
internal const int MaxButtons = (int)JoystickButton.Last + 1;
+ internal const int MaxHats = (int)JoystickHat.Last + 1;
const float ConversionFactor = 1.0f / (short.MaxValue + 0.5f);
- unsafe fixed short axes[MaxAxes];
- int buttons;
int packet_number;
+ int buttons;
+ unsafe fixed short axes[MaxAxes];
+ JoystickHatState hat0;
+ JoystickHatState hat1;
+ JoystickHatState hat2;
+ JoystickHatState hat3;
bool is_connected;
#region Public Members
@@ -76,6 +81,28 @@ public ButtonState GetButton(JoystickButton button)
return (buttons & (1 << (int)button)) != 0 ? ButtonState.Pressed : ButtonState.Released;
}
+ /// <summary>
+ /// Gets the hat.
+ /// </summary>
+ /// <returns>The hat.</returns>
+ /// <param name="hat">Hat.</param>
+ public JoystickHatState GetHat(JoystickHat hat)
+ {
+ switch (hat)
+ {
+ case JoystickHat.Hat0:
+ return hat0;
+ case JoystickHat.Hat1:
+ return hat1;
+ case JoystickHat.Hat2:
+ return hat2;
+ case JoystickHat.Hat3:
+ return hat3;
+ default:
+ return new JoystickHatState();
+ }
+ }
+
/// <summary>
/// Gets a value indicating whether the specified <see cref="JoystickButton"/> is currently pressed.
/// </summary>
@@ -198,6 +225,9 @@ internal void SetAxis(JoystickAxis axis, short value)
internal void SetButton(JoystickButton button, bool value)
{
int index = 1 << (int)button;
+ if (index < 0 || index >= MaxButtons)
+ throw new ArgumentOutOfRangeException("button");
+
if (value)
{
buttons |= index;
@@ -208,6 +238,23 @@ internal void SetButton(JoystickButton button, bool value)
}
}
+ internal void SetHat(JoystickHat hat, JoystickHatState value)
+ {
+ switch (hat)
+ {
+ case JoystickHat.Hat0:
+ hat0 = value;
+ case JoystickHat.Hat1:
+ hat1 = value;
+ case JoystickHat.Hat2:
+ hat2 = value;
+ case JoystickHat.Hat3:
+ hat3 = value;
+ default:
+ throw new ArgumentOutOfRangeException("hat");
+ }
+ }
+
internal void SetIsConnected(bool value)
{
is_connected = value;

0 comments on commit 801d6ea

Please sign in to comment.