From 9be6f849ced6cc09d2944136860ba78e9d1e0f12 Mon Sep 17 00:00:00 2001 From: Nikos Kastellanos Date: Mon, 27 May 2024 10:55:01 +0300 Subject: [PATCH] SdlJoystickDevice --- .../Input/.SDL2/ConcreteJoystick.cs | 66 ++++++++++++------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/MonoGame.Framework/Input/.SDL2/ConcreteJoystick.cs b/MonoGame.Framework/Input/.SDL2/ConcreteJoystick.cs index fd75df0bf42..59a23b744cf 100644 --- a/MonoGame.Framework/Input/.SDL2/ConcreteJoystick.cs +++ b/MonoGame.Framework/Input/.SDL2/ConcreteJoystick.cs @@ -14,7 +14,7 @@ public sealed class ConcreteJoystick : JoystickStrategy { private Sdl SDL { get { return Sdl.Current; } } - private Dictionary _sdlJoysticks = new Dictionary(); + private Dictionary _sdlJoysticks = new Dictionary(); private int _maxConnectedIndex = -1; public override bool PlatformIsSupported @@ -34,24 +34,24 @@ public ConcreteJoystick() ~ConcreteJoystick() { - foreach (IntPtr sdlJoystick in _sdlJoysticks.Values) - SDL.JOYSTICK.Close(sdlJoystick); + foreach (SdlJoystickDevice sdlJoystick in _sdlJoysticks.Values) + SDL.JOYSTICK.Close(sdlJoystick.Handle); _sdlJoysticks.Clear(); } public override JoystickCapabilities PlatformGetCapabilities(int index) { - if (_sdlJoysticks.TryGetValue(index, out IntPtr sdlJoystick)) + if (_sdlJoysticks.TryGetValue(index, out SdlJoystickDevice sdlJoystick)) { return base.CreateJoystickCapabilities( isConnected: true, - displayName: SDL.JOYSTICK.GetJoystickName(sdlJoystick), - identifier: SDL.JOYSTICK.GetGUID(sdlJoystick).ToString(), + displayName: SDL.JOYSTICK.GetJoystickName(sdlJoystick.Handle), + identifier: SDL.JOYSTICK.GetGUID(sdlJoystick.Handle).ToString(), isGamepad: (SDL.GAMECONTROLLER.IsGameController(index) == 1), - axisCount: SDL.JOYSTICK.NumAxes(sdlJoystick), - buttonCount: SDL.JOYSTICK.NumButtons(sdlJoystick), - hatCount: SDL.JOYSTICK.NumHats(sdlJoystick) + axisCount: SDL.JOYSTICK.NumAxes(sdlJoystick.Handle), + buttonCount: SDL.JOYSTICK.NumButtons(sdlJoystick.Handle), + hatCount: SDL.JOYSTICK.NumHats(sdlJoystick.Handle) ); } @@ -60,22 +60,22 @@ public override JoystickCapabilities PlatformGetCapabilities(int index) public override JoystickState PlatformGetState(int index) { - if (_sdlJoysticks.TryGetValue(index, out IntPtr sdlJoystick)) + if (_sdlJoysticks.TryGetValue(index, out SdlJoystickDevice sdlJoystick)) { JoystickCapabilities jcap = PlatformGetCapabilities(index); int[] axes = new int[jcap.AxisCount]; for (int i = 0; i < axes.Length; i++) - axes[i] = SDL.JOYSTICK.GetAxis(sdlJoystick, i); + axes[i] = SDL.JOYSTICK.GetAxis(sdlJoystick.Handle, i); ButtonState[] buttons = new ButtonState[jcap.ButtonCount]; for (int i = 0; i < buttons.Length; i++) - buttons[i] = (SDL.JOYSTICK.GetButton(sdlJoystick, i) == 0) ? ButtonState.Released : ButtonState.Pressed; + buttons[i] = (SDL.JOYSTICK.GetButton(sdlJoystick.Handle, i) == 0) ? ButtonState.Released : ButtonState.Pressed; JoystickHat[] hats = new JoystickHat[jcap.HatCount]; for (int i = 0; i < hats.Length; i++) { - Sdl.Joystick.Hat hatstate = SDL.JOYSTICK.GetHat(sdlJoystick, i); + Sdl.Joystick.Hat hatstate = SDL.JOYSTICK.GetHat(sdlJoystick.Handle, i); Buttons dPadButtons = SDLToXnaDPadButtons(hatstate); hats[i] = base.CreateJoystickHat(dPadButtons); @@ -100,7 +100,7 @@ public override void PlatformGetState(int index, ref JoystickState joystickState ButtonState[] buttons = joystickState.Buttons; JoystickHat[] hats = joystickState.Hats; - if (_sdlJoysticks.TryGetValue(index, out IntPtr sdlJoystick)) + if (_sdlJoysticks.TryGetValue(index, out SdlJoystickDevice sdlJoystick)) { JoystickCapabilities jcap = PlatformGetCapabilities(index); @@ -115,14 +115,14 @@ public override void PlatformGetState(int index, ref JoystickState joystickState hats = new JoystickHat[jcap.HatCount]; for (int i = 0; i < jcap.AxisCount; i++) - axes[i] = SDL.JOYSTICK.GetAxis(sdlJoystick, i); + axes[i] = SDL.JOYSTICK.GetAxis(sdlJoystick.Handle, i); for (int i = 0; i < jcap.ButtonCount; i++) - buttons[i] = (SDL.JOYSTICK.GetButton(sdlJoystick, i) == 0) ? ButtonState.Released : ButtonState.Pressed; + buttons[i] = (SDL.JOYSTICK.GetButton(sdlJoystick.Handle, i) == 0) ? ButtonState.Released : ButtonState.Pressed; for (int i = 0; i < jcap.HatCount; i++) { - Sdl.Joystick.Hat hatstate = SDL.JOYSTICK.GetHat(sdlJoystick, i); + Sdl.Joystick.Hat hatstate = SDL.JOYSTICK.GetHat(sdlJoystick.Handle, i); Buttons dPadButtons = SDLToXnaDPadButtons(hatstate); hats[i] = base.CreateJoystickHat(dPadButtons); @@ -145,7 +145,9 @@ internal void AddDevices() internal void AddDevice(int deviceIndex) { IntPtr sdlJoystick = SDL.JOYSTICK.Open(deviceIndex); - if (_sdlJoysticks.ContainsValue(sdlJoystick)) return; + foreach (SdlJoystickDevice joystickDevice in _sdlJoysticks.Values) + if (joystickDevice.Handle == sdlJoystick) + return; int index = 0; while (_sdlJoysticks.ContainsKey(index)) @@ -153,7 +155,8 @@ internal void AddDevice(int deviceIndex) _maxConnectedIndex = Math.Max(_maxConnectedIndex, index); - _sdlJoysticks.Add(index, sdlJoystick); + SdlJoystickDevice sdlJoystickDevice = new SdlJoystickDevice(sdlJoystick); + _sdlJoysticks.Add(index, sdlJoystickDevice); if (SDL.GAMECONTROLLER.IsGameController(deviceIndex) == 1) ((IPlatformGamePad)GamePad.Current).GetStrategy().AddDevice(deviceIndex); @@ -161,11 +164,11 @@ internal void AddDevice(int deviceIndex) internal void RemoveDevice(int deviceIndex) { - foreach (KeyValuePair item in _sdlJoysticks) + foreach (KeyValuePair item in _sdlJoysticks) { - if (SDL.JOYSTICK.InstanceID(item.Value) == deviceIndex) + if (SDL.JOYSTICK.InstanceID(item.Value.Handle) == deviceIndex) { - SDL.JOYSTICK.Close(item.Value); + SDL.JOYSTICK.Close(item.Value.Handle); _sdlJoysticks.Remove(item.Key); if (_maxConnectedIndex == item.Key) @@ -197,5 +200,24 @@ private Buttons SDLToXnaDPadButtons(Sdl.Joystick.Hat hatstate) } } + + public class SdlJoystickDevice : JoystickDevice + { + public IntPtr Handle { get; private set; } + + public SdlJoystickDevice(IntPtr sdlJoystick) + : base() + { + this.Handle = sdlJoystick; + } + } + + public class JoystickDevice + { + + public JoystickDevice() + { + } + } }