OpenTTD
roadveh_gui.cpp
Go to the documentation of this file.
1 /* $Id: roadveh_gui.cpp 27689 2016-12-10 14:33:58Z 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  VehicleSpriteSeq seq;
153  u->GetImage(dir, image_type, &seq);
154  seq.Draw(px + (rtl ? -offset.x : offset.x), ScaleGUITrad(6) + offset.y, pal, (u->vehstatus & VS_CRASHED) != 0);
155  }
156 
157  px += rtl ? -width : width;
158  }
159 
160  if (v->index == selection) {
161  DrawFrameRect((rtl ? px : 0), 0, (rtl ? max_width : px) - 1, ScaleGUITrad(13) - 1, COLOUR_WHITE, FR_BORDERONLY);
162  }
163 
164  _cur_dpi = old_dpi;
165 }
StationID Source() const
Returns source of the first cargo packet in this list.
Definition: cargopacket.h:337
Functions related to OTTD&#39;s strings.
Road vehicle states.
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:20
VehicleCargoList cargo
The cargo this vehicle is carrying.
Definition: vehicle_base.h:309
Money value
Value of the vehicle.
Definition: vehicle_base.h:241
Data about how and where to blit pixels.
Definition: gfx_type.h:156
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:99
East.
static int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition: zoom_func.h:82
Functions related to vehicles.
void Draw(int x, int y, PaletteID default_pal, bool force_pal) const
Draw the sprite sequence.
Definition: vehicle.cpp:127
Vehicle data structure.
Definition: vehicle_base.h:212
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Money FeederShare() const
Returns total sum of the feeder share for all packets.
Definition: cargopacket.h:346
PaletteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition: vehicle.cpp:1954
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
EngineImageType
Visualisation contexts of vehicles and engines.
Definition: vehicle_type.h:87
uint StoredCount() const
Returns sum of cargo on board the vehicle (ie not only reserved).
Definition: cargopacket.h:366
static RoadVehicle * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Direction
Defines the 8 directions on the map.
Functions, definitions and such used only by the GUI.
bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
Set up a clipping area for only drawing into a certain area.
Definition: gfx.cpp:1480
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:177
Buses, trucks and trams belong to this class.
Definition: roadveh.h:88
uint16 cargo_cap
total capacity
Definition: vehicle_base.h:307
Functions related to low-level strings.
Vehicle is crashed.
Definition: vehicle_base.h:39
West.
uint32 VehicleID
The type all our vehicle IDs have.
Definition: vehicle_type.h:18
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:180
T * Next() const
Get next vehicle in the chain.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
CargoID cargo_type
type of cargo this vehicle is carrying
Definition: vehicle_base.h:305
bool HasArticulatedPart() const
Check if an engine has an articulated part.
Definition: vehicle_base.h:900
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:499
void DrawRoadVehImage(const Vehicle *v, int left, int right, int y, VehicleID selection, EngineImageType image_type, int skip)
Draws an image of a road vehicle chain.
Sprite sequence for a vehicle part.
Definition: vehicle_base.h:130
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
Draw border only, no background.
Definition: window_gui.h:30
Year build_year
Year the vehicle has been built.
Definition: vehicle_base.h:257
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
Class for storing amounts of cargo.
Definition: cargo_type.h:74
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition: sprites.h:1569
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition: vehicle_base.h:582
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:52
Functions related to zooming.
Coordinates of a point in 2D.
void DrawRoadVehDetails(const Vehicle *v, int left, int right, int y)
Draw the details for the given vehicle at the given position.
Definition: roadveh_gui.cpp:32
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:288
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
Text is written right-to-left by default.
Definition: strings_type.h:26
StringID GetCargoSubtypeText(const Vehicle *v)
Get the cargo subtype text from NewGRF for the vehicle details window.
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:201