diff --git a/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs b/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs index 8a08151a..f097b000 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs @@ -46,6 +46,8 @@ namespace OpenTK.Platform.SDL2 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 @@ namespace OpenTK.Platform.SDL2 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 @@ namespace OpenTK.Platform.SDL2 { int index = sdl_instanceid_to_joysticks[id]; JoystickDevice joystick = (JoystickDevice)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 @@ namespace OpenTK.Platform.SDL2 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); }