Skip to content

Commit 36e244d

Browse files
[Input] Fix gamepad movement handling;
[Input] Fix SDL3 Gamepad logic;
1 parent 4bf8395 commit 36e244d

File tree

2 files changed

+13
-30
lines changed

2 files changed

+13
-30
lines changed

Engine/Staple.Core/Input/Input.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ internal static void GamepadMovement(AppEvent appEvent)
679679
{
680680
lock (lockObject)
681681
{
682-
if (gamepads.TryGetValue(appEvent.gamepadButton.index, out var gamepad) == false)
682+
if (gamepads.TryGetValue(appEvent.gamepadMovement.index, out var gamepad) == false)
683683
{
684684
return;
685685
}

Engine/Staple.Core/Rendering/Windowing/Impls/SDL3RenderWindow.cs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public override void Dispose()
4040
private class GamepadState
4141
{
4242
public nint instance;
43+
public int playerIndex;
4344
}
4445

4546
public nint window;
@@ -383,27 +384,6 @@ private static AppEventModifierKeys GetModifiers(SDL.SDL_Keymod mod)
383384
return modifiers;
384385
}
385386

386-
private bool TryFindGamepad(uint which, out uint key, out GamepadState state)
387-
{
388-
foreach (var pair in gamepads)
389-
{
390-
var joystickInstance = SDL.SDL_GetJoystickID(SDL.SDL_GetGamepadJoystick(pair.Value.instance));
391-
392-
if (joystickInstance == which)
393-
{
394-
key = pair.Key;
395-
state = pair.Value;
396-
397-
return true;
398-
}
399-
}
400-
401-
key = default;
402-
state = default;
403-
404-
return false;
405-
}
406-
407387
public void PollEvents()
408388
{
409389
while(SDL.SDL_PollEvent(out var _event))
@@ -496,26 +476,29 @@ public void PollEvents()
496476
{
497477
var instance = SDL.SDL_OpenGamepad(_event.cdevice.which);
498478

479+
var playerIndex = SDL.SDL_GetGamepadPlayerIndex(instance);
480+
499481
gamepads.Add(_event.cdevice.which, new()
500482
{
501483
instance = instance,
484+
playerIndex = playerIndex,
502485
});
503486

504-
Input.GamepadConnect(AppEvent.GamepadConnect((int)_event.cdevice.which, GamepadConnectionState.Connected));
487+
Input.GamepadConnect(AppEvent.GamepadConnect(playerIndex, GamepadConnectionState.Connected));
505488
}
506489

507490
break;
508491

509492
case SDL.SDL_EventType.SDL_EVENT_GAMEPAD_REMOVED:
510493

511494
{
512-
if(TryFindGamepad(_event.cdevice.which, out var key, out var state))
495+
if(gamepads.TryGetValue(_event.cdevice.which, out var state))
513496
{
514497
SDL.SDL_CloseGamepad(state.instance);
515498

516-
gamepads.Remove(key);
499+
gamepads.Remove(_event.cdevice.which);
517500

518-
Input.GamepadConnect(AppEvent.GamepadConnect((int)key, GamepadConnectionState.Disconnected));
501+
Input.GamepadConnect(AppEvent.GamepadConnect(state.playerIndex, GamepadConnectionState.Disconnected));
519502
}
520503
}
521504

@@ -525,9 +508,9 @@ public void PollEvents()
525508
case SDL.SDL_EventType.SDL_EVENT_GAMEPAD_BUTTON_UP:
526509

527510
{
528-
if (TryFindGamepad(_event.cdevice.which, out var key, out _))
511+
if (gamepads.TryGetValue(_event.cdevice.which, out var state))
529512
{
530-
Input.GamepadButton(AppEvent.GamepadButton((int)key,
513+
Input.GamepadButton(AppEvent.GamepadButton(state.playerIndex,
531514
(SDL.SDL_GamepadButton)_event.gbutton.button switch
532515
{
533516
SDL.SDL_GamepadButton.SDL_GAMEPAD_BUTTON_SOUTH => GamepadButton.A,
@@ -562,7 +545,7 @@ public void PollEvents()
562545
case SDL.SDL_EventType.SDL_EVENT_GAMEPAD_AXIS_MOTION:
563546

564547
{
565-
if (TryFindGamepad(_event.cdevice.which, out var key, out _))
548+
if(gamepads.TryGetValue(_event.cdevice.which, out var state))
566549
{
567550
var value = _event.gaxis.value;
568551

@@ -589,7 +572,7 @@ public void PollEvents()
589572
floatValue *= -1;
590573
}
591574

592-
Input.GamepadMovement(AppEvent.GamepadMovement((int)key, axis, floatValue));
575+
Input.GamepadMovement(AppEvent.GamepadMovement(state.playerIndex, axis, floatValue));
593576
}
594577
}
595578

0 commit comments

Comments
 (0)