From e3c2f0e119d0c0e38ddd8ddd68580232481e6503 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 5 Apr 2023 16:52:36 -0700 Subject: [PATCH] fix controller hot swapping --- src/Input/Gamepad.cs | 14 ++++++++++++++ src/Input/Input.cs | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Input/Gamepad.cs b/src/Input/Gamepad.cs index 7b53b3f9..14c9982b 100644 --- a/src/Input/Gamepad.cs +++ b/src/Input/Gamepad.cs @@ -195,6 +195,20 @@ namespace MoonWorks.Input }; } + public void Register(IntPtr handle) + { + Handle = handle; + + IntPtr joystickHandle = SDL.SDL_GameControllerGetJoystick(Handle); + JoystickInstanceID = SDL.SDL_JoystickInstanceID(joystickHandle); + } + + public void Unregister() + { + Handle = IntPtr.Zero; + JoystickInstanceID = -1; + } + internal void Update() { AnyPressed = false; diff --git a/src/Input/Input.cs b/src/Input/Input.cs index 088fa43e..5ac31dfe 100644 --- a/src/Input/Input.cs +++ b/src/Input/Input.cs @@ -95,7 +95,7 @@ namespace MoonWorks.Input } else { - gamepads[slot].Handle = openResult; + gamepads[slot].Register(openResult); System.Console.WriteLine($"Gamepad added to slot {slot}!"); } return; @@ -112,7 +112,7 @@ namespace MoonWorks.Input if (joystickInstanceID == gamepads[slot].JoystickInstanceID) { SDL.SDL_GameControllerClose(gamepads[slot].Handle); - gamepads[slot].Handle = IntPtr.Zero; + gamepads[slot].Unregister(); System.Console.WriteLine($"Removing gamepad from slot {slot}!"); return; }