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"
19 #include "table/strings.h"
22 * Callback for building wagons.
23 * @param result The result of the command.
24 * @param tile The tile the command was executed on.
25 * @param p1 Additional data for the command (for the #CommandProc)
26 * @param p2 Additional data for the command (for the #CommandProc)
28 void CcBuildWagon(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
30 if (result
.Failed()) return;
32 /* find a locomotive in the depot. */
33 const Vehicle
*found
= NULL
;
36 if (t
->IsFrontEngine() && t
->tile
== tile
&& t
->IsStoppedInDepot()) {
37 if (found
!= NULL
) return; // must be exactly one.
42 /* if we found a loco, */
44 found
= found
->Last();
45 /* put the new wagon at the end of the loco. */
46 DoCommandP(0, _new_vehicle_id
, found
->index
, CMD_MOVE_RAIL_VEHICLE
);
47 InvalidateWindowClassesData(WC_TRAINS_LIST
, 0);
52 * Highlight the position where a rail vehicle is dragged over by drawing a light gray background.
53 * @param px The current x position to draw from.
54 * @param max_width The maximum space available to draw.
55 * @param selection Selected vehicle that is dragged.
56 * @return The width of the highlight mark.
58 static int HighlightDragPosition(int px
, int max_width
, VehicleID selection
)
60 bool rtl
= _current_text_dir
== TD_RTL
;
62 assert(selection
!= INVALID_VEHICLE
);
63 int dragged_width
= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
64 for (Train
*t
= Train::Get(selection
); t
!= NULL
; t
= t
->HasArticulatedPart() ? t
->GetNextArticulatedPart() : NULL
) {
65 dragged_width
+= t
->GetDisplayImageWidth(NULL
);
68 int drag_hlight_left
= rtl
? max(px
-dragged_width
, 0) : px
;
69 int drag_hlight_right
= rtl
? px
: min(px
+ dragged_width
, max_width
);
70 int drag_hlight_width
= max(drag_hlight_right
- drag_hlight_left
, 0);
72 if (drag_hlight_width
> 0) {
73 GfxFillRect(drag_hlight_left
+ WD_FRAMERECT_LEFT
, WD_FRAMERECT_TOP
+ 1,
74 drag_hlight_right
- WD_FRAMERECT_RIGHT
, 13 - WD_FRAMERECT_BOTTOM
, _colour_gradient
[COLOUR_GREY
][7]);
77 return drag_hlight_width
;
81 * Draws an image of a whole train
82 * @param v Front vehicle
83 * @param left The minimum horizontal position
84 * @param right The maximum horizontal position
85 * @param y Vertical position to draw at
86 * @param selection Selected vehicle to draw a frame around
87 * @param skip Number of pixels to skip at the front (for scrolling)
88 * @param drag_dest The vehicle another one is dragged over, \c INVALID_VEHICLE if none.
90 void DrawTrainImage(const Train
*v
, int left
, int right
, int y
, VehicleID selection
, EngineImageType image_type
, int skip
, VehicleID drag_dest
)
92 bool rtl
= _current_text_dir
== TD_RTL
;
93 Direction dir
= rtl
? DIR_E
: DIR_W
;
95 DrawPixelInfo tmp_dpi
, *old_dpi
;
96 /* Position of highlight box */
99 int max_width
= right
- left
+ 1;
101 if (!FillDrawPixelInfo(&tmp_dpi
, left
, y
, max_width
, 14)) return;
106 int px
= rtl
? max_width
+ skip
: -skip
;
107 bool sel_articulated
= false;
108 bool dragging
= (drag_dest
!= INVALID_VEHICLE
);
109 bool drag_at_end_of_train
= (drag_dest
== v
->index
); // Head index is used to mark dragging at end of train.
110 for (; v
!= NULL
&& (rtl
? px
> 0 : px
< max_width
); v
= v
->Next()) {
111 if (dragging
&& !drag_at_end_of_train
&& drag_dest
== v
->index
) {
112 /* Highlight the drag-and-drop destination inside the train. */
113 int drag_hlight_width
= HighlightDragPosition(px
, max_width
, selection
);
114 px
+= rtl
? -drag_hlight_width
: drag_hlight_width
;
118 int width
= Train::From(v
)->GetDisplayImageWidth(&offset
);
120 if (rtl
? px
+ width
> 0 : px
- width
< max_width
) {
121 PaletteID pal
= (v
->vehstatus
& VS_CRASHED
) ? PALETTE_CRASH
: GetVehiclePalette(v
);
122 DrawSprite(v
->GetImage(dir
, image_type
), pal
, px
+ (rtl
? -offset
.x
: offset
.x
), 7 + offset
.y
);
125 if (!v
->IsArticulatedPart()) sel_articulated
= false;
127 if (v
->index
== selection
) {
128 /* Set the highlight position */
129 highlight_l
= rtl
? px
- width
: px
;
130 highlight_r
= rtl
? px
- 1 : px
+ width
- 1;
131 sel_articulated
= true;
132 } else if ((_cursor
.vehchain
&& highlight_r
!= 0) || sel_articulated
) {
134 highlight_l
-= width
;
136 highlight_r
+= width
;
140 px
+= rtl
? -width
: width
;
143 if (dragging
&& drag_at_end_of_train
) {
144 /* Highlight the drag-and-drop destination at the end of the train. */
145 HighlightDragPosition(px
, max_width
, selection
);
148 if (highlight_l
!= highlight_r
) {
149 /* Draw the highlight. Now done after drawing all the engines, as
150 * the next engine after the highlight could overlap it. */
151 DrawFrameRect(highlight_l
, 0, highlight_r
, 13, COLOUR_WHITE
, FR_BORDERONLY
);
157 /** Helper struct for the cargo details information */
158 struct CargoSummaryItem
{
159 CargoID cargo
; ///< The cargo that is carried
160 StringID subtype
; ///< STR_EMPTY if none
161 uint capacity
; ///< Amount that can be carried
162 uint amount
; ///< Amount that is carried
163 StationID source
; ///< One of the source stations
165 /** Used by CargoSummary::Find() and similar functions */
166 inline bool operator != (const CargoSummaryItem
&other
) const
168 return this->cargo
!= other
.cargo
|| this->subtype
!= other
.subtype
;
172 static const uint TRAIN_DETAILS_MIN_INDENT
= 32; ///< Minimum indent level in the train details window
173 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
175 /** Container for the cargo summary information. */
176 typedef SmallVector
<CargoSummaryItem
, 2> CargoSummary
;
177 /** Reused container of cargo details */
178 static CargoSummary _cargo_summary
;
181 * Draw the details cargo tab for the given vehicle at the given position
183 * @param item Data to draw
184 * @param left The left most coordinate to draw
185 * @param right The right most coordinate to draw
186 * @param y The y coordinate
188 static void TrainDetailsCargoTab(const CargoSummaryItem
*item
, int left
, int right
, int y
)
191 if (item
->amount
> 0) {
192 SetDParam(0, item
->cargo
);
193 SetDParam(1, item
->amount
);
194 SetDParam(2, item
->source
);
195 SetDParam(3, _settings_game
.vehicle
.freight_trains
);
196 str
= FreightWagonMult(item
->cargo
) > 1 ? STR_VEHICLE_DETAILS_CARGO_FROM_MULT
: STR_VEHICLE_DETAILS_CARGO_FROM
;
198 SetDParam(0, STR_QUANTITY_N_A
);
199 str
= item
->cargo
== INVALID_CARGO
? STR_LTBLUE_STRING
: STR_VEHICLE_DETAILS_CARGO_EMPTY
;
202 DrawString(left
, right
, y
, str
);
206 * Draw the details info tab for the given vehicle at the given position
208 * @param v current vehicle
209 * @param left The left most coordinate to draw
210 * @param right The right most coordinate to draw
211 * @param y The y coordinate
213 static void TrainDetailsInfoTab(const Vehicle
*v
, int left
, int right
, int y
)
215 if (RailVehInfo(v
->engine_type
)->railveh_type
== RAILVEH_WAGON
) {
216 SetDParam(0, v
->engine_type
);
217 SetDParam(1, v
->value
);
218 DrawString(left
, right
, y
, STR_VEHICLE_DETAILS_TRAIN_WAGON_VALUE
);
220 SetDParam(0, v
->engine_type
);
221 SetDParam(1, v
->build_year
);
222 SetDParam(2, v
->value
);
223 DrawString(left
, right
, y
, STR_VEHICLE_DETAILS_TRAIN_ENGINE_BUILT_AND_VALUE
);
228 * Draw the details capacity tab for the given vehicle at the given position
230 * @param item Data to draw
231 * @param left The left most coordinate to draw
232 * @param right The right most coordinate to draw
233 * @param y The y coordinate
235 static void TrainDetailsCapacityTab(const CargoSummaryItem
*item
, int left
, int right
, int y
)
238 if (item
->cargo
!= INVALID_CARGO
) {
239 SetDParam(0, item
->cargo
);
240 SetDParam(1, item
->capacity
);
241 SetDParam(4, item
->subtype
);
242 SetDParam(5, _settings_game
.vehicle
.freight_trains
);
243 str
= FreightWagonMult(item
->cargo
) > 1 ? STR_VEHICLE_INFO_CAPACITY_MULT
: STR_VEHICLE_INFO_CAPACITY
;
245 /* Draw subtype only */
246 SetDParam(0, item
->subtype
);
247 str
= STR_VEHICLE_INFO_NO_CAPACITY
;
249 DrawString(left
, right
, y
, str
);
253 * Collects the cargo transported
254 * @param v Vehicle to process
255 * @param summary Space for the result
257 static void GetCargoSummaryOfArticulatedVehicle(const Train
*v
, CargoSummary
*summary
)
261 if (!v
->GetEngine()->CanCarryCargo()) continue;
263 CargoSummaryItem new_item
;
264 new_item
.cargo
= v
->cargo_cap
> 0 ? v
->cargo_type
: INVALID_CARGO
;
265 new_item
.subtype
= GetCargoSubtypeText(v
);
266 if (new_item
.cargo
== INVALID_CARGO
&& new_item
.subtype
== STR_EMPTY
) continue;
268 CargoSummaryItem
*item
= summary
->Find(new_item
);
269 if (item
== summary
->End()) {
270 item
= summary
->Append();
271 item
->cargo
= new_item
.cargo
;
272 item
->subtype
= new_item
.subtype
;
275 item
->source
= INVALID_STATION
;
278 item
->capacity
+= v
->cargo_cap
;
279 item
->amount
+= v
->cargo
.StoredCount();
280 if (item
->source
== INVALID_STATION
) item
->source
= v
->cargo
.Source();
281 } while ((v
= v
->Next()) != NULL
&& v
->IsArticulatedPart());
285 * Get the length of an articulated vehicle.
286 * @param v the vehicle to get the length of.
287 * @return the length in pixels.
289 static uint
GetLengthOfArticulatedVehicle(const Train
*v
)
294 length
+= v
->GetDisplayImageWidth();
295 } while ((v
= v
->Next()) != NULL
&& v
->IsArticulatedPart());
301 * Determines the number of lines in the train details window
302 * @param veh_id Train
303 * @param det_tab Selected details tab
304 * @return Number of line
306 int GetTrainDetailsWndVScroll(VehicleID veh_id
, TrainDetailsWindowTabs det_tab
)
310 if (det_tab
== TDW_TAB_TOTALS
) { // Total cargo tab
311 CargoArray act_cargo
;
312 CargoArray max_cargo
;
313 for (const Vehicle
*v
= Vehicle::Get(veh_id
); v
!= NULL
; v
= v
->Next()) {
314 act_cargo
[v
->cargo_type
] += v
->cargo
.StoredCount();
315 max_cargo
[v
->cargo_type
] += v
->cargo_cap
;
318 /* Set scroll-amount separately from counting, as to not compute num double
319 * for more carriages of the same type
321 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
322 if (max_cargo
[i
] > 0) num
++; // only count carriages that the train has
324 num
++; // needs one more because first line is description string
326 for (const Train
*v
= Train::Get(veh_id
); v
!= NULL
; v
= v
->GetNextVehicle()) {
327 GetCargoSummaryOfArticulatedVehicle(v
, &_cargo_summary
);
328 num
+= max(1u, _cargo_summary
.Length());
330 uint length
= GetLengthOfArticulatedVehicle(v
);
331 if (length
> TRAIN_DETAILS_MAX_INDENT
) num
++;
339 * Draw the details for the given vehicle at the given position
341 * @param v current vehicle
342 * @param left The left most coordinate to draw
343 * @param right The right most coordinate to draw
344 * @param y The y coordinate
345 * @param vscroll_pos Position of scrollbar
346 * @param vscroll_cap Number of lines currently displayed
347 * @param det_tab Selected details tab
349 void DrawTrainDetails(const Train
*v
, int left
, int right
, int y
, int vscroll_pos
, uint16 vscroll_cap
, TrainDetailsWindowTabs det_tab
)
351 /* draw the first 3 details tabs */
352 if (det_tab
!= TDW_TAB_TOTALS
) {
353 bool rtl
= _current_text_dir
== TD_RTL
;
354 Direction dir
= rtl
? DIR_E
: DIR_W
;
355 int x
= rtl
? right
: left
;
356 int sprite_y_offset
= 4 + (FONT_HEIGHT_NORMAL
- 10) / 2;
357 int line_height
= WD_MATRIX_TOP
+ FONT_HEIGHT_NORMAL
+ WD_MATRIX_BOTTOM
;
358 for (; v
!= NULL
&& vscroll_pos
> -vscroll_cap
; v
= v
->GetNextVehicle()) {
359 GetCargoSummaryOfArticulatedVehicle(v
, &_cargo_summary
);
367 int width
= u
->GetDisplayImageWidth(&offset
);
368 if (vscroll_pos
<= 0 && vscroll_pos
> -vscroll_cap
) {
369 PaletteID pal
= (v
->vehstatus
& VS_CRASHED
) ? PALETTE_CRASH
: GetVehiclePalette(v
);
370 DrawSprite(u
->GetImage(dir
, EIT_IN_DETAILS
), pal
, px
+ (rtl
? -offset
.x
: offset
.x
), y
- line_height
* vscroll_pos
+ sprite_y_offset
+ offset
.y
);
372 px
+= rtl
? -width
: width
;
375 } while (u
!= NULL
&& u
->IsArticulatedPart());
377 bool separate_sprite_row
= (dx
> TRAIN_DETAILS_MAX_INDENT
);
378 if (separate_sprite_row
) {
383 uint num_lines
= max(1u, _cargo_summary
.Length());
384 for (uint i
= 0; i
< num_lines
; i
++) {
385 int sprite_width
= max
<int>(dx
, TRAIN_DETAILS_MIN_INDENT
) + 3;
386 int data_left
= left
+ (rtl
? 0 : sprite_width
);
387 int data_right
= right
- (rtl
? sprite_width
: 0);
388 if (vscroll_pos
<= 0 && vscroll_pos
> -vscroll_cap
) {
389 int py
= y
- line_height
* vscroll_pos
;
390 if (i
> 0 || separate_sprite_row
) {
391 if (vscroll_pos
!= 0) GfxFillRect(left
, py
- WD_MATRIX_TOP
- 1, right
, py
- WD_MATRIX_TOP
, _colour_gradient
[COLOUR_GREY
][5]);
395 if (i
< _cargo_summary
.Length()) {
396 TrainDetailsCargoTab(&_cargo_summary
[i
], data_left
, data_right
, py
);
398 DrawString(data_left
, data_right
, py
, STR_QUANTITY_N_A
, TC_LIGHT_BLUE
);
403 if (i
== 0) TrainDetailsInfoTab(v
, data_left
, data_right
, py
);
406 case TDW_TAB_CAPACITY
:
407 if (i
< _cargo_summary
.Length()) {
408 TrainDetailsCapacityTab(&_cargo_summary
[i
], data_left
, data_right
, py
);
410 SetDParam(0, STR_EMPTY
);
411 DrawString(data_left
, data_right
, py
, STR_VEHICLE_INFO_NO_CAPACITY
);
415 default: NOT_REACHED();
422 CargoArray act_cargo
;
423 CargoArray max_cargo
;
424 Money feeder_share
= 0;
426 for (const Vehicle
*u
= v
; u
!= NULL
; u
= u
->Next()) {
427 act_cargo
[u
->cargo_type
] += u
->cargo
.StoredCount();
428 max_cargo
[u
->cargo_type
] += u
->cargo_cap
;
429 feeder_share
+= u
->cargo
.FeederShare();
432 /* draw total cargo tab */
433 DrawString(left
, right
, y
, STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_TEXT
);
434 y
+= WD_MATRIX_TOP
+ FONT_HEIGHT_NORMAL
+ WD_MATRIX_BOTTOM
;
436 for (CargoID i
= 0; i
< NUM_CARGO
; i
++) {
437 if (max_cargo
[i
] > 0 && --vscroll_pos
< 0 && vscroll_pos
> -vscroll_cap
) {
438 SetDParam(0, i
); // {CARGO} #1
439 SetDParam(1, act_cargo
[i
]); // {CARGO} #2
440 SetDParam(2, i
); // {SHORTCARGO} #1
441 SetDParam(3, max_cargo
[i
]); // {SHORTCARGO} #2
442 SetDParam(4, _settings_game
.vehicle
.freight_trains
);
443 DrawString(left
, right
, y
, FreightWagonMult(i
) > 1 ? STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_MULT
: STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY
);
444 y
+= WD_MATRIX_TOP
+ FONT_HEIGHT_NORMAL
+ WD_MATRIX_BOTTOM
;
447 SetDParam(0, feeder_share
);
448 DrawString(left
, right
, y
, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE
);