Skip to content

Commit

Permalink
[Input] Added hat state to JoystickState
Browse files Browse the repository at this point in the history
  • Loading branch information
thefiddler committed Jan 31, 2014
1 parent 0cacdf6 commit 801d6ea
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions Source/OpenTK/Input/JoystickState.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ public struct JoystickState : IEquatable<JoystickState>
// then we'll need to increase these limits. // then we'll need to increase these limits.
internal const int MaxAxes = (int)JoystickAxis.Last + 1; internal const int MaxAxes = (int)JoystickAxis.Last + 1;
internal const int MaxButtons = (int)JoystickButton.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); const float ConversionFactor = 1.0f / (short.MaxValue + 0.5f);


unsafe fixed short axes[MaxAxes];
int buttons;
int packet_number; int packet_number;
int buttons;
unsafe fixed short axes[MaxAxes];
JoystickHatState hat0;
JoystickHatState hat1;
JoystickHatState hat2;
JoystickHatState hat3;
bool is_connected; bool is_connected;


#region Public Members #region Public Members
Expand Down Expand Up @@ -76,6 +81,28 @@ public ButtonState GetButton(JoystickButton button)
return (buttons & (1 << (int)button)) != 0 ? ButtonState.Pressed : ButtonState.Released; 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> /// <summary>
/// Gets a value indicating whether the specified <see cref="JoystickButton"/> is currently pressed. /// Gets a value indicating whether the specified <see cref="JoystickButton"/> is currently pressed.
/// </summary> /// </summary>
Expand Down Expand Up @@ -198,6 +225,9 @@ internal void SetAxis(JoystickAxis axis, short value)
internal void SetButton(JoystickButton button, bool value) internal void SetButton(JoystickButton button, bool value)
{ {
int index = 1 << (int)button; int index = 1 << (int)button;
if (index < 0 || index >= MaxButtons)
throw new ArgumentOutOfRangeException("button");

if (value) if (value)
{ {
buttons |= index; buttons |= index;
Expand All @@ -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) internal void SetIsConnected(bool value)
{ {
is_connected = value; is_connected = value;
Expand Down

0 comments on commit 801d6ea

Please sign in to comment.