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 build_vehicle_gui.cpp GUI for building vehicles. */
13 #include "engine_base.h"
14 #include "engine_func.h"
15 #include "station_base.h"
16 #include "network/network.h"
17 #include "articulated_vehicles.h"
18 #include "textbuf_gui.h"
19 #include "command_func.h"
20 #include "company_func.h"
21 #include "vehicle_gui.h"
22 #include "newgrf_engine.h"
23 #include "newgrf_text.h"
25 #include "string_func.h"
26 #include "strings_func.h"
27 #include "window_func.h"
28 #include "date_func.h"
29 #include "vehicle_func.h"
30 #include "widgets/dropdown_func.h"
31 #include "engine_gui.h"
32 #include "cargotype.h"
33 #include "core/geometry_func.hpp"
34 #include "autoreplace_func.h"
36 #include "widgets/build_vehicle_widget.h"
38 #include "table/strings.h"
40 #include "safeguards.h"
43 * Get the height of a single 'entry' in the engine lists.
44 * @param type the vehicle type to get the height of
45 * @return the height for the entry
47 uint
GetEngineListHeight(VehicleType type
)
49 return max
<uint
>(FONT_HEIGHT_NORMAL
+ WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
, GetVehicleImageCellSize(type
, EIT_PURCHASE
).height
);
52 static const NWidgetPart _nested_build_vehicle_widgets
[] = {
53 NWidget(NWID_HORIZONTAL
),
54 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
55 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_BV_CAPTION
), SetDataTip(STR_WHITE_STRING
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
56 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
57 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
58 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
60 NWidget(WWT_PANEL
, COLOUR_GREY
),
61 NWidget(NWID_VERTICAL
),
62 NWidget(NWID_HORIZONTAL
),
63 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_BV_SORT_ASCENDING_DESCENDING
), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
64 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_BV_SORT_DROPDOWN
), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_SORT_CRITERIA
),
66 NWidget(NWID_HORIZONTAL
),
67 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BV_SHOW_HIDDEN_ENGINES
),
68 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_BV_CARGO_FILTER_DROPDOWN
), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_FILTER_CRITERIA
),
73 NWidget(NWID_HORIZONTAL
),
74 NWidget(WWT_MATRIX
, COLOUR_GREY
, WID_BV_LIST
), SetResize(1, 1), SetFill(1, 0), SetMatrixDataTip(1, 0, STR_NULL
), SetScrollbar(WID_BV_SCROLLBAR
),
75 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_BV_SCROLLBAR
),
77 /* Panel with details. */
78 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BV_PANEL
), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
79 /* Build/rename buttons, resize button. */
80 NWidget(NWID_HORIZONTAL
),
81 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BV_BUILD_SEL
),
82 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_BV_BUILD
), SetResize(1, 0), SetFill(1, 0),
84 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_BV_SHOW_HIDE
), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_JUST_STRING
, STR_NULL
),
85 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_BV_RENAME
), SetResize(1, 0), SetFill(1, 0),
86 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
90 /** Special cargo filter criteria */
91 static const CargoID CF_ANY
= CT_NO_REFIT
; ///< Show all vehicles independent of carried cargo (i.e. no filtering)
92 static const CargoID CF_NONE
= CT_INVALID
; ///< Show only vehicles which do not carry cargo (e.g. train engines)
94 bool _engine_sort_direction
; ///< \c false = descending, \c true = ascending.
95 byte _engine_sort_last_criteria
[] = {0, 0, 0, 0}; ///< Last set sort criteria, for each vehicle type.
96 bool _engine_sort_last_order
[] = {false, false, false, false}; ///< Last set direction of the sort order, for each vehicle type.
97 bool _engine_sort_show_hidden_engines
[] = {false, false, false, false}; ///< Last set 'show hidden engines' setting for each vehicle type.
98 static CargoID _engine_sort_last_cargo_criteria
[] = {CF_ANY
, CF_ANY
, CF_ANY
, CF_ANY
}; ///< Last set filter criteria, for each vehicle type.
101 * Determines order of engines by engineID
102 * @param *a first engine to compare
103 * @param *b second engine to compare
104 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
106 static int CDECL
EngineNumberSorter(const EngineID
*a
, const EngineID
*b
)
108 int r
= Engine::Get(*a
)->list_position
- Engine::Get(*b
)->list_position
;
110 return _engine_sort_direction
? -r
: r
;
114 * Determines order of engines by introduction date
115 * @param *a first engine to compare
116 * @param *b second engine to compare
117 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
119 static int CDECL
EngineIntroDateSorter(const EngineID
*a
, const EngineID
*b
)
121 const int va
= Engine::Get(*a
)->intro_date
;
122 const int vb
= Engine::Get(*b
)->intro_date
;
123 const int r
= va
- vb
;
125 /* Use EngineID to sort instead since we want consistent sorting */
126 if (r
== 0) return EngineNumberSorter(a
, b
);
127 return _engine_sort_direction
? -r
: r
;
131 * Determines order of engines by name
132 * @param *a first engine to compare
133 * @param *b second engine to compare
134 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
136 static int CDECL
EngineNameSorter(const EngineID
*a
, const EngineID
*b
)
138 static EngineID last_engine
[2] = { INVALID_ENGINE
, INVALID_ENGINE
};
139 static char last_name
[2][64] = { "\0", "\0" };
141 const EngineID va
= *a
;
142 const EngineID vb
= *b
;
144 if (va
!= last_engine
[0]) {
147 GetString(last_name
[0], STR_ENGINE_NAME
, lastof(last_name
[0]));
150 if (vb
!= last_engine
[1]) {
153 GetString(last_name
[1], STR_ENGINE_NAME
, lastof(last_name
[1]));
156 int r
= strnatcmp(last_name
[0], last_name
[1]); // Sort by name (natural sorting).
158 /* Use EngineID to sort instead since we want consistent sorting */
159 if (r
== 0) return EngineNumberSorter(a
, b
);
160 return _engine_sort_direction
? -r
: r
;
164 * Determines order of engines by reliability
165 * @param *a first engine to compare
166 * @param *b second engine to compare
167 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
169 static int CDECL
EngineReliabilitySorter(const EngineID
*a
, const EngineID
*b
)
171 const int va
= Engine::Get(*a
)->reliability
;
172 const int vb
= Engine::Get(*b
)->reliability
;
173 const int r
= va
- vb
;
175 /* Use EngineID to sort instead since we want consistent sorting */
176 if (r
== 0) return EngineNumberSorter(a
, b
);
177 return _engine_sort_direction
? -r
: r
;
181 * Determines order of engines by purchase cost
182 * @param *a first engine to compare
183 * @param *b second engine to compare
184 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
186 static int CDECL
EngineCostSorter(const EngineID
*a
, const EngineID
*b
)
188 Money va
= Engine::Get(*a
)->GetCost();
189 Money vb
= Engine::Get(*b
)->GetCost();
190 int r
= ClampToI32(va
- vb
);
192 /* Use EngineID to sort instead since we want consistent sorting */
193 if (r
== 0) return EngineNumberSorter(a
, b
);
194 return _engine_sort_direction
? -r
: r
;
198 * Determines order of engines by speed
199 * @param *a first engine to compare
200 * @param *b second engine to compare
201 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
203 static int CDECL
EngineSpeedSorter(const EngineID
*a
, const EngineID
*b
)
205 int va
= Engine::Get(*a
)->GetDisplayMaxSpeed();
206 int vb
= Engine::Get(*b
)->GetDisplayMaxSpeed();
209 /* Use EngineID to sort instead since we want consistent sorting */
210 if (r
== 0) return EngineNumberSorter(a
, b
);
211 return _engine_sort_direction
? -r
: r
;
215 * Determines order of engines by power
216 * @param *a first engine to compare
217 * @param *b second engine to compare
218 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
220 static int CDECL
EnginePowerSorter(const EngineID
*a
, const EngineID
*b
)
222 int va
= Engine::Get(*a
)->GetPower();
223 int vb
= Engine::Get(*b
)->GetPower();
226 /* Use EngineID to sort instead since we want consistent sorting */
227 if (r
== 0) return EngineNumberSorter(a
, b
);
228 return _engine_sort_direction
? -r
: r
;
232 * Determines order of engines by tractive effort
233 * @param *a first engine to compare
234 * @param *b second engine to compare
235 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
237 static int CDECL
EngineTractiveEffortSorter(const EngineID
*a
, const EngineID
*b
)
239 int va
= Engine::Get(*a
)->GetDisplayMaxTractiveEffort();
240 int vb
= Engine::Get(*b
)->GetDisplayMaxTractiveEffort();
243 /* Use EngineID to sort instead since we want consistent sorting */
244 if (r
== 0) return EngineNumberSorter(a
, b
);
245 return _engine_sort_direction
? -r
: r
;
249 * Determines order of engines by running costs
250 * @param *a first engine to compare
251 * @param *b second engine to compare
252 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
254 static int CDECL
EngineRunningCostSorter(const EngineID
*a
, const EngineID
*b
)
256 Money va
= Engine::Get(*a
)->GetRunningCost();
257 Money vb
= Engine::Get(*b
)->GetRunningCost();
258 int r
= ClampToI32(va
- vb
);
260 /* Use EngineID to sort instead since we want consistent sorting */
261 if (r
== 0) return EngineNumberSorter(a
, b
);
262 return _engine_sort_direction
? -r
: r
;
266 * Determines order of engines by running costs
267 * @param *a first engine to compare
268 * @param *b second engine to compare
269 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
271 static int CDECL
EnginePowerVsRunningCostSorter(const EngineID
*a
, const EngineID
*b
)
273 const Engine
*e_a
= Engine::Get(*a
);
274 const Engine
*e_b
= Engine::Get(*b
);
276 /* Here we are using a few tricks to get the right sort.
277 * We want power/running cost, but since we usually got higher running cost than power and we store the result in an int,
278 * we will actually calculate cunning cost/power (to make it more than 1).
279 * Because of this, the return value have to be reversed as well and we return b - a instead of a - b.
280 * Another thing is that both power and running costs should be doubled for multiheaded engines.
281 * Since it would be multiplying with 2 in both numerator and denominator, it will even themselves out and we skip checking for multiheaded. */
282 Money va
= (e_a
->GetRunningCost()) / max(1U, (uint
)e_a
->GetPower());
283 Money vb
= (e_b
->GetRunningCost()) / max(1U, (uint
)e_b
->GetPower());
284 int r
= ClampToI32(vb
- va
);
286 /* Use EngineID to sort instead since we want consistent sorting */
287 if (r
== 0) return EngineNumberSorter(a
, b
);
288 return _engine_sort_direction
? -r
: r
;
291 /* Train sorting functions */
294 * Determines order of train engines by capacity
295 * @param *a first engine to compare
296 * @param *b second engine to compare
297 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
299 static int CDECL
TrainEngineCapacitySorter(const EngineID
*a
, const EngineID
*b
)
301 const RailVehicleInfo
*rvi_a
= RailVehInfo(*a
);
302 const RailVehicleInfo
*rvi_b
= RailVehInfo(*b
);
304 int va
= GetTotalCapacityOfArticulatedParts(*a
) * (rvi_a
->railveh_type
== RAILVEH_MULTIHEAD
? 2 : 1);
305 int vb
= GetTotalCapacityOfArticulatedParts(*b
) * (rvi_b
->railveh_type
== RAILVEH_MULTIHEAD
? 2 : 1);
308 /* Use EngineID to sort instead since we want consistent sorting */
309 if (r
== 0) return EngineNumberSorter(a
, b
);
310 return _engine_sort_direction
? -r
: r
;
314 * Determines order of train engines by engine / wagon
315 * @param *a first engine to compare
316 * @param *b second engine to compare
317 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
319 static int CDECL
TrainEnginesThenWagonsSorter(const EngineID
*a
, const EngineID
*b
)
321 int val_a
= (RailVehInfo(*a
)->railveh_type
== RAILVEH_WAGON
? 1 : 0);
322 int val_b
= (RailVehInfo(*b
)->railveh_type
== RAILVEH_WAGON
? 1 : 0);
323 int r
= val_a
- val_b
;
325 /* Use EngineID to sort instead since we want consistent sorting */
326 if (r
== 0) return EngineNumberSorter(a
, b
);
327 return _engine_sort_direction
? -r
: r
;
330 /* Road vehicle sorting functions */
333 * Determines order of road vehicles by capacity
334 * @param *a first engine to compare
335 * @param *b second engine to compare
336 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
338 static int CDECL
RoadVehEngineCapacitySorter(const EngineID
*a
, const EngineID
*b
)
340 int va
= GetTotalCapacityOfArticulatedParts(*a
);
341 int vb
= GetTotalCapacityOfArticulatedParts(*b
);
344 /* Use EngineID to sort instead since we want consistent sorting */
345 if (r
== 0) return EngineNumberSorter(a
, b
);
346 return _engine_sort_direction
? -r
: r
;
349 /* Ship vehicle sorting functions */
352 * Determines order of ships by capacity
353 * @param *a first engine to compare
354 * @param *b second engine to compare
355 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
357 static int CDECL
ShipEngineCapacitySorter(const EngineID
*a
, const EngineID
*b
)
359 const Engine
*e_a
= Engine::Get(*a
);
360 const Engine
*e_b
= Engine::Get(*b
);
362 int va
= e_a
->GetDisplayDefaultCapacity();
363 int vb
= e_b
->GetDisplayDefaultCapacity();
366 /* Use EngineID to sort instead since we want consistent sorting */
367 if (r
== 0) return EngineNumberSorter(a
, b
);
368 return _engine_sort_direction
? -r
: r
;
371 /* Aircraft sorting functions */
374 * Determines order of aircraft by cargo
375 * @param *a first engine to compare
376 * @param *b second engine to compare
377 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
379 static int CDECL
AircraftEngineCargoSorter(const EngineID
*a
, const EngineID
*b
)
381 const Engine
*e_a
= Engine::Get(*a
);
382 const Engine
*e_b
= Engine::Get(*b
);
384 uint16 mail_a
, mail_b
;
385 int va
= e_a
->GetDisplayDefaultCapacity(&mail_a
);
386 int vb
= e_b
->GetDisplayDefaultCapacity(&mail_b
);
390 /* The planes have the same passenger capacity. Check mail capacity instead */
394 /* Use EngineID to sort instead since we want consistent sorting */
395 return EngineNumberSorter(a
, b
);
398 return _engine_sort_direction
? -r
: r
;
402 * Determines order of aircraft by range.
403 * @param *a first engine to compare.
404 * @param *b second engine to compare.
405 * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal.
407 static int CDECL
AircraftRangeSorter(const EngineID
*a
, const EngineID
*b
)
409 uint16 r_a
= Engine::Get(*a
)->GetRange();
410 uint16 r_b
= Engine::Get(*b
)->GetRange();
414 /* Use EngineID to sort instead since we want consistent sorting */
415 if (r
== 0) return EngineNumberSorter(a
, b
);
416 return _engine_sort_direction
? -r
: r
;
419 /** Sort functions for the vehicle sort criteria, for each vehicle type. */
420 EngList_SortTypeFunction
* const _engine_sort_functions
[][11] = {{
426 &EngineTractiveEffortSorter
,
427 &EngineIntroDateSorter
,
429 &EngineRunningCostSorter
,
430 &EnginePowerVsRunningCostSorter
,
431 &EngineReliabilitySorter
,
432 &TrainEngineCapacitySorter
,
439 &EngineTractiveEffortSorter
,
440 &EngineIntroDateSorter
,
442 &EngineRunningCostSorter
,
443 &EnginePowerVsRunningCostSorter
,
444 &EngineReliabilitySorter
,
445 &RoadVehEngineCapacitySorter
,
451 &EngineIntroDateSorter
,
453 &EngineRunningCostSorter
,
454 &EngineReliabilitySorter
,
455 &ShipEngineCapacitySorter
,
461 &EngineIntroDateSorter
,
463 &EngineRunningCostSorter
,
464 &EngineReliabilitySorter
,
465 &AircraftEngineCargoSorter
,
466 &AircraftRangeSorter
,
469 /** Dropdown menu strings for the vehicle sort criteria. */
470 const StringID _engine_sort_listing
[][12] = {{
472 STR_SORT_BY_ENGINE_ID
,
474 STR_SORT_BY_MAX_SPEED
,
476 STR_SORT_BY_TRACTIVE_EFFORT
,
477 STR_SORT_BY_INTRO_DATE
,
479 STR_SORT_BY_RUNNING_COST
,
480 STR_SORT_BY_POWER_VS_RUNNING_COST
,
481 STR_SORT_BY_RELIABILITY
,
482 STR_SORT_BY_CARGO_CAPACITY
,
486 STR_SORT_BY_ENGINE_ID
,
488 STR_SORT_BY_MAX_SPEED
,
490 STR_SORT_BY_TRACTIVE_EFFORT
,
491 STR_SORT_BY_INTRO_DATE
,
493 STR_SORT_BY_RUNNING_COST
,
494 STR_SORT_BY_POWER_VS_RUNNING_COST
,
495 STR_SORT_BY_RELIABILITY
,
496 STR_SORT_BY_CARGO_CAPACITY
,
500 STR_SORT_BY_ENGINE_ID
,
502 STR_SORT_BY_MAX_SPEED
,
503 STR_SORT_BY_INTRO_DATE
,
505 STR_SORT_BY_RUNNING_COST
,
506 STR_SORT_BY_RELIABILITY
,
507 STR_SORT_BY_CARGO_CAPACITY
,
511 STR_SORT_BY_ENGINE_ID
,
513 STR_SORT_BY_MAX_SPEED
,
514 STR_SORT_BY_INTRO_DATE
,
516 STR_SORT_BY_RUNNING_COST
,
517 STR_SORT_BY_RELIABILITY
,
518 STR_SORT_BY_CARGO_CAPACITY
,
523 /** Cargo filter functions */
524 static bool CDECL
CargoFilter(const EngineID
*eid
, const CargoID cid
)
526 if (cid
== CF_ANY
) return true;
527 uint32 refit_mask
= GetUnionOfArticulatedRefitMasks(*eid
, true) & _standard_cargo_mask
;
528 return (cid
== CF_NONE
? refit_mask
== 0 : HasBit(refit_mask
, cid
));
531 static GUIEngineList::FilterFunction
* const _filter_funcs
[] = {
535 static int DrawCargoCapacityInfo(int left
, int right
, int y
, EngineID engine
)
539 GetArticulatedVehicleCargoesAndRefits(engine
, &cap
, &refits
);
541 for (CargoID c
= 0; c
< NUM_CARGO
; c
++) {
542 if (cap
[c
] == 0) continue;
545 SetDParam(1, cap
[c
]);
546 SetDParam(2, HasBit(refits
, c
) ? STR_PURCHASE_INFO_REFITTABLE
: STR_EMPTY
);
547 DrawString(left
, right
, y
, STR_PURCHASE_INFO_CAPACITY
);
548 y
+= FONT_HEIGHT_NORMAL
;
554 /* Draw rail wagon specific details */
555 static int DrawRailWagonPurchaseInfo(int left
, int right
, int y
, EngineID engine_number
, const RailVehicleInfo
*rvi
)
557 const Engine
*e
= Engine::Get(engine_number
);
560 SetDParam(0, e
->GetCost());
561 DrawString(left
, right
, y
, STR_PURCHASE_INFO_COST
);
562 y
+= FONT_HEIGHT_NORMAL
;
564 /* Wagon weight - (including cargo) */
565 uint weight
= e
->GetDisplayWeight();
566 SetDParam(0, weight
);
567 uint cargo_weight
= (e
->CanCarryCargo() ? CargoSpec::Get(e
->GetDefaultCargoType())->weight
* GetTotalCapacityOfArticulatedParts(engine_number
) / 16 : 0);
568 SetDParam(1, cargo_weight
+ weight
);
569 DrawString(left
, right
, y
, STR_PURCHASE_INFO_WEIGHT_CWEIGHT
);
570 y
+= FONT_HEIGHT_NORMAL
;
572 /* Wagon speed limit, displayed if above zero */
573 if (_settings_game
.vehicle
.wagon_speed_limits
) {
574 uint max_speed
= e
->GetDisplayMaxSpeed();
576 SetDParam(0, max_speed
);
577 DrawString(left
, right
, y
, STR_PURCHASE_INFO_SPEED
);
578 y
+= FONT_HEIGHT_NORMAL
;
583 if (rvi
->running_cost_class
!= INVALID_PRICE
) {
584 SetDParam(0, e
->GetRunningCost());
585 DrawString(left
, right
, y
, STR_PURCHASE_INFO_RUNNINGCOST
);
586 y
+= FONT_HEIGHT_NORMAL
;
592 /* Draw locomotive specific details */
593 static int DrawRailEnginePurchaseInfo(int left
, int right
, int y
, EngineID engine_number
, const RailVehicleInfo
*rvi
)
595 const Engine
*e
= Engine::Get(engine_number
);
597 /* Purchase Cost - Engine weight */
598 SetDParam(0, e
->GetCost());
599 SetDParam(1, e
->GetDisplayWeight());
600 DrawString(left
, right
, y
, STR_PURCHASE_INFO_COST_WEIGHT
);
601 y
+= FONT_HEIGHT_NORMAL
;
603 /* Max speed - Engine power */
604 SetDParam(0, e
->GetDisplayMaxSpeed());
605 SetDParam(1, e
->GetPower());
606 DrawString(left
, right
, y
, STR_PURCHASE_INFO_SPEED_POWER
);
607 y
+= FONT_HEIGHT_NORMAL
;
609 /* Max tractive effort - not applicable if old acceleration or maglev */
610 if (_settings_game
.vehicle
.train_acceleration_model
!= AM_ORIGINAL
&& GetRailTypeInfo(rvi
->railtype
)->acceleration_type
!= 2) {
611 SetDParam(0, e
->GetDisplayMaxTractiveEffort());
612 DrawString(left
, right
, y
, STR_PURCHASE_INFO_MAX_TE
);
613 y
+= FONT_HEIGHT_NORMAL
;
617 if (rvi
->running_cost_class
!= INVALID_PRICE
) {
618 SetDParam(0, e
->GetRunningCost());
619 DrawString(left
, right
, y
, STR_PURCHASE_INFO_RUNNINGCOST
);
620 y
+= FONT_HEIGHT_NORMAL
;
623 /* Powered wagons power - Powered wagons extra weight */
624 if (rvi
->pow_wag_power
!= 0) {
625 SetDParam(0, rvi
->pow_wag_power
);
626 SetDParam(1, rvi
->pow_wag_weight
);
627 DrawString(left
, right
, y
, STR_PURCHASE_INFO_PWAGPOWER_PWAGWEIGHT
);
628 y
+= FONT_HEIGHT_NORMAL
;
634 /* Draw road vehicle specific details */
635 static int DrawRoadVehPurchaseInfo(int left
, int right
, int y
, EngineID engine_number
)
637 const Engine
*e
= Engine::Get(engine_number
);
639 if (_settings_game
.vehicle
.roadveh_acceleration_model
!= AM_ORIGINAL
) {
641 SetDParam(0, e
->GetCost());
642 DrawString(left
, right
, y
, STR_PURCHASE_INFO_COST
);
643 y
+= FONT_HEIGHT_NORMAL
;
645 /* Road vehicle weight - (including cargo) */
646 int16 weight
= e
->GetDisplayWeight();
647 SetDParam(0, weight
);
648 uint cargo_weight
= (e
->CanCarryCargo() ? CargoSpec::Get(e
->GetDefaultCargoType())->weight
* GetTotalCapacityOfArticulatedParts(engine_number
) / 16 : 0);
649 SetDParam(1, cargo_weight
+ weight
);
650 DrawString(left
, right
, y
, STR_PURCHASE_INFO_WEIGHT_CWEIGHT
);
651 y
+= FONT_HEIGHT_NORMAL
;
653 /* Max speed - Engine power */
654 SetDParam(0, e
->GetDisplayMaxSpeed());
655 SetDParam(1, e
->GetPower());
656 DrawString(left
, right
, y
, STR_PURCHASE_INFO_SPEED_POWER
);
657 y
+= FONT_HEIGHT_NORMAL
;
659 /* Max tractive effort */
660 SetDParam(0, e
->GetDisplayMaxTractiveEffort());
661 DrawString(left
, right
, y
, STR_PURCHASE_INFO_MAX_TE
);
662 y
+= FONT_HEIGHT_NORMAL
;
664 /* Purchase cost - Max speed */
665 SetDParam(0, e
->GetCost());
666 SetDParam(1, e
->GetDisplayMaxSpeed());
667 DrawString(left
, right
, y
, STR_PURCHASE_INFO_COST_SPEED
);
668 y
+= FONT_HEIGHT_NORMAL
;
672 SetDParam(0, e
->GetRunningCost());
673 DrawString(left
, right
, y
, STR_PURCHASE_INFO_RUNNINGCOST
);
674 y
+= FONT_HEIGHT_NORMAL
;
679 /* Draw ship specific details */
680 static int DrawShipPurchaseInfo(int left
, int right
, int y
, EngineID engine_number
, bool refittable
)
682 const Engine
*e
= Engine::Get(engine_number
);
684 /* Purchase cost - Max speed */
685 uint raw_speed
= e
->GetDisplayMaxSpeed();
686 uint ocean_speed
= e
->u
.ship
.ApplyWaterClassSpeedFrac(raw_speed
, true);
687 uint canal_speed
= e
->u
.ship
.ApplyWaterClassSpeedFrac(raw_speed
, false);
689 SetDParam(0, e
->GetCost());
690 if (ocean_speed
== canal_speed
) {
691 SetDParam(1, ocean_speed
);
692 DrawString(left
, right
, y
, STR_PURCHASE_INFO_COST_SPEED
);
693 y
+= FONT_HEIGHT_NORMAL
;
695 DrawString(left
, right
, y
, STR_PURCHASE_INFO_COST
);
696 y
+= FONT_HEIGHT_NORMAL
;
698 SetDParam(0, ocean_speed
);
699 DrawString(left
, right
, y
, STR_PURCHASE_INFO_SPEED_OCEAN
);
700 y
+= FONT_HEIGHT_NORMAL
;
702 SetDParam(0, canal_speed
);
703 DrawString(left
, right
, y
, STR_PURCHASE_INFO_SPEED_CANAL
);
704 y
+= FONT_HEIGHT_NORMAL
;
707 /* Cargo type + capacity */
708 SetDParam(0, e
->GetDefaultCargoType());
709 SetDParam(1, e
->GetDisplayDefaultCapacity());
710 SetDParam(2, refittable
? STR_PURCHASE_INFO_REFITTABLE
: STR_EMPTY
);
711 DrawString(left
, right
, y
, STR_PURCHASE_INFO_CAPACITY
);
712 y
+= FONT_HEIGHT_NORMAL
;
715 SetDParam(0, e
->GetRunningCost());
716 DrawString(left
, right
, y
, STR_PURCHASE_INFO_RUNNINGCOST
);
717 y
+= FONT_HEIGHT_NORMAL
;
722 /* Draw aircraft specific details */
723 static int DrawAircraftPurchaseInfo(int left
, int right
, int y
, EngineID engine_number
, bool refittable
)
725 const Engine
*e
= Engine::Get(engine_number
);
726 CargoID cargo
= e
->GetDefaultCargoType();
728 /* Purchase cost - Max speed */
729 SetDParam(0, e
->GetCost());
730 SetDParam(1, e
->GetDisplayMaxSpeed());
731 DrawString(left
, right
, y
, STR_PURCHASE_INFO_COST_SPEED
);
732 y
+= FONT_HEIGHT_NORMAL
;
735 uint16 mail_capacity
;
736 uint capacity
= e
->GetDisplayDefaultCapacity(&mail_capacity
);
737 if (mail_capacity
> 0) {
739 SetDParam(1, capacity
);
740 SetDParam(2, CT_MAIL
);
741 SetDParam(3, mail_capacity
);
742 DrawString(left
, right
, y
, STR_PURCHASE_INFO_AIRCRAFT_CAPACITY
);
744 /* Note, if the default capacity is selected by the refit capacity
745 * callback, then the capacity shown is likely to be incorrect. */
747 SetDParam(1, capacity
);
748 SetDParam(2, refittable
? STR_PURCHASE_INFO_REFITTABLE
: STR_EMPTY
);
749 DrawString(left
, right
, y
, STR_PURCHASE_INFO_CAPACITY
);
751 y
+= FONT_HEIGHT_NORMAL
;
754 SetDParam(0, e
->GetRunningCost());
755 DrawString(left
, right
, y
, STR_PURCHASE_INFO_RUNNINGCOST
);
756 y
+= FONT_HEIGHT_NORMAL
;
758 uint16 range
= e
->GetRange();
761 DrawString(left
, right
, y
, STR_PURCHASE_INFO_AIRCRAFT_RANGE
);
762 y
+= FONT_HEIGHT_NORMAL
;
769 * Display additional text from NewGRF in the purchase information window
770 * @param left Left border of text bounding box
771 * @param right Right border of text bounding box
772 * @param y Top border of text bounding box
773 * @param engine Engine to query the additional purchase information for
774 * @return Bottom border of text bounding box
776 static uint
ShowAdditionalText(int left
, int right
, int y
, EngineID engine
)
778 uint16 callback
= GetVehicleCallback(CBID_VEHICLE_ADDITIONAL_TEXT
, 0, 0, engine
, NULL
);
779 if (callback
== CALLBACK_FAILED
|| callback
== 0x400) return y
;
780 const GRFFile
*grffile
= Engine::Get(engine
)->GetGRF();
781 if (callback
> 0x400) {
782 ErrorUnknownCallbackResult(grffile
->grfid
, CBID_VEHICLE_ADDITIONAL_TEXT
, callback
);
786 StartTextRefStackUsage(grffile
, 6);
787 uint result
= DrawStringMultiLine(left
, right
, y
, INT32_MAX
, GetGRFStringID(grffile
->grfid
, 0xD000 + callback
), TC_BLACK
);
788 StopTextRefStackUsage();
793 * Draw the purchase info details of a vehicle at a given location.
794 * @param left,right,y location where to draw the info
795 * @param engine_number the engine of which to draw the info of
796 * @return y after drawing all the text
798 int DrawVehiclePurchaseInfo(int left
, int right
, int y
, EngineID engine_number
)
800 const Engine
*e
= Engine::Get(engine_number
);
802 ConvertDateToYMD(e
->intro_date
, &ymd
);
803 bool refittable
= IsArticulatedVehicleRefittable(engine_number
);
804 bool articulated_cargo
= false;
807 default: NOT_REACHED();
809 if (e
->u
.rail
.railveh_type
== RAILVEH_WAGON
) {
810 y
= DrawRailWagonPurchaseInfo(left
, right
, y
, engine_number
, &e
->u
.rail
);
812 y
= DrawRailEnginePurchaseInfo(left
, right
, y
, engine_number
, &e
->u
.rail
);
814 articulated_cargo
= true;
818 y
= DrawRoadVehPurchaseInfo(left
, right
, y
, engine_number
);
819 articulated_cargo
= true;
823 y
= DrawShipPurchaseInfo(left
, right
, y
, engine_number
, refittable
);
827 y
= DrawAircraftPurchaseInfo(left
, right
, y
, engine_number
, refittable
);
831 if (articulated_cargo
) {
832 /* Cargo type + capacity, or N/A */
833 int new_y
= DrawCargoCapacityInfo(left
, right
, y
, engine_number
);
836 SetDParam(0, CT_INVALID
);
837 SetDParam(2, STR_EMPTY
);
838 DrawString(left
, right
, y
, STR_PURCHASE_INFO_CAPACITY
);
839 y
+= FONT_HEIGHT_NORMAL
;
845 /* Draw details that apply to all types except rail wagons. */
846 if (e
->type
!= VEH_TRAIN
|| e
->u
.rail
.railveh_type
!= RAILVEH_WAGON
) {
847 /* Design date - Life length */
848 SetDParam(0, ymd
.year
);
849 SetDParam(1, e
->GetLifeLengthInDays() / DAYS_IN_LEAP_YEAR
);
850 DrawString(left
, right
, y
, STR_PURCHASE_INFO_DESIGNED_LIFE
);
851 y
+= FONT_HEIGHT_NORMAL
;
854 SetDParam(0, ToPercent16(e
->reliability
));
855 DrawString(left
, right
, y
, STR_PURCHASE_INFO_RELIABILITY
);
856 y
+= FONT_HEIGHT_NORMAL
;
859 if (refittable
) y
= ShowRefitOptionsList(left
, right
, y
, engine_number
);
861 /* Additional text from NewGRF */
862 y
= ShowAdditionalText(left
, right
, y
, engine_number
);
868 * Engine drawing loop
869 * @param type Type of vehicle (VEH_*)
870 * @param l The left most location of the list
871 * @param r The right most location of the list
872 * @param y The top most location of the list
873 * @param eng_list What engines to draw
874 * @param min where to start in the list
875 * @param max where in the list to end
876 * @param selected_id what engine to highlight as selected, if any
877 * @param show_count Whether to show the amount of engines or not
878 * @param selected_group the group to list the engines of
880 void DrawEngineList(VehicleType type
, int l
, int r
, int y
, const GUIEngineList
*eng_list
, uint16 min
, uint16 max
, EngineID selected_id
, bool show_count
, GroupID selected_group
)
882 static const int sprite_y_offsets
[] = { -1, -1, -2, -2 };
884 /* Obligatory sanity checks! */
885 assert(max
<= eng_list
->Length());
887 bool rtl
= _current_text_dir
== TD_RTL
;
888 int step_size
= GetEngineListHeight(type
);
889 int sprite_left
= GetVehicleImageCellSize(type
, EIT_PURCHASE
).extend_left
;
890 int sprite_right
= GetVehicleImageCellSize(type
, EIT_PURCHASE
).extend_right
;
891 int sprite_width
= sprite_left
+ sprite_right
;
893 int sprite_x
= rtl
? r
- sprite_right
- 1 : l
+ sprite_left
+ 1;
894 int sprite_y_offset
= sprite_y_offsets
[type
] + step_size
/ 2;
896 Dimension replace_icon
= {0, 0};
899 replace_icon
= GetSpriteSize(SPR_GROUP_REPLACE_ACTIVE
);
900 SetDParamMaxDigits(0, 3, FS_SMALL
);
901 count_width
= GetStringBoundingBox(STR_TINY_BLACK_COMA
).width
;
904 int text_left
= l
+ (rtl
? WD_FRAMERECT_LEFT
+ replace_icon
.width
+ 8 + count_width
: sprite_width
+ WD_FRAMETEXT_LEFT
);
905 int text_right
= r
- (rtl
? sprite_width
+ WD_FRAMETEXT_RIGHT
: WD_FRAMERECT_RIGHT
+ replace_icon
.width
+ 8 + count_width
);
906 int replace_icon_left
= rtl
? l
+ WD_FRAMERECT_LEFT
: r
- WD_FRAMERECT_RIGHT
- replace_icon
.width
;
908 int count_right
= rtl
? text_left
: r
- WD_FRAMERECT_RIGHT
- replace_icon
.width
- 8;
910 int normal_text_y_offset
= (step_size
- FONT_HEIGHT_NORMAL
) / 2;
911 int small_text_y_offset
= step_size
- FONT_HEIGHT_SMALL
- WD_FRAMERECT_BOTTOM
- 1;
912 int replace_icon_y_offset
= (step_size
- replace_icon
.height
) / 2 - 1;
914 for (; min
< max
; min
++, y
+= step_size
) {
915 const EngineID engine
= (*eng_list
)[min
];
916 /* Note: num_engines is only used in the autoreplace GUI, so it is correct to use _local_company here. */
917 const uint num_engines
= GetGroupNumEngines(_local_company
, selected_group
, engine
);
919 const Engine
*e
= Engine::Get(engine
);
920 bool hidden
= HasBit(e
->company_hidden
, _local_company
);
921 StringID str
= hidden
? STR_HIDDEN_ENGINE_NAME
: STR_ENGINE_NAME
;
922 TextColour tc
= (engine
== selected_id
) ? TC_WHITE
: (TC_NO_SHADE
| (hidden
? TC_GREY
: TC_BLACK
));
924 SetDParam(0, engine
);
925 DrawString(text_left
, text_right
, y
+ normal_text_y_offset
, str
, tc
);
926 DrawVehicleEngine(l
, r
, sprite_x
, y
+ sprite_y_offset
, engine
, (show_count
&& num_engines
== 0) ? PALETTE_CRASH
: GetEnginePalette(engine
, _local_company
), EIT_PURCHASE
);
928 SetDParam(0, num_engines
);
929 DrawString(count_left
, count_right
, y
+ small_text_y_offset
, STR_TINY_BLACK_COMA
, TC_FROMSTRING
, SA_RIGHT
| SA_FORCE
);
930 if (EngineHasReplacementForCompany(Company::Get(_local_company
), engine
, selected_group
)) DrawSprite(SPR_GROUP_REPLACE_ACTIVE
, num_engines
== 0 ? PALETTE_CRASH
: PAL_NONE
, replace_icon_left
, y
+ replace_icon_y_offset
);
936 * Display the dropdown for the vehicle sort criteria.
937 * @param w Parent window (holds the dropdown button).
938 * @param vehicle_type %Vehicle type being sorted.
939 * @param selected Currently selected sort criterium.
940 * @param button Widget button.
942 void DisplayVehicleSortDropDown(Window
*w
, VehicleType vehicle_type
, int selected
, int button
)
944 uint32 hidden_mask
= 0;
945 /* Disable sorting by power or tractive effort when the original acceleration model for road vehicles is being used. */
946 if (vehicle_type
== VEH_ROAD
&& _settings_game
.vehicle
.roadveh_acceleration_model
== AM_ORIGINAL
) {
947 SetBit(hidden_mask
, 3); // power
948 SetBit(hidden_mask
, 4); // tractive effort
949 SetBit(hidden_mask
, 8); // power by running costs
951 /* Disable sorting by tractive effort when the original acceleration model for trains is being used. */
952 if (vehicle_type
== VEH_TRAIN
&& _settings_game
.vehicle
.train_acceleration_model
== AM_ORIGINAL
) {
953 SetBit(hidden_mask
, 4); // tractive effort
955 ShowDropDownMenu(w
, _engine_sort_listing
[vehicle_type
], selected
, button
, 0, hidden_mask
);
958 /** GUI for building vehicles. */
959 struct BuildVehicleWindow
: Window
{
960 VehicleType vehicle_type
; ///< Type of vehicles shown in the window.
962 RailTypeByte railtype
; ///< Rail type to show, or #RAILTYPE_END.
963 RoadTypes roadtypes
; ///< Road type to show, or #ROADTYPES_ALL.
964 } filter
; ///< Filter to apply.
965 bool descending_sort_order
; ///< Sort direction, @see _engine_sort_direction
966 byte sort_criteria
; ///< Current sort criterium.
967 bool show_hidden_engines
; ///< State of the 'show hidden engines' button.
968 bool listview_mode
; ///< If set, only display the available vehicles and do not show a 'build' button.
969 EngineID sel_engine
; ///< Currently selected engine, or #INVALID_ENGINE
970 EngineID rename_engine
; ///< Engine being renamed.
971 GUIEngineList eng_list
;
972 CargoID cargo_filter
[NUM_CARGO
+ 2]; ///< Available cargo filters; CargoID or CF_ANY or CF_NONE
973 StringID cargo_filter_texts
[NUM_CARGO
+ 3]; ///< Texts for filter_cargo, terminated by INVALID_STRING_ID
974 byte cargo_filter_criteria
; ///< Selected cargo filter
975 int details_height
; ///< Minimal needed height of the details panels (found so far).
978 BuildVehicleWindow(WindowDesc
*desc
, TileIndex tile
, VehicleType type
) : Window(desc
)
980 this->vehicle_type
= type
;
981 this->window_number
= tile
== INVALID_TILE
? (int)type
: tile
;
983 this->sel_engine
= INVALID_ENGINE
;
985 this->sort_criteria
= _engine_sort_last_criteria
[type
];
986 this->descending_sort_order
= _engine_sort_last_order
[type
];
987 this->show_hidden_engines
= _engine_sort_show_hidden_engines
[type
];
990 default: NOT_REACHED();
992 this->filter
.railtype
= (tile
== INVALID_TILE
) ? RAILTYPE_END
: GetRailType(tile
);
995 this->filter
.roadtypes
= (tile
== INVALID_TILE
) ? ROADTYPES_ALL
: GetRoadTypes(tile
);
1001 this->listview_mode
= (this->window_number
<= VEH_END
);
1003 this->CreateNestedTree();
1005 this->vscroll
= this->GetScrollbar(WID_BV_SCROLLBAR
);
1007 /* If we are just viewing the list of vehicles, we do not need the Build button.
1008 * So we just hide it, and enlarge the Rename button by the now vacant place. */
1009 if (this->listview_mode
) this->GetWidget
<NWidgetStacked
>(WID_BV_BUILD_SEL
)->SetDisplayedPlane(SZSP_NONE
);
1011 /* disable renaming engines in network games if you are not the server */
1012 this->SetWidgetDisabledState(WID_BV_RENAME
, _networking
&& !_network_server
);
1014 NWidgetCore
*widget
= this->GetWidget
<NWidgetCore
>(WID_BV_LIST
);
1015 widget
->tool_tip
= STR_BUY_VEHICLE_TRAIN_LIST_TOOLTIP
+ type
;
1017 widget
= this->GetWidget
<NWidgetCore
>(WID_BV_SHOW_HIDE
);
1018 widget
->tool_tip
= STR_BUY_VEHICLE_TRAIN_HIDE_SHOW_TOGGLE_TOOLTIP
+ type
;
1020 widget
= this->GetWidget
<NWidgetCore
>(WID_BV_BUILD
);
1021 widget
->widget_data
= STR_BUY_VEHICLE_TRAIN_BUY_VEHICLE_BUTTON
+ type
;
1022 widget
->tool_tip
= STR_BUY_VEHICLE_TRAIN_BUY_VEHICLE_TOOLTIP
+ type
;
1024 widget
= this->GetWidget
<NWidgetCore
>(WID_BV_RENAME
);
1025 widget
->widget_data
= STR_BUY_VEHICLE_TRAIN_RENAME_BUTTON
+ type
;
1026 widget
->tool_tip
= STR_BUY_VEHICLE_TRAIN_RENAME_TOOLTIP
+ type
;
1028 widget
= this->GetWidget
<NWidgetCore
>(WID_BV_SHOW_HIDDEN_ENGINES
);
1029 widget
->widget_data
= STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN
+ type
;
1030 widget
->tool_tip
= STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP
+ type
;
1031 widget
->SetLowered(this->show_hidden_engines
);
1033 this->details_height
= ((this->vehicle_type
== VEH_TRAIN
) ? 10 : 9) * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
1035 this->FinishInitNested(tile
== INVALID_TILE
? (int)type
: tile
);
1037 this->owner
= (tile
!= INVALID_TILE
) ? GetTileOwner(tile
) : _local_company
;
1039 this->eng_list
.ForceRebuild();
1040 this->GenerateBuildList(); // generate the list, since we need it in the next line
1041 /* Select the first engine in the list as default when opening the window */
1042 if (this->eng_list
.Length() > 0) this->sel_engine
= this->eng_list
[0];
1045 /** Populate the filter list and set the cargo filter criteria. */
1046 void SetCargoFilterArray()
1048 uint filter_items
= 0;
1050 /* Add item for disabling filtering. */
1051 this->cargo_filter
[filter_items
] = CF_ANY
;
1052 this->cargo_filter_texts
[filter_items
] = STR_PURCHASE_INFO_ALL_TYPES
;
1055 /* Add item for vehicles not carrying anything, e.g. train engines.
1056 * This could also be useful for eyecandy vehicles of other types, but is likely too confusing for joe, */
1057 if (this->vehicle_type
== VEH_TRAIN
) {
1058 this->cargo_filter
[filter_items
] = CF_NONE
;
1059 this->cargo_filter_texts
[filter_items
] = STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE
;
1063 /* Collect available cargo types for filtering. */
1064 const CargoSpec
*cs
;
1065 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs
) {
1066 this->cargo_filter
[filter_items
] = cs
->Index();
1067 this->cargo_filter_texts
[filter_items
] = cs
->name
;
1071 /* Terminate the filter list. */
1072 this->cargo_filter_texts
[filter_items
] = INVALID_STRING_ID
;
1074 /* If not found, the cargo criteria will be set to all cargoes. */
1075 this->cargo_filter_criteria
= 0;
1077 /* Find the last cargo filter criteria. */
1078 for (uint i
= 0; i
< filter_items
; i
++) {
1079 if (this->cargo_filter
[i
] == _engine_sort_last_cargo_criteria
[this->vehicle_type
]) {
1080 this->cargo_filter_criteria
= i
;
1085 this->eng_list
.SetFilterFuncs(_filter_funcs
);
1086 this->eng_list
.SetFilterState(this->cargo_filter
[this->cargo_filter_criteria
] != CF_ANY
);
1091 this->SetCargoFilterArray();
1094 /** Filter the engine list against the currently selected cargo filter */
1095 void FilterEngineList()
1097 this->eng_list
.Filter(this->cargo_filter
[this->cargo_filter_criteria
]);
1098 if (0 == this->eng_list
.Length()) { // no engine passed through the filter, invalidate the previously selected engine
1099 this->sel_engine
= INVALID_ENGINE
;
1100 } else if (!this->eng_list
.Contains(this->sel_engine
)) { // previously selected engine didn't pass the filter, select the first engine of the list
1101 this->sel_engine
= this->eng_list
[0];
1105 /** Filter a single engine */
1106 bool FilterSingleEngine(EngineID eid
)
1108 CargoID filter_type
= this->cargo_filter
[this->cargo_filter_criteria
];
1109 return (filter_type
== CF_ANY
|| CargoFilter(&eid
, filter_type
));
1112 /* Figure out what train EngineIDs to put in the list */
1113 void GenerateBuildTrainList()
1115 EngineID sel_id
= INVALID_ENGINE
;
1116 int num_engines
= 0;
1119 this->filter
.railtype
= (this->listview_mode
) ? RAILTYPE_END
: GetRailType(this->window_number
);
1121 this->eng_list
.Clear();
1123 /* Make list of all available train engines and wagons.
1124 * Also check to see if the previously selected engine is still available,
1125 * and if not, reset selection to INVALID_ENGINE. This could be the case
1126 * when engines become obsolete and are removed */
1128 FOR_ALL_ENGINES_OF_TYPE(e
, VEH_TRAIN
) {
1129 if (!this->show_hidden_engines
&& e
->IsHidden(_local_company
)) continue;
1130 EngineID eid
= e
->index
;
1131 const RailVehicleInfo
*rvi
= &e
->u
.rail
;
1133 if (this->filter
.railtype
!= RAILTYPE_END
&& !HasPowerOnRail(rvi
->railtype
, this->filter
.railtype
)) continue;
1134 if (!IsEngineBuildable(eid
, VEH_TRAIN
, _local_company
)) continue;
1136 /* Filter now! So num_engines and num_wagons is valid */
1137 if (!FilterSingleEngine(eid
)) continue;
1139 *this->eng_list
.Append() = eid
;
1141 if (rvi
->railveh_type
!= RAILVEH_WAGON
) {
1147 if (eid
== this->sel_engine
) sel_id
= eid
;
1150 this->sel_engine
= sel_id
;
1152 /* make engines first, and then wagons, sorted by selected sort_criteria */
1153 _engine_sort_direction
= false;
1154 EngList_Sort(&this->eng_list
, TrainEnginesThenWagonsSorter
);
1156 /* and then sort engines */
1157 _engine_sort_direction
= this->descending_sort_order
;
1158 EngList_SortPartial(&this->eng_list
, _engine_sort_functions
[0][this->sort_criteria
], 0, num_engines
);
1160 /* and finally sort wagons */
1161 EngList_SortPartial(&this->eng_list
, _engine_sort_functions
[0][this->sort_criteria
], num_engines
, num_wagons
);
1164 /* Figure out what road vehicle EngineIDs to put in the list */
1165 void GenerateBuildRoadVehList()
1167 EngineID sel_id
= INVALID_ENGINE
;
1169 this->eng_list
.Clear();
1172 FOR_ALL_ENGINES_OF_TYPE(e
, VEH_ROAD
) {
1173 if (!this->show_hidden_engines
&& e
->IsHidden(_local_company
)) continue;
1174 EngineID eid
= e
->index
;
1175 if (!IsEngineBuildable(eid
, VEH_ROAD
, _local_company
)) continue;
1176 if (!HasBit(this->filter
.roadtypes
, HasBit(EngInfo(eid
)->misc_flags
, EF_ROAD_TRAM
) ? ROADTYPE_TRAM
: ROADTYPE_ROAD
)) continue;
1177 *this->eng_list
.Append() = eid
;
1179 if (eid
== this->sel_engine
) sel_id
= eid
;
1181 this->sel_engine
= sel_id
;
1184 /* Figure out what ship EngineIDs to put in the list */
1185 void GenerateBuildShipList()
1187 EngineID sel_id
= INVALID_ENGINE
;
1188 this->eng_list
.Clear();
1191 FOR_ALL_ENGINES_OF_TYPE(e
, VEH_SHIP
) {
1192 if (!this->show_hidden_engines
&& e
->IsHidden(_local_company
)) continue;
1193 EngineID eid
= e
->index
;
1194 if (!IsEngineBuildable(eid
, VEH_SHIP
, _local_company
)) continue;
1195 *this->eng_list
.Append() = eid
;
1197 if (eid
== this->sel_engine
) sel_id
= eid
;
1199 this->sel_engine
= sel_id
;
1202 /* Figure out what aircraft EngineIDs to put in the list */
1203 void GenerateBuildAircraftList()
1205 EngineID sel_id
= INVALID_ENGINE
;
1207 this->eng_list
.Clear();
1209 const Station
*st
= this->listview_mode
? NULL
: Station::GetByTile(this->window_number
);
1211 /* Make list of all available planes.
1212 * Also check to see if the previously selected plane is still available,
1213 * and if not, reset selection to INVALID_ENGINE. This could be the case
1214 * when planes become obsolete and are removed */
1216 FOR_ALL_ENGINES_OF_TYPE(e
, VEH_AIRCRAFT
) {
1217 if (!this->show_hidden_engines
&& e
->IsHidden(_local_company
)) continue;
1218 EngineID eid
= e
->index
;
1219 if (!IsEngineBuildable(eid
, VEH_AIRCRAFT
, _local_company
)) continue;
1220 /* First VEH_END window_numbers are fake to allow a window open for all different types at once */
1221 if (!this->listview_mode
&& !CanVehicleUseStation(eid
, st
)) continue;
1223 *this->eng_list
.Append() = eid
;
1224 if (eid
== this->sel_engine
) sel_id
= eid
;
1227 this->sel_engine
= sel_id
;
1230 /* Generate the list of vehicles */
1231 void GenerateBuildList()
1233 if (!this->eng_list
.NeedRebuild()) return;
1234 switch (this->vehicle_type
) {
1235 default: NOT_REACHED();
1237 this->GenerateBuildTrainList();
1238 this->eng_list
.Compact();
1239 this->eng_list
.RebuildDone();
1240 return; // trains should not reach the last sorting
1242 this->GenerateBuildRoadVehList();
1245 this->GenerateBuildShipList();
1248 this->GenerateBuildAircraftList();
1252 this->FilterEngineList();
1254 _engine_sort_direction
= this->descending_sort_order
;
1255 EngList_Sort(&this->eng_list
, _engine_sort_functions
[this->vehicle_type
][this->sort_criteria
]);
1257 this->eng_list
.Compact();
1258 this->eng_list
.RebuildDone();
1261 void OnClick(Point pt
, int widget
, int click_count
)
1264 case WID_BV_SORT_ASCENDING_DESCENDING
:
1265 this->descending_sort_order
^= true;
1266 _engine_sort_last_order
[this->vehicle_type
] = this->descending_sort_order
;
1267 this->eng_list
.ForceRebuild();
1271 case WID_BV_SHOW_HIDDEN_ENGINES
:
1272 this->show_hidden_engines
^= true;
1273 _engine_sort_show_hidden_engines
[this->vehicle_type
] = this->show_hidden_engines
;
1274 this->eng_list
.ForceRebuild();
1275 this->SetWidgetLoweredState(widget
, this->show_hidden_engines
);
1280 uint i
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_BV_LIST
);
1281 size_t num_items
= this->eng_list
.Length();
1282 this->sel_engine
= (i
< num_items
) ? this->eng_list
[i
] : INVALID_ENGINE
;
1284 if (_ctrl_pressed
) {
1285 this->OnClick(pt
, WID_BV_SHOW_HIDE
, 1);
1286 } else if (click_count
> 1 && !this->listview_mode
) {
1287 this->OnClick(pt
, WID_BV_BUILD
, 1);
1292 case WID_BV_SORT_DROPDOWN
: // Select sorting criteria dropdown menu
1293 DisplayVehicleSortDropDown(this, this->vehicle_type
, this->sort_criteria
, WID_BV_SORT_DROPDOWN
);
1296 case WID_BV_CARGO_FILTER_DROPDOWN
: // Select cargo filtering criteria dropdown menu
1297 ShowDropDownMenu(this, this->cargo_filter_texts
, this->cargo_filter_criteria
, WID_BV_CARGO_FILTER_DROPDOWN
, 0, 0);
1300 case WID_BV_SHOW_HIDE
: {
1301 const Engine
*e
= (this->sel_engine
== INVALID_ENGINE
) ? NULL
: Engine::Get(this->sel_engine
);
1303 DoCommandP(0, 0, this->sel_engine
| (e
->IsHidden(_current_company
) ? 0 : (1u << 31)), CMD_SET_VEHICLE_VISIBILITY
);
1308 case WID_BV_BUILD
: {
1309 EngineID sel_eng
= this->sel_engine
;
1310 if (sel_eng
!= INVALID_ENGINE
) {
1311 CommandCallback
*callback
= (this->vehicle_type
== VEH_TRAIN
&& RailVehInfo(sel_eng
)->railveh_type
== RAILVEH_WAGON
) ? CcBuildWagon
: CcBuildPrimaryVehicle
;
1312 DoCommandP(this->window_number
, sel_eng
, 0, GetCmdBuildVeh(this->vehicle_type
), callback
);
1317 case WID_BV_RENAME
: {
1318 EngineID sel_eng
= this->sel_engine
;
1319 if (sel_eng
!= INVALID_ENGINE
) {
1320 this->rename_engine
= sel_eng
;
1321 SetDParam(0, sel_eng
);
1322 ShowQueryString(STR_ENGINE_NAME
, STR_QUERY_RENAME_TRAIN_TYPE_CAPTION
+ this->vehicle_type
, MAX_LENGTH_ENGINE_NAME_CHARS
, this, CS_ALPHANUMERAL
, QSF_ENABLE_DEFAULT
| QSF_LEN_IN_CHARS
);
1330 * Some data on this window has become invalid.
1331 * @param data Information about the changed data.
1332 * @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.
1334 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
1336 if (!gui_scope
) return;
1337 /* When switching to original acceleration model for road vehicles, clear the selected sort criteria if it is not available now. */
1338 if (this->vehicle_type
== VEH_ROAD
&&
1339 _settings_game
.vehicle
.roadveh_acceleration_model
== AM_ORIGINAL
&&
1340 this->sort_criteria
> 7) {
1341 this->sort_criteria
= 0;
1342 _engine_sort_last_criteria
[VEH_ROAD
] = 0;
1344 this->eng_list
.ForceRebuild();
1347 virtual void SetStringParameters(int widget
) const
1350 case WID_BV_CAPTION
:
1351 if (this->vehicle_type
== VEH_TRAIN
&& !this->listview_mode
) {
1352 const RailtypeInfo
*rti
= GetRailTypeInfo(this->filter
.railtype
);
1353 SetDParam(0, rti
->strings
.build_caption
);
1355 SetDParam(0, (this->listview_mode
? STR_VEHICLE_LIST_AVAILABLE_TRAINS
: STR_BUY_VEHICLE_TRAIN_ALL_CAPTION
) + this->vehicle_type
);
1359 case WID_BV_SORT_DROPDOWN
:
1360 SetDParam(0, _engine_sort_listing
[this->vehicle_type
][this->sort_criteria
]);
1363 case WID_BV_CARGO_FILTER_DROPDOWN
:
1364 SetDParam(0, this->cargo_filter_texts
[this->cargo_filter_criteria
]);
1367 case WID_BV_SHOW_HIDE
: {
1368 const Engine
*e
= (this->sel_engine
== INVALID_ENGINE
) ? NULL
: Engine::Get(this->sel_engine
);
1369 if (e
!= NULL
&& e
->IsHidden(_local_company
)) {
1370 SetDParam(0, STR_BUY_VEHICLE_TRAIN_SHOW_TOGGLE_BUTTON
+ this->vehicle_type
);
1372 SetDParam(0, STR_BUY_VEHICLE_TRAIN_HIDE_TOGGLE_BUTTON
+ this->vehicle_type
);
1379 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
1383 resize
->height
= GetEngineListHeight(this->vehicle_type
);
1384 size
->height
= 3 * resize
->height
;
1385 size
->width
= max(size
->width
, GetVehicleImageCellSize(this->vehicle_type
, EIT_PURCHASE
).extend_left
+ GetVehicleImageCellSize(this->vehicle_type
, EIT_PURCHASE
).extend_right
+ 165);
1389 size
->height
= this->details_height
;
1392 case WID_BV_SORT_ASCENDING_DESCENDING
: {
1393 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
1394 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
1395 d
.height
+= padding
.height
;
1396 *size
= maxdim(*size
, d
);
1400 case WID_BV_SHOW_HIDE
:
1401 *size
= GetStringBoundingBox(STR_BUY_VEHICLE_TRAIN_HIDE_TOGGLE_BUTTON
+ this->vehicle_type
);
1402 *size
= maxdim(*size
, GetStringBoundingBox(STR_BUY_VEHICLE_TRAIN_SHOW_TOGGLE_BUTTON
+ this->vehicle_type
));
1403 size
->width
+= padding
.width
;
1404 size
->height
+= padding
.height
;
1409 virtual void DrawWidget(const Rect
&r
, int widget
) const
1413 DrawEngineList(this->vehicle_type
, r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, r
.top
+ WD_FRAMERECT_TOP
, &this->eng_list
, this->vscroll
->GetPosition(), min(this->vscroll
->GetPosition() + this->vscroll
->GetCapacity(), this->eng_list
.Length()), this->sel_engine
, false, DEFAULT_GROUP
);
1416 case WID_BV_SORT_ASCENDING_DESCENDING
:
1417 this->DrawSortButtonState(WID_BV_SORT_ASCENDING_DESCENDING
, this->descending_sort_order
? SBS_DOWN
: SBS_UP
);
1422 virtual void OnPaint()
1424 this->GenerateBuildList();
1425 this->vscroll
->SetCount(this->eng_list
.Length());
1427 this->SetWidgetDisabledState(WID_BV_SHOW_HIDE
, this->sel_engine
== INVALID_ENGINE
);
1429 this->DrawWidgets();
1431 if (!this->IsShaded()) {
1432 int needed_height
= this->details_height
;
1433 /* Draw details panels. */
1434 if (this->sel_engine
!= INVALID_ENGINE
) {
1435 NWidgetBase
*nwi
= this->GetWidget
<NWidgetBase
>(WID_BV_PANEL
);
1436 int text_end
= DrawVehiclePurchaseInfo(nwi
->pos_x
+ WD_FRAMETEXT_LEFT
, nwi
->pos_x
+ nwi
->current_x
- WD_FRAMETEXT_RIGHT
,
1437 nwi
->pos_y
+ WD_FRAMERECT_TOP
, this->sel_engine
);
1438 needed_height
= max(needed_height
, text_end
- (int)nwi
->pos_y
+ WD_FRAMERECT_BOTTOM
);
1440 if (needed_height
!= this->details_height
) { // Details window are not high enough, enlarge them.
1441 int resize
= needed_height
- this->details_height
;
1442 this->details_height
= needed_height
;
1443 this->ReInit(0, resize
);
1449 virtual void OnQueryTextFinished(char *str
)
1451 if (str
== NULL
) return;
1453 DoCommandP(0, this->rename_engine
, 0, CMD_RENAME_ENGINE
| CMD_MSG(STR_ERROR_CAN_T_RENAME_TRAIN_TYPE
+ this->vehicle_type
), NULL
, str
);
1456 virtual void OnDropdownSelect(int widget
, int index
)
1459 case WID_BV_SORT_DROPDOWN
:
1460 if (this->sort_criteria
!= index
) {
1461 this->sort_criteria
= index
;
1462 _engine_sort_last_criteria
[this->vehicle_type
] = this->sort_criteria
;
1463 this->eng_list
.ForceRebuild();
1467 case WID_BV_CARGO_FILTER_DROPDOWN
: // Select a cargo filter criteria
1468 if (this->cargo_filter_criteria
!= index
) {
1469 this->cargo_filter_criteria
= index
;
1470 _engine_sort_last_cargo_criteria
[this->vehicle_type
] = this->cargo_filter
[this->cargo_filter_criteria
];
1471 /* deactivate filter if criteria is 'Show All', activate it otherwise */
1472 this->eng_list
.SetFilterState(this->cargo_filter
[this->cargo_filter_criteria
] != CF_ANY
);
1473 this->eng_list
.ForceRebuild();
1480 virtual void OnResize()
1482 this->vscroll
->SetCapacityFromWidget(this, WID_BV_LIST
);
1486 static WindowDesc
_build_vehicle_desc(
1487 WDP_AUTO
, "build_vehicle", 240, 268,
1488 WC_BUILD_VEHICLE
, WC_NONE
,
1490 _nested_build_vehicle_widgets
, lengthof(_nested_build_vehicle_widgets
)
1493 void ShowBuildVehicleWindow(TileIndex tile
, VehicleType type
)
1495 /* We want to be able to open both Available Train as Available Ships,
1496 * so if tile == INVALID_TILE (Available XXX Window), use 'type' as unique number.
1497 * As it always is a low value, it won't collide with any real tile
1499 uint num
= (tile
== INVALID_TILE
) ? (int)type
: tile
;
1501 assert(IsCompanyBuildableVehicleType(type
));
1503 DeleteWindowById(WC_BUILD_VEHICLE
, num
);
1505 new BuildVehicleWindow(&_build_vehicle_desc
, tile
, type
);