OpenTTD: src/train_gui.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

train_gui.cpp
Go to the documentation of this file.
1 /* $Id: train_gui.cpp 27134 2015-02-01 20:54:24Z frosch $ */
2
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 */
9
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"
19
20 #include "table/strings.h"
21
22 #include "safeguards.h"
23
31 void CcBuildWagon(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
32 {
33 if (result.Failed()) return;
34
35 /* find a locomotive in the depot. */
36 const Vehicle *found = NULL;
37 const Train *t;
38 FOR_ALL_TRAINS(t) {
39 if (t->IsFrontEngine() && t->tile == tile && t->IsStoppedInDepot()) {
40 if (found != NULL) return; // must be exactly one.
41 found = t;
42 }
43 }
44
45 /* if we found a loco, */
46 if (found != NULL) {
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);
51 }
52 }
53
61 static int HighlightDragPosition(int px, int max_width, VehicleID selection)
62 {
63 bool rtl = _current_text_dir == TD_RTL;
64
65 assert(selection != INVALID_VEHICLE);
66 int dragged_width = WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
67 for (Train *t = Train::Get(selection); t != NULL; t = t->HasArticulatedPart() ? t->GetNextArticulatedPart() : NULL) {
68 dragged_width += t->GetDisplayImageWidth(NULL);
69 }
70
71 int drag_hlight_left = rtl ? max(px -dragged_width, 0) : px;
72 int drag_hlight_right = rtl ? px : min(px + dragged_width, max_width);
73 int drag_hlight_width = max(drag_hlight_right - drag_hlight_left, 0);
74
75 if (drag_hlight_width > 0) {
76 GfxFillRect(drag_hlight_left + WD_FRAMERECT_LEFT, WD_FRAMERECT_TOP + 1,
77 drag_hlight_right - WD_FRAMERECT_RIGHT, ScaleGUITrad(13) - WD_FRAMERECT_BOTTOM, _colour_gradient[COLOUR_GREY][7]);
78 }
79
80 return drag_hlight_width;
81 }
82
93 void DrawTrainImage(const Train *v, int left, int right, int y, VehicleID selection, EngineImageType image_type, int skip, VehicleID drag_dest)
94 {
95 bool rtl = _current_text_dir == TD_RTL;
96 Direction dir = rtl ? DIR_E : DIR_W;
97
98 DrawPixelInfo tmp_dpi, *old_dpi;
99 /* Position of highlight box */
100 int highlight_l = 0;
101 int highlight_r = 0;
102 int max_width = right - left + 1;
103 int height = ScaleGUITrad(14);
104
105 if (!FillDrawPixelInfo(&tmp_dpi, left, y, max_width, height)) return;
106
107 old_dpi = _cur_dpi;
108 _cur_dpi = &tmp_dpi;
109
110 int px = rtl ? max_width + skip : -skip;
111 bool sel_articulated = false;
112 bool dragging = (drag_dest != INVALID_VEHICLE);
113 bool drag_at_end_of_train = (drag_dest == v->index); // Head index is used to mark dragging at end of train.
114 for (; v != NULL && (rtl ? px > 0 : px < max_width); v = v->Next()) {
115 if (dragging && !drag_at_end_of_train && drag_dest == v->index) {
116 /* Highlight the drag-and-drop destination inside the train. */
117 int drag_hlight_width = HighlightDragPosition(px, max_width, selection);
118 px += rtl ? -drag_hlight_width : drag_hlight_width;
119 }
120
121 Point offset;
122 int width = Train::From(v)->GetDisplayImageWidth(&offset);
123
124 if (rtl ? px + width > 0 : px - width < max_width) {
125 PaletteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
126 DrawSprite(v->GetImage(dir, image_type), pal, px + (rtl ? -offset.x : offset.x), height / 2 + offset.y);
127 }
128
129 if (!v->IsArticulatedPart()) sel_articulated = false;
130
131 if (v->index == selection) {
132 /* Set the highlight position */
133 highlight_l = rtl ? px - width : px;
134 highlight_r = rtl ? px - 1 : px + width - 1;
135 sel_articulated = true;
136 } else if ((_cursor.vehchain && highlight_r != 0) || sel_articulated) {
137 if (rtl) {
138 highlight_l -= width;
139 } else {
140 highlight_r += width;
141 }
142 }
143
144 px += rtl ? -width : width;
145 }
146
147 if (dragging && drag_at_end_of_train) {
148 /* Highlight the drag-and-drop destination at the end of the train. */
149 HighlightDragPosition(px, max_width, selection);
150 }
151
152 if (highlight_l != highlight_r) {
153 /* Draw the highlight. Now done after drawing all the engines, as
154 * the next engine after the highlight could overlap it. */
155 DrawFrameRect(highlight_l, 0, highlight_r, height - 1, COLOUR_WHITE, FR_BORDERONLY);
156 }
157
158 _cur_dpi = old_dpi;
159 }
160
162 struct CargoSummaryItem {
163 CargoID cargo;
164 StringID subtype;
165 uint capacity;
166 uint amount;
167 StationID source;
168
170 inline bool operator != (const CargoSummaryItem &other) const
171 {
172 return this->cargo != other.cargo || this->subtype != other.subtype;
173 }
174 };
175
176 static const uint TRAIN_DETAILS_MIN_INDENT = 32;
177 static const uint TRAIN_DETAILS_MAX_INDENT = 72;
178
180 typedef SmallVector<CargoSummaryItem, 2> CargoSummary;
182 static CargoSummary _cargo_summary;
183
192 static void TrainDetailsCargoTab(const CargoSummaryItem *item, int left, int right, int y)
193 {
194 StringID str;
195 if (item->amount > 0) {
196 SetDParam(0, item->cargo);
197 SetDParam(1, item->amount);
198 SetDParam(2, item->source);
199 SetDParam(3, _settings_game.vehicle.freight_trains);
200 str = FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_DETAILS_CARGO_FROM_MULT : STR_VEHICLE_DETAILS_CARGO_FROM;
201 } else {
202 SetDParam(0, STR_QUANTITY_N_A);
203 str = item->cargo == INVALID_CARGO ? STR_LTBLUE_STRING : STR_VEHICLE_DETAILS_CARGO_EMPTY;
204 }
205
206 DrawString(left, right, y, str);
207 }
208
217 static void TrainDetailsInfoTab(const Vehicle *v, int left, int right, int y)
218 {
219 if (RailVehInfo(v->engine_type)->railveh_type == RAILVEH_WAGON) {
220 SetDParam(0, v->engine_type);
221 SetDParam(1, v->value);
222 DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_WAGON_VALUE);
223 } else {
224 SetDParam(0, v->engine_type);
225 SetDParam(1, v->build_year);
226 SetDParam(2, v->value);
227 DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_ENGINE_BUILT_AND_VALUE);
228 }
229 }
230
239 static void TrainDetailsCapacityTab(const CargoSummaryItem *item, int left, int right, int y)
240 {
241 StringID str;
242 if (item->cargo != INVALID_CARGO) {
243 SetDParam(0, item->cargo);
244 SetDParam(1, item->capacity);
245 SetDParam(4, item->subtype);
246 SetDParam(5, _settings_game.vehicle.freight_trains);
247 str = FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_INFO_CAPACITY_MULT : STR_VEHICLE_INFO_CAPACITY;
248 } else {
249 /* Draw subtype only */
250 SetDParam(0, item->subtype);
251 str = STR_VEHICLE_INFO_NO_CAPACITY;
252 }
253 DrawString(left, right, y, str);
254 }
255
261 static void GetCargoSummaryOfArticulatedVehicle(const Train *v, CargoSummary *summary)
262 {
263 summary->Clear();
264 do {
265 if (!v->GetEngine()->CanCarryCargo()) continue;
266
267 CargoSummaryItem new_item;
268 new_item.cargo = v->cargo_cap > 0 ? v->cargo_type : INVALID_CARGO;
269 new_item.subtype = GetCargoSubtypeText(v);
270 if (new_item.cargo == INVALID_CARGO && new_item.subtype == STR_EMPTY) continue;
271
272 CargoSummaryItem *item = summary->Find(new_item);
273 if (item == summary->End()) {
274 item = summary->Append();
275 item->cargo = new_item.cargo;
276 item->subtype = new_item.subtype;
277 item->capacity = 0;
278 item->amount = 0;
279 item->source = INVALID_STATION;
280 }
281
282 item->capacity += v->cargo_cap;
283 item->amount += v->cargo.StoredCount();
284 if (item->source == INVALID_STATION) item->source = v->cargo.Source();
285 } while ((v = v->Next()) != NULL && v->IsArticulatedPart());
286 }
287
293 static uint GetLengthOfArticulatedVehicle(const Train *v)
294 {
295 uint length = 0;
296
297 do {
298 length += v->GetDisplayImageWidth();
299 } while ((v = v->Next()) != NULL && v->IsArticulatedPart());
300
301 return length;
302 }
303
310 int GetTrainDetailsWndVScroll(VehicleID veh_id, TrainDetailsWindowTabs det_tab)
311 {
312 int num = 0;
313
314 if (det_tab == TDW_TAB_TOTALS) { // Total cargo tab
315 CargoArray act_cargo;
316 CargoArray max_cargo;
317 for (const Vehicle *v = Vehicle::Get(veh_id); v != NULL; v = v->Next()) {
318 act_cargo[v->cargo_type] += v->cargo.StoredCount();
319 max_cargo[v->cargo_type] += v->cargo_cap;
320 }
321
322 /* Set scroll-amount separately from counting, as to not compute num double
323 * for more carriages of the same type
324 */
325 for (CargoID i = 0; i < NUM_CARGO; i++) {
326 if (max_cargo[i] > 0) num++; // only count carriages that the train has
327 }
328 num++; // needs one more because first line is description string
329 } else {
330 for (const Train *v = Train::Get(veh_id); v != NULL; v = v->GetNextVehicle()) {
331 GetCargoSummaryOfArticulatedVehicle(v, &_cargo_summary);
332 num += max(1u, _cargo_summary.Length());
333
334 uint length = GetLengthOfArticulatedVehicle(v);
335 if (length > TRAIN_DETAILS_MAX_INDENT) num++;
336 }
337 }
338
339 return num;
340 }
341
353 void DrawTrainDetails(const Train *v, int left, int right, int y, int vscroll_pos, uint16 vscroll_cap, TrainDetailsWindowTabs det_tab)
354 {
355 /* get rid of awkward offset */
356 y -= WD_MATRIX_TOP;
357
358 int sprite_height = ScaleGUITrad(GetVehicleHeight(VEH_TRAIN));
359 int line_height = max(sprite_height, WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM);
360 int sprite_y_offset = line_height / 2;
361 int text_y_offset = (line_height - FONT_HEIGHT_NORMAL) / 2;
362
363 /* draw the first 3 details tabs */
364 if (det_tab != TDW_TAB_TOTALS) {
365 bool rtl = _current_text_dir == TD_RTL;
366 Direction dir = rtl ? DIR_E : DIR_W;
367 int x = rtl ? right : left;
368 for (; v != NULL && vscroll_pos > -vscroll_cap; v = v->GetNextVehicle()) {
369 GetCargoSummaryOfArticulatedVehicle(v, &_cargo_summary);
370
371 /* Draw sprites */
372 uint dx = 0;
373 int px = x;
374 const Train *u = v;
375 do {
376 Point offset;
377 int width = u->GetDisplayImageWidth(&offset);
378 if (vscroll_pos <= 0 && vscroll_pos > -vscroll_cap) {
379 int pitch = 0;
380 const Engine *e = Engine::Get(v->engine_type);
381 if (e->GetGRF() != NULL) {
382 pitch = ScaleGUITrad(e->GetGRF()->traininfo_vehicle_pitch);
383 }
384 PaletteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
385 DrawSprite(u->GetImage(dir, EIT_IN_DETAILS), pal, px + (rtl ? -offset.x : offset.x), y - line_height * vscroll_pos + sprite_y_offset + pitch);
386 }
387 px += rtl ? -width : width;
388 dx += width;
389 u = u->Next();
390 } while (u != NULL && u->IsArticulatedPart());
391
392 bool separate_sprite_row = (dx > (uint)ScaleGUITrad(TRAIN_DETAILS_MAX_INDENT));
393 if (separate_sprite_row) {
394 vscroll_pos--;
395 dx = 0;
396 }
397
398 uint num_lines = max(1u, _cargo_summary.Length());
399 for (uint i = 0; i < num_lines; i++) {
400 int sprite_width = max<int>(dx, ScaleGUITrad(TRAIN_DETAILS_MIN_INDENT)) + 3;
401 int data_left = left + (rtl ? 0 : sprite_width);
402 int data_right = right - (rtl ? sprite_width : 0);
403 if (vscroll_pos <= 0 && vscroll_pos > -vscroll_cap) {
404 int py = y - line_height * vscroll_pos + text_y_offset;
405 if (i > 0 || separate_sprite_row) {
406 if (vscroll_pos != 0) GfxFillRect(left, py - WD_MATRIX_TOP - 1, right, py - WD_MATRIX_TOP, _colour_gradient[COLOUR_GREY][5]);
407 }
408 switch (det_tab) {
409 case TDW_TAB_CARGO:
410 if (i < _cargo_summary.Length()) {
411 TrainDetailsCargoTab(&_cargo_summary[i], data_left, data_right, py);
412 } else {
413 DrawString(data_left, data_right, py, STR_QUANTITY_N_A, TC_LIGHT_BLUE);
414 }
415 break;
416
417 case TDW_TAB_INFO:
418 if (i == 0) TrainDetailsInfoTab(v, data_left, data_right, py);
419 break;
420
421 case TDW_TAB_CAPACITY:
422 if (i < _cargo_summary.Length()) {
423 TrainDetailsCapacityTab(&_cargo_summary[i], data_left, data_right, py);
424 } else {
425 SetDParam(0, STR_EMPTY);
426 DrawString(data_left, data_right, py, STR_VEHICLE_INFO_NO_CAPACITY);
427 }
428 break;
429
430 default: NOT_REACHED();
431 }
432 }
433 vscroll_pos--;
434 }
435 }
436 } else {
437 CargoArray act_cargo;
438 CargoArray max_cargo;
439 Money feeder_share = 0;
440
441 for (const Vehicle *u = v; u != NULL; u = u->Next()) {
442 act_cargo[u->cargo_type] += u->cargo.StoredCount();
443 max_cargo[u->cargo_type] += u->cargo_cap;
444 feeder_share += u->cargo.FeederShare();
445 }
446
447 /* draw total cargo tab */
448 DrawString(left, right, y + text_y_offset, STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_TEXT);
449 y += line_height;
450
451 for (CargoID i = 0; i < NUM_CARGO; i++) {
452 if (max_cargo[i] > 0 && --vscroll_pos < 0 && vscroll_pos > -vscroll_cap) {
453 SetDParam(0, i); // {CARGO} #1
454 SetDParam(1, act_cargo[i]); // {CARGO} #2
455 SetDParam(2, i); // {SHORTCARGO} #1
456 SetDParam(3, max_cargo[i]); // {SHORTCARGO} #2
457 SetDParam(4, _settings_game.vehicle.freight_trains);
458 DrawString(left, right, y + text_y_offset, FreightWagonMult(i) > 1 ? STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_MULT : STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY);
459 y += line_height;
460 }
461 }
462 SetDParam(0, feeder_share);
463 DrawString(left, right, y + text_y_offset, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);
464 }
465 }