fix: main pad idx not swapping when controller disconnects

This commit is contained in:
2026-08-09 02:02:37 -04:00
parent a9df365c5c
commit 26a297f1de
2 changed files with 61 additions and 10 deletions
+59 -10
View File
@@ -647,23 +647,72 @@ void CInput::HandleGamepadEvents()
}
break;
case SDL_EVENT_GAMEPAD_REMOVED:
for (int i = 0; i < MAX_JOYPADS; i++)
{
if (m_Joypads[i].m_pGamepad &&
SDL_GetGamepadID(m_Joypads[i].m_pGamepad) == event.gdevice.which)
{
SDL_CloseGamepad(m_Joypads[i].m_pGamepad);
m_Joypads[i].m_pGamepad = nullptr;
m_Joypads[i].m_bIsConnected = false;
{
int removedIndex = -1;
break;
for (int i = 0; i < MAX_JOYPADS; i++)
{
if (m_Joypads[i].m_pGamepad &&
SDL_GetGamepadID(m_Joypads[i].m_pGamepad) == event.gdevice.which)
{
SDL_CloseGamepad(m_Joypads[i].m_pGamepad);
m_Joypads[i].m_pGamepad = nullptr;
m_Joypads[i].m_bIsConnected = false;
removedIndex = i;
break;
}
}
if (removedIndex == 0)
{
for (int i = 1; i < MAX_JOYPADS; i++)
{
if (m_Joypads[i].m_bIsConnected && m_Joypads[i].m_pGamepad)
{
ShiftGamepadData(0, i);
SDL_SetGamepadPlayerIndex(m_Joypads[0].m_pGamepad, 0);
ClearGamepadSlot(i);
break;
}
}
}
break;
}
break;
}
}
}
void CInput::ShiftGamepadData(int destIndex, int srcIndex)
{
m_Joypads[destIndex] = m_Joypads[srcIndex];
m_Joypads[destIndex].m_pfLeftThumbXAxisMap = &m_Joypads[destIndex].m_fNormalizedLeftThumbX;
m_Joypads[destIndex].m_pfLeftThumbYAxisMap = &m_Joypads[destIndex].m_fNormalizedLeftThumbY;
m_Joypads[destIndex].m_pfRightThumbXAxisMap = &m_Joypads[destIndex].m_fNormalizedRightThumbX;
m_Joypads[destIndex].m_pfRightThumbYAxisMap = &m_Joypads[destIndex].m_fNormalizedRightThumbY;
m_Joypads[destIndex].m_pucLeftTriggerAxisMap = &m_Joypads[destIndex].m_ucLeftTriggerState;
m_Joypads[destIndex].m_pucRightTriggerAxisMap = &m_Joypads[destIndex].m_ucRightTriggerState;
}
void CInput::ClearGamepadSlot(int index)
{
m_Joypads[index].m_pGamepad = nullptr;
m_Joypads[index].m_bIsConnected = false;
m_Joypads[index].m_uiButtons = 0;
m_Joypads[index].m_ucInputStateIndex = -1;
m_Joypads[index].m_ucLeftTriggerState = 0;
m_Joypads[index].m_ucRightTriggerState = 0;
m_Joypads[index].m_iRightThumbX = 0;
m_Joypads[index].m_iRightThumbY = 0;
m_Joypads[index].m_pfLeftThumbXAxisMap = &m_Joypads[index].m_fNormalizedLeftThumbX;
m_Joypads[index].m_pfLeftThumbYAxisMap = &m_Joypads[index].m_fNormalizedLeftThumbY;
m_Joypads[index].m_pfRightThumbXAxisMap = &m_Joypads[index].m_fNormalizedRightThumbX;
m_Joypads[index].m_pfRightThumbYAxisMap = &m_Joypads[index].m_fNormalizedRightThumbY;
m_Joypads[index].m_pucLeftTriggerAxisMap = &m_Joypads[index].m_ucLeftTriggerState;
m_Joypads[index].m_pucRightTriggerAxisMap = &m_Joypads[index].m_ucRightTriggerState;
}
bool CInput::UpdateJoypads()
{
assert(m_bJoypadMapArrayIsSetup);
+2
View File
@@ -157,6 +157,8 @@ public:
FLOAT GetIdleSeconds(int iPad);
void HandleGamepadEvents();
void ShiftGamepadData(int destIndex, int srcIndex);
void ClearGamepadSlot(int index);
bool UpdateJoypads(void);
void ClearJoypadValues(JOYPAD *pThisPad);