mirror of https://github.com/OpenTTD/OpenTTD
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.pull/11458/head
parent
7a6d102c4b
commit
c877494f7a
|
@ -270,6 +270,21 @@ class ReplaceVehicleWindow : public Window {
|
||||||
Command<CMD_SET_AUTOREPLACE>::Post(this->sel_group, veh_from, veh_to, replace_when_old);
|
Command<CMD_SET_AUTOREPLACE>::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:
|
public:
|
||||||
ReplaceVehicleWindow(WindowDesc *desc, VehicleType vehicletype, GroupID id_g) : Window(desc)
|
ReplaceVehicleWindow(WindowDesc *desc, VehicleType vehicletype, GroupID id_g) : Window(desc)
|
||||||
{
|
{
|
||||||
|
@ -294,11 +309,6 @@ public:
|
||||||
widget->SetLowered(this->show_hidden_engines);
|
widget->SetLowered(this->show_hidden_engines);
|
||||||
this->FinishInitNested(vehicletype);
|
this->FinishInitNested(vehicletype);
|
||||||
|
|
||||||
if (vehicletype == VEH_TRAIN || vehicletype == VEH_ROAD) {
|
|
||||||
widget = this->GetWidget<NWidgetCore>(WID_RV_RAIL_ROAD_TYPE_DROPDOWN);
|
|
||||||
widget->tool_tip = STR_REPLACE_HELP_RAILTYPE + vehicletype;
|
|
||||||
}
|
|
||||||
|
|
||||||
this->sort_criteria = _engine_sort_last_criteria[vehicletype];
|
this->sort_criteria = _engine_sort_last_criteria[vehicletype];
|
||||||
this->descending_sort_order = _engine_sort_last_order[vehicletype];
|
this->descending_sort_order = _engine_sort_last_order[vehicletype];
|
||||||
this->owner = _local_company;
|
this->owner = _local_company;
|
||||||
|
@ -357,28 +367,21 @@ public:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case WID_RV_RAIL_ROAD_TYPE_DROPDOWN: {
|
case WID_RV_RAIL_TYPE_DROPDOWN: {
|
||||||
Dimension d = {0, 0};
|
Dimension d = {0, 0};
|
||||||
switch (this->window_number) {
|
for (const RailType &rt : _sorted_railtypes) {
|
||||||
case VEH_TRAIN:
|
d = maxdim(d, GetStringBoundingBox(GetRailTypeInfo(rt)->strings.replace_text));
|
||||||
for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
|
}
|
||||||
const RailTypeInfo *rti = GetRailTypeInfo(rt);
|
d.width += padding.width;
|
||||||
/* Skip rail type if it has no label */
|
d.height += padding.height;
|
||||||
if (rti->label == 0) continue;
|
*size = maxdim(*size, d);
|
||||||
d = maxdim(d, GetStringBoundingBox(rti->strings.replace_text));
|
break;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
case VEH_ROAD:
|
case WID_RV_ROAD_TYPE_DROPDOWN: {
|
||||||
for (RoadType rt = ROADTYPE_BEGIN; rt < ROADTYPE_END; rt++) {
|
Dimension d = {0, 0};
|
||||||
const RoadTypeInfo *rti = GetRoadTypeInfo(rt);
|
for (const RoadType &rt : _sorted_roadtypes) {
|
||||||
/* Skip road type if it has no label */
|
d = maxdim(d, GetStringBoundingBox(GetRoadTypeInfo(rt)->strings.replace_text));
|
||||||
if (rti->label == 0) continue;
|
|
||||||
d = maxdim(d, GetStringBoundingBox(rti->strings.replace_text));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default: NOT_REACHED();
|
|
||||||
}
|
}
|
||||||
d.width += padding.width;
|
d.width += padding.width;
|
||||||
d.height += padding.height;
|
d.height += padding.height;
|
||||||
|
@ -443,6 +446,14 @@ public:
|
||||||
case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN:
|
case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN:
|
||||||
SetDParam(0, this->replace_engines ? STR_REPLACE_ENGINES : STR_REPLACE_WAGONS);
|
SetDParam(0, this->replace_engines ? STR_REPLACE_ENGINES : STR_REPLACE_WAGONS);
|
||||||
break;
|
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 */
|
* 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));
|
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<NWidgetCore>(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<NWidgetCore>(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();
|
this->DrawWidgets();
|
||||||
|
|
||||||
if (!this->IsShaded()) {
|
if (!this->IsShaded()) {
|
||||||
|
@ -574,16 +571,12 @@ public:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case WID_RV_RAIL_ROAD_TYPE_DROPDOWN: // Rail/roadtype selection dropdown menu
|
case WID_RV_RAIL_TYPE_DROPDOWN: // Railtype selection dropdown menu
|
||||||
switch (this->window_number) {
|
ShowDropDownList(this, GetRailTypeDropDownList(true, true), this->sel_railtype, widget);
|
||||||
case VEH_TRAIN:
|
break;
|
||||||
ShowDropDownList(this, GetRailTypeDropDownList(true, true), sel_railtype, WID_RV_RAIL_ROAD_TYPE_DROPDOWN);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case VEH_ROAD:
|
case WID_RV_ROAD_TYPE_DROPDOWN: // Roadtype selection dropdown menu
|
||||||
ShowDropDownList(this, GetRoadTypeDropDownList(RTTB_ROAD | RTTB_TRAM, true, true), sel_roadtype, WID_RV_RAIL_ROAD_TYPE_DROPDOWN);
|
ShowDropDownList(this, GetRoadTypeDropDownList(RTTB_ROAD | RTTB_TRAM, true, true), this->sel_roadtype, widget);
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: {
|
case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: {
|
||||||
|
@ -674,34 +667,21 @@ public:
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WID_RV_RAIL_ROAD_TYPE_DROPDOWN:
|
case WID_RV_RAIL_TYPE_DROPDOWN: {
|
||||||
switch (this->window_number) {
|
RailType temp = (RailType)index;
|
||||||
case VEH_TRAIN: {
|
if (temp == this->sel_railtype) return; // we didn't select a new one. No need to change anything
|
||||||
RailType temp = (RailType)index;
|
this->sel_railtype = temp;
|
||||||
if (temp == sel_railtype) return; // we didn't select a new one. No need to change anything
|
this->OnRailRoadTypeChange();
|
||||||
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();
|
|
||||||
break;
|
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: {
|
case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN: {
|
||||||
this->replace_engines = index != 0;
|
this->replace_engines = index != 0;
|
||||||
|
@ -771,7 +751,7 @@ static const NWidgetPart _nested_replace_rail_vehicle_widgets[] = {
|
||||||
NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
|
NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
|
||||||
NWidget(NWID_VERTICAL),
|
NWidget(NWID_VERTICAL),
|
||||||
NWidget(NWID_HORIZONTAL),
|
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),
|
NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_TRAIN_ENGINEWAGON_DROPDOWN), SetDataTip(STR_JUST_STRING, STR_REPLACE_ENGINE_WAGON_SELECT_HELP),
|
||||||
EndContainer(),
|
EndContainer(),
|
||||||
NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), EndContainer(),
|
NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), EndContainer(),
|
||||||
|
@ -834,7 +814,7 @@ static const NWidgetPart _nested_replace_road_vehicle_widgets[] = {
|
||||||
EndContainer(),
|
EndContainer(),
|
||||||
NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
|
NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
|
||||||
NWidget(NWID_VERTICAL),
|
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(),
|
NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), EndContainer(),
|
||||||
EndContainer(),
|
EndContainer(),
|
||||||
NWidget(NWID_VERTICAL),
|
NWidget(NWID_VERTICAL),
|
||||||
|
|
|
@ -32,12 +32,13 @@ enum ReplaceVehicleWidgets {
|
||||||
WID_RV_INFO_TAB, ///< Info tab.
|
WID_RV_INFO_TAB, ///< Info tab.
|
||||||
WID_RV_STOP_REPLACE, ///< Stop Replacing button.
|
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. */
|
/* 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_ENGINEWAGON_DROPDOWN, ///< Dropdown to select engines and/or wagons.
|
||||||
WID_RV_TRAIN_WAGONREMOVE_TOGGLE, ///< Button to toggle removing 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 */
|
#endif /* WIDGETS_AUTOREPLACE_WIDGET_H */
|
||||||
|
|
Loading…
Reference in New Issue