mirror of https://github.com/OpenTTD/OpenTTD.git
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.
This commit is contained in:
parent
dec7ff6b0c
commit
2511649938
|
@ -276,7 +276,7 @@ struct GSConfigWindow : public Window {
|
|||
int num = it - this->visible_settings.begin();
|
||||
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;
|
||||
}
|
||||
|
@ -292,7 +292,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 {
|
||||
|
@ -408,7 +408,7 @@ struct GSConfigWindow : public Window {
|
|||
this->SetWidgetDisabledState(WID_GSC_TEXTFILE + tft, !GameConfig::GetConfig()->GetTextfile(tft, (CompanyID)OWNER_DEITY).has_value());
|
||||
}
|
||||
this->RebuildVisibleSettings();
|
||||
HideDropDownMenu(this);
|
||||
this->CloseChildWindows(WC_DROPDOWN_MENU);
|
||||
this->CloseChildWindows(WC_QUERY_STRING);
|
||||
}
|
||||
private:
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -363,7 +363,7 @@ struct NewGRFParametersWindow : public Window {
|
|||
uint num = it - this->grf_config->param_info.begin();
|
||||
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;
|
||||
}
|
||||
|
@ -379,7 +379,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 {
|
||||
|
|
|
@ -854,7 +854,6 @@ public:
|
|||
if (this->selected_order == -1) break;
|
||||
|
||||
this->CloseChildWindows();
|
||||
HideDropDownMenu(this);
|
||||
this->selected_order = -1;
|
||||
break;
|
||||
|
||||
|
@ -886,7 +885,6 @@ public:
|
|||
if (to == INVALID_VEH_ORDER_ID) {
|
||||
/* Deleting selected order */
|
||||
this->CloseChildWindows();
|
||||
HideDropDownMenu(this);
|
||||
this->selected_order = -1;
|
||||
break;
|
||||
}
|
||||
|
@ -1186,7 +1184,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 */
|
||||
|
|
|
@ -434,7 +434,7 @@ struct ScriptSettingsWindow : public Window {
|
|||
int num = it - this->visible_settings.begin();
|
||||
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;
|
||||
}
|
||||
|
@ -450,7 +450,7 @@ struct ScriptSettingsWindow : 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 {
|
||||
|
@ -560,7 +560,7 @@ struct ScriptSettingsWindow : 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);
|
||||
}
|
||||
|
||||
|
|
|
@ -2349,7 +2349,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;
|
||||
|
|
|
@ -1966,7 +1966,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
|
||||
|
|
|
@ -117,8 +117,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.
|
||||
|
@ -175,8 +173,7 @@ struct DropdownWindow : Window {
|
|||
this->vscroll->SetCapacity(size.height * this->list.size() / list_height);
|
||||
this->vscroll->SetCount(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;
|
||||
|
@ -190,13 +187,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);
|
||||
}
|
||||
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
|
||||
|
@ -292,17 +286,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;
|
||||
}
|
||||
|
||||
|
@ -495,17 +483,15 @@ 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<DropdownWindow*>(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;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue