1
1
#include " UInputMan.h"
2
2
#include " Constants.h"
3
- #include " SDL3/SDL_events.h"
4
- #include " SDL3/SDL_keyboard.h"
5
- #include " SDL3/SDL_mouse.h"
6
3
#include " SceneMan.h"
7
4
#include " ActivityMan.h"
8
5
#include " MetaMan.h"
14
11
#include " Icon.h"
15
12
#include " GameActivity.h"
16
13
#include " System.h"
14
+
17
15
#include < SDL3/SDL.h>
18
16
#include < string>
19
17
#include < unordered_map>
@@ -82,7 +80,7 @@ int UInputMan::Initialize() {
82
80
83
81
int controllerIndex = 0 ;
84
82
int joystickCount = 0 ;
85
- SDL_JoystickID* joysticks = SDL_GetJoysticks (&joystickCount);
83
+ SDL_JoystickID* joysticks = SDL_GetGamepads (&joystickCount);
86
84
87
85
for (size_t index = 0 ; index < std::min (joystickCount, static_cast <int >(Players::MaxPlayerCount)); ++index) {
88
86
if (SDL_IsGamepad (joysticks[index])) {
@@ -997,11 +995,12 @@ int UInputMan::Update() {
997
995
}
998
996
}
999
997
break ;
1000
- case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
1001
- case SDL_EVENT_GAMEPAD_BUTTON_UP:
1002
998
case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
1003
999
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 ()) {
1005
1004
int button = -1 ;
1006
1005
int down = false ;
1007
1006
if (SDL_IsGamepad (device->m_DeviceIndex )) {
@@ -1020,10 +1019,14 @@ int UInputMan::Update() {
1020
1019
device->m_Buttons [button] = down;
1021
1020
}
1022
1021
break ;
1022
+ }
1023
1023
case SDL_EVENT_JOYSTICK_ADDED:
1024
+ case SDL_EVENT_GAMEPAD_ADDED: {
1024
1025
HandleGamepadHotPlug (inputEvent.jdevice .which );
1025
1026
break ;
1027
+ }
1026
1028
case SDL_EVENT_JOYSTICK_REMOVED:
1029
+ case SDL_EVENT_GAMEPAD_REMOVED:
1027
1030
if (std::vector<Gamepad>::iterator prevDevice = std::find (s_PrevJoystickStates.begin (), s_PrevJoystickStates.end (), inputEvent.jdevice .which ); prevDevice != s_PrevJoystickStates.end ()) {
1028
1031
g_ConsoleMan.PrintString (" INFO: Gamepad " + std::to_string (prevDevice->m_DeviceIndex + 1 ) + " disconnected!" );
1029
1032
SDL_CloseGamepad (SDL_GetGamepadFromID (prevDevice->m_JoystickID ));
@@ -1281,17 +1284,19 @@ void UInputMan::UpdateJoystickDigitalAxis() {
1281
1284
}
1282
1285
}
1283
1286
1284
- void UInputMan::HandleGamepadHotPlug (int deviceIndex) {
1287
+ void UInputMan::HandleGamepadHotPlug (SDL_JoystickID deviceIndex) {
1285
1288
SDL_Joystick* controller = nullptr ;
1286
1289
int controllerIndex = 0 ;
1287
1290
1288
1291
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 ) {
1290
1296
if (SDL_IsGamepad (deviceIndex)) {
1291
1297
SDL_Gamepad* gameController = SDL_OpenGamepad (deviceIndex);
1292
1298
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!" );
1295
1300
break ;
1296
1301
}
1297
1302
controller = SDL_GetGamepadJoystick (gameController);
@@ -1300,12 +1305,10 @@ void UInputMan::HandleGamepadHotPlug(int deviceIndex) {
1300
1305
controller = SDL_OpenJoystick (deviceIndex);
1301
1306
}
1302
1307
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!" );
1305
1309
break ;
1306
1310
}
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." );
1309
1312
break ;
1310
1313
}
1311
1314
}
@@ -1315,7 +1318,6 @@ void UInputMan::HandleGamepadHotPlug(int deviceIndex) {
1315
1318
}
1316
1319
1317
1320
if (controller) {
1318
- SDL_JoystickID id = SDL_GetJoystickID (controller);
1319
1321
int numAxis = 0 ;
1320
1322
int numButtons = 0 ;
1321
1323
if (SDL_IsGamepad (deviceIndex)) {
@@ -1325,8 +1327,8 @@ void UInputMan::HandleGamepadHotPlug(int deviceIndex) {
1325
1327
numAxis = SDL_GetNumJoystickAxes (controller);
1326
1328
numButtons = SDL_GetNumJoystickButtons (controller);
1327
1329
}
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);
1330
1332
m_NumJoysticks++;
1331
1333
}
1332
1334
}
0 commit comments