4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file autoreplace_gui.cpp GUI for autoreplace handling. */
13 #include "command_func.h"
14 #include "vehicle_gui.h"
15 #include "newgrf_engine.h"
17 #include "strings_func.h"
18 #include "window_func.h"
19 #include "autoreplace_func.h"
20 #include "company_func.h"
21 #include "engine_base.h"
22 #include "window_gui.h"
23 #include "engine_gui.h"
24 #include "settings_func.h"
25 #include "core/geometry_func.hpp"
27 #include "widgets/dropdown_func.h"
29 #include "widgets/autoreplace_widget.h"
31 void DrawEngineList (VehicleType type
, BlitArea
*dpi
, int x
, int r
, int y
,
32 const EngineID
*eng
, uint num
,
33 EngineID selected_id
, bool show_count
, GroupID selected_group
);
35 static int CDECL
EngineNumberSorter(const EngineID
*a
, const EngineID
*b
)
37 int r
= Engine::Get(*a
)->list_position
- Engine::Get(*b
)->list_position
;
43 * Rebuild the left autoreplace list if an engine is removed or added
44 * @param e Engine to check if it is removed or added
45 * @param id_g The group the engine belongs to
46 * Note: this function only works if it is called either
47 * - when a new vehicle is build, but before it's counted in num_engines
48 * - when a vehicle is deleted and after it's subtracted from num_engines
49 * - when not changing the count (used when changing replace orders)
51 void InvalidateAutoreplaceWindow(EngineID e
, GroupID id_g
)
53 if (GetGroupNumEngines(_local_company
, id_g
, e
) == 0 || GetGroupNumEngines(_local_company
, ALL_GROUP
, e
) == 0) {
54 /* We don't have any of this engine type.
55 * Either we just sold the last one, we build a new one or we stopped replacing it.
56 * In all cases, we need to update the left list */
57 InvalidateWindowData(WC_REPLACE_VEHICLE
, Engine::Get(e
)->type
, 1);
62 * When an engine is made buildable or is removed from being buildable, add/remove it from the build/autoreplace lists
63 * @param type The type of engine
65 void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type
)
67 InvalidateWindowData(WC_REPLACE_VEHICLE
, type
, 0); // Update the autoreplace window
68 InvalidateWindowClassesData(WC_BUILD_VEHICLE
); // The build windows needs updating as well
71 static const StringID _start_replace_dropdown
[] = {
72 STR_REPLACE_VEHICLES_NOW
,
73 STR_REPLACE_VEHICLES_WHEN_OLD
,
78 * Window for the autoreplacing of vehicles.
80 class ReplaceVehicleWindow
: public Window
{
81 typedef GUIList
<EngineID
> GUIEngineList
;
83 EngineID sel_engine
[2]; ///< Selected engine left and right.
84 GUIEngineList engines
[2]; ///< Left and right list of engines.
85 bool replace_engines
; ///< If \c true, engines are replaced, if \c false, wagons are replaced (only for trains).
86 bool reset_sel_engine
; ///< Also reset #sel_engine while updating left and/or right (#update_left and/or #update_right) and no valid engine selected.
87 GroupID sel_group
; ///< Group selected to replace.
88 int details_height
; ///< Minimal needed height of the details panels (found so far).
89 byte sort_criteria
; ///< Criteria of sorting vehicles.
90 bool descending_sort_order
; ///< Order of sorting vehicles.
91 bool show_hidden_engines
; ///< Whether to show the hidden engines.
92 RailType sel_railtype
; ///< Type of rail tracks selected. #INVALID_RAILTYPE to show all.
93 Scrollbar
*vscroll
[2];
96 * Figure out if an engine should be added to a list.
97 * @param e The EngineID.
98 * @param draw_left If \c true, the left list is drawn (the engines specific to the railtype you selected).
99 * @param show_engines If \c true, the locomotives are drawn, else the wagons are drawn (never both).
100 * @return \c true if the engine should be in the list (based on this check), else \c false.
102 bool GenerateReplaceRailList(EngineID e
, bool draw_left
, bool show_engines
)
104 const RailVehicleInfo
*rvi
= RailVehInfo(e
);
106 /* Ensure that the wagon/engine selection fits the engine. */
107 if ((rvi
->railveh_type
== RAILVEH_WAGON
) == show_engines
) return false;
109 if (draw_left
&& this->sel_railtype
!= INVALID_RAILTYPE
) {
110 /* Ensure that the railtype is specific to the selected one */
111 if (rvi
->railtype
!= this->sel_railtype
) return false;
118 * Generate an engines list
119 * @param draw_left true if generating the left list, otherwise false
121 void GenerateReplaceVehList(bool draw_left
)
123 EngineID selected_engine
= INVALID_ENGINE
;
124 VehicleType type
= (VehicleType
)this->window_number
;
125 byte side
= draw_left
? 0 : 1;
127 GUIEngineList
*list
= &this->engines
[side
];
131 FOR_ALL_ENGINES_OF_TYPE(e
, type
) {
132 if (!draw_left
&& !this->show_hidden_engines
&& e
->IsHidden(_local_company
)) continue;
133 EngineID eid
= e
->index
;
134 if (type
== VEH_TRAIN
&& !this->GenerateReplaceRailList(eid
, draw_left
, this->replace_engines
)) continue; // special rules for trains
137 const uint num_engines
= GetGroupNumEngines(_local_company
, this->sel_group
, eid
);
139 /* Skip drawing the engines we don't have any of and haven't set for replacement */
140 if (num_engines
== 0 && EngineReplacementForCompany(Company::Get(_local_company
), eid
, this->sel_group
) == INVALID_ENGINE
) continue;
142 if (!CheckAutoreplaceValidity(this->sel_engine
[0], eid
, _local_company
)) continue;
145 *list
->Append() = eid
;
146 if (eid
== this->sel_engine
[side
]) selected_engine
= eid
; // The selected engine is still in the list
148 this->sel_engine
[side
] = selected_engine
; // update which engine we selected (the same or none, if it's not in the list anymore)
150 list
->SmallVector
<EngineID
, 32>::Sort (&EngineNumberSorter
);
152 _engine_sort_direction
= this->descending_sort_order
;
153 list
->SmallVector
<EngineID
, 32>::Sort (_engine_sort_functions
[this->window_number
][this->sort_criteria
]);
157 /** Generate the lists */
160 EngineID e
= this->sel_engine
[0];
162 if (this->engines
[0].NeedRebuild()) {
163 /* We need to rebuild the left engines list */
164 this->GenerateReplaceVehList(true);
165 this->vscroll
[0]->SetCount(this->engines
[0].Length());
166 if (this->reset_sel_engine
&& this->sel_engine
[0] == INVALID_ENGINE
&& this->engines
[0].Length() != 0) {
167 this->sel_engine
[0] = this->engines
[0][0];
171 if (this->engines
[1].NeedRebuild() || e
!= this->sel_engine
[0]) {
172 /* Either we got a request to rebuild the right engines list, or the left engines list selected a different engine */
173 if (this->sel_engine
[0] == INVALID_ENGINE
) {
174 /* Always empty the right engines list when nothing is selected in the left engines list */
175 this->engines
[1].Clear();
176 this->sel_engine
[1] = INVALID_ENGINE
;
178 if (this->reset_sel_engine
&& this->sel_engine
[0] != INVALID_ENGINE
) {
179 /* Select the current replacement for sel_engine[0]. */
180 const Company
*c
= Company::Get(_local_company
);
181 this->sel_engine
[1] = EngineReplacementForCompany(c
, this->sel_engine
[0], this->sel_group
);
183 /* Regenerate the list on the right. Note: This resets sel_engine[1] to INVALID_ENGINE, if it is no longer available. */
184 this->GenerateReplaceVehList(false);
185 this->vscroll
[1]->SetCount(this->engines
[1].Length());
186 if (this->reset_sel_engine
&& this->sel_engine
[1] != INVALID_ENGINE
) {
188 for (EngineID
*it
= this->engines
[1].Begin(); it
!= this->engines
[1].End(); ++it
) {
189 if (*it
== this->sel_engine
[1]) break;
192 this->vscroll
[1]->ScrollTowards(position
);
196 /* Reset the flags about needed updates */
197 this->engines
[0].RebuildDone();
198 this->engines
[1].RebuildDone();
199 this->reset_sel_engine
= false;
203 * Handle click on the start replace button.
204 * @param replace_when_old Replace now or only when old?
206 void ReplaceClick_StartReplace(bool replace_when_old
)
208 EngineID veh_from
= this->sel_engine
[0];
209 EngineID veh_to
= this->sel_engine
[1];
210 DoCommandP(0, (replace_when_old
? 1 : 0) | (this->sel_group
<< 16), veh_from
+ (veh_to
<< 16), CMD_SET_AUTOREPLACE
);
214 * Handle click on the engine/wagon toggle.
215 * @param engines Whether to select engines or wagons.
217 void ReplaceClick_EngineWagonToggle (bool engines
)
219 this->replace_engines
= engines
;
220 this->engines
[0].ForceRebuild();
221 this->reset_sel_engine
= true;
226 ReplaceVehicleWindow (const WindowDesc
*desc
, VehicleType vehicletype
, GroupID id_g
)
227 : Window (desc
), sel_engine(), engines(),
228 replace_engines (false), reset_sel_engine (false),
229 sel_group (0), details_height (0), sort_criteria (0),
230 descending_sort_order (false), show_hidden_engines (false),
231 sel_railtype (INVALID_RAILTYPE
), vscroll()
233 this->sel_engine
[0] = this->sel_engine
[1] = (EngineID
)0;
234 this->vscroll
[0] = this->vscroll
[1] = NULL
;
236 this->replace_engines
= true; // start with locomotives (all other vehicles will not read this bool)
237 this->engines
[0].ForceRebuild();
238 this->engines
[1].ForceRebuild();
239 this->reset_sel_engine
= true;
240 this->details_height
= ((vehicletype
== VEH_TRAIN
) ? 10 : 9) * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
241 this->sel_engine
[0] = INVALID_ENGINE
;
242 this->sel_engine
[1] = INVALID_ENGINE
;
243 this->show_hidden_engines
= _engine_sort_show_hidden_engines
[vehicletype
];
245 this->CreateNestedTree();
246 this->vscroll
[0] = this->GetScrollbar(WID_RV_LEFT_SCROLLBAR
);
247 this->vscroll
[1] = this->GetScrollbar(WID_RV_RIGHT_SCROLLBAR
);
249 NWidgetCore
*widget
= this->GetWidget
<NWidgetCore
>(WID_RV_SHOW_HIDDEN_ENGINES
);
250 widget
->widget_data
= STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN
+ vehicletype
;
251 widget
->tool_tip
= STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP
+ vehicletype
;
252 widget
->SetLowered(this->show_hidden_engines
);
253 this->InitNested(vehicletype
);
255 this->sort_criteria
= _engine_sort_last_criteria
[vehicletype
];
256 this->descending_sort_order
= _engine_sort_last_order
[vehicletype
];
257 this->owner
= _local_company
;
258 this->sel_group
= id_g
;
261 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
264 case WID_RV_SORT_ASCENDING_DESCENDING
: {
265 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
266 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
267 d
.height
+= padding
.height
;
268 *size
= maxdim(*size
, d
);
272 case WID_RV_LEFT_MATRIX
:
273 case WID_RV_RIGHT_MATRIX
:
274 resize
->height
= GetEngineListHeight((VehicleType
)this->window_number
);
275 size
->height
= (this->window_number
<= VEH_ROAD
? 8 : 4) * resize
->height
;
278 case WID_RV_LEFT_DETAILS
:
279 case WID_RV_RIGHT_DETAILS
:
280 size
->height
= this->details_height
;
283 case WID_RV_TRAIN_WAGONREMOVE_TOGGLE
: {
284 StringID str
= this->GetWidget
<NWidgetCore
>(widget
)->widget_data
;
285 SetDParam(0, STR_CONFIG_SETTING_ON
);
286 Dimension d
= GetStringBoundingBox(str
);
287 SetDParam(0, STR_CONFIG_SETTING_OFF
);
288 d
= maxdim(d
, GetStringBoundingBox(str
));
289 d
.width
+= padding
.width
;
290 d
.height
+= padding
.height
;
291 *size
= maxdim(*size
, d
);
295 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN
: {
296 StringID str
= this->GetWidget
<NWidgetCore
>(widget
)->widget_data
;
297 SetDParam(0, STR_REPLACE_ENGINES
);
298 Dimension d
= GetStringBoundingBox(str
);
299 SetDParam(0, STR_REPLACE_WAGONS
);
300 d
= maxdim(d
, GetStringBoundingBox(str
));
301 d
.width
+= padding
.width
;
302 d
.height
+= padding
.height
;
303 *size
= maxdim(*size
, d
);
307 case WID_RV_INFO_TAB
: {
308 Dimension d
= GetStringBoundingBox(STR_REPLACE_NOT_REPLACING
);
309 d
= maxdim(d
, GetStringBoundingBox(STR_REPLACE_NOT_REPLACING_VEHICLE_SELECTED
));
310 Dimension h
= GetStringBoundingBox(STR_REPLACE_INFO_TAB
);
311 d
.width
= max (d
.width
, h
.width
);
312 d
.width
+= WD_FRAMETEXT_LEFT
+ WD_FRAMETEXT_RIGHT
;
313 d
.height
+= h
.height
+ 2 * (WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
);
314 *size
= maxdim(*size
, d
);
318 case WID_RV_TRAIN_RAILTYPE_DROPDOWN
: {
319 Dimension d
= {0, 0};
320 for (RailType rt
= RAILTYPE_BEGIN
; rt
!= RAILTYPE_END
; rt
++) {
321 const RailtypeInfo
*rti
= GetRailTypeInfo(rt
);
322 /* Skip rail type if it has no label */
323 if (rti
->label
== 0) continue;
324 d
= maxdim(d
, GetStringBoundingBox(rti
->strings
.replace_text
));
326 d
.width
+= padding
.width
;
327 d
.height
+= padding
.height
;
328 *size
= maxdim(*size
, d
);
332 case WID_RV_START_REPLACE
: {
333 Dimension d
= GetStringBoundingBox(STR_REPLACE_VEHICLES_START
);
334 for (int i
= 0; _start_replace_dropdown
[i
] != INVALID_STRING_ID
; i
++) {
335 d
= maxdim(d
, GetStringBoundingBox(_start_replace_dropdown
[i
]));
337 d
.width
+= padding
.width
;
338 d
.height
+= padding
.height
;
339 *size
= maxdim(*size
, d
);
345 virtual void SetStringParameters(int widget
) const
349 SetDParam(0, STR_REPLACE_VEHICLE_TRAIN
+ this->window_number
);
350 switch (this->sel_group
) {
352 SetDParam(1, STR_GROUP_ALL_TRAINS
+ this->window_number
);
356 SetDParam(1, STR_GROUP_DEFAULT_TRAINS
+ this->window_number
);
360 SetDParam(1, STR_GROUP_NAME
);
361 SetDParam(2, sel_group
);
366 case WID_RV_SORT_DROPDOWN
:
367 SetDParam(0, _engine_sort_listing
[this->window_number
][this->sort_criteria
]);
370 case WID_RV_TRAIN_WAGONREMOVE_TOGGLE
: {
371 const Company
*c
= Company::Get(_local_company
);
372 SetDParam(0, c
->settings
.renew_keep_length
? STR_CONFIG_SETTING_ON
: STR_CONFIG_SETTING_OFF
);
376 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN
:
377 SetDParam(0, this->replace_engines
? STR_REPLACE_ENGINES
: STR_REPLACE_WAGONS
);
382 void DrawWidget (BlitArea
*dpi
, const Rect
&r
, int widget
) const OVERRIDE
385 case WID_RV_SORT_ASCENDING_DESCENDING
:
386 this->DrawSortButtonState (dpi
, WID_RV_SORT_ASCENDING_DESCENDING
, this->descending_sort_order
? SBS_DOWN
: SBS_UP
);
389 case WID_RV_INFO_TAB
: {
390 DrawString (dpi
, r
.left
+ WD_FRAMETEXT_LEFT
, r
.right
- WD_FRAMETEXT_RIGHT
, r
.top
+ WD_FRAMERECT_TOP
, STR_REPLACE_INFO_TAB
, TC_BLACK
, SA_HOR_CENTER
);
392 const Company
*c
= Company::Get(_local_company
);
394 if (this->sel_engine
[0] != INVALID_ENGINE
) {
395 if (!EngineHasReplacementForCompany(c
, this->sel_engine
[0], this->sel_group
)) {
396 str
= STR_REPLACE_NOT_REPLACING
;
398 bool when_old
= false;
399 EngineID e
= EngineReplacementForCompany(c
, this->sel_engine
[0], this->sel_group
, &when_old
);
400 str
= when_old
? STR_REPLACE_REPLACING_WHEN_OLD
: STR_ENGINE_NAME
;
404 str
= STR_REPLACE_NOT_REPLACING_VEHICLE_SELECTED
;
407 DrawString (dpi
, r
.left
+ WD_FRAMETEXT_LEFT
, r
.right
- WD_FRAMETEXT_RIGHT
, r
.top
+ 2 * WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
+ GetStringBoundingBox(STR_REPLACE_INFO_TAB
).height
, str
, TC_BLACK
, SA_HOR_CENTER
);
411 case WID_RV_LEFT_MATRIX
:
412 case WID_RV_RIGHT_MATRIX
: {
413 int side
= (widget
== WID_RV_LEFT_MATRIX
) ? 0 : 1;
414 EngineID start
= this->vscroll
[side
]->GetPosition(); // what is the offset for the start (scrolling)
415 EngineID end
= min(this->vscroll
[side
]->GetCapacity() + start
, this->engines
[side
].Length());
417 /* Do the actual drawing */
418 DrawEngineList ((VehicleType
)this->window_number
, dpi
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, r
.top
+ WD_FRAMERECT_TOP
,
419 this->engines
[side
].Get(start
), end
- start
,
420 this->sel_engine
[side
], side
== 0, this->sel_group
);
426 void OnPaint (BlitArea
*dpi
) OVERRIDE
428 if (this->engines
[0].NeedRebuild() || this->engines
[1].NeedRebuild()) this->GenerateLists();
430 Company
*c
= Company::Get(_local_company
);
432 /* Disable the "Start Replacing" button if:
433 * Either engines list is empty
434 * or The selected replacement engine has a replacement (to prevent loops). */
435 this->SetWidgetDisabledState(WID_RV_START_REPLACE
,
436 this->sel_engine
[0] == INVALID_ENGINE
||
437 this->sel_engine
[1] == INVALID_ENGINE
||
438 EngineReplacementForCompany(c
, this->sel_engine
[1], this->sel_group
) != INVALID_ENGINE
);
440 /* Disable the "Stop Replacing" button if:
441 * The left engines list (existing vehicle) is empty
442 * or The selected vehicle has no replacement set up */
443 this->SetWidgetDisabledState(WID_RV_STOP_REPLACE
,
444 this->sel_engine
[0] == INVALID_ENGINE
||
445 !EngineHasReplacementForCompany(c
, this->sel_engine
[0], this->sel_group
));
447 if (this->window_number
== VEH_TRAIN
) {
448 /* Show the selected railtype in the pulldown menu */
449 this->GetWidget
<NWidgetCore
>(WID_RV_TRAIN_RAILTYPE_DROPDOWN
)->widget_data
= sel_railtype
== INVALID_RAILTYPE
? STR_REPLACE_ALL_RAILTYPE
: GetRailTypeInfo(sel_railtype
)->strings
.replace_text
;
452 this->DrawWidgets (dpi
);
454 if (!this->IsShaded()) {
455 int needed_height
= this->details_height
;
456 /* Draw details panels. */
457 for (int side
= 0; side
< 2; side
++) {
458 if (this->sel_engine
[side
] != INVALID_ENGINE
) {
459 NWidgetBase
*nwi
= this->GetWidget
<NWidgetBase
>(side
== 0 ? WID_RV_LEFT_DETAILS
: WID_RV_RIGHT_DETAILS
);
460 int text_end
= DrawVehiclePurchaseInfo (dpi
, nwi
->pos_x
+ WD_FRAMETEXT_LEFT
, nwi
->pos_x
+ nwi
->current_x
- WD_FRAMETEXT_RIGHT
,
461 nwi
->pos_y
+ WD_FRAMERECT_TOP
, this->sel_engine
[side
]);
462 needed_height
= max(needed_height
, text_end
- (int)nwi
->pos_y
+ WD_FRAMERECT_BOTTOM
);
465 if (needed_height
!= this->details_height
) { // Details window are not high enough, enlarge them.
466 this->details_height
= needed_height
;
473 virtual void OnClick(Point pt
, int widget
, int click_count
)
476 case WID_RV_SORT_ASCENDING_DESCENDING
:
477 this->descending_sort_order
^= true;
478 _engine_sort_last_order
[this->window_number
] = this->descending_sort_order
;
479 this->engines
[1].ForceRebuild();
483 case WID_RV_SHOW_HIDDEN_ENGINES
:
484 this->show_hidden_engines
^= true;
485 _engine_sort_show_hidden_engines
[this->window_number
] = this->show_hidden_engines
;
486 this->engines
[1].ForceRebuild();
487 this->SetWidgetLoweredState(widget
, this->show_hidden_engines
);
491 case WID_RV_SORT_DROPDOWN
:
492 DisplayVehicleSortDropDown(this, static_cast<VehicleType
>(this->window_number
), this->sort_criteria
, WID_RV_SORT_DROPDOWN
);
495 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN
: {
496 if (this->GetWidget
<NWidgetLeaf
>(widget
)->ButtonHit(pt
)) {
497 this->HandleButtonClick (WID_RV_TRAIN_ENGINEWAGON_DROPDOWN
);
498 ReplaceClick_EngineWagonToggle (!this->replace_engines
);
500 DropDownList
*list
= new DropDownList();
501 *list
->Append() = new DropDownListStringItem (STR_REPLACE_ENGINES
, 0, false);
502 *list
->Append() = new DropDownListStringItem (STR_REPLACE_WAGONS
, 1, false);
503 ShowDropDownList (this, list
, this->replace_engines
? 0 : 1, WID_RV_TRAIN_ENGINEWAGON_DROPDOWN
);
508 case WID_RV_TRAIN_RAILTYPE_DROPDOWN
: // Railtype selection dropdown menu
509 ShowDropDownList(this, GetRailTypeDropDownList(true, true), sel_railtype
, WID_RV_TRAIN_RAILTYPE_DROPDOWN
);
512 case WID_RV_TRAIN_WAGONREMOVE_TOGGLE
: // toggle renew_keep_length
513 DoCommandP(0, GetCompanySettingIndex("company.renew_keep_length"), Company::Get(_local_company
)->settings
.renew_keep_length
? 0 : 1, CMD_CHANGE_COMPANY_SETTING
);
516 case WID_RV_START_REPLACE
: { // Start replacing
517 if (this->GetWidget
<NWidgetLeaf
>(widget
)->ButtonHit(pt
)) {
518 this->HandleButtonClick(WID_RV_START_REPLACE
);
519 ReplaceClick_StartReplace(false);
521 bool replacment_when_old
= EngineHasReplacementWhenOldForCompany(Company::Get(_local_company
), this->sel_engine
[0], this->sel_group
);
522 ShowDropDownMenu(this, _start_replace_dropdown
, replacment_when_old
? 1 : 0, WID_RV_START_REPLACE
, !this->replace_engines
? 1 << 1 : 0, 0);
527 case WID_RV_STOP_REPLACE
: { // Stop replacing
528 EngineID veh_from
= this->sel_engine
[0];
529 DoCommandP(0, this->sel_group
<< 16, veh_from
+ (INVALID_ENGINE
<< 16), CMD_SET_AUTOREPLACE
);
533 case WID_RV_LEFT_MATRIX
:
534 case WID_RV_RIGHT_MATRIX
: {
536 if (widget
== WID_RV_LEFT_MATRIX
) {
541 uint i
= this->vscroll
[click_side
]->GetScrolledRowFromWidget(pt
.y
, this, widget
);
542 size_t engine_count
= this->engines
[click_side
].Length();
544 EngineID e
= engine_count
> i
? this->engines
[click_side
][i
] : INVALID_ENGINE
;
545 if (e
== this->sel_engine
[click_side
]) break; // we clicked the one we already selected
546 this->sel_engine
[click_side
] = e
;
547 if (click_side
== 0) {
548 this->engines
[1].ForceRebuild();
549 this->reset_sel_engine
= true;
557 virtual void OnDropdownSelect(int widget
, int index
)
560 case WID_RV_SORT_DROPDOWN
:
561 if (this->sort_criteria
!= index
) {
562 this->sort_criteria
= index
;
563 _engine_sort_last_criteria
[this->window_number
] = this->sort_criteria
;
564 this->engines
[1].ForceRebuild();
569 case WID_RV_TRAIN_RAILTYPE_DROPDOWN
: {
570 RailType temp
= (RailType
)index
;
571 if (temp
== sel_railtype
) return; // we didn't select a new one. No need to change anything
573 /* Reset scrollbar positions */
574 this->vscroll
[0]->SetPosition(0);
575 this->vscroll
[1]->SetPosition(0);
576 /* Rebuild the lists */
577 this->engines
[0].ForceRebuild();
578 this->engines
[1].ForceRebuild();
579 this->reset_sel_engine
= true;
584 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN
: {
585 bool engines
= (index
== 0);
586 if (engines
== this->replace_engines
) return;
587 this->ReplaceClick_EngineWagonToggle (engines
);
591 case WID_RV_START_REPLACE
:
592 this->ReplaceClick_StartReplace(index
!= 0);
597 virtual void OnResize()
599 this->vscroll
[0]->SetCapacityFromWidget(this, WID_RV_LEFT_MATRIX
);
600 this->vscroll
[1]->SetCapacityFromWidget(this, WID_RV_RIGHT_MATRIX
);
604 * Some data on this window has become invalid.
605 * @param data Information about the changed data.
606 * @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.
608 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
611 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
612 this->engines
[0].ForceRebuild();
614 this->engines
[1].ForceRebuild();
619 static const NWidgetPart _nested_replace_rail_vehicle_widgets
[] = {
620 NWidget(NWID_HORIZONTAL
),
621 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
622 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_RV_CAPTION
), SetDataTip(STR_REPLACE_VEHICLES_WHITE
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
623 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
624 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
625 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
627 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
628 NWidget(WWT_PANEL
, COLOUR_GREY
),
629 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),
631 NWidget(WWT_PANEL
, COLOUR_GREY
),
632 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),
635 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
636 NWidget(NWID_HORIZONTAL
),
637 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_RV_TRAIN_RAILTYPE_DROPDOWN
), SetDataTip(0x0, STR_REPLACE_HELP_RAILTYPE
), SetResize(1, 0), SetFill(1, 1),
638 NWidget(NWID_PUSHBUTTON_DROPDOWN
, COLOUR_GREY
, WID_RV_TRAIN_ENGINEWAGON_DROPDOWN
), SetDataTip(STR_JUST_STRING
, STR_REPLACE_ENGINE_WAGON_SELECT_HELP
), SetMinimalSize(80, 12),
640 NWidget(NWID_VERTICAL
),
641 NWidget(NWID_HORIZONTAL
),
642 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_RV_TRAIN_WAGONREMOVE_TOGGLE
), SetMinimalSize(120, 12), SetDataTip(STR_REPLACE_REMOVE_WAGON
, STR_REPLACE_REMOVE_WAGON_HELP
), SetResize(1, 0), SetFill(1, 0),
643 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_RV_SHOW_HIDDEN_ENGINES
), SetDataTip(STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN
, STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP
), SetResize(1, 0), SetFill(1, 0),
647 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
648 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), EndContainer(),
649 NWidget(NWID_HORIZONTAL
),
650 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_RV_SORT_ASCENDING_DESCENDING
), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
), SetFill(1, 0),
651 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_RV_SORT_DROPDOWN
), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_SORT_CRITERIA
),
654 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
655 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
),
656 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_RV_LEFT_SCROLLBAR
),
657 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
),
658 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_RV_RIGHT_SCROLLBAR
),
660 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
661 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_LEFT_DETAILS
), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
662 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_RIGHT_DETAILS
), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
664 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
665 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_INFO_TAB
), SetMinimalSize(240, 24), SetDataTip(0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB
), SetResize(1, 0), EndContainer(),
666 NWidget(NWID_VERTICAL
),
667 NWidget(NWID_PUSHBUTTON_DROPDOWN
, COLOUR_GREY
, WID_RV_START_REPLACE
), SetMinimalSize(240, 12), SetDataTip(STR_REPLACE_VEHICLES_START
, STR_REPLACE_HELP_START_BUTTON
), SetResize(1, 0),
668 NWidget(NWID_HORIZONTAL
),
669 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_RV_STOP_REPLACE
), SetMinimalSize(228, 12), SetDataTip(STR_REPLACE_VEHICLES_STOP
, STR_REPLACE_HELP_STOP_BUTTON
), SetResize(1, 0),
670 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
676 static WindowDesc::Prefs
_replace_rail_vehicle_prefs ("replace_vehicle_train");
678 static const WindowDesc
_replace_rail_vehicle_desc(
680 WC_REPLACE_VEHICLE
, WC_NONE
,
682 _nested_replace_rail_vehicle_widgets
, lengthof(_nested_replace_rail_vehicle_widgets
),
683 &_replace_rail_vehicle_prefs
686 static const NWidgetPart _nested_replace_vehicle_widgets
[] = {
687 NWidget(NWID_HORIZONTAL
),
688 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
689 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_RV_CAPTION
), SetDataTip(STR_REPLACE_VEHICLES_WHITE
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
690 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
691 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
692 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
694 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
695 NWidget(WWT_PANEL
, COLOUR_GREY
),
696 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),
698 NWidget(WWT_PANEL
, COLOUR_GREY
),
699 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),
702 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
703 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), EndContainer(),
704 NWidget(NWID_VERTICAL
),
705 NWidget(NWID_HORIZONTAL
),
706 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), SetFill(1, 1), EndContainer(),
707 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_RV_SHOW_HIDDEN_ENGINES
), SetDataTip(STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN
, STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP
), SetMinimalSize(80, 12),
709 NWidget(NWID_HORIZONTAL
),
710 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_RV_SORT_ASCENDING_DESCENDING
), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
711 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_RV_SORT_DROPDOWN
), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_SORT_CRITERIA
),
715 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
716 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
),
717 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_RV_LEFT_SCROLLBAR
),
718 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
),
719 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_RV_RIGHT_SCROLLBAR
),
721 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
722 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_LEFT_DETAILS
), SetMinimalSize(228, 92), SetResize(1, 0), EndContainer(),
723 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_RIGHT_DETAILS
), SetMinimalSize(228, 92), SetResize(1, 0), EndContainer(),
725 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
726 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_RV_INFO_TAB
), SetMinimalSize(228, 24), SetDataTip(0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB
), SetResize(1, 0), EndContainer(),
727 NWidget(NWID_VERTICAL
),
728 NWidget(NWID_PUSHBUTTON_DROPDOWN
, COLOUR_GREY
, WID_RV_START_REPLACE
), SetMinimalSize(228, 12), SetDataTip(STR_REPLACE_VEHICLES_START
, STR_REPLACE_HELP_START_BUTTON
), SetResize(1, 0),
729 NWidget(NWID_HORIZONTAL
),
730 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_RV_STOP_REPLACE
), SetMinimalSize(216, 12), SetDataTip(STR_REPLACE_VEHICLES_STOP
, STR_REPLACE_HELP_STOP_BUTTON
), SetResize(1, 0),
731 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
737 static WindowDesc::Prefs
_replace_vehicle_prefs ("replace_vehicle");
739 static const WindowDesc
_replace_vehicle_desc(
741 WC_REPLACE_VEHICLE
, WC_NONE
,
743 _nested_replace_vehicle_widgets
, lengthof(_nested_replace_vehicle_widgets
),
744 &_replace_vehicle_prefs
748 * Show the autoreplace configuration window for a particular group.
749 * @param id_g The group to replace the vehicles for.
750 * @param vehicletype The type of vehicles in the group.
752 void ShowReplaceGroupVehicleWindow(GroupID id_g
, VehicleType vehicletype
)
754 DeleteWindowById(WC_REPLACE_VEHICLE
, vehicletype
);
755 new ReplaceVehicleWindow(vehicletype
== VEH_TRAIN
? &_replace_rail_vehicle_desc
: &_replace_vehicle_desc
, vehicletype
, id_g
);