From ca783d447a831088e21c13919e4865353e98d54d Mon Sep 17 00:00:00 2001 From: Didac Perez Parera Date: Tue, 6 Apr 2021 03:47:44 -0700 Subject: Feature: Button to reset game settings to their default values (#8958) --- src/lang/english.txt | 3 +++ src/settings_gui.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++- src/widgets/settings_widget.h | 1 + 3 files changed, 55 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/lang/english.txt b/src/lang/english.txt index e55fcba1a..0f4b4a99d 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -1138,6 +1138,7 @@ STR_CONFIG_SETTING_TREE_CAPTION :{WHITE}Settings STR_CONFIG_SETTING_FILTER_TITLE :{BLACK}Filter string: STR_CONFIG_SETTING_EXPAND_ALL :{BLACK}Expand all STR_CONFIG_SETTING_COLLAPSE_ALL :{BLACK}Collapse all +STR_CONFIG_SETTING_RESET_ALL :{BLACK}Reset all values STR_CONFIG_SETTING_NO_EXPLANATION_AVAILABLE_HELPTEXT :(no explanation available) STR_CONFIG_SETTING_DEFAULT_VALUE :{LTBLUE}Default value: {ORANGE}{STRING1} STR_CONFIG_SETTING_TYPE :{LTBLUE}Setting type: {ORANGE}{STRING} @@ -1146,6 +1147,8 @@ STR_CONFIG_SETTING_TYPE_GAME_MENU :Game setting (s STR_CONFIG_SETTING_TYPE_GAME_INGAME :Game setting (stored in save; affects only current game) STR_CONFIG_SETTING_TYPE_COMPANY_MENU :Company setting (stored in saves; affects only new games) STR_CONFIG_SETTING_TYPE_COMPANY_INGAME :Company setting (stored in save; affects only current company) +STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_CAPTION :{WHITE}Caution! +STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_TEXT :{WHITE}This action will reset all game settings to their default values.{}Are you sure you want to proceed? STR_CONFIG_SETTING_RESTRICT_CATEGORY :{BLACK}Category: STR_CONFIG_SETTING_RESTRICT_TYPE :{BLACK}Type: diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index b75450877..68587c46a 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -733,6 +733,7 @@ struct BaseSettingEntry { virtual void Init(byte level = 0); virtual void FoldAll() {} virtual void UnFoldAll() {} + virtual void ResetAll() = 0; /** * Set whether this is the last visible entry of the parent node. @@ -769,6 +770,7 @@ struct SettingEntry : BaseSettingEntry { SettingEntry(const char *name); virtual void Init(byte level = 0); + virtual void ResetAll(); virtual uint Length() const; virtual uint GetMaxHelpHeight(int maxw); virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible); @@ -806,6 +808,7 @@ struct SettingsContainer { } void Init(byte level = 0); + void ResetAll(); void FoldAll(); void UnFoldAll(); @@ -828,6 +831,7 @@ struct SettingsPage : BaseSettingEntry, SettingsContainer { SettingsPage(StringID title); virtual void Init(byte level = 0); + virtual void ResetAll(); virtual void FoldAll(); virtual void UnFoldAll(); @@ -970,6 +974,13 @@ void SettingEntry::Init(byte level) assert(this->setting != nullptr); } +/* Sets the given setting entry to its default value */ +void SettingEntry::ResetAll() +{ + int32 default_value = ReadValue(&this->setting->desc.def, this->setting->save.conv); + SetSettingValue(this->index, default_value); +} + /** * Set the button-depressed flags (#SEF_LEFT_DEPRESSED and #SEF_RIGHT_DEPRESSED) to a specified value * @param new_val New value for the button flags @@ -1081,7 +1092,6 @@ bool SettingEntry::UpdateFilterState(SettingFilter &filter, bool force_visible) return visible; } - static const void *ResolveVariableAddress(const GameSettings *settings_ptr, const SettingDesc *sd) { if ((sd->desc.flags & SGF_PER_COMPANY) != 0) { @@ -1172,6 +1182,14 @@ void SettingsContainer::Init(byte level) } } +/** Resets all settings to their default values */ +void SettingsContainer::ResetAll() +{ + for (auto settings_entry : this->entries) { + settings_entry->ResetAll(); + } +} + /** Recursively close all folds of sub-pages */ void SettingsContainer::FoldAll() { @@ -1323,6 +1341,14 @@ void SettingsPage::Init(byte level) SettingsContainer::Init(level + 1); } +/** Resets all settings to their default values */ +void SettingsPage::ResetAll() +{ + for (auto settings_entry : this->entries) { + settings_entry->ResetAll(); + } +} + /** Recursively close all (filtered) folds of sub-pages */ void SettingsPage::FoldAll() { @@ -1794,6 +1820,20 @@ enum WarnHiddenResult { WHR_CATEGORY_TYPE, ///< Both category and type settings filtered matches away. }; +/** + * Callback function for the reset all settings button + * @param w Window which is calling this callback + * @param confirmed boolean value, true when yes was clicked, false otherwise + */ +static void ResetAllSettingsConfirmationCallback(Window *w, bool confirmed) +{ + if (confirmed) { + GetSettingsTree().ResetAll(); + GetSettingsTree().FoldAll(); + w->InvalidateData(); + } +} + /** Window to edit settings of the game. */ struct GameSettingsWindow : Window { static const int SETTINGTREE_LEFT_OFFSET = 5; ///< Position of left edge of setting values @@ -2031,6 +2071,15 @@ struct GameSettingsWindow : Window { this->InvalidateData(); break; + case WID_GS_RESET_ALL: + ShowQuery( + STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_CAPTION, + STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_TEXT, + this, + ResetAllSettingsConfirmationCallback + ); + break; + case WID_GS_RESTRICT_DROPDOWN: { DropDownList list = this->BuildDropDownList(widget); if (!list.empty()) { @@ -2387,6 +2436,7 @@ static const NWidgetPart _nested_settings_selection_widgets[] = { NWidget(NWID_HORIZONTAL), NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_GS_EXPAND_ALL), SetDataTip(STR_CONFIG_SETTING_EXPAND_ALL, STR_NULL), NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_GS_COLLAPSE_ALL), SetDataTip(STR_CONFIG_SETTING_COLLAPSE_ALL, STR_NULL), + NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_GS_RESET_ALL), SetDataTip(STR_CONFIG_SETTING_RESET_ALL, STR_NULL), NWidget(WWT_PANEL, COLOUR_MAUVE), SetFill(1, 0), SetResize(1, 0), EndContainer(), NWidget(WWT_RESIZEBOX, COLOUR_MAUVE), diff --git a/src/widgets/settings_widget.h b/src/widgets/settings_widget.h index 7f8b0e111..6cb2d7e94 100644 --- a/src/widgets/settings_widget.h +++ b/src/widgets/settings_widget.h @@ -44,6 +44,7 @@ enum GameSettingsWidgets { WID_GS_HELP_TEXT, ///< Information area to display help text of the selected option. WID_GS_EXPAND_ALL, ///< Expand all button. WID_GS_COLLAPSE_ALL, ///< Collapse all button. + WID_GS_RESET_ALL, ///< Reset all button. WID_GS_RESTRICT_CATEGORY, ///< Label upfront to the category drop-down box to restrict the list of settings to show WID_GS_RESTRICT_TYPE, ///< Label upfront to the type drop-down box to restrict the list of settings to show WID_GS_RESTRICT_DROPDOWN, ///< The drop down box to restrict the list of settings -- cgit v1.2.3-70-g09d2