OpenTTD
roadveh_gui.cpp
Go to the documentation of this file.
1 /* $Id: roadveh_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 "roadveh.h"
14 #include "window_gui.h"
15 #include "strings_func.h"
16 #include "vehicle_func.h"
17 #include "string_func.h"
18 #include "zoom_func.h"
19 
20 #include "table/strings.h"
21 
22 #include "safeguards.h"
23 
32 void DrawRoadVehDetails(const Vehicle *v, int left, int right, int y)
33 {
34  uint y_offset = v->HasArticulatedPart() ? ScaleGUITrad(15) : 0; // Draw the first line below the sprite of an articulated RV instead of after it.
35  StringID str;
36  Money feeder_share = 0;
37 
38  SetDParam(0, v->engine_type);
39  SetDParam(1, v->build_year);
40  SetDParam(2, v->value);
41  DrawString(left, right, y + y_offset, STR_VEHICLE_INFO_BUILT_VALUE);
42 
43  if (v->HasArticulatedPart()) {
44  CargoArray max_cargo;
45  StringID subtype_text[NUM_CARGO];
46  char capacity[512];
47 
48  memset(subtype_text, 0, sizeof(subtype_text));
49 
50  for (const Vehicle *u = v; u != NULL; u = u->Next()) {
51  max_cargo[u->cargo_type] += u->cargo_cap;
52  if (u->cargo_cap > 0) {
53  StringID text = GetCargoSubtypeText(u);
54  if (text != STR_EMPTY) subtype_text[u->cargo_type] = text;
55  }
56  }
57 
58  GetString(capacity, STR_VEHICLE_DETAILS_TRAIN_ARTICULATED_RV_CAPACITY, lastof(capacity));
59 
60  bool first = true;
61  for (CargoID i = 0; i < NUM_CARGO; i++) {
62  if (max_cargo[i] > 0) {
63  char buffer[128];
64 
65  SetDParam(0, i);
66  SetDParam(1, max_cargo[i]);
67  GetString(buffer, STR_JUST_CARGO, lastof(buffer));
68 
69  if (!first) strecat(capacity, ", ", lastof(capacity));
70  strecat(capacity, buffer, lastof(capacity));
71 
72  if (subtype_text[i] != 0) {
73  GetString(buffer, subtype_text[i], lastof(buffer));
74  strecat(capacity, buffer, lastof(capacity));
75  }
76 
77  first = false;
78  }
79  }
80 
81  DrawString(left, right, y + FONT_HEIGHT_NORMAL + y_offset, capacity, TC_BLUE);
82 
83  for (const Vehicle *u = v; u != NULL; u = u->Next()) {
84  if (u->cargo_cap == 0) continue;
85 
86  str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
87  if (u->cargo.StoredCount() > 0) {
88  SetDParam(0, u->cargo_type);
89  SetDParam(1, u->cargo.StoredCount());
90  SetDParam(2, u->cargo.Source());
91  str = STR_VEHICLE_DETAILS_CARGO_FROM;
92  feeder_share += u->cargo.FeederShare();
93  }
94  DrawString(left, right, y + 2 * FONT_HEIGHT_NORMAL + 1 + y_offset, str);
95 
96  y_offset += FONT_HEIGHT_NORMAL + 1;
97  }
98 
99  y_offset -= FONT_HEIGHT_NORMAL + 1;
100  } else {
101  SetDParam(0, v->cargo_type);
102  SetDParam(1, v->cargo_cap);
104  DrawString(left, right, y + FONT_HEIGHT_NORMAL + y_offset, STR_VEHICLE_INFO_CAPACITY);
105 
106  str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
107  if (v->cargo.StoredCount() > 0) {
108  SetDParam(0, v->cargo_type);
109  SetDParam(1, v->cargo.StoredCount());
110  SetDParam(2, v->cargo.Source());
111  str = STR_VEHICLE_DETAILS_CARGO_FROM;
112  feeder_share += v->cargo.FeederShare();
113  }
114  DrawString(left, right, y + 2 * FONT_HEIGHT_NORMAL + 1 + y_offset, str);
115  }
116 
117  /* Draw Transfer credits text */
118  SetDParam(0, feeder_share);
119  DrawString(left, right, y + 3 * FONT_HEIGHT_NORMAL + 3 + y_offset, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);
120 }
121 
131 void DrawRoadVehImage(const Vehicle *v, int left, int right, int y, VehicleID selection, EngineImageType image_type, int skip)
132 {
133  bool rtl = _current_text_dir == TD_RTL;
134  Direction dir = rtl ? DIR_E : DIR_W;
135  const RoadVehicle *u = RoadVehicle::From(v);
136 
137  DrawPixelInfo tmp_dpi, *old_dpi;
138  int max_width = right - left + 1;
139 
140  if (!FillDrawPixelInfo(&tmp_dpi, left, y, max_width, ScaleGUITrad(14))) return;
141 
142  old_dpi = _cur_dpi;
143  _cur_dpi = &tmp_dpi;
144 
145  int px = rtl ? max_width + skip : -skip;
146  for (; u != NULL && (rtl ? px > 0 : px < max_width); u = u->Next()) {
147  Point offset;
148  int width = u->GetDisplayImageWidth(&offset);
149 
150  if (rtl ? px + width > 0 : px - width < max_width) {
151  PaletteID pal = (u->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(u);
152  DrawSprite(u->GetImage(dir, image_type), pal, px + (rtl ? -offset.x : offset.x), ScaleGUITrad(6) + offset.y);
153  }
154 
155  px += rtl ? -width : width;
156  }
157 
158  if (v->index == selection) {
159  DrawFrameRect((rtl ? px : 0), 0, (rtl ? max_width : px) - 1, ScaleGUITrad(13) - 1, COLOUR_WHITE, FR_BORDERONLY);
160  }
161 
162  _cur_dpi = old_dpi;
163 }