Fixed an issue where the joystick counter would not decrease when removing a controller.

This commit is contained in:
Leon Styhre 2021-07-03 12:24:23 +02:00
parent 64372a4a70
commit 52e12da55a
2 changed files with 12 additions and 16 deletions

View file

@ -272,20 +272,6 @@ std::string InputManager::getTemporaryConfigPath()
return path;
}
int InputManager::getNumJoysticks()
{
int numJoysticks = 0;
// This is a workaround to exclude the keyboard (ID -1) from the total joystick count.
// It's incorrectly added when configuring the keyboard in GuiInputConfig, but I've
// been unable to find a proper fix to not having it added to mJoysticks.
for (auto it = mJoysticks.cbegin(); it != mJoysticks.cend(); it++) {
if ((*it).first >= 0)
numJoysticks += 1;
}
return numJoysticks;
}
int InputManager::getNumConfiguredDevices()
{
int num = 0;
@ -672,7 +658,7 @@ void InputManager::removeControllerByJoystickID(SDL_JoystickID joyID)
auto it = mInputConfigs.find(joyID);
mInputConfigs.erase(it);
// Close the controllers.
// Close the controller and remove its entry.
auto controllerIt = mControllers.find(joyID);
if (controllerIt != mControllers.cend()) {
SDL_GameControllerClose(controllerIt->second);
@ -681,4 +667,13 @@ void InputManager::removeControllerByJoystickID(SDL_JoystickID joyID)
else {
LOG(LogError) << "Couldn't find controller to close (instance ID: " << joyID << ")";
}
// Remove the joystick entry.
auto joystickIt = mJoysticks.find(joyID);
if (joystickIt != mJoysticks.cend()) {
mJoysticks.erase(joystickIt);
}
else {
LOG(LogError) << "Couldn't find joystick entry to remove (instance ID: " << joyID << ")";
}
}

View file

@ -43,7 +43,6 @@ public:
static std::string getConfigPath();
static std::string getTemporaryConfigPath();
int getNumJoysticks();
int getNumConfiguredDevices();
int getAxisCountByDevice(int deviceId);
int getButtonCountByDevice(int deviceId);
@ -53,6 +52,8 @@ public:
bool parseEvent(const SDL_Event& event, Window* window);
int getNumJoysticks() { return mJoysticks.size(); }
private:
bool initialized() const;