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 train_gui.cpp GUI for trains. */
13 #include "window_gui.h"
14 #include "command_func.h"
16 #include "strings_func.h"
17 #include "vehicle_func.h"
18 #include "zoom_func.h"
20 #include "table/strings.h"
22 #include "safeguards.h"
25 * Callback for building wagons.
26 * @param result The result of the command.
27 * @param tile The tile the command was executed on.
28 * @param p1 Additional data for the command (for the #CommandProc)
29 * @param p2 Additional data for the command (for the #CommandProc)
31 void CcBuildWagon(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
33 if (result
.Failed()) return;
35 /* find a locomotive in the depot. */
36 const Vehicle
*found
= NULL
;
39 if (t
->IsFrontEngine() && t
->tile
== tile
&& t
->IsStoppedInDepot()) {
40 if (found
!= NULL
) return; // must be exactly one.
45 /* if we found a loco, */
47 found
= found
->Last();
48 /* put the new wagon at the end of the loco. */
49 DoCommandP(0, _new_vehicle_id
, found
->index
, CMD_MOVE_RAIL_VEHICLE
);
50 InvalidateWindowClassesData(WC_TRAINS_LIST
, 0);
55 * Highlight the position where a rail vehicle is dragged over by drawing a light gray background.
56 * @param px The current x position to draw from.
57 * @param max_width The maximum space available to draw.
58 * @param selection Selected vehicle that is dragged.
59 * @param chain Whether a whole chain is dragged.
60 * @return The width of the highlight mark.
62 static int HighlightDragPosition(int px
, int max_width
, VehicleID selection
, bool chain
)
64 bool rtl
= _current_text_dir
== TD_RTL
;
66 assert(selection
!= INVALID_VEHICLE
);
67 int dragged_width
= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
68 for (Train
*t
= Train::Get(selection
); t
!= NULL
; t
= chain
? t
->Next() : (t
->HasArticulatedPart() ? t
->GetNextArticulatedPart() : NULL
)) {
69 dragged_width
+= t
->GetDisplayImageWidth(NULL
);
72 int drag_hlight_left
= rtl
? max(px
-dragged_width
, 0) : px
;
73 int drag_hlight_right
= rtl
? px
: min(px
+ dragged_width
, max_width
);
74 int drag_hlight_width
= max(drag_hlight_right
- drag_hlight_left
, 0);
76 if (drag_hlight_width
> 0) {
77 GfxFillRect(drag_hlight_left
+ WD_FRAMERECT_LEFT
, WD_FRAMERECT_TOP
+ 1,
78 drag_hlight_right
- WD_FRAMERECT_RIGHT
, ScaleGUITrad(13) - WD_FRAMERECT_BOTTOM
, _colour_gradient
[COLOUR_GREY
][7]);
81 return drag_hlight_width
;
85 * Draws an image of a whole train
86 * @param v Front vehicle
87 * @param left The minimum horizontal position
88 * @param right The maximum horizontal position
89 * @param y Vertical position to draw at
90 * @param selection Selected vehicle to draw a frame around
91 * @param skip Number of pixels to skip at the front (for scrolling)
92 * @param drag_dest The vehicle another one is dragged over, \c INVALID_VEHICLE if none.
94 void DrawTrainImage(const Train
*v
, int left
, int right
, int y
, VehicleID selection
, EngineImageType image_type
, int skip
, VehicleID drag_dest
)
96 bool rtl
= _current_text_dir
== TD_RTL
;
97 Direction dir
= rtl
? DIR_E
: DIR_W
;
99 DrawPixelInfo tmp_dpi
, *old_dpi
;
100 /* Position of highlight box */
103 int max_width
= right
- left
+ 1;
104 int height
= ScaleGUITrad(14);
106 if (!FillDrawPixelInfo(&tmp_dpi
, left
, y
, max_width
, height
)) return;
111 int px
= rtl
? max_width
+ skip
: -skip
;
112 bool sel_articulated
= false;
113 bool dragging
= (drag_dest
!= INVALID_VEHICLE
);
114 bool drag_at_end_of_train
= (drag_dest
== v
->index
); // Head index is used to mark dragging at end of train.
115 for (; v
!= NULL
&& (rtl
? px
> 0 : px
< max_width
); v
= v
->Next()) {
116 if (dragging
&& !drag_at_end_of_train
&& drag_dest
== v
->index
) {
117 /* Highlight the drag-and-drop destination inside the train. */
118 int drag_hlight_width
= HighlightDragPosition(px
, max_width
, selection
, _cursor
.vehchain
);
119 px
+= rtl
? -drag_hlight_width
: drag_hlight_width
;
123 int width
= Train::From(v
)->GetDisplayImageWidth(&offset
);
125 if (rtl
? px
+ width
> 0 : px
- width
< max_width
) {
126 PaletteID pal
= (v
->vehstatus
& VS_CRASHED
) ? PALETTE_CRASH
: GetVehiclePalette(v
);
127 VehicleSpriteSeq seq
;
128 v
->GetImage(dir
, image_type
, &seq
);
129 seq
.Draw(px
+ (rtl
? -offset
.x
: offset
.x
), height
/ 2 + offset
.y
, pal
, (v
->vehstatus
& VS_CRASHED
) != 0);
132 if (!v
->IsArticulatedPart()) sel_articulated
= false;
134 if (v
->index
== selection
) {
135 /* Set the highlight position */
136 highlight_l
= rtl
? px
- width
: px
;
137 highlight_r
= rtl
? px
- 1 : px
+ width
- 1;
138 sel_articulated
= true;
139 } else if ((_cursor
.vehchain
&& highlight_r
!= 0) || sel_articulated
) {
141 highlight_l
-= width
;
143 highlight_r
+= width
;
147 px
+= rtl
? -width
: width
;
150 if (dragging
&& drag_at_end_of_train
) {
151 /* Highlight the drag-and-drop destination at the end of the train. */
152 HighlightDragPosition(px
, max_width
, selection
, _cursor
.vehchain
);
155 if (highlight_l
!= highlight_r
) {
156 /* Draw the highlight. Now done after drawing all the engines, as
157 * the next engine after the highlight could overlap it. */
158 DrawFrameRect(highlight_l
, 0, highlight_r
, height
- 1, COLOUR_WHITE
, FR_BORDERONLY
);
164 /** Helper struct for the cargo details information */
165 struct CargoSummaryItem
{
166 CargoID cargo
; ///< The cargo that is carried
167 StringID subtype
; ///< STR_EMPTY if none
168 uint capacity
; ///< Amount that can be carried
169 uint amount
; ///< Amount that is carried
170 StationID source
; ///< One of the source stations
172 /** Used by CargoSummary::Find() and similar functions */
173 inline bool operator != (const CargoSummaryItem
&other
) const
175 return this->cargo
!= other
.cargo
|| this->subtype
!= other
.subtype
;
179 static const uint TRAIN_DETAILS_MIN_INDENT
= 32; ///< Minimum indent level in the train details window
180 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
182 /** Container for the cargo summary information. */
183 typedef SmallVector
<CargoSummaryItem
, 2> CargoSummary
;
184 /** Reused container of cargo details */
185 static CargoSummary _cargo_summary
;
188 * Draw the details cargo tab for the given vehicle at the given position
190 * @param item Data to draw
191 * @param left The left most coordinate to draw
192 * @param right The right most coordinate to draw
193 * @param y The y coordinate
195 static void TrainDetailsCargoTab(const CargoSummaryItem
*item
, int left
, int right
, int y
)
198 if (item
->amount
> 0) {
199 SetDParam(0, item
->cargo
);
200 SetDParam(1, item
->amount
);
201 SetDParam(2, item
->source
);
202 SetDParam(3, _settings_game
.vehicle
.freight_trains
);
203 str
= FreightWagonMult(item
->cargo
) > 1 ? STR_VEHICLE_DETAILS_CARGO_FROM_MULT
: STR_VEHICLE_DETAILS_CARGO_FROM
;
205 SetDParam(0, STR_QUANTITY_N_A
);
206 str
= item
->cargo
== INVALID_CARGO
? STR_LTBLUE_STRING
: STR_VEHICLE_DETAILS_CARGO_EMPTY
;
209 DrawString(left
, right
, y
, str
);
213 * Draw the details info tab for the given vehicle at the given position
215 * @param v current vehicle
216 * @param left The left most coordinate to draw
217 * @param right The right most coordinate to draw
218 * @param y The y coordinate
220 static void TrainDetailsInfoTab(const Vehicle
*v
, int left
, int right
, int y
)
222 if (RailVehInfo(v
->engine_type
)->railveh_type
== RAILVEH_WAGON
) {
223 SetDParam(0, v
->engine_type
);
224 SetDParam(1, v
->value
);
225 DrawString(left
, right
, y
, STR_VEHICLE_DETAILS_TRAIN_WAGON_VALUE
);
227 SetDParam(0, v
->engine_type
);
228 SetDParam(1, v
->build_year
);
229 SetDParam(2, v
->value
);
230 DrawString(left
, right
, y
, STR_VEHICLE_DETAILS_TRAIN_ENGINE_BUILT_AND_VALUE
);
235 * Draw the details capacity tab for the given vehicle at the given position
237 * @param item Data to draw
238 * @param left The left most coordinate to draw
239 * @param right The right most coordinate to draw
240 * @param y The y coordinate
242 static void TrainDetailsCapacityTab(const CargoSummaryItem
*item
, int left
, int right
, int y
)
245 if (item
->cargo
!= INVALID_CARGO
) {
246 SetDParam(0, item
->cargo
);
247 SetDParam(1, item
->capacity
);
248 SetDParam(4, item
->subtype
);
249 SetDParam(5, _settings_game
.vehicle
.freight_trains
);
250 str
= FreightWagonMult(item
->cargo
) > 1 ? STR_VEHICLE_INFO_CAPACITY_MULT
: STR_VEHICLE_INFO_CAPACITY
;
252 /* Draw subtype only */
253 SetDParam(0, item
->subtype
);
254 str
= STR_VEHICLE_INFO_NO_CAPACITY
;
256 DrawString(left
, right
, y
, str
);
260 * Collects the cargo transported
261 * @param v Vehicle to process
262 * @param summary Space for the result
264 static void GetCargoSummaryOfArticulatedVehicle(const Train
*v
, CargoSummary
*summary
)
268 if (!v
->GetEngine()->CanCarryCargo()) continue;
270 CargoSummaryItem new_item
;
271 new_item
.cargo
= v
->cargo_cap
> 0 ? v
->cargo_type
: INVALID_CARGO
;
272 new_item
.subtype
= GetCargoSubtypeText(v
);
273 if (new_item
.cargo
== INVALID_CARGO
&& new_item
.subtype
== STR_EMPTY
) continue;
275 CargoSummaryItem
*item
= summary
->Find(new_item
);
276 if (item
== summary
->End()) {
277 item
= summary
->Append();
278 item
->cargo
= new_item
.cargo
;
279 item
->subtype
= new_item
.subtype
;
282 item
->source
= INVALID_STATION
;
285 item
->capacity
+= v
->cargo_cap
;
286 item
->amount
+= v
->cargo
.StoredCount();
287 if (item
->source
== INVALID_STATION
) item
->source
= v
->cargo
.Source();
288 } while ((v
= v
->Next()) != NULL
&& v
->IsArticulatedPart());
292 * Get the length of an articulated vehicle.
293 * @param v the vehicle to get the length of.
294 * @return the length in pixels.
296 static uint
GetLengthOfArticulatedVehicle(const Train
*v
)
301 length
+= v
->GetDisplayImageWidth();
302 } while ((v
= v
->Next()) != NULL
&& v
->IsArticulatedPart());
308 * Determines the number of lines in the train details window
309 * @param veh_id Train
310 * @param det_tab Selected details tab
311 * @return Number of line
313 int GetTrainDetailsWndVScroll(VehicleID veh_id
, TrainDetailsWindowTabs det_tab
)
317 if (det_tab
== TDW_TAB_TOTALS
) { // Total cargo tab
318 CargoArray act_cargo
;
319 CargoArray max_cargo
;
320 for (const Vehicle
*v
= Vehicle::Get(veh_id
); v
!= NULL
; v
= v
->Next()) {
321 act_cargo
[v
->cargo_type
] += v
->cargo
.StoredCount();
322 max_cargo
[v
->cargo_type
] += v
->cargo_cap
;
325 /* Set scroll-amount separately from counting, as to not compute num double
326 * for more carriages of the same type
328 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
329 if (max_cargo
[i
] > 0) num
++; // only count carriages that the train has
331 num
++; // needs one more because first line is description string
333 for (const Train
*v
= Train::Get(veh_id
); v
!= NULL
; v
= v
->GetNextVehicle()) {
334 GetCargoSummaryOfArticulatedVehicle(v
, &_cargo_summary
);
335 num
+= max(1u, _cargo_summary
.Length());
337 uint length
= GetLengthOfArticulatedVehicle(v
);
338 if (length
> TRAIN_DETAILS_MAX_INDENT
) num
++;
346 * Draw the details for the given vehicle at the given position
348 * @param v current vehicle
349 * @param left The left most coordinate to draw
350 * @param right The right most coordinate to draw
351 * @param y The y coordinate
352 * @param vscroll_pos Position of scrollbar
353 * @param vscroll_cap Number of lines currently displayed
354 * @param det_tab Selected details tab
356 void DrawTrainDetails(const Train
*v
, int left
, int right
, int y
, int vscroll_pos
, uint16 vscroll_cap
, TrainDetailsWindowTabs det_tab
)
358 /* get rid of awkward offset */
361 int sprite_height
= ScaleGUITrad(GetVehicleHeight(VEH_TRAIN
));
362 int line_height
= max(sprite_height
, WD_MATRIX_TOP
+ FONT_HEIGHT_NORMAL
+ WD_MATRIX_BOTTOM
);
363 int sprite_y_offset
= line_height
/ 2;
364 int text_y_offset
= (line_height
- FONT_HEIGHT_NORMAL
) / 2;
366 /* draw the first 3 details tabs */
367 if (det_tab
!= TDW_TAB_TOTALS
) {
368 bool rtl
= _current_text_dir
== TD_RTL
;
369 Direction dir
= rtl
? DIR_E
: DIR_W
;
370 int x
= rtl
? right
: left
;
371 for (; v
!= NULL
&& vscroll_pos
> -vscroll_cap
; v
= v
->GetNextVehicle()) {
372 GetCargoSummaryOfArticulatedVehicle(v
, &_cargo_summary
);
380 int width
= u
->GetDisplayImageWidth(&offset
);
381 if (vscroll_pos
<= 0 && vscroll_pos
> -vscroll_cap
) {
383 const Engine
*e
= Engine::Get(v
->engine_type
);
384 if (e
->GetGRF() != NULL
) {
385 pitch
= ScaleGUITrad(e
->GetGRF()->traininfo_vehicle_pitch
);
387 PaletteID pal
= (v
->vehstatus
& VS_CRASHED
) ? PALETTE_CRASH
: GetVehiclePalette(v
);
388 VehicleSpriteSeq seq
;
389 u
->GetImage(dir
, EIT_IN_DETAILS
, &seq
);
390 seq
.Draw(px
+ (rtl
? -offset
.x
: offset
.x
), y
- line_height
* vscroll_pos
+ sprite_y_offset
+ pitch
, pal
, (v
->vehstatus
& VS_CRASHED
) != 0);
392 px
+= rtl
? -width
: width
;
395 } while (u
!= NULL
&& u
->IsArticulatedPart());
397 bool separate_sprite_row
= (dx
> (uint
)ScaleGUITrad(TRAIN_DETAILS_MAX_INDENT
));
398 if (separate_sprite_row
) {
403 uint num_lines
= max(1u, _cargo_summary
.Length());
404 for (uint i
= 0; i
< num_lines
; i
++) {
405 int sprite_width
= max
<int>(dx
, ScaleGUITrad(TRAIN_DETAILS_MIN_INDENT
)) + 3;
406 int data_left
= left
+ (rtl
? 0 : sprite_width
);
407 int data_right
= right
- (rtl
? sprite_width
: 0);
408 if (vscroll_pos
<= 0 && vscroll_pos
> -vscroll_cap
) {
409 int py
= y
- line_height
* vscroll_pos
+ text_y_offset
;
410 if (i
> 0 || separate_sprite_row
) {
411 if (vscroll_pos
!= 0) GfxFillRect(left
, py
- WD_MATRIX_TOP
- 1, right
, py
- WD_MATRIX_TOP
, _colour_gradient
[COLOUR_GREY
][5]);
415 if (i
< _cargo_summary
.Length()) {
416 TrainDetailsCargoTab(&_cargo_summary
[i
], data_left
, data_right
, py
);
418 DrawString(data_left
, data_right
, py
, STR_QUANTITY_N_A
, TC_LIGHT_BLUE
);
423 if (i
== 0) TrainDetailsInfoTab(v
, data_left
, data_right
, py
);
426 case TDW_TAB_CAPACITY
:
427 if (i
< _cargo_summary
.Length()) {
428 TrainDetailsCapacityTab(&_cargo_summary
[i
], data_left
, data_right
, py
);
430 SetDParam(0, STR_EMPTY
);
431 DrawString(data_left
, data_right
, py
, STR_VEHICLE_INFO_NO_CAPACITY
);
435 default: NOT_REACHED();
442 CargoArray act_cargo
;
443 CargoArray max_cargo
;
444 Money feeder_share
= 0;
446 for (const Vehicle
*u
= v
; u
!= NULL
; u
= u
->Next()) {
447 act_cargo
[u
->cargo_type
] += u
->cargo
.StoredCount();
448 max_cargo
[u
->cargo_type
] += u
->cargo_cap
;
449 feeder_share
+= u
->cargo
.FeederShare();
452 /* draw total cargo tab */
453 DrawString(left
, right
, y
+ text_y_offset
, STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_TEXT
);
456 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
457 if (max_cargo
[i
] > 0 && --vscroll_pos
< 0 && vscroll_pos
> -vscroll_cap
) {
458 SetDParam(0, i
); // {CARGO} #1
459 SetDParam(1, act_cargo
[i
]); // {CARGO} #2
460 SetDParam(2, i
); // {SHORTCARGO} #1
461 SetDParam(3, max_cargo
[i
]); // {SHORTCARGO} #2
462 SetDParam(4, _settings_game
.vehicle
.freight_trains
);
463 DrawString(left
, right
, y
+ text_y_offset
, FreightWagonMult(i
) > 1 ? STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_MULT
: STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY
);
467 SetDParam(0, feeder_share
);
468 DrawString(left
, right
, y
+ text_y_offset
, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE
);