From 3c56c0fe2ee9a37d2012102ee984f32787092bc3 Mon Sep 17 00:00:00 2001 From: SamuXarick <43006711+SamuXarick@users.noreply.github.com> Date: Thu, 15 May 2025 18:27:28 +0100 Subject: [PATCH] Add: [Script] Default button in query string to reset an integer setting --- src/game/game_gui.cpp | 23 +++++++++++++++++++---- src/script/script_config.cpp | 10 ++++++++++ src/script/script_config.hpp | 5 +++++ src/script/script_gui.cpp | 23 +++++++++++++++++++---- 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/game/game_gui.cpp b/src/game/game_gui.cpp index 207a4c7daf..2b346c9b95 100644 --- a/src/game/game_gui.cpp +++ b/src/game/game_gui.cpp @@ -325,7 +325,7 @@ struct GSConfigWindow : public Window { } } else if (!bool_item && !config_item.complete_labels) { /* Display a query box so users can enter a custom value. */ - ShowQueryString(GetString(STR_JUST_INT, old_val), STR_CONFIG_SETTING_QUERY_CAPTION, INT32_DIGITS_WITH_SIGN_AND_TERMINATION, this, CS_NUMERAL_SIGNED, {}); + ShowQueryString(GetString(STR_JUST_INT, old_val), STR_CONFIG_SETTING_QUERY_CAPTION, INT32_DIGITS_WITH_SIGN_AND_TERMINATION, this, CS_NUMERAL_SIGNED, QueryStringFlag::EnableDefault); } this->SetDirty(); break; @@ -347,10 +347,17 @@ struct GSConfigWindow : public Window { void OnQueryTextFinished(std::optional str) override { + /* The user pressed cancel */ if (!str.has_value()) return; - auto value = ParseInteger(*str, 10, true); - if (!value.has_value()) return; - this->SetValue(*value); + + if (!str->empty()) { + auto value = ParseInteger(*str, 10, true); + if (!value.has_value()) return; + + this->SetValue(*value); + } else { + this->SetDefaultValue(); + } } void OnDropdownSelect(WidgetID widget, int index, int) override @@ -419,6 +426,14 @@ private: this->gs_config->SetSetting(config_item.name, value); this->SetDirty(); } + + void SetDefaultValue() + { + const ScriptConfigItem &config_item = *this->visible_settings[this->clicked_row]; + if (_game_mode == GM_NORMAL && !config_item.flags.Test(ScriptConfigFlag::InGame)) return; + this->gs_config->ResetSetting(config_item.name); + this->SetDirty(); + } }; /** Open the GS config window. */ diff --git a/src/script/script_config.cpp b/src/script/script_config.cpp index a599a7e30a..2348ea70f6 100644 --- a/src/script/script_config.cpp +++ b/src/script/script_config.cpp @@ -106,6 +106,16 @@ void ScriptConfig::ResetSettings() this->settings.clear(); } +void ScriptConfig::ResetSetting(std::string_view name) +{ + if (this->info == nullptr) return; + + const ScriptConfigItem *config_item = this->info->GetConfigItem(name); + if (config_item == nullptr) return; + + this->settings.erase(std::string{name}); +} + void ScriptConfig::ResetEditableSettings(bool yet_to_start) { if (this->info == nullptr) return ResetSettings(); diff --git a/src/script/script_config.hpp b/src/script/script_config.hpp index 76da137bcf..58250f9246 100644 --- a/src/script/script_config.hpp +++ b/src/script/script_config.hpp @@ -129,6 +129,11 @@ public: */ void ResetSettings(); + /** + * Reset a setting to its default value. + */ + void ResetSetting(std::string_view name); + /** * Reset only editable and visible settings to their default value. */ diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp index d2ef7819a9..5a75b969e6 100644 --- a/src/script/script_gui.cpp +++ b/src/script/script_gui.cpp @@ -470,7 +470,7 @@ struct ScriptSettingsWindow : public Window { } } else if (!bool_item && !config_item.complete_labels) { /* Display a query box so users can enter a custom value. */ - ShowQueryString(GetString(STR_JUST_INT, old_val), STR_CONFIG_SETTING_QUERY_CAPTION, INT32_DIGITS_WITH_SIGN_AND_TERMINATION, this, CS_NUMERAL_SIGNED, {}); + ShowQueryString(GetString(STR_JUST_INT, old_val), STR_CONFIG_SETTING_QUERY_CAPTION, INT32_DIGITS_WITH_SIGN_AND_TERMINATION, this, CS_NUMERAL_SIGNED, QueryStringFlag::EnableDefault); } this->SetDirty(); break; @@ -485,10 +485,17 @@ struct ScriptSettingsWindow : public Window { void OnQueryTextFinished(std::optional str) override { + /* The user pressed cancel */ if (!str.has_value()) return; - auto value = ParseInteger(*str, 10, true); - if (!value.has_value()) return; - this->SetValue(*value); + + if (!str->empty()) { + auto value = ParseInteger(*str, 10, true); + if (!value.has_value()) return; + + this->SetValue(*value); + } else { + this->SetDefaultValue(); + } } void OnDropdownSelect(WidgetID widget, int index, int) override @@ -552,6 +559,14 @@ private: this->script_config->SetSetting(config_item.name, value); this->SetDirty(); } + + void SetDefaultValue() + { + const ScriptConfigItem &config_item = *this->visible_settings[this->clicked_row]; + if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && !config_item.flags.Test(ScriptConfigFlag::InGame)) return; + this->script_config->ResetSetting(config_item.name); + this->SetDirty(); + } }; /** Widgets for the Script settings window. */