From f40816503f59a3541a65686f3279c7e71bfcb8be Mon Sep 17 00:00:00 2001 From: frosch Date: Wed, 20 Sep 2023 21:26:42 +0200 Subject: [PATCH] Codechange: Add enum items for dynmically created setting dropdowns. --- src/game/game_gui.cpp | 6 +++--- src/newgrf_gui.cpp | 6 +++--- src/script/script_gui.cpp | 6 +++--- src/settings_gui.cpp | 20 +++++++++----------- src/widgets/game_widget.h | 2 ++ src/widgets/newgrf_widget.h | 2 ++ src/widgets/script_widget.h | 2 ++ src/widgets/settings_widget.h | 2 ++ 8 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/game/game_gui.cpp b/src/game/game_gui.cpp index fd9e2c9957..faca456f13 100644 --- a/src/game/game_gui.cpp +++ b/src/game/game_gui.cpp @@ -314,7 +314,7 @@ struct GSConfigWindow : public Window { list.emplace_back(new DropDownListStringItem(config_item.labels.find(i)->second, i, false)); } - ShowDropDownListAt(this, std::move(list), old_val, -1, wi_rect, COLOUR_ORANGE); + ShowDropDownListAt(this, std::move(list), old_val, WID_GSC_SETTING_DROPDOWN, wi_rect, COLOUR_ORANGE); } } } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) { @@ -367,14 +367,14 @@ struct GSConfigWindow : public Window { void OnDropdownSelect(int widget, int index) override { - if (widget >= 0) return; + if (widget != WID_GSC_SETTING_DROPDOWN) return; assert(this->clicked_dropdown); SetValue(index); } void OnDropdownClose(Point, int widget, int, bool) override { - if (widget >= 0) return; + if (widget != WID_GSC_SETTING_DROPDOWN) return; /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether * the same dropdown button was clicked again, and then not open the dropdown again. * So, we only remember that it was closed, and process it on the next OnPaint, which is diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 8ecdb1d657..9aab5e9b6f 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -395,7 +395,7 @@ struct NewGRFParametersWindow : public Window { list.emplace_back(new DropDownListStringItem(GetGRFStringFromGRFText(par_info.value_names.find(i)->second), i, false)); } - ShowDropDownListAt(this, std::move(list), old_val, -1, wi_rect, COLOUR_ORANGE); + ShowDropDownListAt(this, std::move(list), old_val, WID_NP_SETTING_DROPDOWN, wi_rect, COLOUR_ORANGE); } } } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) { @@ -453,7 +453,7 @@ struct NewGRFParametersWindow : public Window { void OnDropdownSelect(int widget, int index) override { - if (widget >= 0) return; + if (widget != WID_NP_SETTING_DROPDOWN) return; assert(this->clicked_dropdown); GRFParameterInfo &par_info = this->GetParameterInfo(this->clicked_row); par_info.SetValue(this->grf_config, index); @@ -462,7 +462,7 @@ struct NewGRFParametersWindow : public Window { void OnDropdownClose(Point, int widget, int, bool) override { - if (widget >= 0) return; + if (widget != WID_NP_SETTING_DROPDOWN) return; /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether * the same dropdown button was clicked again, and then not open the dropdown again. * So, we only remember that it was closed, and process it on the next OnPaint, which is diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp index 91e2f47e04..21033cbf9c 100644 --- a/src/script/script_gui.cpp +++ b/src/script/script_gui.cpp @@ -472,7 +472,7 @@ struct ScriptSettingsWindow : public Window { list.emplace_back(new DropDownListStringItem(config_item.labels.find(i)->second, i, false)); } - ShowDropDownListAt(this, std::move(list), old_val, -1, wi_rect, COLOUR_ORANGE); + ShowDropDownListAt(this, std::move(list), old_val, WID_SCRS_SETTING_DROPDOWN, wi_rect, COLOUR_ORANGE); } } } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) { @@ -526,14 +526,14 @@ struct ScriptSettingsWindow : public Window { void OnDropdownSelect(int widget, int index) override { - if (widget >= 0) return; + if (widget != WID_SCRS_SETTING_DROPDOWN) return; assert(this->clicked_dropdown); SetValue(index); } void OnDropdownClose(Point, int widget, int, bool) override { - if (widget >= 0) return; + if (widget != WID_SCRS_SETTING_DROPDOWN) return; /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether * the same dropdown button was clicked again, and then not open the dropdown again. * So, we only remember that it was closed, and process it on the next OnPaint, which is diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index e6fba68826..fd81ce74a1 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -2394,7 +2394,7 @@ struct GameSettingsWindow : Window { list.emplace_back(new DropDownListStringItem(sd->str_val + i - sd->min, i, false)); } - ShowDropDownListAt(this, std::move(list), value, -1, wi_rect, COLOUR_ORANGE); + ShowDropDownListAt(this, std::move(list), value, WID_GS_SETTING_DROPDOWN, wi_rect, COLOUR_ORANGE); } } this->SetDirty(); @@ -2526,23 +2526,21 @@ struct GameSettingsWindow : Window { this->InvalidateData(); break; - default: - if (widget < 0) { - /* Deal with drop down boxes on the panel. */ - assert(this->valuedropdown_entry != nullptr); - const IntSettingDesc *sd = this->valuedropdown_entry->setting; - assert(sd->flags & SF_GUI_DROPDOWN); + case WID_GS_SETTING_DROPDOWN: + /* Deal with drop down boxes on the panel. */ + assert(this->valuedropdown_entry != nullptr); + const IntSettingDesc *sd = this->valuedropdown_entry->setting; + assert(sd->flags & SF_GUI_DROPDOWN); - SetSettingValue(sd, index); - this->SetDirty(); - } + SetSettingValue(sd, index); + this->SetDirty(); break; } } void OnDropdownClose(Point pt, int widget, int index, bool instant_close) override { - if (widget >= 0) { + if (widget != WID_GS_SETTING_DROPDOWN) { /* Normally the default implementation of OnDropdownClose() takes care of * a few things. We want that behaviour here too, but only for * "normal" dropdown boxes. The special dropdown boxes added for every diff --git a/src/widgets/game_widget.h b/src/widgets/game_widget.h index 5694bc4607..6ab78fa8e9 100644 --- a/src/widgets/game_widget.h +++ b/src/widgets/game_widget.h @@ -23,6 +23,8 @@ enum GSConfigWidgets { WID_GSC_CONTENT_DOWNLOAD = WID_GSC_TEXTFILE + TFT_CONTENT_END, ///< Download content button. WID_GSC_ACCEPT, ///< Accept ("Close") button WID_GSC_RESET, ///< Reset button. + + WID_GSC_SETTING_DROPDOWN = -1, ///< Dynamically created dropdown for changing setting value. }; #endif /* WIDGETS_GS_WIDGET_H */ diff --git a/src/widgets/newgrf_widget.h b/src/widgets/newgrf_widget.h index 03b81bb584..2d39665447 100644 --- a/src/widgets/newgrf_widget.h +++ b/src/widgets/newgrf_widget.h @@ -26,6 +26,8 @@ enum NewGRFParametersWidgets { WID_NP_RESET, ///< Reset button. WID_NP_SHOW_DESCRIPTION, ///< #NWID_SELECTION to optionally display parameter descriptions. WID_NP_DESCRIPTION, ///< Multi-line description of a parameter. + + WID_NP_SETTING_DROPDOWN = -1, ///< Dynamically created dropdown for changing setting value. }; /** Widgets of the #NewGRFWindow class. */ diff --git a/src/widgets/script_widget.h b/src/widgets/script_widget.h index 9c757e12fa..0566995f8d 100644 --- a/src/widgets/script_widget.h +++ b/src/widgets/script_widget.h @@ -29,6 +29,8 @@ enum ScriptSettingsWidgets { WID_SCRS_SCROLLBAR, ///< Scrollbar to scroll through all settings. WID_SCRS_ACCEPT, ///< Accept button. WID_SCRS_RESET, ///< Reset button. + + WID_SCRS_SETTING_DROPDOWN = -1, ///< Dynamically created dropdown for changing setting value. }; /** Widgets of the #ScriptDebugWindow class. */ diff --git a/src/widgets/settings_widget.h b/src/widgets/settings_widget.h index 5f372eaec5..f358e6ccda 100644 --- a/src/widgets/settings_widget.h +++ b/src/widgets/settings_widget.h @@ -64,6 +64,8 @@ enum GameSettingsWidgets { 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 WID_GS_TYPE_DROPDOWN, ///< The drop down box to choose client/game/company/all settings + + WID_GS_SETTING_DROPDOWN = -1, ///< Dynamically created dropdown for changing setting value. }; /** Widgets of the #CustomCurrencyWindow class. */