Let HandleWindowDragging return a boolean status
[openttd/fttd.git] / src / train_gui.cpp
blobe1b0955296ef3a08d3e4c403e085fda93d9e3bd3
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file train_gui.cpp GUI for trains. */
12 #include "stdafx.h"
13 #include "window_gui.h"
14 #include "command_func.h"
15 #include "train.h"
16 #include "strings_func.h"
17 #include "vehicle_func.h"
18 #include "zoom_func.h"
20 #include "table/strings.h"
22 /**
23 * Highlight the position where a rail vehicle is dragged over by drawing a light gray background.
24 * @param dpi The area to draw on.
25 * @param px The current x position to draw from.
26 * @param max_width The maximum space available to draw.
27 * @param selection Selected vehicle that is dragged.
28 * @param chain Whether a whole chain is dragged.
29 * @return The width of the highlight mark.
31 static int HighlightDragPosition (BlitArea *dpi, int px, int max_width, VehicleID selection, bool chain)
33 bool rtl = _current_text_dir == TD_RTL;
35 assert(selection != INVALID_VEHICLE);
36 int dragged_width = WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
37 for (Train *t = Train::Get(selection); t != NULL; t = chain ? t->Next() : (t->HasArticulatedPart() ? t->GetNextArticulatedPart() : NULL)) {
38 dragged_width += t->GetDisplayImageWidth(NULL);
41 int drag_hlight_left = rtl ? max(px -dragged_width, 0) : px;
42 int drag_hlight_right = rtl ? px : min(px + dragged_width, max_width);
44 if (drag_hlight_right <= drag_hlight_left) return 0;
46 GfxFillRect (dpi, drag_hlight_left + WD_FRAMERECT_LEFT, WD_FRAMERECT_TOP + 1,
47 drag_hlight_right - WD_FRAMERECT_RIGHT, ScaleGUITrad(13) - WD_FRAMERECT_BOTTOM, _colour_gradient[COLOUR_GREY][7]);
49 return drag_hlight_right - drag_hlight_left;
52 /**
53 * Draws an image of a whole train
54 * @param v Front vehicle
55 * @param dpi The area to draw on
56 * @param left The minimum horizontal position
57 * @param right The maximum horizontal position
58 * @param y Vertical position to draw at
59 * @param selection Selected vehicle to draw a frame around
60 * @param chain Whether the selection is a chain
61 * @param skip Number of pixels to skip at the front (for scrolling)
62 * @param drag_dest The vehicle another one is dragged over, \c INVALID_VEHICLE if none.
64 void DrawTrainImage (const Train *v, BlitArea *dpi, int left, int right,
65 int y, VehicleID selection, bool chain, EngineImageType image_type,
66 int skip, VehicleID drag_dest)
68 bool rtl = _current_text_dir == TD_RTL;
69 Direction dir = rtl ? DIR_E : DIR_W;
71 BlitArea tmp_dpi;
72 /* Position of highlight box */
73 int highlight_l = 0;
74 int highlight_r = 0;
75 int max_width = right - left + 1;
76 int height = ScaleGUITrad(14);
78 if (!InitBlitArea (dpi, &tmp_dpi, left, y, max_width, height)) return;
80 int px = rtl ? max_width + skip : -skip;
81 bool sel_articulated = false;
82 bool dragging = (drag_dest != INVALID_VEHICLE);
83 bool drag_at_end_of_train = (drag_dest == v->index); // Head index is used to mark dragging at end of train.
84 for (; v != NULL && (rtl ? px > 0 : px < max_width); v = v->Next()) {
85 if (dragging && !drag_at_end_of_train && drag_dest == v->index) {
86 /* Highlight the drag-and-drop destination inside the train. */
87 int drag_hlight_width = HighlightDragPosition (&tmp_dpi, px, max_width, selection, chain);
88 px += rtl ? -drag_hlight_width : drag_hlight_width;
91 Point offset;
92 int width = Train::From(v)->GetDisplayImageWidth(&offset);
94 if (rtl ? px + width > 0 : px - width < max_width) {
95 PaletteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
96 VehicleSpriteSeq seq;
97 v->GetImage(dir, image_type, &seq);
98 seq.Draw (&tmp_dpi, px + (rtl ? -offset.x : offset.x), height / 2 + offset.y, pal, (v->vehstatus & VS_CRASHED) != 0);
101 if (!v->IsArticulatedPart()) sel_articulated = false;
103 if (v->index == selection) {
104 /* Set the highlight position */
105 highlight_l = rtl ? px - width : px;
106 highlight_r = rtl ? px - 1 : px + width - 1;
107 sel_articulated = true;
108 } else if ((chain && highlight_r != 0) || sel_articulated) {
109 if (rtl) {
110 highlight_l -= width;
111 } else {
112 highlight_r += width;
116 px += rtl ? -width : width;
119 if (dragging && drag_at_end_of_train) {
120 /* Highlight the drag-and-drop destination at the end of the train. */
121 HighlightDragPosition (&tmp_dpi, px, max_width, selection, chain);
124 if (highlight_l != highlight_r) {
125 /* Draw the highlight. Now done after drawing all the engines, as
126 * the next engine after the highlight could overlap it. */
127 DrawFrameRect (&tmp_dpi, highlight_l, 0, highlight_r, height - 1, COLOUR_WHITE, FR_BORDERONLY);
131 /** Helper struct for the cargo details information */
132 struct CargoSummaryItem {
133 CargoID cargo; ///< The cargo that is carried
134 StringID subtype; ///< STR_EMPTY if none
135 uint capacity; ///< Amount that can be carried
136 uint amount; ///< Amount that is carried
137 StationID source; ///< One of the source stations
139 /** Used by CargoSummary::Find() and similar functions */
140 inline bool operator != (const CargoSummaryItem &other) const
142 return this->cargo != other.cargo || this->subtype != other.subtype;
146 static const uint TRAIN_DETAILS_MIN_INDENT = 32; ///< Minimum indent level in the train details window
147 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
149 /** Container for the cargo summary information. */
150 typedef SmallVector<CargoSummaryItem, 2> CargoSummary;
151 /** Reused container of cargo details */
152 static CargoSummary _cargo_summary;
155 * Set up string parameters for the details cargo tab for the given cargo item.
156 * @param item Data to draw.
157 * @return String to draw.
159 static StringID TrainDetailsCargoTab (const CargoSummaryItem *item)
161 if (item->amount > 0) {
162 SetDParam(0, item->cargo);
163 SetDParam(1, item->amount);
164 SetDParam(2, item->source);
165 SetDParam(3, _settings_game.vehicle.freight_trains);
166 return FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_DETAILS_CARGO_FROM_MULT : STR_VEHICLE_DETAILS_CARGO_FROM;
167 } else {
168 return item->cargo == INVALID_CARGO ? STR_QUANTITY_N_A : STR_VEHICLE_DETAILS_CARGO_EMPTY;
173 * Set up string parameters for the details info tab for the given vehicle.
174 * @param v Current vehicle.
175 * @return String to draw.
177 static StringID TrainDetailsInfoTab (const Vehicle *v)
179 if (RailVehInfo(v->engine_type)->railveh_type == RAILVEH_WAGON) {
180 SetDParam(0, v->engine_type);
181 SetDParam(1, v->value);
182 return STR_VEHICLE_DETAILS_TRAIN_WAGON_VALUE;
183 } else {
184 SetDParam(0, v->engine_type);
185 SetDParam(1, v->build_year);
186 SetDParam(2, v->value);
187 return STR_VEHICLE_DETAILS_TRAIN_ENGINE_BUILT_AND_VALUE;
192 * Set up string parameters for the details capacity tab for the given cargo item.
193 * @param item Data to draw.
194 * @return String to draw.
196 static StringID TrainDetailsCapacityTab (const CargoSummaryItem *item)
198 if (item->cargo != INVALID_CARGO) {
199 SetDParam(0, item->cargo);
200 SetDParam(1, item->capacity);
201 SetDParam(4, item->subtype);
202 SetDParam(5, _settings_game.vehicle.freight_trains);
203 return FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_INFO_CAPACITY_MULT : STR_VEHICLE_INFO_CAPACITY;
204 } else {
205 /* Draw subtype only */
206 SetDParam(0, item->subtype);
207 return STR_VEHICLE_INFO_NO_CAPACITY;
212 * Collects the cargo transported
213 * @param v Vehicle to process
214 * @param summary Space for the result
216 static void GetCargoSummaryOfArticulatedVehicle(const Train *v, CargoSummary *summary)
218 summary->Clear();
219 do {
220 if (!v->GetEngine()->CanCarryCargo()) continue;
222 CargoSummaryItem new_item;
223 new_item.cargo = v->cargo_cap > 0 ? v->cargo_type : INVALID_CARGO;
224 new_item.subtype = GetCargoSubtypeText(v);
225 if (new_item.cargo == INVALID_CARGO && new_item.subtype == STR_EMPTY) continue;
227 CargoSummaryItem *item = summary->Find(new_item);
228 if (item == summary->End()) {
229 item = summary->Append();
230 item->cargo = new_item.cargo;
231 item->subtype = new_item.subtype;
232 item->capacity = 0;
233 item->amount = 0;
234 item->source = INVALID_STATION;
237 item->capacity += v->cargo_cap;
238 item->amount += v->cargo.StoredCount();
239 if (item->source == INVALID_STATION) item->source = v->cargo.Source();
240 } while ((v = v->Next()) != NULL && v->IsArticulatedPart());
244 * Get the length of an articulated vehicle.
245 * @param v the vehicle to get the length of.
246 * @return the length in pixels.
248 static uint GetLengthOfArticulatedVehicle(const Train *v)
250 uint length = 0;
252 do {
253 length += v->GetDisplayImageWidth();
254 } while ((v = v->Next()) != NULL && v->IsArticulatedPart());
256 return length;
260 * Determines the number of lines in the train details window
261 * @param veh_id Train
262 * @param det_tab Selected details tab
263 * @return Number of line
265 int GetTrainDetailsWndVScroll(VehicleID veh_id, TrainDetailsWindowTabs det_tab)
267 int num = 0;
269 if (det_tab == TDW_TAB_TOTALS) { // Total cargo tab
270 CargoArray act_cargo;
271 CargoArray max_cargo;
272 for (const Vehicle *v = Vehicle::Get(veh_id); v != NULL; v = v->Next()) {
273 act_cargo[v->cargo_type] += v->cargo.StoredCount();
274 max_cargo[v->cargo_type] += v->cargo_cap;
277 /* Set scroll-amount separately from counting, as to not compute num double
278 * for more carriages of the same type
280 for (CargoID i = 0; i < NUM_CARGO; i++) {
281 if (max_cargo[i] > 0) num++; // only count carriages that the train has
283 num++; // needs one more because first line is description string
284 } else {
285 for (const Train *v = Train::Get(veh_id); v != NULL; v = v->GetNextVehicle()) {
286 GetCargoSummaryOfArticulatedVehicle(v, &_cargo_summary);
287 num += max(1u, _cargo_summary.Length());
289 uint length = GetLengthOfArticulatedVehicle(v);
290 if (length > TRAIN_DETAILS_MAX_INDENT) num++;
294 return num;
298 * Draw the details for the given vehicle at the given position
300 * @param v current vehicle
301 * @param dpi The area to draw on
302 * @param left The left most coordinate to draw
303 * @param right The right most coordinate to draw
304 * @param y The y coordinate
305 * @param vscroll_pos Position of scrollbar
306 * @param vscroll_cap Number of lines currently displayed
307 * @param det_tab Selected details tab
309 void DrawTrainDetails (const Train *v, BlitArea *dpi, int left, int right, int y, int vscroll_pos, uint16 vscroll_cap, TrainDetailsWindowTabs det_tab)
311 /* get rid of awkward offset */
312 y -= WD_MATRIX_TOP;
314 int sprite_height = ScaleGUITrad(GetVehicleHeight(VEH_TRAIN));
315 int line_height = max(sprite_height, WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM);
316 int sprite_y_offset = line_height / 2;
317 int text_y_offset = (line_height - FONT_HEIGHT_NORMAL) / 2;
319 /* draw the first 3 details tabs */
320 if (det_tab != TDW_TAB_TOTALS) {
321 bool rtl = _current_text_dir == TD_RTL;
322 Direction dir = rtl ? DIR_E : DIR_W;
323 int x = rtl ? right : left;
324 for (; v != NULL && vscroll_pos > -vscroll_cap; v = v->GetNextVehicle()) {
325 GetCargoSummaryOfArticulatedVehicle(v, &_cargo_summary);
327 /* Draw sprites */
328 uint dx = 0;
329 int px = x;
330 const Train *u = v;
331 do {
332 Point offset;
333 int width = u->GetDisplayImageWidth(&offset);
334 if (vscroll_pos <= 0 && vscroll_pos > -vscroll_cap) {
335 int pitch = 0;
336 const Engine *e = Engine::Get(v->engine_type);
337 if (e->GetGRF() != NULL) {
338 pitch = ScaleGUITrad(e->GetGRF()->traininfo_vehicle_pitch);
340 PaletteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
341 VehicleSpriteSeq seq;
342 u->GetImage(dir, EIT_IN_DETAILS, &seq);
343 seq.Draw (dpi, px + (rtl ? -offset.x : offset.x), y - line_height * vscroll_pos + sprite_y_offset + pitch, pal, (v->vehstatus & VS_CRASHED) != 0);
345 px += rtl ? -width : width;
346 dx += width;
347 u = u->Next();
348 } while (u != NULL && u->IsArticulatedPart());
350 bool separate_sprite_row = (dx > (uint)ScaleGUITrad(TRAIN_DETAILS_MAX_INDENT));
351 if (separate_sprite_row) {
352 vscroll_pos--;
353 dx = 0;
356 uint num_lines = max(1u, _cargo_summary.Length());
357 for (uint i = 0; i < num_lines; i++) {
358 int sprite_width = max<int>(dx, ScaleGUITrad(TRAIN_DETAILS_MIN_INDENT)) + 3;
359 int data_left = left + (rtl ? 0 : sprite_width);
360 int data_right = right - (rtl ? sprite_width : 0);
361 if (vscroll_pos <= 0 && vscroll_pos > -vscroll_cap) {
362 int py = y - line_height * vscroll_pos + text_y_offset;
363 if (i > 0 || separate_sprite_row) {
364 if (vscroll_pos != 0) GfxFillRect (dpi, left, py - WD_MATRIX_TOP - 1, right, py - WD_MATRIX_TOP, _colour_gradient[COLOUR_GREY][5]);
366 switch (det_tab) {
367 case TDW_TAB_CARGO: {
368 StringID str = (i < _cargo_summary.Length()) ?
369 TrainDetailsCargoTab (&_cargo_summary[i]) : STR_QUANTITY_N_A;
370 DrawString (dpi, data_left, data_right, py, str, TC_LIGHT_BLUE);
371 break;
374 case TDW_TAB_INFO:
375 if (i == 0) DrawString (dpi, data_left, data_right, py, TrainDetailsInfoTab(v));
376 break;
378 case TDW_TAB_CAPACITY: {
379 StringID str;
380 if (i < _cargo_summary.Length()) {
381 str = TrainDetailsCapacityTab (&_cargo_summary[i]);
382 } else {
383 SetDParam(0, STR_EMPTY);
384 str = STR_VEHICLE_INFO_NO_CAPACITY;
386 DrawString (dpi, data_left, data_right, py, str);
387 break;
390 default: NOT_REACHED();
393 vscroll_pos--;
396 } else {
397 CargoArray act_cargo;
398 CargoArray max_cargo;
399 Money feeder_share = 0;
401 for (const Vehicle *u = v; u != NULL; u = u->Next()) {
402 act_cargo[u->cargo_type] += u->cargo.StoredCount();
403 max_cargo[u->cargo_type] += u->cargo_cap;
404 feeder_share += u->cargo.FeederShare();
407 /* draw total cargo tab */
408 DrawString (dpi, left, right, y + text_y_offset, STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_TEXT);
409 y += line_height;
411 for (CargoID i = 0; i < NUM_CARGO; i++) {
412 if (max_cargo[i] > 0 && --vscroll_pos < 0 && vscroll_pos > -vscroll_cap) {
413 SetDParam(0, i); // {CARGO} #1
414 SetDParam(1, act_cargo[i]); // {CARGO} #2
415 SetDParam(2, i); // {SHORTCARGO} #1
416 SetDParam(3, max_cargo[i]); // {SHORTCARGO} #2
417 SetDParam(4, _settings_game.vehicle.freight_trains);
418 DrawString (dpi, left, right, y + text_y_offset, FreightWagonMult(i) > 1 ? STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_MULT : STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY);
419 y += line_height;
422 SetDParam(0, feeder_share);
423 DrawString (dpi, left, right, y + text_y_offset, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);