Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[SDL2] Implemented support for hat state

  • Loading branch information...
commit 168c45f0e2ae10c40253cb4813ed5f1dfe534f0b 1 parent 801d6ea
thefiddler thefiddler authored
Showing with 44 additions and 1 deletion.
  1. +44 −1 Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs
45 Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs
View
@@ -46,6 +46,8 @@ class Sdl2JoystickDetails
public int HatCount { get; set; }
public int BallCount { get; set; }
public bool IsConnected { get; set; }
+ public readonly JoystickHatState[] Hat =
+ new JoystickHatState[JoystickState.MaxHats];
}
// For IJoystickDriver2 implementation
@@ -127,6 +129,35 @@ bool IsJoystickInstanceValid(int instance_id)
return sdl_instanceid_to_joysticks.ContainsKey(instance_id);
}
+ OpenTK.Input.HatPosition TranslateHat(HatPosition value)
+ {
+ if ((value & HatPosition.LeftUp) == value)
+ return OpenTK.Input.HatPosition.UpLeft;
+
+ if ((value & HatPosition.RightUp) == value)
+ return OpenTK.Input.HatPosition.UpRight;
+
+ if ((value & HatPosition.LeftDown) == value)
+ return OpenTK.Input.HatPosition.DownLeft;
+
+ if ((value & HatPosition.RightDown) == value)
+ return OpenTK.Input.HatPosition.DownRight;
+
+ if ((value & HatPosition.Up) == value)
+ return OpenTK.Input.HatPosition.Up;
+
+ if ((value & HatPosition.Right) == value)
+ return OpenTK.Input.HatPosition.Right;
+
+ if ((value & HatPosition.Down) == value)
+ return OpenTK.Input.HatPosition.Down;
+
+ if ((value & HatPosition.Left) == value)
+ return OpenTK.Input.HatPosition.Left;
+
+ return OpenTK.Input.HatPosition.Centered;
+ }
+
#if USE_SDL2_GAMECONTROLLER
bool IsControllerValid(int id)
{
@@ -386,7 +417,14 @@ public void ProcessJoystickEvent(JoyHatEvent ev)
{
int index = sdl_instanceid_to_joysticks[id];
JoystickDevice<Sdl2JoystickDetails> joystick = (JoystickDevice<Sdl2JoystickDetails>)joysticks[index];
- // Todo: map hat to an extra axis
+ if (ev.Hat >= 0 && ev.Hat < JoystickState.MaxHats)
+ {
+ joystick.Details.Hat[ev.Hat] = new JoystickHatState(TranslateHat(ev.Value));
+ }
+ else
+ {
+ Debug.Print("[SDL2] Hat {0} out of range [0, {1}]", ev.Hat, JoystickState.MaxHats);
+ }
joystick.Details.PacketNumber = Math.Max(0, unchecked(joystick.Details.PacketNumber + 1));
}
else
@@ -596,6 +634,11 @@ JoystickState IJoystickDriver2.GetState(int index)
state.SetButton(JoystickButton.Button0 + i, joystick.Button[i]);
}
+ for (int i = 0; i < joystick.Details.HatCount; i++)
+ {
+ state.SetHat(JoystickHat.Hat0 + i, joystick.Details.Hat[i]);
+ }
+
state.SetIsConnected(joystick.Details.IsConnected);
state.SetPacketNumber(joystick.Details.PacketNumber);
}
Please sign in to comment.
Something went wrong with that request. Please try again.