From c877494f7aec2b7cdb74649ba5933484bd9081c0 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Mon, 13 Nov 2023 12:32:34 +0000 Subject: [PATCH] Codechange: Simplify autoreplace rail/road types by using separate widget. (#11457) This avoids needing to determine which type of list to deal with by additionally checking the window number for VEH_TRAIN/VEH_ROAD. --- src/autoreplace_gui.cpp | 134 +++++++++++++------------------ src/widgets/autoreplace_widget.h | 7 +- 2 files changed, 61 insertions(+), 80 deletions(-) diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp index a883cc3b8d..508a96433d 100644 --- a/src/autoreplace_gui.cpp +++ b/src/autoreplace_gui.cpp @@ -270,6 +270,21 @@ class ReplaceVehicleWindow : public Window { Command::Post(this->sel_group, veh_from, veh_to, replace_when_old); } + /** + * Perform tasks after rail or road type is changed. + */ + void OnRailRoadTypeChange() + { + /* Reset scrollbar positions */ + this->vscroll[0]->SetPosition(0); + this->vscroll[1]->SetPosition(0); + /* Rebuild the lists */ + this->engines[0].ForceRebuild(); + this->engines[1].ForceRebuild(); + this->reset_sel_engine = true; + this->SetDirty(); + } + public: ReplaceVehicleWindow(WindowDesc *desc, VehicleType vehicletype, GroupID id_g) : Window(desc) { @@ -294,11 +309,6 @@ public: widget->SetLowered(this->show_hidden_engines); this->FinishInitNested(vehicletype); - if (vehicletype == VEH_TRAIN || vehicletype == VEH_ROAD) { - widget = this->GetWidget(WID_RV_RAIL_ROAD_TYPE_DROPDOWN); - widget->tool_tip = STR_REPLACE_HELP_RAILTYPE + vehicletype; - } - this->sort_criteria = _engine_sort_last_criteria[vehicletype]; this->descending_sort_order = _engine_sort_last_order[vehicletype]; this->owner = _local_company; @@ -357,28 +367,21 @@ public: break; } - case WID_RV_RAIL_ROAD_TYPE_DROPDOWN: { + case WID_RV_RAIL_TYPE_DROPDOWN: { Dimension d = {0, 0}; - switch (this->window_number) { - case VEH_TRAIN: - for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) { - const RailTypeInfo *rti = GetRailTypeInfo(rt); - /* Skip rail type if it has no label */ - if (rti->label == 0) continue; - d = maxdim(d, GetStringBoundingBox(rti->strings.replace_text)); - } - break; + for (const RailType &rt : _sorted_railtypes) { + d = maxdim(d, GetStringBoundingBox(GetRailTypeInfo(rt)->strings.replace_text)); + } + d.width += padding.width; + d.height += padding.height; + *size = maxdim(*size, d); + break; + } - case VEH_ROAD: - for (RoadType rt = ROADTYPE_BEGIN; rt < ROADTYPE_END; rt++) { - const RoadTypeInfo *rti = GetRoadTypeInfo(rt); - /* Skip road type if it has no label */ - if (rti->label == 0) continue; - d = maxdim(d, GetStringBoundingBox(rti->strings.replace_text)); - } - break; - - default: NOT_REACHED(); + case WID_RV_ROAD_TYPE_DROPDOWN: { + Dimension d = {0, 0}; + for (const RoadType &rt : _sorted_roadtypes) { + d = maxdim(d, GetStringBoundingBox(GetRoadTypeInfo(rt)->strings.replace_text)); } d.width += padding.width; d.height += padding.height; @@ -443,6 +446,14 @@ public: case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN: SetDParam(0, this->replace_engines ? STR_REPLACE_ENGINES : STR_REPLACE_WAGONS); break; + + case WID_RV_RAIL_TYPE_DROPDOWN: + SetDParam(0, this->sel_railtype == INVALID_RAILTYPE ? STR_REPLACE_ALL_RAILTYPE : GetRailTypeInfo(this->sel_railtype)->strings.replace_text); + break; + + case WID_RV_ROAD_TYPE_DROPDOWN: + SetDParam(0, this->sel_roadtype == INVALID_ROADTYPE ? STR_REPLACE_ALL_ROADTYPE : GetRoadTypeInfo(this->sel_roadtype)->strings.replace_text); + break; } } @@ -503,20 +514,6 @@ public: * or The selected vehicle has no replacement set up */ this->SetWidgetDisabledState(WID_RV_STOP_REPLACE, this->sel_engine[0] == INVALID_ENGINE || !EngineHasReplacementForCompany(c, this->sel_engine[0], this->sel_group)); - switch (this->window_number) { - case VEH_TRAIN: - /* Show the selected railtype in the pulldown menu */ - this->GetWidget(WID_RV_RAIL_ROAD_TYPE_DROPDOWN)->widget_data = sel_railtype == INVALID_RAILTYPE ? STR_REPLACE_ALL_RAILTYPE : GetRailTypeInfo(sel_railtype)->strings.replace_text; - break; - - case VEH_ROAD: - /* Show the selected roadtype in the pulldown menu */ - this->GetWidget(WID_RV_RAIL_ROAD_TYPE_DROPDOWN)->widget_data = sel_roadtype == INVALID_ROADTYPE ? STR_REPLACE_ALL_ROADTYPE : GetRoadTypeInfo(sel_roadtype)->strings.replace_text; - break; - - default: break; - } - this->DrawWidgets(); if (!this->IsShaded()) { @@ -574,16 +571,12 @@ public: break; } - case WID_RV_RAIL_ROAD_TYPE_DROPDOWN: // Rail/roadtype selection dropdown menu - switch (this->window_number) { - case VEH_TRAIN: - ShowDropDownList(this, GetRailTypeDropDownList(true, true), sel_railtype, WID_RV_RAIL_ROAD_TYPE_DROPDOWN); - break; + case WID_RV_RAIL_TYPE_DROPDOWN: // Railtype selection dropdown menu + ShowDropDownList(this, GetRailTypeDropDownList(true, true), this->sel_railtype, widget); + break; - case VEH_ROAD: - ShowDropDownList(this, GetRoadTypeDropDownList(RTTB_ROAD | RTTB_TRAM, true, true), sel_roadtype, WID_RV_RAIL_ROAD_TYPE_DROPDOWN); - break; - } + case WID_RV_ROAD_TYPE_DROPDOWN: // Roadtype selection dropdown menu + ShowDropDownList(this, GetRoadTypeDropDownList(RTTB_ROAD | RTTB_TRAM, true, true), this->sel_roadtype, widget); break; case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: { @@ -674,34 +667,21 @@ public: } break; - case WID_RV_RAIL_ROAD_TYPE_DROPDOWN: - switch (this->window_number) { - case VEH_TRAIN: { - RailType temp = (RailType)index; - if (temp == sel_railtype) return; // we didn't select a new one. No need to change anything - sel_railtype = temp; - break; - } - - case VEH_ROAD: { - RoadType temp = (RoadType)index; - if (temp == sel_roadtype) return; // we didn't select a new one. No need to change anything - sel_roadtype = temp; - break; - } - - default: NOT_REACHED(); - } - - /* Reset scrollbar positions */ - this->vscroll[0]->SetPosition(0); - this->vscroll[1]->SetPosition(0); - /* Rebuild the lists */ - this->engines[0].ForceRebuild(); - this->engines[1].ForceRebuild(); - this->reset_sel_engine = true; - this->SetDirty(); + case WID_RV_RAIL_TYPE_DROPDOWN: { + RailType temp = (RailType)index; + if (temp == this->sel_railtype) return; // we didn't select a new one. No need to change anything + this->sel_railtype = temp; + this->OnRailRoadTypeChange(); break; + } + + case WID_RV_ROAD_TYPE_DROPDOWN: { + RoadType temp = (RoadType)index; + if (temp == this->sel_roadtype) return; // we didn't select a new one. No need to change anything + this->sel_roadtype = temp; + this->OnRailRoadTypeChange(); + break; + } case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN: { this->replace_engines = index != 0; @@ -771,7 +751,7 @@ static const NWidgetPart _nested_replace_rail_vehicle_widgets[] = { NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), NWidget(NWID_VERTICAL), NWidget(NWID_HORIZONTAL), - NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_RAIL_ROAD_TYPE_DROPDOWN), SetMinimalSize(136, 12), SetDataTip(0x0, STR_REPLACE_HELP_RAILTYPE), SetFill(1, 0), SetResize(1, 0), + NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_RAIL_TYPE_DROPDOWN), SetMinimalSize(136, 12), SetDataTip(STR_JUST_STRING, STR_REPLACE_HELP_RAILTYPE), SetFill(1, 0), SetResize(1, 0), NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_TRAIN_ENGINEWAGON_DROPDOWN), SetDataTip(STR_JUST_STRING, STR_REPLACE_ENGINE_WAGON_SELECT_HELP), EndContainer(), NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), EndContainer(), @@ -834,7 +814,7 @@ static const NWidgetPart _nested_replace_road_vehicle_widgets[] = { EndContainer(), NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), NWidget(NWID_VERTICAL), - NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_RAIL_ROAD_TYPE_DROPDOWN), SetMinimalSize(136, 12), SetDataTip(0x0, STR_REPLACE_HELP_RAILTYPE), SetFill(1, 0), SetResize(1, 0), + NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_ROAD_TYPE_DROPDOWN), SetMinimalSize(136, 12), SetDataTip(STR_JUST_STRING, STR_REPLACE_HELP_ROADTYPE), SetFill(1, 0), SetResize(1, 0), NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), EndContainer(), EndContainer(), NWidget(NWID_VERTICAL), diff --git a/src/widgets/autoreplace_widget.h b/src/widgets/autoreplace_widget.h index 3522dbc8d6..ab0b14aad2 100644 --- a/src/widgets/autoreplace_widget.h +++ b/src/widgets/autoreplace_widget.h @@ -32,12 +32,13 @@ enum ReplaceVehicleWidgets { WID_RV_INFO_TAB, ///< Info tab. WID_RV_STOP_REPLACE, ///< Stop Replacing button. - /* Train/road only widgets */ - WID_RV_RAIL_ROAD_TYPE_DROPDOWN, ///< Dropdown menu about the rail/roadtype. - /* Train only widgets. */ + WID_RV_RAIL_TYPE_DROPDOWN, ///< Dropdown to select railtype. WID_RV_TRAIN_ENGINEWAGON_DROPDOWN, ///< Dropdown to select engines and/or wagons. WID_RV_TRAIN_WAGONREMOVE_TOGGLE, ///< Button to toggle removing wagons. + + /* Road only widgets. */ + WID_RV_ROAD_TYPE_DROPDOWN, ///< Dropdown to select roadtype. }; #endif /* WIDGETS_AUTOREPLACE_WIDGET_H */