fix controller hot swapping

remotes/1695061714407202320/main
cosmonaut 2023-04-05 16:52:36 -07:00
parent 3584e670ee
commit e3c2f0e119
2 changed files with 16 additions and 2 deletions

View File

@ -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;

View File

@ -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;
}