Changeset - r28136:83ff15e9369b
[Not reviewed]
master
0 2 0
Peter Nelson - 12 months ago 2023-11-13 12:32:34
peter1138@openttd.org
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.
2 files changed with 52 insertions and 71 deletions:
0 comments (0 inline, 0 general)
src/autoreplace_gui.cpp
Show inline comments
 
@@ -270,6 +270,21 @@ class ReplaceVehicleWindow : public Wind
 
		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:
 
	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<NWidgetCore>(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));
 
				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<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();
 

	
 
		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);
 
			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,35 +667,22 @@ public:
 
				}
 
				break;
 

	
 
			case WID_RV_RAIL_ROAD_TYPE_DROPDOWN:
 
				switch (this->window_number) {
 
					case VEH_TRAIN: {
 
			case WID_RV_RAIL_TYPE_DROPDOWN: {
 
						RailType temp = (RailType)index;
 
						if (temp == sel_railtype) return; // we didn't select a new one. No need to change anything
 
						sel_railtype = temp;
 
				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 VEH_ROAD: {
 
			case WID_RV_ROAD_TYPE_DROPDOWN: {
 
						RoadType temp = (RoadType)index;
 
						if (temp == sel_roadtype) return; // we didn't select a new one. No need to change anything
 
						sel_roadtype = temp;
 
				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;
 
					}
 

	
 
					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;
 

	
 
			case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN: {
 
				this->replace_engines = index != 0;
 
				this->engines[0].ForceRebuild();
 
@@ -771,7 +751,7 @@ static const NWidgetPart _nested_replace
 
	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
 
	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),
src/widgets/autoreplace_widget.h
Show inline comments
 
@@ -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 */
0 comments (0 inline, 0 general)