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