From 45b8f228d974dd90a7ce87a7f32d5722ee63c977 Mon Sep 17 00:00:00 2001 From: Christopher Leggett Date: Sun, 23 Mar 2025 14:07:12 -0400 Subject: [PATCH] Remove MenuOptionIndex Middleman and fix rando presets (#5185) * Remove CVar Middleman and fix rando presets * Throwing in a couple of typo corrections --- soh/soh/Enhancements/presets.cpp | 3 -- soh/soh/Enhancements/randomizer/option.cpp | 34 ++++--------------- soh/soh/Enhancements/randomizer/option.h | 22 +----------- .../Enhancements/randomizer/randomizer.cpp | 2 +- soh/soh/Enhancements/randomizer/settings.cpp | 17 ++-------- soh/soh/Enhancements/randomizer/settings.h | 7 ---- soh/soh/OTRGlobals.cpp | 1 - soh/soh/SohGui/SohMenuEnhancements.cpp | 4 +-- 8 files changed, 13 insertions(+), 77 deletions(-) diff --git a/soh/soh/Enhancements/presets.cpp b/soh/soh/Enhancements/presets.cpp index c8760caca..3c542762f 100644 --- a/soh/soh/Enhancements/presets.cpp +++ b/soh/soh/Enhancements/presets.cpp @@ -75,9 +75,6 @@ void DrawPresetSelector(PresetType presetTypeId) { } CVarSetInteger(presetTypeCvar.c_str(), selectedPresetId); Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame(); - if (presetTypeId == PRESET_TYPE_RANDOMIZER){ - Rando::Settings::GetInstance()->ReloadOptions(); - } } UIWidgets::PopStyleButton(); } diff --git a/soh/soh/Enhancements/randomizer/option.cpp b/soh/soh/Enhancements/randomizer/option.cpp index 64ca107c5..84fc4c28b 100644 --- a/soh/soh/Enhancements/randomizer/option.cpp +++ b/soh/soh/Enhancements/randomizer/option.cpp @@ -62,8 +62,8 @@ const std::string& Option::GetDescription() const { return description; } -uint8_t Option::GetMenuOptionIndex() const { - return menuSelection; +uint8_t Option::GetOptionIndex() const { + return CVarGetInteger(cvarName.c_str(), defaultOption); } const std::string& Option::GetOptionText(size_t index) const { @@ -74,18 +74,6 @@ const std::string& Option::GetCVarName() const { return cvarName; } -void Option::SaveCVar() const { - if (!cvarName.empty()) { - CVarSetInteger(cvarName.c_str(), GetMenuOptionIndex()); - } -} - -void Option::SetFromCVar() { - if (!cvarName.empty()) { - SetMenuIndex(CVarGetInteger(cvarName.c_str(), defaultOption)); - } -} - void Option::SetDelayedOption() { delayedSelection = contextSelection; } @@ -94,13 +82,6 @@ void Option::RestoreDelayedOption() { contextSelection = delayedSelection; } -void Option::SetMenuIndex(size_t idx) { - menuSelection = idx; - if (menuSelection > options.size() - 1) { - menuSelection = options.size() - 1; - } -} - void Option::SetContextIndex(size_t idx) { // TODO: Set to Context's OptionValue array. contextSelection = idx; @@ -122,8 +103,8 @@ bool Option::IsHidden() const { } void Option::ChangeOptions(std::vector opts) { - if (menuSelection >= opts.size()) { - menuSelection = opts.size() - 1; + if (GetOptionIndex() >= opts.size()) { + CVarSetInteger(cvarName.c_str(), opts.size() - 1); } options = std::move(opts); } @@ -202,10 +183,9 @@ Option::Option(size_t key_, std::string name_, std::vector options_ : key(key_), name(std::move(name_)), options(std::move(options_)), category(category_), cvarName(std::move(cvarName_)), description(std::move(description_)), widgetType(widgetType_), defaultOption(defaultOption_), defaultHidden(defaultHidden_), imFlags(imFlags_) { - menuSelection = contextSelection = defaultOption; + contextSelection = defaultOption; hidden = defaultHidden; PopulateTextToNum(); - SetFromCVar(); } bool Option::RenderCheckbox() { @@ -245,10 +225,9 @@ bool Option::RenderCombobox() { bool Option::RenderSlider() { bool changed = false; - int val = GetMenuOptionIndex(); + int val = CVarGetInteger(cvarName.c_str(), defaultOption); if (val > options.size() - 1) { val = options.size() - 1; - CVarSetInteger(cvarName.c_str(), val); changed = true; } UIWidgets::IntSliderOptions widgetOptions = UIWidgets::IntSliderOptions().Color(THEME_COLOR).Min(0).Max(options.size() - 1).Tooltip(description.c_str()).Format(options[val].c_str()).DefaultValue(defaultOption); @@ -266,7 +245,6 @@ bool Option::RenderSlider() { } if (changed) { CVarSetInteger(cvarName.c_str(), val); - SetFromCVar(); Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame(); } return changed; diff --git a/soh/soh/Enhancements/randomizer/option.h b/soh/soh/Enhancements/randomizer/option.h index 518487219..3b1ae8521 100644 --- a/soh/soh/Enhancements/randomizer/option.h +++ b/soh/soh/Enhancements/randomizer/option.h @@ -225,19 +225,7 @@ class Option { * * @return uint8_t */ - uint8_t GetMenuOptionIndex() const; - - /** - * @brief Sets the CVar corresponding to the property `cvarName` equal to the value - * of the property `selectedValue`. - */ - void SaveCVar() const; - - /** - * @brief Sets the value of property `selectedValue` equal to the CVar corresponding - * to the property `cvarName`. - */ - void SetFromCVar(); + uint8_t GetOptionIndex() const; /** * @brief Set the delayedOption to the currently selected index so it can be restored later. @@ -249,13 +237,6 @@ class Option { */ void RestoreDelayedOption(); - /** - * @brief Set the menu index for this Option. Also calls `SetVariable()`. - * - * @param idx the index to set as the selected index. - */ - void SetMenuIndex(size_t idx); - /** * @brief Set the rando context index for this Option. Also calls `SetVariable()`. * @@ -344,7 +325,6 @@ protected: void PopulateTextToNum(); std::string name; std::vector options; - uint8_t menuSelection = 0; uint8_t contextSelection = 0; uint8_t delayedSelection = 0; bool hidden = false; diff --git a/soh/soh/Enhancements/randomizer/randomizer.cpp b/soh/soh/Enhancements/randomizer/randomizer.cpp index 244afb9c4..4b88d7e2a 100644 --- a/soh/soh/Enhancements/randomizer/randomizer.cpp +++ b/soh/soh/Enhancements/randomizer/randomizer.cpp @@ -1871,7 +1871,6 @@ void GenerateRandomizerImgui(std::string seed = "") { CVarSave(); auto ctx = Rando::Context::GetInstance(); //RANDOTODO proper UI for selecting if a spoiler loaded should be used for settings - Rando::Settings::GetInstance()->SetAllFromCVar(); Rando::Settings::GetInstance()->SetAllToContext(); // todo: this efficently when we build out cvar array support @@ -1972,6 +1971,7 @@ void RandomizerSettingsWindow::DrawElement() { } CVarSetInteger(presetTypeCvar.c_str(), randomizerPresetSelected); Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame(); + mSettings->UpdateOptionProperties(); } UIWidgets::Spacer(0); diff --git a/soh/soh/Enhancements/randomizer/settings.cpp b/soh/soh/Enhancements/randomizer/settings.cpp index a2bf3660d..2e6a61e89 100644 --- a/soh/soh/Enhancements/randomizer/settings.cpp +++ b/soh/soh/Enhancements/randomizer/settings.cpp @@ -1147,12 +1147,6 @@ const OptionGroup& Settings::GetOptionGroup(const RandomizerSettingGroupKey key) return mOptionGroups[key]; } -void Settings::SetAllFromCVar() { - for (auto& option : mOptions) { - option.SetFromCVar(); - } -} - void Settings::UpdateOptionProperties() { // Default to hiding bridge opts and the extra sliders. mOptions[RSK_RAINBOW_BRIDGE].AddFlag(IMFLAG_SEPARATOR_BOTTOM); @@ -2222,11 +2216,6 @@ void Settings::ParseJson(nlohmann::json spoilerFileJson) { } } -void Settings::ReloadOptions() { - for (int i = 0; i < RSK_MAX; i++) { - mOptions[i].SetFromCVar(); - } -} void Settings::AssignContext(std::shared_ptr ctx) { mContext = ctx; } @@ -2237,13 +2226,13 @@ void Settings::ClearContext() { void Settings::SetAllToContext() { for (int i = 0; i < RSK_MAX; i++) { - mContext->GetOption(static_cast(i)).Set(mOptions[i].GetMenuOptionIndex()); + mContext->GetOption(static_cast(i)).Set(mOptions[i].GetOptionIndex()); } for (int i = 0; i < RT_MAX; i++) { - mContext->GetTrickOption(static_cast(i)).Set(mTrickOptions[i].GetMenuOptionIndex()); + mContext->GetTrickOption(static_cast(i)).Set(mTrickOptions[i].GetOptionIndex()); } for (int i = 0; i < RC_MAX; i++) { - mContext->GetItemLocation(i)->SetExcludedOption(StaticData::GetLocation(static_cast(i))->GetExcludedOption()->GetMenuOptionIndex()); + mContext->GetItemLocation(i)->SetExcludedOption(StaticData::GetLocation(static_cast(i))->GetExcludedOption()->GetOptionIndex()); } } diff --git a/soh/soh/Enhancements/randomizer/settings.h b/soh/soh/Enhancements/randomizer/settings.h index 6810b6832..271774c40 100644 --- a/soh/soh/Enhancements/randomizer/settings.h +++ b/soh/soh/Enhancements/randomizer/settings.h @@ -88,12 +88,6 @@ class Settings { */ const OptionGroup& GetOptionGroup(RandomizerSettingGroupKey key); - /** - * @brief sets the `selectedOption` of all Options to the value of the CVar - * corresponding to their `cvarName`s. - */ - void SetAllFromCVar(); - /** * @brief Updates various properties of options based on the value of other options. * Used to update visibility, whether or not interaction is disabled, and what the @@ -113,7 +107,6 @@ class Settings { */ void ParseJson(nlohmann::json spoilerFileJson); std::map> mTricksByArea = {}; - void ReloadOptions(); /** * @brief Assigns a Rando::Context instance to this settings instance diff --git a/soh/soh/OTRGlobals.cpp b/soh/soh/OTRGlobals.cpp index 3690e8648..3809a2ac6 100644 --- a/soh/soh/OTRGlobals.cpp +++ b/soh/soh/OTRGlobals.cpp @@ -2514,7 +2514,6 @@ void SoH_ProcessDroppedFiles(std::string filePath) { } Rando::Settings::GetInstance()->UpdateOptionProperties(); - Rando::Settings::GetInstance()->SetAllFromCVar(); auto gui = Ship::Context::GetInstance()->GetWindow()->GetGui(); gui->GetGuiWindow("Console")->Hide(); diff --git a/soh/soh/SohGui/SohMenuEnhancements.cpp b/soh/soh/SohGui/SohMenuEnhancements.cpp index eb64859e6..85cbc6734 100644 --- a/soh/soh/SohGui/SohMenuEnhancements.cpp +++ b/soh/soh/SohGui/SohMenuEnhancements.cpp @@ -904,7 +904,7 @@ void SohMenu::AddMenuEnhancements() { .Options(CheckboxOptions().Tooltip( "Removes the Dungeon Entrance icon on the top-left corner of the screen when no dungeon is present on the " "current map.")); - AddWidget(path, "Fix Two-Handled Idle Animations", WIDGET_CVAR_CHECKBOX) + AddWidget(path, "Fix Two-Handed Idle Animations", WIDGET_CVAR_CHECKBOX) .CVar(CVAR_ENHANCEMENT("TwoHandedIdle")) .Options(CheckboxOptions().Tooltip( "Re-Enables the two-handed idle animation, a seemingly finished animation that was disabled on accident " @@ -975,7 +975,7 @@ void SohMenu::AddMenuEnhancements() { .Options(CheckboxOptions().Tooltip( "Restore pre-release behavior where defeating a Gold Skulltula will play a cutscene showing it die.")); AddWidget(path, "Pulsate Boss Icon", WIDGET_CVAR_CHECKBOX) - .CVar(CVAR_ENHANCEMENT("Pulsate Boss Icon")) + .CVar(CVAR_ENHANCEMENT("PulsateBossIcon")) .Options(CheckboxOptions().Tooltip( "Restores an unfinished feature to pulsate the boss room icon when you are in the boss room."));