From 26437223c362e91cbd2e7cba9eed79c904e82ca9 Mon Sep 17 00:00:00 2001 From: Malkierian Date: Mon, 28 Apr 2025 17:50:37 -0700 Subject: [PATCH] Apply `CVarClear()` calls in CVar-prefixed widget functions. --- libultraship | 2 +- soh/soh/SohGui/UIWidgets.cpp | 52 +++++++++++++++++++++++++++--------- soh/soh/SohGui/UIWidgets.hpp | 23 +++++++++++++--- 3 files changed, 61 insertions(+), 16 deletions(-) diff --git a/libultraship b/libultraship index 1d7c33869..35c3a7e80 160000 --- a/libultraship +++ b/libultraship @@ -1 +1 @@ -Subproject commit 1d7c3386928fdb919a99e62c2f44ea4942d153e1 +Subproject commit 35c3a7e80721e5f5a5260753c14499dc943dd3d8 diff --git a/soh/soh/SohGui/UIWidgets.cpp b/soh/soh/SohGui/UIWidgets.cpp index 0963c5ef1..484c58dd4 100644 --- a/soh/soh/SohGui/UIWidgets.cpp +++ b/soh/soh/SohGui/UIWidgets.cpp @@ -378,7 +378,11 @@ bool CVarCheckbox(const char* label, const char* cvarName, const CheckboxOptions bool dirty = false; bool value = (bool)CVarGetInteger(cvarName, options.defaultValue); if (Checkbox(label, &value, options)) { - CVarSetInteger(cvarName, value); + if (value == options.defaultValue) { + CVarClear(cvarName); + } else { + CVarSetInteger(cvarName, value); + } Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame(); ShipInit::Init(cvarName); dirty = true; @@ -609,7 +613,11 @@ bool CVarSliderInt(const char* label, const char* cvarName, const IntSliderOptio bool dirty = false; int32_t value = CVarGetInteger(cvarName, options.defaultValue); if (SliderInt(label, &value, options)) { - CVarSetInteger(cvarName, value); + if (value == options.defaultValue) { + CVarClear(cvarName); + } else { + CVarSetInteger(cvarName, value); + } Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame(); ShipInit::Init(cvarName); dirty = true; @@ -740,7 +748,11 @@ bool CVarSliderFloat(const char* label, const char* cvarName, const FloatSliderO bool dirty = false; float value = CVarGetFloat(cvarName, options.defaultValue); if (SliderFloat(label, &value, options)) { - CVarSetFloat(cvarName, value); + if (value == options.defaultValue) { + CVarClear(cvarName); + } else { + CVarSetFloat(cvarName, value); + } Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame(); ShipInit::Init(cvarName); dirty = true; @@ -814,7 +826,11 @@ bool CVarInputString(const char* label, const char* cvarName, const InputOptions bool dirty = false; std::string value = CVarGetString(cvarName, options.defaultValue.c_str()); if (InputString(label, &value, options)) { - CVarSetString(cvarName, value.c_str()); + if (value == options.defaultValue) { + CVarClear(cvarName); + } else { + CVarSetString(cvarName, value.c_str()); + } Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame(); ShipInit::Init(cvarName); dirty = true; @@ -866,7 +882,11 @@ bool CVarInputInt(const char* label, const char* cvarName, const InputOptions& o int32_t defaultValue = std::stoi(options.defaultValue); int32_t value = CVarGetInteger(cvarName, defaultValue); if (InputInt(label, &value, options)) { - CVarSetInteger(cvarName, value); + if (value == defaultValue) { + CVarClear(cvarName); + } else { + CVarSetInteger(cvarName, value); + } Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame(); ShipInit::Init(cvarName); dirty = true; @@ -952,11 +972,15 @@ bool CVarColorPicker(const char* label, const char* cvarName, Color_RGBA8 defaul UIWidgets::CheckboxOptions({ { .tooltip = "Prevents this color from being changed" } }).Color(themeColor)); } if (changed) { - color.r = (uint8_t)(colorVec.x * 255.0f); - color.g = (uint8_t)(colorVec.y * 255.0f); - color.b = (uint8_t)(colorVec.z * 255.0f); - color.a = (uint8_t)(colorVec.w * 255.0f); - CVarSetColor(valueCVar.c_str(), color); + if (color.r == defaultColor.r && color.g == defaultColor.g && color.b == defaultColor.b && color.a == defaultColor.a) { + CVarClear(valueCVar.c_str()); + } else { + color.r = (uint8_t)(colorVec.x * 255.0f); + color.g = (uint8_t)(colorVec.y * 255.0f); + color.b = (uint8_t)(colorVec.z * 255.0f); + color.a = (uint8_t)(colorVec.w * 255.0f); + CVarSetColor(valueCVar.c_str(), color); + } Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame(); ShipInit::Init(valueCVar.c_str()); changed = true; @@ -1028,10 +1052,14 @@ bool CVarRadioButton(const char* text, const char* cvarName, int32_t id, const R std::string make_invisible = "##" + std::string(text) + std::string(cvarName); bool ret = false; - int val = CVarGetInteger(cvarName, 0); + int val = CVarGetInteger(cvarName, options.defaultIndex); PushStyleCheckbox(options.color); if (ImGui::RadioButton(make_invisible.c_str(), id == val)) { - CVarSetInteger(cvarName, id); + if (id == options.defaultIndex) { + CVarClear(cvarName); + } else { + CVarSetInteger(cvarName, id); + } Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame(); ret = true; } diff --git a/soh/soh/SohGui/UIWidgets.hpp b/soh/soh/SohGui/UIWidgets.hpp index 05b7ff239..d5a3a2c44 100644 --- a/soh/soh/SohGui/UIWidgets.hpp +++ b/soh/soh/SohGui/UIWidgets.hpp @@ -399,6 +399,7 @@ struct FloatSliderOptions : WidgetOptions { struct RadioButtonsOptions : WidgetOptions { std::unordered_map buttonMap; + int32_t defaultIndex = 0; Colors color = Colors::LightBlue; RadioButtonsOptions& ButtonMap(std::unordered_map buttonMap_) { @@ -413,6 +414,10 @@ struct RadioButtonsOptions : WidgetOptions { color = color_; return *this; } + RadioButtonsOptions& DefaultIndex(float defaultIndex_) { + defaultIndex = defaultIndex_; + return *this; + } }; struct InputOptions : WidgetOptions { @@ -875,7 +880,11 @@ bool CVarCombobox(const char* label, const char* cvarName, const std::unordered_ bool dirty = false; int32_t value = CVarGetInteger(cvarName, options.defaultIndex); if (Combobox(label, &value, comboMap, options)) { - CVarSetInteger(cvarName, value); + if (value == options.defaultIndex) { + CVarClear(cvarName); + } else { + CVarSetInteger(cvarName, value); + } Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame(); ShipInit::Init(cvarName); dirty = true; @@ -889,7 +898,11 @@ bool CVarCombobox(const char* label, const char* cvarName, const std::vector(label, &value, comboVector, options)) { - CVarSetInteger(cvarName, value); + if (value == options.defaultIndex) { + CVarClear(cvarName); + } else { + CVarSetInteger(cvarName, value); + } Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame(); ShipInit::Init(cvarName); dirty = true; @@ -903,7 +916,11 @@ bool CVarCombobox(const char* label, const char* cvarName, const char* (&comboAr bool dirty = false; int32_t value = CVarGetInteger(cvarName, options.defaultIndex); if (Combobox(label, &value, comboArray, options)) { - CVarSetInteger(cvarName, value); + if (value == options.defaultIndex) { + CVarClear(cvarName); + } else { + CVarSetInteger(cvarName, value); + } Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame(); ShipInit::Init(cvarName); dirty = true;