OpenTTD
roadveh_gui.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "stdafx.h"
11 #include "roadveh.h"
12 #include "window_gui.h"
13 #include "strings_func.h"
14 #include "vehicle_func.h"
15 #include "string_func.h"
16 #include "zoom_func.h"
17 
18 #include "table/strings.h"
19 
20 #include "safeguards.h"
21 
30 void DrawRoadVehDetails(const Vehicle *v, int left, int right, int y)
31 {
32  uint y_offset = v->HasArticulatedPart() ? ScaleGUITrad(15) : 0; // Draw the first line below the sprite of an articulated RV instead of after it.
33  StringID str;
34  Money feeder_share = 0;
35 
36  SetDParam(0, v->engine_type);
37  SetDParam(1, v->build_year);
38  SetDParam(2, v->value);
39  DrawString(left, right, y + y_offset, STR_VEHICLE_INFO_BUILT_VALUE);
40 
41  if (v->HasArticulatedPart()) {
42  CargoArray max_cargo;
43  StringID subtype_text[NUM_CARGO];
44  char capacity[512];
45 
46  memset(subtype_text, 0, sizeof(subtype_text));
47 
48  for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
49  max_cargo[u->cargo_type] += u->cargo_cap;
50  if (u->cargo_cap > 0) {
51  StringID text = GetCargoSubtypeText(u);
52  if (text != STR_EMPTY) subtype_text[u->cargo_type] = text;
53  }
54  }
55 
56  GetString(capacity, STR_VEHICLE_DETAILS_TRAIN_ARTICULATED_RV_CAPACITY, lastof(capacity));
57 
58  bool first = true;
59  for (CargoID i = 0; i < NUM_CARGO; i++) {
60  if (max_cargo[i] > 0) {
61  char buffer[128];
62 
63  SetDParam(0, i);
64  SetDParam(1, max_cargo[i]);
65  GetString(buffer, STR_JUST_CARGO, lastof(buffer));
66 
67  if (!first) strecat(capacity, ", ", lastof(capacity));
68  strecat(capacity, buffer, lastof(capacity));
69 
70  if (subtype_text[i] != 0) {
71  GetString(buffer, subtype_text[i], lastof(buffer));
72  strecat(capacity, buffer, lastof(capacity));
73  }
74 
75  first = false;
76  }
77  }
78 
79  DrawString(left, right, y + FONT_HEIGHT_NORMAL + y_offset, capacity, TC_BLUE);
80 
81  for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
82  if (u->cargo_cap == 0) continue;
83 
84  str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
85  if (u->cargo.StoredCount() > 0) {
86  SetDParam(0, u->cargo_type);
87  SetDParam(1, u->cargo.StoredCount());
88  SetDParam(2, u->cargo.Source());
89  str = STR_VEHICLE_DETAILS_CARGO_FROM;
90  feeder_share += u->cargo.FeederShare();
91  }
92  DrawString(left, right, y + 2 * FONT_HEIGHT_NORMAL + 1 + y_offset, str);
93 
94  y_offset += FONT_HEIGHT_NORMAL + 1;
95  }
96 
97  y_offset -= FONT_HEIGHT_NORMAL + 1;
98  } else {
99  SetDParam(0, v->cargo_type);
100  SetDParam(1, v->cargo_cap);
102  DrawString(left, right, y + FONT_HEIGHT_NORMAL + y_offset, STR_VEHICLE_INFO_CAPACITY);
103 
104  str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
105  if (v->cargo.StoredCount() > 0) {
106  SetDParam(0, v->cargo_type);
107  SetDParam(1, v->cargo.StoredCount());
108  SetDParam(2, v->cargo.Source());
109  str = STR_VEHICLE_DETAILS_CARGO_FROM;
110  feeder_share += v->cargo.FeederShare();
111  }
112  DrawString(left, right, y + 2 * FONT_HEIGHT_NORMAL + 1 + y_offset, str);
113  }
114 
115  /* Draw Transfer credits text */
116  SetDParam(0, feeder_share);
117  DrawString(left, right, y + 3 * FONT_HEIGHT_NORMAL + 3 + y_offset, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);
118 }
119 
129 void DrawRoadVehImage(const Vehicle *v, int left, int right, int y, VehicleID selection, EngineImageType image_type, int skip)
130 {
131  bool rtl = _current_text_dir == TD_RTL;
132  Direction dir = rtl ? DIR_E : DIR_W;
133  const RoadVehicle *u = RoadVehicle::From(v);
134 
135  DrawPixelInfo tmp_dpi, *old_dpi;
136  int max_width = right - left + 1;
137 
138  if (!FillDrawPixelInfo(&tmp_dpi, left, y, max_width, ScaleGUITrad(14))) return;
139 
140  old_dpi = _cur_dpi;
141  _cur_dpi = &tmp_dpi;
142 
143  int px = rtl ? max_width + skip : -skip;
144  for (; u != nullptr && (rtl ? px > 0 : px < max_width); u = u->Next()) {
145  Point offset;
146  int width = u->GetDisplayImageWidth(&offset);
147 
148  if (rtl ? px + width > 0 : px - width < max_width) {
150  VehicleSpriteSeq seq;
151  u->GetImage(dir, image_type, &seq);
152  seq.Draw(px + (rtl ? -offset.x : offset.x), ScaleGUITrad(6) + offset.y, pal, (u->vehstatus & VS_CRASHED) != 0);
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 }
StationID Source() const
Returns source of the first cargo packet in this list.
Definition: cargopacket.h:322
Functions related to OTTD&#39;s strings.
Road vehicle states.
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:18
int GetDisplayImageWidth(Point *offset=nullptr) const
Get the width of a road vehicle image in the GUI.
Definition: roadveh_cmd.cpp:90
VehicleCargoList cargo
The cargo this vehicle is carrying.
Definition: vehicle_base.h:307
Money value
Value of the vehicle.
Definition: vehicle_base.h:239
Data about how and where to blit pixels.
Definition: gfx_type.h:154
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:97
West.
static int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition: zoom_func.h:76
Maximal number of cargo types in a game.
Definition: cargo_type.h:64
Functions related to vehicles.
void Draw(int x, int y, PaletteID default_pal, bool force_pal) const
Draw the sprite sequence.
Definition: vehicle.cpp:126
Vehicle data structure.
Definition: vehicle_base.h:210
Tindex index
Index of this pool item.
Definition: pool_type.hpp:189
Money FeederShare() const
Returns total sum of the feeder share for all packets.
Definition: cargopacket.h:331
PaletteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition: vehicle.cpp:1982
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
byte vehstatus
Status.
Definition: vehicle_base.h:315
EngineImageType
Visualisation contexts of vehicles and engines.
Definition: vehicle_type.h:85
uint StoredCount() const
Returns sum of cargo on board the vehicle (ie not only reserved).
Definition: cargopacket.h:351
static RoadVehicle * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
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:1481
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:175
Buses, trucks and trams belong to this class.
Definition: roadveh.h:107
uint16 cargo_cap
total capacity
Definition: vehicle_base.h:305
Functions related to low-level strings.
Vehicle is crashed.
Definition: vehicle_base.h:37
East.
uint32 VehicleID
The type all our vehicle IDs have.
Definition: vehicle_type.h:16
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:176
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.
Direction
Defines the 8 directions on the map.
CargoID cargo_type
type of cargo this vehicle is carrying
Definition: vehicle_base.h:303
bool HasArticulatedPart() const
Check if an engine has an articulated part.
Definition: vehicle_base.h:899
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:498
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:128
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Draw border only, no background.
Definition: window_gui.h:28
Year build_year
Year the vehicle has been built.
Definition: vehicle_base.h:255
Class for storing amounts of cargo.
Definition: cargo_type.h:81
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition: sprites.h:1587
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition: vehicle_base.h:579
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:48
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:30
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:286
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
Text is written right-to-left by default.
Definition: strings_type.h:24
StringID GetCargoSubtypeText(const Vehicle *v)
Get the cargo subtype text from NewGRF for the vehicle details window.
void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
Gets the sprite to show for the given direction.
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:199