From 83af0ff4af03a6726a79e6a842fe510548cf035f Mon Sep 17 00:00:00 2001 From: Rosalie Wanders Date: Sat, 11 Feb 2023 13:41:26 +0100 Subject: [PATCH] RMG-Input: improve per game profile handling --- .../UserInterface/Widget/ControllerWidget.cpp | 67 ++++++++++++++----- .../UserInterface/Widget/ControllerWidget.hpp | 5 +- Source/RMG-Input/main.cpp | 12 +++- 3 files changed, 64 insertions(+), 20 deletions(-) diff --git a/Source/RMG-Input/UserInterface/Widget/ControllerWidget.cpp b/Source/RMG-Input/UserInterface/Widget/ControllerWidget.cpp index 05875d66..da86d81d 100644 --- a/Source/RMG-Input/UserInterface/Widget/ControllerWidget.cpp +++ b/Source/RMG-Input/UserInterface/Widget/ControllerWidget.cpp @@ -302,6 +302,11 @@ bool ControllerWidget::isSectionUserProfile(QString section) return section.contains("User Profile"); } +bool ControllerWidget::isSectionGameProfile(QString section) +{ + return !this->isSectionUserProfile(section) && section.contains("Game"); +} + void ControllerWidget::setPluggedIn(bool value) { QWidget* widgetList[] = @@ -513,11 +518,7 @@ void ControllerWidget::on_profileComboBox_currentIndexChanged(int value) if (this->previousProfileComboBoxIndex < this->profileComboBox->count()) { QString section = this->profileComboBox->itemData(this->previousProfileComboBoxIndex).toString(); - // TODO: how to handle game specific profiles here? - if (!section.startsWith(this->settingsSection + " Game")) - { - this->SaveSettings(section); - } + this->SaveSettings(section); } } @@ -659,8 +660,15 @@ void ControllerWidget::on_removeProfileButton_clicked() emit this->UserProfileRemoved(this->profileComboBox->currentText(), currentSection); } - // change current index - this->profileComboBox->setCurrentIndex(0); + // switch back to main profile when deleting + // a per game profile + if (this->isSectionGameProfile(currentSection)) + { + // ensure we don't save the game specific profile + // upon switching to the main profile + this->previousProfileComboBoxIndex = -1; + this->profileComboBox->setCurrentIndex(0); + } } void ControllerWidget::on_resetButton_clicked() @@ -1083,7 +1091,6 @@ void ControllerWidget::LoadSettings() } QString section = this->settingsSection; - QString gameSection; std::vector userProfiles; // if the main profile section doesn't exist, @@ -1101,26 +1108,33 @@ void ControllerWidget::LoadSettings() if (CoreGetCurrentRomSettings(romSettings) && CoreGetCurrentRomHeader(romHeader)) { - gameSection = section + " Game " + QString::fromStdString(romSettings.MD5); + this->gameSection = section + " Game " + QString::fromStdString(romSettings.MD5); // use internal rom name QString internalName = QString::fromStdString(romHeader.Name); // add game specific profile when // it doesn't exist yet - int index = this->profileComboBox->findData(gameSection); + int index = this->profileComboBox->findData(this->gameSection); if (index == -1) { - this->profileComboBox->addItem(internalName, gameSection); + this->profileComboBox->addItem(internalName, this->gameSection); } // if a game specific section exists, // select it in the profile combobox // and use it to load the settings - if (CoreSettingsSectionExists(gameSection.toStdString())) + if (CoreSettingsSectionExists(this->gameSection.toStdString())) { - this->profileComboBox->setCurrentText(internalName); - section = gameSection; + // check if the key 'UseGameProfile' exists, + // if it doesn't then use the profile, + // else check if it's true and use it + if (!CoreSettingsKeyExists(this->gameSection.toStdString(), "UseGameProfile") || + CoreSettingsGetBoolValue(SettingsID::Input_UseGameProfile, this->gameSection.toStdString())) + { + this->profileComboBox->setCurrentText(internalName); + section = this->gameSection; + } } } @@ -1154,7 +1168,16 @@ void ControllerWidget::LoadSettings(QString sectionQString, bool loadUserProfile // do nothing if the section doesn't exist if (!CoreSettingsSectionExists(section)) { - return; + // fallback to main section for per game profiles + if (this->isSectionGameProfile(sectionQString)) + { + sectionQString = this->settingsSection; + section = this->settingsSection.toStdString(); + } + else + { + return; + } } // see if the profile contains the UseProfile setting, @@ -1258,7 +1281,7 @@ void ControllerWidget::SaveSettings() { return; } - + this->SaveSettings(this->getCurrentSettingsSection()); } @@ -1295,6 +1318,18 @@ void ControllerWidget::SaveSettings(QString section) CoreSettingsSetValue(SettingsID::Input_UseProfile, mainSettingsSection, std::string("")); } + // when section is a game specific profile, + // make sure we tell it whether or not to use it + if (this->isSectionGameProfile(section)) + { + CoreSettingsSetValue(SettingsID::Input_UseGameProfile, sectionStr, true); + } + else if (!this->gameSection.isEmpty() && + CoreSettingsSectionExists(this->gameSection.toStdString())) + { + CoreSettingsSetValue(SettingsID::Input_UseGameProfile, this->gameSection.toStdString(), false); + } + this->GetCurrentInputDevice(deviceName, deviceNum, true); CoreSettingsSetValue(SettingsID::Input_PluggedIn, sectionStr, this->IsPluggedIn()); diff --git a/Source/RMG-Input/UserInterface/Widget/ControllerWidget.hpp b/Source/RMG-Input/UserInterface/Widget/ControllerWidget.hpp index b9a4f073..ed9b9367 100644 --- a/Source/RMG-Input/UserInterface/Widget/ControllerWidget.hpp +++ b/Source/RMG-Input/UserInterface/Widget/ControllerWidget.hpp @@ -36,6 +36,7 @@ class ControllerWidget : public QWidget, Ui::ControllerWidget private: bool initialized = false; QString settingsSection; + QString gameSection; OptionsDialogSettings optionsDialogSettings; @@ -82,9 +83,11 @@ private: void removeDuplicates(MappingButton* button); QString getCurrentSettingsSection(); - + QString getUserProfileSectionName(QString profile); + bool isSectionUserProfile(QString section); + bool isSectionGameProfile(QString section); void setPluggedIn(bool value); diff --git a/Source/RMG-Input/main.cpp b/Source/RMG-Input/main.cpp index b005f63f..f9bf5f1d 100644 --- a/Source/RMG-Input/main.cpp +++ b/Source/RMG-Input/main.cpp @@ -183,13 +183,19 @@ static void load_settings(void) } // if game ID was retrieved, - // check if game section exists, if it does - // use that section instead of the main section + // check if game section exists, + // if it does, check if the 'UseGameProfile' key exists, + // if it doesn't then use the profile, else + // check if the value is true if (!gameId.empty()) { if (CoreSettingsSectionExists(gameSection)) { - section = gameSection; + if (!CoreSettingsKeyExists(gameSection, "UseGameProfile") || + CoreSettingsGetBoolValue(SettingsID::Input_UseGameProfile, gameSection)) + { + section = gameSection; + } } }