From 81e5cd23e0a085596ae2bd1173dd9ea73bb7d924 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 2 Jun 2023 14:27:06 +0100 Subject: [PATCH] Codechange: Use window parent association for dropdowns. This replaces the separate window class and number properties, and allows the window system to close dropdowns automatically. --- src/ai/ai_gui.cpp | 6 +++--- src/game/game_gui.cpp | 6 +++--- src/group_gui.cpp | 4 ++-- src/newgrf_gui.cpp | 4 ++-- src/order_gui.cpp | 3 --- src/settings_gui.cpp | 2 +- src/vehicle_gui.cpp | 2 +- src/widgets/dropdown.cpp | 34 ++++++++++------------------------ 8 files changed, 22 insertions(+), 39 deletions(-) diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp index 4fe2f4510b..c43430e2a4 100644 --- a/src/ai/ai_gui.cpp +++ b/src/ai/ai_gui.cpp @@ -437,7 +437,7 @@ struct AISettingsWindow : public Window { if (this->clicked_row != num) { this->CloseChildWindows(WC_QUERY_STRING); - HideDropDownMenu(this); + this->CloseChildWindows(WC_DROPDOWN_MENU); this->clicked_row = num; this->clicked_dropdown = false; } @@ -452,7 +452,7 @@ struct AISettingsWindow : public Window { if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) { if (this->clicked_dropdown) { /* unclick the dropdown */ - HideDropDownMenu(this); + this->CloseChildWindows(WC_DROPDOWN_MENU); this->clicked_dropdown = false; this->closing_dropdown = false; } else { @@ -564,7 +564,7 @@ struct AISettingsWindow : public Window { void OnInvalidateData(int data = 0, bool gui_scope = true) override { this->RebuildVisibleSettings(); - HideDropDownMenu(this); + this->CloseChildWindows(WC_DROPDOWN_MENU); this->CloseChildWindows(WC_QUERY_STRING); } diff --git a/src/game/game_gui.cpp b/src/game/game_gui.cpp index dad7a71e41..08da5eba27 100644 --- a/src/game/game_gui.cpp +++ b/src/game/game_gui.cpp @@ -301,7 +301,7 @@ struct GSConfigWindow : public Window { if (this->clicked_row != num) { this->CloseChildWindows(WC_QUERY_STRING); - HideDropDownMenu(this); + this->CloseChildWindows(WC_DROPDOWN_MENU); this->clicked_row = num; this->clicked_dropdown = false; } @@ -316,7 +316,7 @@ struct GSConfigWindow : public Window { if (!bool_item && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && config_item.complete_labels) { if (this->clicked_dropdown) { /* unclick the dropdown */ - HideDropDownMenu(this); + this->CloseChildWindows(WC_DROPDOWN_MENU); this->clicked_dropdown = false; this->closing_dropdown = false; } else { @@ -434,7 +434,7 @@ struct GSConfigWindow : public Window { this->SetWidgetDisabledState(WID_GSC_TEXTFILE + tft, GameConfig::GetConfig()->GetTextfile(tft, (CompanyID)OWNER_DEITY) == nullptr); } this->RebuildVisibleSettings(); - HideDropDownMenu(this); + this->CloseChildWindows(WC_DROPDOWN_MENU); this->CloseChildWindows(WC_QUERY_STRING); } private: diff --git a/src/group_gui.cpp b/src/group_gui.cpp index 5766ce0380..bcf5c161ec 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -461,7 +461,7 @@ public: if (!(IsAllGroupID(this->vli.index) || IsDefaultGroupID(this->vli.index) || Group::IsValidID(this->vli.index))) { this->vli.index = ALL_GROUP; - HideDropDownMenu(this); + this->CloseChildWindows(WC_DROPDOWN_MENU); } this->SetDirty(); } @@ -512,7 +512,7 @@ public: /* The drop down menu is out, *but* it may not be used, retract it. */ if (this->vehicles.size() == 0 && this->IsWidgetLowered(WID_GL_MANAGE_VEHICLES_DROPDOWN)) { this->RaiseWidget(WID_GL_MANAGE_VEHICLES_DROPDOWN); - HideDropDownMenu(this); + this->CloseChildWindows(WC_DROPDOWN_MENU); } /* Disable all lists management button when the list is empty */ diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 223c1e21af..069ddf60bf 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -342,7 +342,7 @@ struct NewGRFParametersWindow : public Window { if (num >= this->vscroll->GetCount()) break; if (this->clicked_row != num) { this->CloseChildWindows(WC_QUERY_STRING); - HideDropDownMenu(this); + this->CloseChildWindows(WC_DROPDOWN_MENU); this->clicked_row = num; this->clicked_dropdown = false; } @@ -359,7 +359,7 @@ struct NewGRFParametersWindow : public Window { if (par_info->type != PTYPE_BOOL && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && par_info->complete_labels) { if (this->clicked_dropdown) { /* unclick the dropdown */ - HideDropDownMenu(this); + this->CloseChildWindows(WC_DROPDOWN_MENU); this->clicked_dropdown = false; this->closing_dropdown = false; } else { diff --git a/src/order_gui.cpp b/src/order_gui.cpp index 816a1b85b3..217f40d9cc 100644 --- a/src/order_gui.cpp +++ b/src/order_gui.cpp @@ -849,7 +849,6 @@ public: if (this->selected_order == -1) break; this->CloseChildWindows(); - HideDropDownMenu(this); this->selected_order = -1; break; @@ -881,7 +880,6 @@ public: if (to == INVALID_VEH_ORDER_ID) { /* Deleting selected order */ this->CloseChildWindows(); - HideDropDownMenu(this); this->selected_order = -1; break; } @@ -1181,7 +1179,6 @@ public: /* This order won't be selected any more, close all child windows and dropdowns */ this->CloseChildWindows(); - HideDropDownMenu(this); if (sel == INVALID_VEH_ORDER_ID || this->vehicle->owner != _local_company) { /* Deselect clicked order */ diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 5d14637e64..59a7a09f7d 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -2229,7 +2229,7 @@ struct GameSettingsWindow : Window { if (this->valuedropdown_entry == pe) { /* unclick the dropdown */ - HideDropDownMenu(this); + this->CloseChildWindows(WC_DROPDOWN_MENU); this->closing_dropdown = false; this->valuedropdown_entry->SetButtons(0); this->valuedropdown_entry = nullptr; diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 42633aff51..baa6f65bbb 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -1965,7 +1965,7 @@ public: this->SortVehicleList(); if (this->vehicles.size() == 0 && this->IsWidgetLowered(WID_VL_MANAGE_VEHICLES_DROPDOWN)) { - HideDropDownMenu(this); + this->CloseChildWindows(WC_DROPDOWN_MENU); } /* Hide the widgets that we will not use in this window diff --git a/src/widgets/dropdown.cpp b/src/widgets/dropdown.cpp index 8ac7c852a0..c7f57edc4b 100644 --- a/src/widgets/dropdown.cpp +++ b/src/widgets/dropdown.cpp @@ -119,8 +119,6 @@ static WindowDesc _dropdown_desc( /** Drop-down menu window */ struct DropdownWindow : Window { - WindowClass parent_wnd_class; ///< Parent window class. - WindowNumber parent_wnd_num; ///< Parent window number. int parent_button; ///< Parent widget number where the window is dropped from. const DropDownList list; ///< List with dropdown menu items. int selected_index; ///< Index of the selected item in the list. @@ -178,8 +176,7 @@ struct DropdownWindow : Window { this->vscroll->SetCapacity(size.height * (uint16)this->list.size() / list_height); this->vscroll->SetCount((uint16)this->list.size()); - this->parent_wnd_class = parent->window_class; - this->parent_wnd_num = parent->window_number; + this->parent = parent; this->parent_button = button; this->selected_index = selected; this->click_delay = 0; @@ -194,13 +191,10 @@ struct DropdownWindow : Window { * Also mark it dirty in case the callback deals with the screen. (e.g. screenshots). */ this->Window::Close(); - Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num); - if (w2 != nullptr) { - Point pt = _cursor.pos; - pt.x -= w2->left; - pt.y -= w2->top; - w2->OnDropdownClose(pt, this->parent_button, this->selected_index, this->instant_close); - } + Point pt = _cursor.pos; + pt.x -= this->parent->left; + pt.y -= this->parent->top; + this->parent->OnDropdownClose(pt, this->parent_button, this->selected_index, this->instant_close); } void OnFocusLost() override @@ -304,17 +298,11 @@ struct DropdownWindow : Window { void OnMouseLoop() override { - Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num); - if (w2 == nullptr) { - this->Close(); - return; - } - if (this->click_delay != 0 && --this->click_delay == 0) { /* Close the dropdown, so it doesn't affect new window placement. * Also mark it dirty in case the callback deals with the screen. (e.g. screenshots). */ this->Close(); - w2->OnDropdownSelect(this->parent_button, this->selected_index); + this->parent->OnDropdownSelect(this->parent_button, this->selected_index); return; } @@ -510,16 +498,14 @@ void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int butt int HideDropDownMenu(Window *pw) { for (Window *w : Window::Iterate()) { + if (w->parent != pw) continue; if (w->window_class != WC_DROPDOWN_MENU) continue; DropdownWindow *dw = dynamic_cast(w); assert(dw != nullptr); - if (pw->window_class == dw->parent_wnd_class && - pw->window_number == dw->parent_wnd_num) { - int parent_button = dw->parent_button; - dw->Close(); - return parent_button; - } + int parent_button = dw->parent_button; + dw->Close(); + return parent_button; } return -1;