11#include " UInputMan.h"
22#include " Constants.h"
3- #include " SDL3/SDL_events.h"
4- #include " SDL3/SDL_keyboard.h"
5- #include " SDL3/SDL_mouse.h"
63#include " SceneMan.h"
74#include " ActivityMan.h"
85#include " MetaMan.h"
1411#include " Icon.h"
1512#include " GameActivity.h"
1613#include " System.h"
14+
1715#include < SDL3/SDL.h>
1816#include < string>
1917#include < unordered_map>
@@ -82,7 +80,7 @@ int UInputMan::Initialize() {
8280
8381 int controllerIndex = 0 ;
8482 int joystickCount = 0 ;
85- SDL_JoystickID* joysticks = SDL_GetJoysticks (&joystickCount);
83+ SDL_JoystickID* joysticks = SDL_GetGamepads (&joystickCount);
8684
8785 for (size_t index = 0 ; index < std::min (joystickCount, static_cast <int >(Players::MaxPlayerCount)); ++index) {
8886 if (SDL_IsGamepad (joysticks[index])) {
@@ -997,11 +995,12 @@ int UInputMan::Update() {
997995 }
998996 }
999997 break ;
1000- case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
1001- case SDL_EVENT_GAMEPAD_BUTTON_UP:
1002998 case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
1003999 case SDL_EVENT_JOYSTICK_BUTTON_UP:
1004- if (std::vector<Gamepad>::iterator device = std::find (s_PrevJoystickStates.begin (), s_PrevJoystickStates.end (), (inputEvent.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN || inputEvent.type == SDL_EVENT_GAMEPAD_BUTTON_UP) ? inputEvent.gbutton .which : inputEvent.jbutton .which ); device != s_PrevJoystickStates.end ()) {
1000+ case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
1001+ case SDL_EVENT_GAMEPAD_BUTTON_UP: {
1002+ bool joystickEvent = (inputEvent.type == SDL_EVENT_JOYSTICK_BUTTON_DOWN || inputEvent.type == SDL_EVENT_JOYSTICK_BUTTON_UP);
1003+ if (std::vector<Gamepad>::iterator device = std::find (s_PrevJoystickStates.begin (), s_PrevJoystickStates.end (), joystickEvent ? inputEvent.jbutton .which : inputEvent.gbutton .which ); device != s_PrevJoystickStates.end ()) {
10051004 int button = -1 ;
10061005 int down = false ;
10071006 if (SDL_IsGamepad (device->m_DeviceIndex )) {
@@ -1020,10 +1019,14 @@ int UInputMan::Update() {
10201019 device->m_Buttons [button] = down;
10211020 }
10221021 break ;
1022+ }
10231023 case SDL_EVENT_JOYSTICK_ADDED:
1024+ case SDL_EVENT_GAMEPAD_ADDED: {
10241025 HandleGamepadHotPlug (inputEvent.jdevice .which );
10251026 break ;
1027+ }
10261028 case SDL_EVENT_JOYSTICK_REMOVED:
1029+ case SDL_EVENT_GAMEPAD_REMOVED:
10271030 if (std::vector<Gamepad>::iterator prevDevice = std::find (s_PrevJoystickStates.begin (), s_PrevJoystickStates.end (), inputEvent.jdevice .which ); prevDevice != s_PrevJoystickStates.end ()) {
10281031 g_ConsoleMan.PrintString (" INFO: Gamepad " + std::to_string (prevDevice->m_DeviceIndex + 1 ) + " disconnected!" );
10291032 SDL_CloseGamepad (SDL_GetGamepadFromID (prevDevice->m_JoystickID ));
@@ -1281,17 +1284,19 @@ void UInputMan::UpdateJoystickDigitalAxis() {
12811284 }
12821285}
12831286
1284- void UInputMan::HandleGamepadHotPlug (int deviceIndex) {
1287+ void UInputMan::HandleGamepadHotPlug (SDL_JoystickID deviceIndex) {
12851288 SDL_Joystick* controller = nullptr ;
12861289 int controllerIndex = 0 ;
12871290
12881291 for (controllerIndex = 0 ; controllerIndex < s_PrevJoystickStates.size (); ++controllerIndex) {
1289- if (s_PrevJoystickStates[controllerIndex].m_DeviceIndex == deviceIndex || s_PrevJoystickStates[controllerIndex].m_DeviceIndex == -1 ) {
1292+ if (s_PrevJoystickStates[controllerIndex].m_JoystickID == deviceIndex) {
1293+ return ;
1294+ }
1295+ if (s_PrevJoystickStates[controllerIndex].m_JoystickID == -1 ) {
12901296 if (SDL_IsGamepad (deviceIndex)) {
12911297 SDL_Gamepad* gameController = SDL_OpenGamepad (deviceIndex);
12921298 if (!gameController) {
1293- std::string connectString = s_PrevJoystickStates[controllerIndex].m_DeviceIndex == deviceIndex ? " reconnect" : " connect" ;
1294- g_ConsoleMan.PrintString (" ERROR: Failed to " + connectString + " Gamepad " + std::to_string (controllerIndex + 1 ));
1299+ g_ConsoleMan.PrintString (" ERROR: Failed to connect Gamepad!" );
12951300 break ;
12961301 }
12971302 controller = SDL_GetGamepadJoystick (gameController);
@@ -1300,12 +1305,10 @@ void UInputMan::HandleGamepadHotPlug(int deviceIndex) {
13001305 controller = SDL_OpenJoystick (deviceIndex);
13011306 }
13021307 if (!controller) {
1303- std::string connectString = s_PrevJoystickStates[controllerIndex].m_DeviceIndex == deviceIndex ? " reconnect" : " connect" ;
1304- g_ConsoleMan.PrintString (" ERROR: Failed to " + connectString + " Gamepad " + std::to_string (controllerIndex + 1 ));
1308+ g_ConsoleMan.PrintString (" ERROR: Failed to connect Gamepad!" );
13051309 break ;
13061310 }
1307- std::string connectString = s_PrevJoystickStates[controllerIndex].m_DeviceIndex == deviceIndex ? " reconnected" : " connected" ;
1308- g_ConsoleMan.PrintString (" INFO: Gamepad " + std::to_string (controllerIndex + 1 ) + connectString);
1311+ g_ConsoleMan.PrintString (" INFO: Gamepad " + std::to_string (controllerIndex + 1 ) + " connected." );
13091312 break ;
13101313 }
13111314 }
@@ -1315,7 +1318,6 @@ void UInputMan::HandleGamepadHotPlug(int deviceIndex) {
13151318 }
13161319
13171320 if (controller) {
1318- SDL_JoystickID id = SDL_GetJoystickID (controller);
13191321 int numAxis = 0 ;
13201322 int numButtons = 0 ;
13211323 if (SDL_IsGamepad (deviceIndex)) {
@@ -1325,8 +1327,8 @@ void UInputMan::HandleGamepadHotPlug(int deviceIndex) {
13251327 numAxis = SDL_GetNumJoystickAxes (controller);
13261328 numButtons = SDL_GetNumJoystickButtons (controller);
13271329 }
1328- s_PrevJoystickStates[controllerIndex] = Gamepad (deviceIndex, id , numAxis, numButtons);
1329- s_ChangedJoystickStates[controllerIndex] = Gamepad (deviceIndex, id , numAxis, numButtons);
1330+ s_PrevJoystickStates[controllerIndex] = Gamepad (controllerIndex, deviceIndex , numAxis, numButtons);
1331+ s_ChangedJoystickStates[controllerIndex] = Gamepad (controllerIndex, deviceIndex , numAxis, numButtons);
13301332 m_NumJoysticks++;
13311333 }
13321334}
0 commit comments