From 484af186738856b689afbecd76840c2f6c355c0f Mon Sep 17 00:00:00 2001 From: thefiddler Date: Thu, 19 Dec 2013 16:27:26 +0100 Subject: [PATCH] Updated internal IGamePadDriver interface --- Source/OpenTK/Input/IGamePadDriver.cs | 14 +--- .../Platform/SDL2/Sdl2JoystickDriver.cs | 66 ++++++++++++++++--- .../OpenTK/Platform/Windows/WinMMJoystick.cs | 9 ++- Source/OpenTK/Platform/X11/X11Joystick.cs | 10 +-- 4 files changed, 71 insertions(+), 28 deletions(-) diff --git a/Source/OpenTK/Input/IGamePadDriver.cs b/Source/OpenTK/Input/IGamePadDriver.cs index 718d1bc1..fc24c8f8 100644 --- a/Source/OpenTK/Input/IGamePadDriver.cs +++ b/Source/OpenTK/Input/IGamePadDriver.cs @@ -6,19 +6,11 @@ namespace OpenTK.Input { interface IGamePadDriver { - /// - /// Retrieves the combined for all gamepad devices. - /// - /// A structure containing the combined state for all gamepad devices. - GamePadState GetState(); - /// - /// Retrieves the for the specified gamepad device. - /// - /// The index of the keyboard device. - /// A structure containing the state of the gamepad device. GamePadState GetState(int index); + GamePadCapabilities GetCapabilities(int index); + /// /// Retrieves the device name for the gamepad device. /// @@ -26,6 +18,6 @@ namespace OpenTK.Input /// A with the name of the specified device or . /// /// If no device exists at the specified index, the return value is . - string GetDeviceName(int index); + string GetName(int index); } } diff --git a/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs b/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs index 380bbd67..2af3f977 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs @@ -34,18 +34,32 @@ namespace OpenTK.Platform.SDL2 { class Sdl2JoystickDriver : IJoystickDriver, IGamePadDriver, IDisposable { + const float RangeMultiplier = 1.0f / 32768.0f; + struct Sdl2JoystickDetails { public IntPtr Handle { get; set; } - public float RangeMultiplier { get { return 1.0f / 32768.0f; } } public int HatCount { get; set; } public int BallCount { get; set; } public bool IsConnected { get; set; } } + class Sdl2GamePad + { + public IntPtr Handle { get; private set; } + public GamePadState State { get; set; } + + public Sdl2GamePad(IntPtr handle) + { + Handle = handle; + } + } + readonly List joysticks = new List(); + readonly Dictionary controllers = new Dictionary(); + IList joysticks_readonly; - bool disposed = false; + bool disposed; public Sdl2JoystickDriver() { @@ -94,6 +108,11 @@ namespace OpenTK.Platform.SDL2 return id >= 0 && id < joysticks.Count; } + bool IsControllerValid(int id) + { + return controllers.ContainsKey(id); + } + #endregion #region Public Members @@ -145,7 +164,7 @@ namespace OpenTK.Platform.SDL2 if (IsJoystickValid(id)) { JoystickDevice joystick = (JoystickDevice)joysticks[id]; - float value = ev.Value * joystick.Details.RangeMultiplier; + float value = ev.Value * RangeMultiplier; joystick.SetAxis((JoystickAxis)ev.Axis, value); } else @@ -203,12 +222,27 @@ namespace OpenTK.Platform.SDL2 switch (ev.Type) { case EventType.CONTROLLERDEVICEADDED: + if (SDL.IsGameController(id)) + { + IntPtr handle = SDL.GameControllerOpen(id); + if (handle != IntPtr.Zero) + { + Sdl2GamePad pad = new Sdl2GamePad(handle); + pad.State.SetConnected(true); + controllers.Add(id, pad); + } + } break; case EventType.CONTROLLERDEVICEREMOVED: + if (IsControllerValid(id)) + { + controllers[id].State.SetConnected(false); + } break; case EventType.CONTROLLERDEVICEREMAPPED: + // Todo: what should we do in this case? break; } } @@ -216,15 +250,27 @@ namespace OpenTK.Platform.SDL2 public void ProcessControllerEvent(ControllerAxisEvent ev) { int id = ev.Which; - - + if (IsControllerValid(id)) + { + controllers[id].State.SetAxis((GamePadAxis)ev.Axis, ev.Value); + } + else + { + Debug.Print("[SDL2] Invalid game controller handle {0} in {1}", id, ev.Type); + } } public void ProcessControllerEvent(ControllerButtonEvent ev) { int id = ev.Which; - - + if (IsControllerValid(id)) + { + controllers[id].State.SetButton((Buttons)ev.Button, ev.State == State.Pressed); + } + else + { + Debug.Print("[SDL2] Invalid game controller handle {0} in {1}", id, ev.Type); + } } #endregion @@ -248,9 +294,9 @@ namespace OpenTK.Platform.SDL2 #region IGamePadDriver Members - public GamePadState GetState() + public GamePadCapabilities GetCapabilities(int index) { - return new GamePadState(); + return new GamePadCapabilities(); } public GamePadState GetState(int index) @@ -263,7 +309,7 @@ namespace OpenTK.Platform.SDL2 return new GamePadState(); } - public string GetDeviceName(int index) + public string GetName(int index) { return String.Empty; } diff --git a/Source/OpenTK/Platform/Windows/WinMMJoystick.cs b/Source/OpenTK/Platform/Windows/WinMMJoystick.cs index 32a00c7a..ca229700 100644 --- a/Source/OpenTK/Platform/Windows/WinMMJoystick.cs +++ b/Source/OpenTK/Platform/Windows/WinMMJoystick.cs @@ -428,8 +428,9 @@ namespace OpenTK.Platform.Windows #endregion - //HACK implement - public GamePadState GetState() + #region IGamePadDriver Members + + public GamePadCapabilities GetCapabilities(int index) { throw new NotImplementedException(); } @@ -439,9 +440,11 @@ namespace OpenTK.Platform.Windows throw new NotImplementedException(); } - public string GetDeviceName(int index) + public string GetName(int index) { throw new NotImplementedException(); } + + #endregion } } diff --git a/Source/OpenTK/Platform/X11/X11Joystick.cs b/Source/OpenTK/Platform/X11/X11Joystick.cs index 85299eb1..b4948169 100644 --- a/Source/OpenTK/Platform/X11/X11Joystick.cs +++ b/Source/OpenTK/Platform/X11/X11Joystick.cs @@ -259,21 +259,23 @@ namespace OpenTK.Platform.X11 #endregion - //HACK implement - public GamePadState GetState() + #region IGamePadDriver Members + + public GamePadCapabilities GetCapabilities(int index) { throw new NotImplementedException(); } public GamePadState GetState(int index) { - Poll(); throw new NotImplementedException(); } - public string GetDeviceName(int index) + public string GetName(int index) { throw new NotImplementedException(); } + + #endregion } }