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 train_gui.cpp GUI for trains. */
11 #include "window_gui.h"
12 #include "command_func.h"
14 #include "strings_func.h"
15 #include "vehicle_func.h"
16 #include "zoom_func.h"
18 #include "table/strings.h"
20 #include "safeguards.h"
23 * Callback for building wagons.
24 * @param result The result of the command.
25 * @param tile The tile the command was executed on.
26 * @param p1 Additional data for the command (for the #CommandProc)
27 * @param p2 Additional data for the command (for the #CommandProc)
30 void CcBuildWagon(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
, uint32 cmd
)
32 if (result
.Failed()) return;
34 /* find a locomotive in the depot. */
35 const Vehicle
*found
= nullptr;
36 for (const Train
*t
: Train::Iterate()) {
37 if (t
->IsFrontEngine() && t
->tile
== tile
&& t
->IsStoppedInDepot()) {
38 if (found
!= nullptr) return; // must be exactly one.
43 /* if we found a loco, */
44 if (found
!= nullptr) {
45 found
= found
->Last();
46 /* put the new wagon at the end of the loco. */
47 DoCommandP(0, _new_vehicle_id
, found
->index
, CMD_MOVE_RAIL_VEHICLE
);
48 InvalidateWindowClassesData(WC_TRAINS_LIST
, 0);
53 * Highlight the position where a rail vehicle is dragged over by drawing a light gray background.
54 * @param px The current x position to draw from.
55 * @param max_width The maximum space available to draw.
56 * @param selection Selected vehicle that is dragged.
57 * @param chain Whether a whole chain is dragged.
58 * @return The width of the highlight mark.
60 static int HighlightDragPosition(int px
, int max_width
, VehicleID selection
, bool chain
)
62 bool rtl
= _current_text_dir
== TD_RTL
;
64 assert(selection
!= INVALID_VEHICLE
);
65 int dragged_width
= 0;
66 for (Train
*t
= Train::Get(selection
); t
!= nullptr; t
= chain
? t
->Next() : (t
->HasArticulatedPart() ? t
->GetNextArticulatedPart() : nullptr)) {
67 dragged_width
+= t
->GetDisplayImageWidth(nullptr);
70 int drag_hlight_left
= rtl
? std::max(px
- dragged_width
+ 1, 0) : px
;
71 int drag_hlight_right
= rtl
? px
: std::min(px
+ dragged_width
, max_width
) - 1;
72 int drag_hlight_width
= std::max(drag_hlight_right
- drag_hlight_left
+ 1, 0);
74 if (drag_hlight_width
> 0) {
75 GfxFillRect(drag_hlight_left
+ WD_FRAMERECT_LEFT
, WD_FRAMERECT_TOP
+ 1,
76 drag_hlight_right
- WD_FRAMERECT_RIGHT
, ScaleGUITrad(13) - WD_FRAMERECT_BOTTOM
, _colour_gradient
[COLOUR_GREY
][7]);
79 return drag_hlight_width
;
83 * Draws an image of a whole train
84 * @param v Front vehicle
85 * @param left The minimum horizontal position
86 * @param right The maximum horizontal position
87 * @param y Vertical position to draw at
88 * @param selection Selected vehicle to draw a frame around
89 * @param skip Number of pixels to skip at the front (for scrolling)
90 * @param drag_dest The vehicle another one is dragged over, \c INVALID_VEHICLE if none.
92 void DrawTrainImage(const Train
*v
, int left
, int right
, int y
, VehicleID selection
, EngineImageType image_type
, int skip
, VehicleID drag_dest
)
94 bool rtl
= _current_text_dir
== TD_RTL
;
95 Direction dir
= rtl
? DIR_E
: DIR_W
;
97 DrawPixelInfo tmp_dpi
, *old_dpi
;
98 /* Position of highlight box */
101 int max_width
= right
- left
+ 1;
102 int height
= ScaleGUITrad(14);
104 if (!FillDrawPixelInfo(&tmp_dpi
, left
, y
, max_width
, height
)) return;
109 int px
= rtl
? max_width
+ skip
: -skip
;
110 bool sel_articulated
= false;
111 bool dragging
= (drag_dest
!= INVALID_VEHICLE
);
112 bool drag_at_end_of_train
= (drag_dest
== v
->index
); // Head index is used to mark dragging at end of train.
113 for (; v
!= nullptr && (rtl
? px
> 0 : px
< max_width
); v
= v
->Next()) {
114 if (dragging
&& !drag_at_end_of_train
&& drag_dest
== v
->index
) {
115 /* Highlight the drag-and-drop destination inside the train. */
116 int drag_hlight_width
= HighlightDragPosition(px
, max_width
, selection
, _cursor
.vehchain
);
117 px
+= rtl
? -drag_hlight_width
: drag_hlight_width
;
121 int width
= Train::From(v
)->GetDisplayImageWidth(&offset
);
123 if (rtl
? px
+ width
> 0 : px
- width
< max_width
) {
124 PaletteID pal
= (v
->vehstatus
& VS_CRASHED
) ? PALETTE_CRASH
: GetVehiclePalette(v
);
125 VehicleSpriteSeq seq
;
126 v
->GetImage(dir
, image_type
, &seq
);
127 seq
.Draw(px
+ (rtl
? -offset
.x
: offset
.x
), height
/ 2 + offset
.y
, pal
, (v
->vehstatus
& VS_CRASHED
) != 0);
130 if (!v
->IsArticulatedPart()) sel_articulated
= false;
132 if (v
->index
== selection
) {
133 /* Set the highlight position */
134 highlight_l
= rtl
? px
- width
: px
;
135 highlight_r
= rtl
? px
- 1 : px
+ width
- 1;
136 sel_articulated
= true;
137 } else if ((_cursor
.vehchain
&& highlight_r
!= 0) || sel_articulated
) {
139 highlight_l
-= width
;
141 highlight_r
+= width
;
145 px
+= rtl
? -width
: width
;
148 if (dragging
&& drag_at_end_of_train
) {
149 /* Highlight the drag-and-drop destination at the end of the train. */
150 HighlightDragPosition(px
, max_width
, selection
, _cursor
.vehchain
);
153 if (highlight_l
!= highlight_r
) {
154 /* Draw the highlight. Now done after drawing all the engines, as
155 * the next engine after the highlight could overlap it. */
156 DrawFrameRect(highlight_l
, 0, highlight_r
, height
- 1, COLOUR_WHITE
, FR_BORDERONLY
);
162 /** Helper struct for the cargo details information */
163 struct CargoSummaryItem
{
164 CargoID cargo
; ///< The cargo that is carried
165 StringID subtype
; ///< STR_EMPTY if none
166 uint capacity
; ///< Amount that can be carried
167 uint amount
; ///< Amount that is carried
168 StationID source
; ///< One of the source stations
170 /** Used by CargoSummary::Find() and similar functions */
171 inline bool operator != (const CargoSummaryItem
&other
) const
173 return this->cargo
!= other
.cargo
|| this->subtype
!= other
.subtype
;
176 /** Used by std::find() and similar functions */
177 inline bool operator == (const CargoSummaryItem
&other
) const
179 return !(this->cargo
!= other
.cargo
);
183 static const uint TRAIN_DETAILS_MIN_INDENT
= 32; ///< Minimum indent level in the train details window
184 static const uint TRAIN_DETAILS_MAX_INDENT
= 72; ///< Maximum indent level in the train details window; wider than this and we start on a new line
186 /** Container for the cargo summary information. */
187 typedef std::vector
<CargoSummaryItem
> CargoSummary
;
188 /** Reused container of cargo details */
189 static CargoSummary _cargo_summary
;
192 * Draw the details cargo tab for the given vehicle at the given position
194 * @param item Data to draw
195 * @param left The left most coordinate to draw
196 * @param right The right most coordinate to draw
197 * @param y The y coordinate
199 static void TrainDetailsCargoTab(const CargoSummaryItem
*item
, int left
, int right
, int y
)
202 if (item
->amount
> 0) {
203 SetDParam(0, item
->cargo
);
204 SetDParam(1, item
->amount
);
205 SetDParam(2, item
->source
);
206 SetDParam(3, _settings_game
.vehicle
.freight_trains
);
207 str
= FreightWagonMult(item
->cargo
) > 1 ? STR_VEHICLE_DETAILS_CARGO_FROM_MULT
: STR_VEHICLE_DETAILS_CARGO_FROM
;
209 SetDParam(0, STR_QUANTITY_N_A
);
210 str
= item
->cargo
== INVALID_CARGO
? STR_LTBLUE_STRING
: STR_VEHICLE_DETAILS_CARGO_EMPTY
;
213 DrawString(left
, right
, y
, str
);
217 * Draw the details info tab for the given vehicle at the given position
219 * @param v current vehicle
220 * @param left The left most coordinate to draw
221 * @param right The right most coordinate to draw
222 * @param y The y coordinate
224 static void TrainDetailsInfoTab(const Vehicle
*v
, int left
, int right
, int y
)
226 if (RailVehInfo(v
->engine_type
)->railveh_type
== RAILVEH_WAGON
) {
227 SetDParam(0, v
->engine_type
);
228 SetDParam(1, v
->value
);
229 DrawString(left
, right
, y
, STR_VEHICLE_DETAILS_TRAIN_WAGON_VALUE
);
231 SetDParam(0, v
->engine_type
);
232 SetDParam(1, v
->build_year
);
233 SetDParam(2, v
->value
);
234 DrawString(left
, right
, y
, STR_VEHICLE_DETAILS_TRAIN_ENGINE_BUILT_AND_VALUE
);
239 * Draw the details capacity tab for the given vehicle at the given position
241 * @param item Data to draw
242 * @param left The left most coordinate to draw
243 * @param right The right most coordinate to draw
244 * @param y The y coordinate
246 static void TrainDetailsCapacityTab(const CargoSummaryItem
*item
, int left
, int right
, int y
)
249 if (item
->cargo
!= INVALID_CARGO
) {
250 SetDParam(0, item
->cargo
);
251 SetDParam(1, item
->capacity
);
252 SetDParam(4, item
->subtype
);
253 SetDParam(5, _settings_game
.vehicle
.freight_trains
);
254 str
= FreightWagonMult(item
->cargo
) > 1 ? STR_VEHICLE_INFO_CAPACITY_MULT
: STR_VEHICLE_INFO_CAPACITY
;
256 /* Draw subtype only */
257 SetDParam(0, item
->subtype
);
258 str
= STR_VEHICLE_INFO_NO_CAPACITY
;
260 DrawString(left
, right
, y
, str
);
264 * Collects the cargo transported
265 * @param v Vehicle to process
266 * @param summary Space for the result
268 static void GetCargoSummaryOfArticulatedVehicle(const Train
*v
, CargoSummary
*summary
)
272 if (!v
->GetEngine()->CanCarryCargo()) continue;
274 CargoSummaryItem new_item
;
275 new_item
.cargo
= v
->cargo_cap
> 0 ? v
->cargo_type
: INVALID_CARGO
;
276 new_item
.subtype
= GetCargoSubtypeText(v
);
277 if (new_item
.cargo
== INVALID_CARGO
&& new_item
.subtype
== STR_EMPTY
) continue;
279 auto item
= std::find(summary
->begin(), summary
->end(), new_item
);
280 if (item
== summary
->end()) {
281 summary
->emplace_back();
282 item
= summary
->end() - 1;
283 item
->cargo
= new_item
.cargo
;
284 item
->subtype
= new_item
.subtype
;
287 item
->source
= INVALID_STATION
;
290 item
->capacity
+= v
->cargo_cap
;
291 item
->amount
+= v
->cargo
.StoredCount();
292 if (item
->source
== INVALID_STATION
) item
->source
= v
->cargo
.Source();
293 } while ((v
= v
->Next()) != nullptr && v
->IsArticulatedPart());
297 * Get the length of an articulated vehicle.
298 * @param v the vehicle to get the length of.
299 * @return the length in pixels.
301 static uint
GetLengthOfArticulatedVehicle(const Train
*v
)
306 length
+= v
->GetDisplayImageWidth();
307 } while ((v
= v
->Next()) != nullptr && v
->IsArticulatedPart());
313 * Determines the number of lines in the train details window
314 * @param veh_id Train
315 * @param det_tab Selected details tab
316 * @return Number of line
318 int GetTrainDetailsWndVScroll(VehicleID veh_id
, TrainDetailsWindowTabs det_tab
)
322 if (det_tab
== TDW_TAB_TOTALS
) { // Total cargo tab
323 CargoArray act_cargo
;
324 CargoArray max_cargo
;
325 for (const Vehicle
*v
= Vehicle::Get(veh_id
); v
!= nullptr; v
= v
->Next()) {
326 act_cargo
[v
->cargo_type
] += v
->cargo
.StoredCount();
327 max_cargo
[v
->cargo_type
] += v
->cargo_cap
;
330 /* Set scroll-amount separately from counting, as to not compute num double
331 * for more carriages of the same type
333 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
334 if (max_cargo
[i
] > 0) num
++; // only count carriages that the train has
336 num
++; // needs one more because first line is description string
338 for (const Train
*v
= Train::Get(veh_id
); v
!= nullptr; v
= v
->GetNextVehicle()) {
339 GetCargoSummaryOfArticulatedVehicle(v
, &_cargo_summary
);
340 num
+= std::max(1u, (unsigned)_cargo_summary
.size());
342 uint length
= GetLengthOfArticulatedVehicle(v
);
343 if (length
> TRAIN_DETAILS_MAX_INDENT
) num
++;
351 * Draw the details for the given vehicle at the given position
353 * @param v current vehicle
354 * @param left The left most coordinate to draw
355 * @param right The right most coordinate to draw
356 * @param y The y coordinate
357 * @param vscroll_pos Position of scrollbar
358 * @param vscroll_cap Number of lines currently displayed
359 * @param det_tab Selected details tab
361 void DrawTrainDetails(const Train
*v
, int left
, int right
, int y
, int vscroll_pos
, uint16 vscroll_cap
, TrainDetailsWindowTabs det_tab
)
363 /* get rid of awkward offset */
366 int sprite_height
= ScaleGUITrad(GetVehicleHeight(VEH_TRAIN
));
367 int line_height
= std::max(sprite_height
, WD_MATRIX_TOP
+ FONT_HEIGHT_NORMAL
+ WD_MATRIX_BOTTOM
);
368 int sprite_y_offset
= line_height
/ 2;
369 int text_y_offset
= (line_height
- FONT_HEIGHT_NORMAL
) / 2;
371 /* draw the first 3 details tabs */
372 if (det_tab
!= TDW_TAB_TOTALS
) {
373 bool rtl
= _current_text_dir
== TD_RTL
;
374 Direction dir
= rtl
? DIR_E
: DIR_W
;
375 int x
= rtl
? right
: left
;
376 for (; v
!= nullptr && vscroll_pos
> -vscroll_cap
; v
= v
->GetNextVehicle()) {
377 GetCargoSummaryOfArticulatedVehicle(v
, &_cargo_summary
);
385 int width
= u
->GetDisplayImageWidth(&offset
);
386 if (vscroll_pos
<= 0 && vscroll_pos
> -vscroll_cap
) {
388 const Engine
*e
= Engine::Get(v
->engine_type
);
389 if (e
->GetGRF() != nullptr) {
390 pitch
= ScaleGUITrad(e
->GetGRF()->traininfo_vehicle_pitch
);
392 PaletteID pal
= (v
->vehstatus
& VS_CRASHED
) ? PALETTE_CRASH
: GetVehiclePalette(v
);
393 VehicleSpriteSeq seq
;
394 u
->GetImage(dir
, EIT_IN_DETAILS
, &seq
);
395 seq
.Draw(px
+ (rtl
? -offset
.x
: offset
.x
), y
- line_height
* vscroll_pos
+ sprite_y_offset
+ pitch
, pal
, (v
->vehstatus
& VS_CRASHED
) != 0);
397 px
+= rtl
? -width
: width
;
400 } while (u
!= nullptr && u
->IsArticulatedPart());
402 bool separate_sprite_row
= (dx
> (uint
)ScaleGUITrad(TRAIN_DETAILS_MAX_INDENT
));
403 if (separate_sprite_row
) {
408 uint num_lines
= std::max(1u, (unsigned)_cargo_summary
.size());
409 for (uint i
= 0; i
< num_lines
; i
++) {
410 int sprite_width
= std::max
<int>(dx
, ScaleGUITrad(TRAIN_DETAILS_MIN_INDENT
)) + 3;
411 int data_left
= left
+ (rtl
? 0 : sprite_width
);
412 int data_right
= right
- (rtl
? sprite_width
: 0);
413 if (vscroll_pos
<= 0 && vscroll_pos
> -vscroll_cap
) {
414 int py
= y
- line_height
* vscroll_pos
+ text_y_offset
;
415 if (i
> 0 || separate_sprite_row
) {
416 if (vscroll_pos
!= 0) GfxFillRect(left
, py
- WD_MATRIX_TOP
- 1, right
, py
- WD_MATRIX_TOP
, _colour_gradient
[COLOUR_GREY
][5]);
420 if (i
< _cargo_summary
.size()) {
421 TrainDetailsCargoTab(&_cargo_summary
[i
], data_left
, data_right
, py
);
423 DrawString(data_left
, data_right
, py
, STR_QUANTITY_N_A
, TC_LIGHT_BLUE
);
428 if (i
== 0) TrainDetailsInfoTab(v
, data_left
, data_right
, py
);
431 case TDW_TAB_CAPACITY
:
432 if (i
< _cargo_summary
.size()) {
433 TrainDetailsCapacityTab(&_cargo_summary
[i
], data_left
, data_right
, py
);
435 SetDParam(0, STR_EMPTY
);
436 DrawString(data_left
, data_right
, py
, STR_VEHICLE_INFO_NO_CAPACITY
);
440 default: NOT_REACHED();
447 CargoArray act_cargo
;
448 CargoArray max_cargo
;
449 Money feeder_share
= 0;
451 for (const Vehicle
*u
= v
; u
!= nullptr; u
= u
->Next()) {
452 act_cargo
[u
->cargo_type
] += u
->cargo
.StoredCount();
453 max_cargo
[u
->cargo_type
] += u
->cargo_cap
;
454 feeder_share
+= u
->cargo
.FeederShare();
457 /* draw total cargo tab */
458 DrawString(left
, right
, y
+ text_y_offset
, STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_TEXT
);
461 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
462 if (max_cargo
[i
] > 0 && --vscroll_pos
< 0 && vscroll_pos
> -vscroll_cap
) {
463 SetDParam(0, i
); // {CARGO} #1
464 SetDParam(1, act_cargo
[i
]); // {CARGO} #2
465 SetDParam(2, i
); // {SHORTCARGO} #1
466 SetDParam(3, max_cargo
[i
]); // {SHORTCARGO} #2
467 SetDParam(4, _settings_game
.vehicle
.freight_trains
);
468 DrawString(left
, right
, y
+ text_y_offset
, FreightWagonMult(i
) > 1 ? STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_MULT
: STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY
);
472 SetDParam(0, feeder_share
);
473 DrawString(left
, right
, y
+ text_y_offset
, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE
);