2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file autoreplace_gui.cpp GUI for autoreplace handling. */
11 #include "command_func.h"
12 #include "vehicle_gui.h"
13 #include "newgrf_engine.h"
16 #include "strings_func.h"
17 #include "window_func.h"
18 #include "autoreplace_func.h"
19 #include "company_func.h"
20 #include "engine_base.h"
21 #include "window_gui.h"
22 #include "engine_gui.h"
23 #include "settings_func.h"
24 #include "core/geometry_func.hpp"
27 #include "widgets/dropdown_func.h"
29 #include "widgets/autoreplace_widget.h"
31 #include "safeguards.h"
33 void DrawEngineList(VehicleType type
, int x
, int r
, int y
, const GUIEngineList
*eng_list
, uint16 min
, uint16 max
, EngineID selected_id
, bool show_count
, GroupID selected_group
);
35 static bool EngineNumberSorter(const EngineID
&a
, const EngineID
&b
)
37 return Engine::Get(a
)->list_position
< Engine::Get(b
)->list_position
;
41 * Rebuild the left autoreplace list if an engine is removed or added
42 * @param e Engine to check if it is removed or added
43 * @param id_g The group the engine belongs to
44 * Note: this function only works if it is called either
45 * - when a new vehicle is build, but before it's counted in num_engines
46 * - when a vehicle is deleted and after it's subtracted from num_engines
47 * - when not changing the count (used when changing replace orders)
49 void InvalidateAutoreplaceWindow(EngineID e
, GroupID id_g
)
51 if (GetGroupNumEngines(_local_company
, id_g
, e
) == 0 || GetGroupNumEngines(_local_company
, ALL_GROUP
, e
) == 0) {
52 /* We don't have any of this engine type.
53 * Either we just sold the last one, we build a new one or we stopped replacing it.
54 * In all cases, we need to update the left list */
55 InvalidateWindowData(WC_REPLACE_VEHICLE
, Engine::Get(e
)->type
, 1);
60 * When an engine is made buildable or is removed from being buildable, add/remove it from the build/autoreplace lists
61 * @param type The type of engine
63 void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type
)
65 InvalidateWindowData(WC_REPLACE_VEHICLE
, type
, 0); // Update the autoreplace window
66 InvalidateWindowClassesData(WC_BUILD_VEHICLE
); // The build windows needs updating as well
69 static const StringID _start_replace_dropdown
[] = {
70 STR_REPLACE_VEHICLES_NOW
,
71 STR_REPLACE_VEHICLES_WHEN_OLD
,
76 * Window for the autoreplacing of vehicles.
78 class ReplaceVehicleWindow
: public Window
{
79 EngineID sel_engine
[2]; ///< Selected engine left and right.
80 GUIEngineList engines
[2]; ///< Left and right list of engines.
81 bool replace_engines
; ///< If \c true, engines are replaced, if \c false, wagons are replaced (only for trains).
82 bool reset_sel_engine
; ///< Also reset #sel_engine while updating left and/or right and no valid engine selected.
83 GroupID sel_group
; ///< Group selected to replace.
84 int details_height
; ///< Minimal needed height of the details panels (found so far).
85 byte sort_criteria
; ///< Criteria of sorting vehicles.
86 bool descending_sort_order
; ///< Order of sorting vehicles.
87 bool show_hidden_engines
; ///< Whether to show the hidden engines.
88 RailType sel_railtype
; ///< Type of rail tracks selected. #INVALID_RAILTYPE to show all.
89 RoadType sel_roadtype
; ///< Type of road selected. #INVALID_ROADTYPE to show all.
90 Scrollbar
*vscroll
[2];
93 * Figure out if an engine should be added to a list.
94 * @param e The EngineID.
95 * @param draw_left If \c true, the left list is drawn (the engines specific to the railtype you selected).
96 * @param show_engines If \c true, the locomotives are drawn, else the wagons are drawn (never both).
97 * @return \c true if the engine should be in the list (based on this check), else \c false.
99 bool GenerateReplaceRailList(EngineID e
, bool draw_left
, bool show_engines
)
101 const RailVehicleInfo
*rvi
= RailVehInfo(e
);
103 /* Ensure that the wagon/engine selection fits the engine. */
104 if ((rvi
->railveh_type
== RAILVEH_WAGON
) == show_engines
) return false;
106 if (draw_left
&& this->sel_railtype
!= INVALID_RAILTYPE
) {
107 /* Ensure that the railtype is specific to the selected one */
108 if (rvi
->railtype
!= this->sel_railtype
) return false;
115 * Generate an engines list
116 * @param draw_left true if generating the left list, otherwise false
118 void GenerateReplaceVehList(bool draw_left
)
120 EngineID selected_engine
= INVALID_ENGINE
;
121 VehicleType type
= (VehicleType
)this->window_number
;
122 byte side
= draw_left
? 0 : 1;
124 GUIEngineList
*list
= &this->engines
[side
];
127 for (const Engine
*e
: Engine::IterateType(type
)) {
128 if (!draw_left
&& !this->show_hidden_engines
&& e
->IsHidden(_local_company
)) continue;
129 EngineID eid
= e
->index
;
132 if (!this->GenerateReplaceRailList(eid
, draw_left
, this->replace_engines
)) continue; // special rules for trains
136 if (draw_left
&& this->sel_roadtype
!= INVALID_ROADTYPE
) {
137 /* Ensure that the roadtype is specific to the selected one */
138 if (e
->u
.road
.roadtype
!= this->sel_roadtype
) continue;
147 const uint num_engines
= GetGroupNumEngines(_local_company
, this->sel_group
, eid
);
149 /* Skip drawing the engines we don't have any of and haven't set for replacement */
150 if (num_engines
== 0 && EngineReplacementForCompany(Company::Get(_local_company
), eid
, this->sel_group
) == INVALID_ENGINE
) continue;
152 if (!CheckAutoreplaceValidity(this->sel_engine
[0], eid
, _local_company
)) continue;
155 list
->push_back(eid
);
156 if (eid
== this->sel_engine
[side
]) selected_engine
= eid
; // The selected engine is still in the list
158 this->sel_engine
[side
] = selected_engine
; // update which engine we selected (the same or none, if it's not in the list anymore)
160 EngList_Sort(list
, &EngineNumberSorter
);
162 _engine_sort_direction
= this->descending_sort_order
;
163 EngList_Sort(list
, _engine_sort_functions
[this->window_number
][this->sort_criteria
]);
167 /** Generate the lists */
170 EngineID e
= this->sel_engine
[0];
172 if (this->engines
[0].NeedRebuild()) {
173 /* We need to rebuild the left engines list */
174 this->GenerateReplaceVehList(true);
175 this->vscroll
[0]->SetCount((uint
)this->engines
[0].size());
176 if (this->reset_sel_engine
&& this->sel_engine
[0] == INVALID_ENGINE
&& this->engines
[0].size() != 0) {
177 this->sel_engine
[0] = this->engines
[0][0];
181 if (this->engines
[1].NeedRebuild() || e
!= this->sel_engine
[0]) {
182 /* Either we got a request to rebuild the right engines list, or the left engines list selected a different engine */
183 if (this->sel_engine
[0] == INVALID_ENGINE
) {
184 /* Always empty the right engines list when nothing is selected in the left engines list */
185 this->engines
[1].clear();
186 this->sel_engine
[1] = INVALID_ENGINE
;
188 if (this->reset_sel_engine
&& this->sel_engine
[0] != INVALID_ENGINE
) {
189 /* Select the current replacement for sel_engine[0]. */
190 const Company
*c
= Company::Get(_local_company
);
191 this->sel_engine
[1] = EngineReplacementForCompany(c
, this->sel_engine
[0], this->sel_group
);
193 /* Regenerate the list on the right. Note: This resets sel_engine[1] to INVALID_ENGINE, if it is no longer available. */
194 this->GenerateReplaceVehList(false);
195 this->vscroll
[1]->SetCount((uint
)this->engines
[1].size());
196 if (this->reset_sel_engine
&& this->sel_engine
[1] != INVALID_ENGINE
) {
198 for (EngineID
&eid
: this->engines
[1]) {
199 if (eid
== this->sel_engine
[1]) break;
202 this->vscroll
[1]->ScrollTowards(position
);
206 /* Reset the flags about needed updates */
207 this->engines
[0].RebuildDone();
208 this->engines
[1].RebuildDone();
209 this->reset_sel_engine
= false;
213 * Handle click on the start replace button.
214 * @param replace_when_old Replace now or only when old?
216 void ReplaceClick_StartReplace(bool replace_when_old
)
218 EngineID veh_from
= this->sel_engine
[0];
219 EngineID veh_to
= this->sel_engine
[1];
220 DoCommandP(0, (replace_when_old
? 1 : 0) | (this->sel_group
<< 16), veh_from
+ (veh_to
<< 16), CMD_SET_AUTOREPLACE
);
224 ReplaceVehicleWindow(WindowDesc
*desc
, VehicleType vehicletype
, GroupID id_g
) : Window(desc
)
226 this->sel_railtype
= INVALID_RAILTYPE
;
227 this->sel_roadtype
= INVALID_ROADTYPE
;
228 this->replace_engines
= true; // start with locomotives (all other vehicles will not read this bool)
229 this->engines
[0].ForceRebuild();
230 this->engines
[1].ForceRebuild();
231 this->reset_sel_engine
= true;
232 this->details_height
= ((vehicletype
== VEH_TRAIN
) ? 10 : 9) * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
233 this->sel_engine
[0] = INVALID_ENGINE
;
234 this->sel_engine
[1] = INVALID_ENGINE
;
235 this->show_hidden_engines
= _engine_sort_show_hidden_engines
[vehicletype
];
237 this->CreateNestedTree();
238 this->vscroll
[0] = this->GetScrollbar(WID_RV_LEFT_SCROLLBAR
);
239 this->vscroll
[1] = this->GetScrollbar(WID_RV_RIGHT_SCROLLBAR
);
241 NWidgetCore
*widget
= this->GetWidget
<NWidgetCore
>(WID_RV_SHOW_HIDDEN_ENGINES
);
242 widget
->widget_data
= STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN
+ vehicletype
;
243 widget
->tool_tip
= STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP
+ vehicletype
;
244 widget
->SetLowered(this->show_hidden_engines
);
245 this->FinishInitNested(vehicletype
);
247 if (vehicletype
== VEH_TRAIN
|| vehicletype
== VEH_ROAD
) {
248 widget
= this->GetWidget
<NWidgetCore
>(WID_RV_RAIL_ROAD_TYPE_DROPDOWN
);
249 widget
->tool_tip
= STR_REPLACE_HELP_RAILTYPE
+ vehicletype
;
252 this->sort_criteria
= _engine_sort_last_criteria
[vehicletype
];
253 this->descending_sort_order
= _engine_sort_last_order
[vehicletype
];
254 this->owner
= _local_company
;
255 this->sel_group
= id_g
;
258 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
261 case WID_RV_SORT_ASCENDING_DESCENDING
: {
262 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
263 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
264 d
.height
+= padding
.height
;
265 *size
= maxdim(*size
, d
);
269 case WID_RV_LEFT_MATRIX
:
270 case WID_RV_RIGHT_MATRIX
:
271 resize
->height
= GetEngineListHeight((VehicleType
)this->window_number
);
272 size
->height
= (this->window_number
<= VEH_ROAD
? 8 : 4) * resize
->height
;
275 case WID_RV_LEFT_DETAILS
:
276 case WID_RV_RIGHT_DETAILS
:
277 size
->height
= this->details_height
;
280 case WID_RV_TRAIN_WAGONREMOVE_TOGGLE
: {
281 StringID str
= this->GetWidget
<NWidgetCore
>(widget
)->widget_data
;
282 SetDParam(0, STR_CONFIG_SETTING_ON
);
283 Dimension d
= GetStringBoundingBox(str
);
284 SetDParam(0, STR_CONFIG_SETTING_OFF
);
285 d
= maxdim(d
, GetStringBoundingBox(str
));
286 d
.width
+= padding
.width
;
287 d
.height
+= padding
.height
;
288 *size
= maxdim(*size
, d
);
292 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN
: {
293 Dimension d
= GetStringBoundingBox(STR_REPLACE_ENGINES
);
294 d
= maxdim(d
, GetStringBoundingBox(STR_REPLACE_WAGONS
));
295 d
.width
+= padding
.width
;
296 d
.height
+= padding
.height
;
297 *size
= maxdim(*size
, d
);
301 case WID_RV_INFO_TAB
: {
302 Dimension d
= GetStringBoundingBox(STR_REPLACE_NOT_REPLACING
);
303 d
= maxdim(d
, GetStringBoundingBox(STR_REPLACE_NOT_REPLACING_VEHICLE_SELECTED
));
304 d
.width
+= WD_FRAMETEXT_LEFT
+ WD_FRAMETEXT_RIGHT
;
305 d
.height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
306 *size
= maxdim(*size
, d
);
310 case WID_RV_RAIL_ROAD_TYPE_DROPDOWN
: {
311 Dimension d
= {0, 0};
312 switch (this->window_number
) {
314 for (RailType rt
= RAILTYPE_BEGIN
; rt
!= RAILTYPE_END
; rt
++) {
315 const RailtypeInfo
*rti
= GetRailTypeInfo(rt
);
316 /* Skip rail type if it has no label */
317 if (rti
->label
== 0) continue;
318 d
= maxdim(d
, GetStringBoundingBox(rti
->strings
.replace_text
));
323 for (RoadType rt
= ROADTYPE_BEGIN
; rt
< ROADTYPE_END
; rt
++) {
324 const RoadTypeInfo
*rti
= GetRoadTypeInfo(rt
);
325 /* Skip road type if it has no label */
326 if (rti
->label
== 0) continue;
327 d
= maxdim(d
, GetStringBoundingBox(rti
->strings
.replace_text
));
331 default: NOT_REACHED();
333 d
.width
+= padding
.width
;
334 d
.height
+= padding
.height
;
335 *size
= maxdim(*size
, d
);
339 case WID_RV_START_REPLACE
: {
340 Dimension d
= GetStringBoundingBox(STR_REPLACE_VEHICLES_START
);
341 for (int i
= 0; _start_replace_dropdown
[i
] != INVALID_STRING_ID
; i
++) {
342 d
= maxdim(d
, GetStringBoundingBox(_start_replace_dropdown
[i
]));
344 d
.width
+= padding
.width
;
345 d
.height
+= padding
.height
;
346 *size
= maxdim(*size
, d
);
352 void SetStringParameters(int widget
) const override
356 SetDParam(0, STR_REPLACE_VEHICLE_TRAIN
+ this->window_number
);
357 switch (this->sel_group
) {
359 SetDParam(1, STR_GROUP_ALL_TRAINS
+ this->window_number
);
363 SetDParam(1, STR_GROUP_DEFAULT_TRAINS
+ this->window_number
);
367 SetDParam(1, STR_GROUP_NAME
);
368 SetDParam(2, sel_group
);
373 case WID_RV_SORT_DROPDOWN
:
374 SetDParam(0, _engine_sort_listing
[this->window_number
][this->sort_criteria
]);
377 case WID_RV_TRAIN_WAGONREMOVE_TOGGLE
: {
378 const Company
*c
= Company::Get(_local_company
);
379 SetDParam(0, c
->settings
.renew_keep_length
? STR_CONFIG_SETTING_ON
: STR_CONFIG_SETTING_OFF
);
383 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN
:
384 SetDParam(0, this->replace_engines
? STR_REPLACE_ENGINES
: STR_REPLACE_WAGONS
);
389 void DrawWidget(const Rect
&r
, int widget
) const override
392 case WID_RV_SORT_ASCENDING_DESCENDING
:
393 this->DrawSortButtonState(WID_RV_SORT_ASCENDING_DESCENDING
, this->descending_sort_order
? SBS_DOWN
: SBS_UP
);
396 case WID_RV_INFO_TAB
: {
397 const Company
*c
= Company::Get(_local_company
);
399 if (this->sel_engine
[0] != INVALID_ENGINE
) {
400 if (!EngineHasReplacementForCompany(c
, this->sel_engine
[0], this->sel_group
)) {
401 str
= STR_REPLACE_NOT_REPLACING
;
403 bool when_old
= false;
404 EngineID e
= EngineReplacementForCompany(c
, this->sel_engine
[0], this->sel_group
, &when_old
);
405 str
= when_old
? STR_REPLACE_REPLACING_WHEN_OLD
: STR_ENGINE_NAME
;
409 str
= STR_REPLACE_NOT_REPLACING_VEHICLE_SELECTED
;
412 DrawString(r
.left
+ WD_FRAMETEXT_LEFT
, r
.right
- WD_FRAMETEXT_RIGHT
, r
.top
+ WD_FRAMERECT_TOP
, str
, TC_BLACK
, SA_HOR_CENTER
);
416 case WID_RV_LEFT_MATRIX
:
417 case WID_RV_RIGHT_MATRIX
: {
418 int side
= (widget
== WID_RV_LEFT_MATRIX
) ? 0 : 1;
419 EngineID start
= this->vscroll
[side
]->GetPosition(); // what is the offset for the start (scrolling)
420 EngineID end
= min(this->vscroll
[side
]->GetCapacity() + start
, (uint
)this->engines
[side
].size());
422 /* Do the actual drawing */
423 DrawEngineList((VehicleType
)this->window_number
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, r
.top
+ WD_FRAMERECT_TOP
,
424 &this->engines
[side
], start
, end
, this->sel_engine
[side
], side
== 0, this->sel_group
);
430 void OnPaint() override
432 if (this->engines
[0].NeedRebuild() || this->engines
[1].NeedRebuild()) this->GenerateLists();
434 Company
*c
= Company::Get(_local_company
);
436 /* Disable the "Start Replacing" button if:
437 * Either engines list is empty
438 * or The selected replacement engine has a replacement (to prevent loops). */
439 this->SetWidgetDisabledState(WID_RV_START_REPLACE
,
440 this->sel_engine
[0] == INVALID_ENGINE
|| this->sel_engine
[1] == INVALID_ENGINE
|| EngineReplacementForCompany(c
, this->sel_engine
[1], this->sel_group
) != INVALID_ENGINE
);
442 /* Disable the "Stop Replacing" button if:
443 * The left engines list (existing vehicle) is empty
444 * or The selected vehicle has no replacement set up */
445 this->SetWidgetDisabledState(WID_RV_STOP_REPLACE
, this->sel_engine
[0] == INVALID_ENGINE
|| !EngineHasReplacementForCompany(c
, this->sel_engine
[0], this->sel_group
));
447 switch (this->window_number
) {
449 /* Show the selected railtype in the pulldown menu */
450 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
;
454 /* Show the selected roadtype in the pulldown menu */
455 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
;
463 if (!this->IsShaded()) {
464 int needed_height
= this->details_height
;
465 /* Draw details panels. */
466 for (int side
= 0; side
< 2; side
++) {
467 if (this->sel_engine
[side
] != INVALID_ENGINE
) {
468 /* Use default engine details without refitting */
469 const Engine
*e
= Engine::Get(this->sel_engine
[side
]);
470 TestedEngineDetails ted
;
472 ted
.cargo
= e
->GetDefaultCargoType();
473 ted
.capacity
= e
->GetDisplayDefaultCapacity(&ted
.mail_capacity
);
475 NWidgetBase
*nwi
= this->GetWidget
<NWidgetBase
>(side
== 0 ? WID_RV_LEFT_DETAILS
: WID_RV_RIGHT_DETAILS
);
476 int text_end
= DrawVehiclePurchaseInfo(nwi
->pos_x
+ WD_FRAMETEXT_LEFT
, nwi
->pos_x
+ nwi
->current_x
- WD_FRAMETEXT_RIGHT
,
477 nwi
->pos_y
+ WD_FRAMERECT_TOP
, this->sel_engine
[side
], ted
);
478 needed_height
= max(needed_height
, text_end
- (int)nwi
->pos_y
+ WD_FRAMERECT_BOTTOM
);
481 if (needed_height
!= this->details_height
) { // Details window are not high enough, enlarge them.
482 this->details_height
= needed_height
;
489 void OnClick(Point pt
, int widget
, int click_count
) override
492 case WID_RV_SORT_ASCENDING_DESCENDING
:
493 this->descending_sort_order
^= true;
494 _engine_sort_last_order
[this->window_number
] = this->descending_sort_order
;
495 this->engines
[1].ForceRebuild();
499 case WID_RV_SHOW_HIDDEN_ENGINES
:
500 this->show_hidden_engines
^= true;
501 _engine_sort_show_hidden_engines
[this->window_number
] = this->show_hidden_engines
;
502 this->engines
[1].ForceRebuild();
503 this->SetWidgetLoweredState(widget
, this->show_hidden_engines
);
507 case WID_RV_SORT_DROPDOWN
:
508 DisplayVehicleSortDropDown(this, static_cast<VehicleType
>(this->window_number
), this->sort_criteria
, WID_RV_SORT_DROPDOWN
);
511 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN
: {
513 list
.emplace_back(new DropDownListStringItem(STR_REPLACE_ENGINES
, 1, false));
514 list
.emplace_back(new DropDownListStringItem(STR_REPLACE_WAGONS
, 0, false));
515 ShowDropDownList(this, std::move(list
), this->replace_engines
? 1 : 0, WID_RV_TRAIN_ENGINEWAGON_DROPDOWN
);
519 case WID_RV_RAIL_ROAD_TYPE_DROPDOWN
: // Rail/roadtype selection dropdown menu
520 switch (this->window_number
) {
522 ShowDropDownList(this, GetRailTypeDropDownList(true, true), sel_railtype
, WID_RV_RAIL_ROAD_TYPE_DROPDOWN
);
526 ShowDropDownList(this, GetRoadTypeDropDownList(RTTB_ROAD
| RTTB_TRAM
, true, true), sel_roadtype
, WID_RV_RAIL_ROAD_TYPE_DROPDOWN
);
531 case WID_RV_TRAIN_WAGONREMOVE_TOGGLE
: // toggle renew_keep_length
532 DoCommandP(0, GetCompanySettingIndex("company.renew_keep_length"), Company::Get(_local_company
)->settings
.renew_keep_length
? 0 : 1, CMD_CHANGE_COMPANY_SETTING
);
535 case WID_RV_START_REPLACE
: { // Start replacing
536 if (this->GetWidget
<NWidgetLeaf
>(widget
)->ButtonHit(pt
)) {
537 this->HandleButtonClick(WID_RV_START_REPLACE
);
538 ReplaceClick_StartReplace(false);
540 bool replacment_when_old
= EngineHasReplacementWhenOldForCompany(Company::Get(_local_company
), this->sel_engine
[0], this->sel_group
);
541 ShowDropDownMenu(this, _start_replace_dropdown
, replacment_when_old
? 1 : 0, WID_RV_START_REPLACE
, !this->replace_engines
? 1 << 1 : 0, 0);
546 case WID_RV_STOP_REPLACE
: { // Stop replacing
547 EngineID veh_from
= this->sel_engine
[0];
548 DoCommandP(0, this->sel_group
<< 16, veh_from
+ (INVALID_ENGINE
<< 16), CMD_SET_AUTOREPLACE
);
552 case WID_RV_LEFT_MATRIX
:
553 case WID_RV_RIGHT_MATRIX
: {
555 if (widget
== WID_RV_LEFT_MATRIX
) {
560 uint i
= this->vscroll
[click_side
]->GetScrolledRowFromWidget(pt
.y
, this, widget
);
561 size_t engine_count
= this->engines
[click_side
].size();
563 EngineID e
= engine_count
> i
? this->engines
[click_side
][i
] : INVALID_ENGINE
;
564 if (e
== this->sel_engine
[click_side
]) break; // we clicked the one we already selected
565 this->sel_engine
[click_side
] = e
;
566 if (click_side
== 0) {
567 this->engines
[1].ForceRebuild();
568 this->reset_sel_engine
= true;
576 void OnDropdownSelect(int widget
, int index
) override
579 case WID_RV_SORT_DROPDOWN
:
580 if (this->sort_criteria
!= index
) {
581 this->sort_criteria
= index
;
582 _engine_sort_last_criteria
[this->window_number
] = this->sort_criteria
;
583 this->engines
[1].ForceRebuild();
588 case WID_RV_RAIL_ROAD_TYPE_DROPDOWN
:
589 switch (this->window_number
) {
591 RailType temp
= (RailType
)index
;
592 if (temp
== sel_railtype
) return; // we didn't select a new one. No need to change anything
598 RoadType temp
= (RoadType
)index
;
599 if (temp
== sel_roadtype
) return; // we didn't select a new one. No need to change anything
604 default: NOT_REACHED();
607 /* Reset scrollbar positions */
608 this->vscroll
[0]->SetPosition(0);
609 this->vscroll
[1]->SetPosition(0);
610 /* Rebuild the lists */
611 this->engines
[0].ForceRebuild();
612 this->engines
[1].ForceRebuild();
613 this->reset_sel_engine
= true;
617 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN
: {
618 this->replace_engines
= index
!= 0;
619 this->engines
[0].ForceRebuild();
620 this->reset_sel_engine
= true;
625 case WID_RV_START_REPLACE
:
626 this->ReplaceClick_StartReplace(index
!= 0);
631 void OnResize() override
633 this->vscroll
[0]->SetCapacityFromWidget(this, WID_RV_LEFT_MATRIX
);
634 this->vscroll
[1]->SetCapacityFromWidget(this, WID_RV_RIGHT_MATRIX
);
638 * Some data on this window has become invalid.
639 * @param data Information about the changed data.
640 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
642 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
645 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
646 this->engines
[0].ForceRebuild();
648 this->engines
[1].ForceRebuild();
653 static const NWidgetPart _nested_replace_rail_vehicle_widgets
[] = {
654 NWidget(NWID_HORIZONTAL
),
655 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
656 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_RV_CAPTION
), SetDataTip(STR_REPLACE_VEHICLES_WHITE
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
657 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
658 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
659 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
661 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
662 NWidget(WWT_PANEL
, COLOUR_GREY
),
663 NWidget(WWT_LABEL
, COLOUR_GREY
), SetDataTip(STR_REPLACE_VEHICLE_VEHICLES_IN_USE
, STR_REPLACE_VEHICLE_VEHICLES_IN_USE_TOOLTIP
), SetFill(1, 1), SetMinimalSize(0, 12), SetResize(1, 0),
665 NWidget(WWT_PANEL
, COLOUR_GREY
),
666 NWidget(WWT_LABEL
, COLOUR_GREY
), SetDataTip(STR_REPLACE_VEHICLE_AVAILABLE_VEHICLES
, STR_REPLACE_VEHICLE_AVAILABLE_VEHICLES_TOOLTIP
), SetFill(1, 1), SetMinimalSize(0, 12), SetResize(1, 0),
669 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
670 NWidget(NWID_VERTICAL
),
671 NWidget(NWID_HORIZONTAL
),
672 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),
673 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_RV_TRAIN_ENGINEWAGON_DROPDOWN
), SetDataTip(STR_BLACK_STRING
, STR_REPLACE_ENGINE_WAGON_SELECT_HELP
),
675 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), EndContainer(),
677 NWidget(NWID_VERTICAL
),
678 NWidget(NWID_HORIZONTAL
),
679 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_RV_SORT_ASCENDING_DESCENDING
), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
), SetFill(1, 1),
680 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_RV_SORT_DROPDOWN
), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_SORT_CRITERIA
),
682 NWidget(NWID_HORIZONTAL
),
683 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_RV_SHOW_HIDDEN_ENGINES
), SetDataTip(STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN
, STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP
),
684 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), SetFill(1, 1), EndContainer(),
688 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
689 NWidget(WWT_MATRIX
, COLOUR_GREY
, WID_RV_LEFT_MATRIX
), SetMinimalSize(216, 0), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_REPLACE_HELP_LEFT_ARRAY
), SetResize(1, 1), SetScrollbar(WID_RV_LEFT_SCROLLBAR
),
690 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_RV_LEFT_SCROLLBAR
),
691 NWidget(WWT_MATRIX
, COLOUR_GREY
, WID_RV_RIGHT_MATRIX
), SetMinimalSize(216, 0), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_REPLACE_HELP_RIGHT_ARRAY
), SetResize(1, 1), SetScrollbar(WID_RV_RIGHT_SCROLLBAR
),
692 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_RV_RIGHT_SCROLLBAR
),
694 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
695 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_LEFT_DETAILS
), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
696 NWidget(NWID_VERTICAL
),
697 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_RIGHT_DETAILS
), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
698 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_RV_TRAIN_WAGONREMOVE_TOGGLE
), SetMinimalSize(138, 12), SetDataTip(STR_REPLACE_REMOVE_WAGON
, STR_REPLACE_REMOVE_WAGON_HELP
), SetFill(1, 0), SetResize(1, 0),
701 NWidget(NWID_HORIZONTAL
),
702 NWidget(NWID_PUSHBUTTON_DROPDOWN
, COLOUR_GREY
, WID_RV_START_REPLACE
), SetMinimalSize(139, 12), SetDataTip(STR_REPLACE_VEHICLES_START
, STR_REPLACE_HELP_START_BUTTON
),
703 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_INFO_TAB
), SetMinimalSize(167, 12), SetDataTip(0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB
), SetResize(1, 0),
705 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_RV_STOP_REPLACE
), SetMinimalSize(150, 12), SetDataTip(STR_REPLACE_VEHICLES_STOP
, STR_REPLACE_HELP_STOP_BUTTON
),
706 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
710 static WindowDesc
_replace_rail_vehicle_desc(
711 WDP_AUTO
, "replace_vehicle_train", 500, 140,
712 WC_REPLACE_VEHICLE
, WC_NONE
,
714 _nested_replace_rail_vehicle_widgets
, lengthof(_nested_replace_rail_vehicle_widgets
)
717 static const NWidgetPart _nested_replace_road_vehicle_widgets
[] = {
718 NWidget(NWID_HORIZONTAL
),
719 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
720 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_RV_CAPTION
), SetDataTip(STR_REPLACE_VEHICLES_WHITE
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
721 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
722 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
723 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
725 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
726 NWidget(WWT_PANEL
, COLOUR_GREY
),
727 NWidget(WWT_LABEL
, COLOUR_GREY
), SetDataTip(STR_REPLACE_VEHICLE_VEHICLES_IN_USE
, STR_REPLACE_VEHICLE_VEHICLES_IN_USE_TOOLTIP
), SetFill(1, 1), SetMinimalSize(0, 12), SetResize(1, 0),
729 NWidget(WWT_PANEL
, COLOUR_GREY
),
730 NWidget(WWT_LABEL
, COLOUR_GREY
), SetDataTip(STR_REPLACE_VEHICLE_AVAILABLE_VEHICLES
, STR_REPLACE_VEHICLE_AVAILABLE_VEHICLES_TOOLTIP
), SetFill(1, 1), SetMinimalSize(0, 12), SetResize(1, 0),
733 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
734 NWidget(NWID_VERTICAL
),
735 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),
736 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), EndContainer(),
738 NWidget(NWID_VERTICAL
),
739 NWidget(NWID_HORIZONTAL
),
740 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_RV_SORT_ASCENDING_DESCENDING
), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
), SetFill(1, 1),
741 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_RV_SORT_DROPDOWN
), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_SORT_CRITERIA
),
743 NWidget(NWID_HORIZONTAL
),
744 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_RV_SHOW_HIDDEN_ENGINES
), SetDataTip(STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN
, STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP
),
745 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), SetFill(1, 1), EndContainer(),
749 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
750 NWidget(WWT_MATRIX
, COLOUR_GREY
, WID_RV_LEFT_MATRIX
), SetMinimalSize(216, 0), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_REPLACE_HELP_LEFT_ARRAY
), SetResize(1, 1), SetScrollbar(WID_RV_LEFT_SCROLLBAR
),
751 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_RV_LEFT_SCROLLBAR
),
752 NWidget(WWT_MATRIX
, COLOUR_GREY
, WID_RV_RIGHT_MATRIX
), SetMinimalSize(216, 0), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_REPLACE_HELP_RIGHT_ARRAY
), SetResize(1, 1), SetScrollbar(WID_RV_RIGHT_SCROLLBAR
),
753 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_RV_RIGHT_SCROLLBAR
),
755 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
756 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_LEFT_DETAILS
), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
757 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_RIGHT_DETAILS
), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
759 NWidget(NWID_HORIZONTAL
),
760 NWidget(NWID_PUSHBUTTON_DROPDOWN
, COLOUR_GREY
, WID_RV_START_REPLACE
), SetMinimalSize(139, 12), SetDataTip(STR_REPLACE_VEHICLES_START
, STR_REPLACE_HELP_START_BUTTON
),
761 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_INFO_TAB
), SetMinimalSize(167, 12), SetDataTip(0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB
), SetResize(1, 0),
763 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_RV_STOP_REPLACE
), SetMinimalSize(150, 12), SetDataTip(STR_REPLACE_VEHICLES_STOP
, STR_REPLACE_HELP_STOP_BUTTON
),
764 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
768 static WindowDesc
_replace_road_vehicle_desc(
769 WDP_AUTO
, "replace_vehicle_road", 500, 140,
770 WC_REPLACE_VEHICLE
, WC_NONE
,
772 _nested_replace_road_vehicle_widgets
, lengthof(_nested_replace_road_vehicle_widgets
)
775 static const NWidgetPart _nested_replace_vehicle_widgets
[] = {
776 NWidget(NWID_HORIZONTAL
),
777 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
778 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_RV_CAPTION
), SetMinimalSize(433, 14), SetDataTip(STR_REPLACE_VEHICLES_WHITE
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
779 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
780 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
781 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
783 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
784 NWidget(WWT_PANEL
, COLOUR_GREY
),
785 NWidget(WWT_LABEL
, COLOUR_GREY
), SetDataTip(STR_REPLACE_VEHICLE_VEHICLES_IN_USE
, STR_REPLACE_VEHICLE_VEHICLES_IN_USE_TOOLTIP
), SetFill(1, 1), SetMinimalSize(0, 12), SetResize(1, 0),
787 NWidget(WWT_PANEL
, COLOUR_GREY
),
788 NWidget(WWT_LABEL
, COLOUR_GREY
), SetDataTip(STR_REPLACE_VEHICLE_AVAILABLE_VEHICLES
, STR_REPLACE_VEHICLE_AVAILABLE_VEHICLES_TOOLTIP
), SetFill(1, 1), SetMinimalSize(0, 12), SetResize(1, 0),
791 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
792 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), EndContainer(),
793 NWidget(NWID_VERTICAL
),
794 NWidget(NWID_HORIZONTAL
),
795 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_RV_SORT_ASCENDING_DESCENDING
), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
796 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_RV_SORT_DROPDOWN
), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_SORT_CRITERIA
),
798 NWidget(NWID_HORIZONTAL
),
799 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_RV_SHOW_HIDDEN_ENGINES
), SetDataTip(STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN
, STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP
),
800 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), SetFill(1, 1), EndContainer(),
804 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
805 NWidget(WWT_MATRIX
, COLOUR_GREY
, WID_RV_LEFT_MATRIX
), SetMinimalSize(216, 0), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_REPLACE_HELP_LEFT_ARRAY
), SetResize(1, 1), SetScrollbar(WID_RV_LEFT_SCROLLBAR
),
806 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_RV_LEFT_SCROLLBAR
),
807 NWidget(WWT_MATRIX
, COLOUR_GREY
, WID_RV_RIGHT_MATRIX
), SetMinimalSize(216, 0), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_REPLACE_HELP_RIGHT_ARRAY
), SetResize(1, 1), SetScrollbar(WID_RV_RIGHT_SCROLLBAR
),
808 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_RV_RIGHT_SCROLLBAR
),
810 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
811 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_LEFT_DETAILS
), SetMinimalSize(228, 92), SetResize(1, 0), EndContainer(),
812 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_RIGHT_DETAILS
), SetMinimalSize(228, 92), SetResize(1, 0), EndContainer(),
814 NWidget(NWID_HORIZONTAL
),
815 NWidget(NWID_PUSHBUTTON_DROPDOWN
, COLOUR_GREY
, WID_RV_START_REPLACE
), SetMinimalSize(139, 12), SetDataTip(STR_REPLACE_VEHICLES_START
, STR_REPLACE_HELP_START_BUTTON
),
816 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_INFO_TAB
), SetMinimalSize(167, 12), SetDataTip(0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB
), SetResize(1, 0), EndContainer(),
817 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_RV_STOP_REPLACE
), SetMinimalSize(138, 12), SetDataTip(STR_REPLACE_VEHICLES_STOP
, STR_REPLACE_HELP_STOP_BUTTON
),
818 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
822 static WindowDesc
_replace_vehicle_desc(
823 WDP_AUTO
, "replace_vehicle", 456, 118,
824 WC_REPLACE_VEHICLE
, WC_NONE
,
826 _nested_replace_vehicle_widgets
, lengthof(_nested_replace_vehicle_widgets
)
830 * Show the autoreplace configuration window for a particular group.
831 * @param id_g The group to replace the vehicles for.
832 * @param vehicletype The type of vehicles in the group.
834 void ShowReplaceGroupVehicleWindow(GroupID id_g
, VehicleType vehicletype
)
836 DeleteWindowById(WC_REPLACE_VEHICLE
, vehicletype
);
838 switch (vehicletype
) {
839 case VEH_TRAIN
: desc
= &_replace_rail_vehicle_desc
; break;
840 case VEH_ROAD
: desc
= &_replace_road_vehicle_desc
; break;
841 default: desc
= &_replace_vehicle_desc
; break;
843 new ReplaceVehicleWindow(desc
, vehicletype
, id_g
);