OpenTTD Source  13.2.1
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 
28 void DrawRoadVehDetails(const Vehicle *v, const Rect &r)
29 {
30  int y = r.top + (v->HasArticulatedPart() ? ScaleSpriteTrad(15) : 0); // Draw the first line below the sprite of an articulated RV instead of after it.
31  StringID str;
32  Money feeder_share = 0;
33 
35  SetDParam(1, v->build_year);
36  SetDParam(2, v->value);
37  DrawString(r.left, r.right, y, STR_VEHICLE_INFO_BUILT_VALUE);
38  y += FONT_HEIGHT_NORMAL;
39 
40  if (v->HasArticulatedPart()) {
41  CargoArray max_cargo;
42  StringID subtype_text[NUM_CARGO];
43  char capacity[512];
44 
45  memset(subtype_text, 0, sizeof(subtype_text));
46 
47  for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
48  max_cargo[u->cargo_type] += u->cargo_cap;
49  if (u->cargo_cap > 0) {
50  StringID text = GetCargoSubtypeText(u);
51  if (text != STR_EMPTY) subtype_text[u->cargo_type] = text;
52  }
53  }
54 
55  GetString(capacity, STR_VEHICLE_DETAILS_TRAIN_ARTICULATED_RV_CAPACITY, lastof(capacity));
56 
57  bool first = true;
58  for (CargoID i = 0; i < NUM_CARGO; i++) {
59  if (max_cargo[i] > 0) {
60  char buffer[128];
61 
62  SetDParam(0, i);
63  SetDParam(1, max_cargo[i]);
64  GetString(buffer, STR_JUST_CARGO, lastof(buffer));
65 
66  if (!first) strecat(capacity, ", ", lastof(capacity));
67  strecat(capacity, buffer, lastof(capacity));
68 
69  if (subtype_text[i] != 0) {
70  GetString(buffer, subtype_text[i], lastof(buffer));
71  strecat(capacity, buffer, lastof(capacity));
72  }
73 
74  first = false;
75  }
76  }
77 
78  DrawString(r.left, r.right, y, 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(r.left, r.right, y, str);
93  y += FONT_HEIGHT_NORMAL;
94  }
96  } else {
97  SetDParam(0, v->cargo_type);
98  SetDParam(1, v->cargo_cap);
100  DrawString(r.left, r.right, y, STR_VEHICLE_INFO_CAPACITY);
102 
103  str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
104  if (v->cargo.StoredCount() > 0) {
105  SetDParam(0, v->cargo_type);
106  SetDParam(1, v->cargo.StoredCount());
107  SetDParam(2, v->cargo.Source());
108  str = STR_VEHICLE_DETAILS_CARGO_FROM;
109  feeder_share += v->cargo.FeederShare();
110  }
111  DrawString(r.left, r.right, y, str);
113  }
114 
115  /* Draw Transfer credits text */
116  SetDParam(0, feeder_share);
117  DrawString(r.left, r.right, y, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);
118 }
119 
127 void DrawRoadVehImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type, int skip)
128 {
129  bool rtl = _current_text_dir == TD_RTL;
130  Direction dir = rtl ? DIR_E : DIR_W;
131  const RoadVehicle *u = RoadVehicle::From(v);
132 
133  DrawPixelInfo tmp_dpi, *old_dpi;
134  int max_width = r.Width();
135 
136  if (!FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.Width(), r.Height())) return;
137 
138  old_dpi = _cur_dpi;
139  _cur_dpi = &tmp_dpi;
140 
141  int px = rtl ? max_width + skip : -skip;
142  int y = r.Height() / 2;
143  for (; u != nullptr && (rtl ? px > 0 : px < max_width); u = u->Next())
144  {
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), y + offset.y, pal, (u->vehstatus & VS_CRASHED) != 0);
153  }
154 
155  px += rtl ? -width : width;
156  }
157 
158  _cur_dpi = old_dpi;
159 
160  if (v->index == selection) {
161  int height = ScaleSpriteTrad(12);
162  Rect hr = {(rtl ? px : 0), 0, (rtl ? max_width : px) - 1, height - 1};
163  DrawFrameRect(hr.Translate(r.left, CenterBounds(r.top, r.bottom, height)).Expand(WidgetDimensions::scaled.bevel), COLOUR_WHITE, FR_BORDERONLY);
164  }
165 }
RoadVehicle
Buses, trucks and trams belong to this class.
Definition: roadveh.h:107
VehicleCargoList::StoredCount
uint StoredCount() const
Returns sum of cargo on board the vehicle (ie not only reserved).
Definition: cargopacket.h:352
Rect::Height
int Height() const
Get height of Rect.
Definition: geometry_type.hpp:85
Direction
Direction
Defines the 8 directions on the map.
Definition: direction_type.h:24
Vehicle::value
Money value
Value of the vehicle.
Definition: vehicle_base.h:256
PackEngineNameDParam
uint64 PackEngineNameDParam(EngineID engine_id, EngineNameContext context, uint32 extra_data=0)
Combine an engine ID and a name context to an engine name dparam.
Definition: engine_type.h:196
Vehicle::Next
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition: vehicle_base.h:610
SpecializedVehicle::Next
T * Next() const
Get next vehicle in the chain.
Definition: vehicle_base.h:1098
Vehicle::vehstatus
byte vehstatus
Status.
Definition: vehicle_base.h:332
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
CargoArray
Class for storing amounts of cargo.
Definition: cargo_type.h:82
zoom_func.h
DrawString
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:644
DIR_W
@ DIR_W
West.
Definition: direction_type.h:32
Rect::Expand
Rect Expand(int s) const
Copy and expand Rect by s pixels.
Definition: geometry_type.hpp:147
EngineImageType
EngineImageType
Visualisation contexts of vehicles and engines.
Definition: vehicle_type.h:86
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:224
VehicleSpriteSeq::Draw
void Draw(int x, int y, PaletteID default_pal, bool force_pal) const
Draw the sprite sequence.
Definition: vehicle.cpp:126
RoadVehicle::GetImage
void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const override
Gets the sprite to show for the given direction.
Definition: roadveh_cmd.cpp:119
SetDParam
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:196
window_gui.h
Rect::Translate
Rect Translate(int x, int y) const
Copy and translate Rect by x,y pixels.
Definition: geometry_type.hpp:168
DIR_E
@ DIR_E
East.
Definition: direction_type.h:28
Vehicle::engine_type
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:302
VS_CRASHED
@ VS_CRASHED
Vehicle is crashed.
Definition: vehicle_base.h:41
VehicleSpriteSeq
Sprite sequence for a vehicle part.
Definition: vehicle_base.h:132
Vehicle::cargo
VehicleCargoList cargo
The cargo this vehicle is carrying.
Definition: vehicle_base.h:324
ScaleSpriteTrad
static int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition: zoom_func.h:107
safeguards.h
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
VehicleDetails
@ VehicleDetails
Name is shown in the vehicle details GUI.
Definition: engine_type.h:190
DrawRoadVehImage
void DrawRoadVehImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type, int skip)
Draws an image of a road vehicle chain.
Definition: roadveh_gui.cpp:127
stdafx.h
FillDrawPixelInfo
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:1786
GetCargoSubtypeText
StringID GetCargoSubtypeText(const Vehicle *v)
Get the cargo subtype text from NewGRF for the vehicle details window.
Definition: vehicle_gui.cpp:1280
DrawRoadVehDetails
void DrawRoadVehDetails(const Vehicle *v, const Rect &r)
Draw the details for the given vehicle at the given position.
Definition: roadveh_gui.cpp:28
string_func.h
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
vehicle_func.h
PALETTE_CRASH
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition: sprites.h:1598
strings_func.h
Vehicle::cargo_cap
uint16 cargo_cap
total capacity
Definition: vehicle_base.h:322
RoadVehicle::GetDisplayImageWidth
int GetDisplayImageWidth(Point *offset=nullptr) const
Get the width of a road vehicle image in the GUI.
Definition: roadveh_cmd.cpp:92
SpecializedVehicle< RoadVehicle, Type >::From
static RoadVehicle * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition: vehicle_base.h:1183
GetVehiclePalette
PaletteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition: vehicle.cpp:2062
FONT_HEIGHT_NORMAL
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:206
PaletteID
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:18
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:65
Vehicle::HasArticulatedPart
bool HasArticulatedPart() const
Check if an engine has an articulated part.
Definition: vehicle_base.h:931
Vehicle::build_year
Year build_year
Year the vehicle has been built.
Definition: vehicle_base.h:272
WidgetDimensions::bevel
RectPadding bevel
Widths of bevel border.
Definition: window_gui.h:45
VehicleCargoList::Source
StationID Source() const
Returns source of the first cargo packet in this list.
Definition: cargopacket.h:323
VehicleID
uint32 VehicleID
The type all our vehicle IDs have.
Definition: vehicle_type.h:16
DrawFrameRect
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:414
CenterBounds
static int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
Definition: gfx_func.h:178
OverflowSafeInt< int64 >
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
Vehicle::cargo_type
CargoID cargo_type
type of cargo this vehicle is carrying
Definition: vehicle_base.h:320
Rect::Width
int Width() const
Get width of Rect.
Definition: geometry_type.hpp:79
WidgetDimensions::scaled
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition: window_gui.h:68
strecat
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: string.cpp:85
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:69
VehicleCargoList::FeederShare
Money FeederShare() const
Returns total sum of the feeder share for all packets.
Definition: cargopacket.h:332
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:402
TD_RTL
@ TD_RTL
Text is written right-to-left by default.
Definition: strings_type.h:24
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:49
FR_BORDERONLY
@ FR_BORDERONLY
Draw border only, no background.
Definition: window_gui.h:33
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:151
roadveh.h
WidgetDimensions::vsep_normal
int vsep_normal
Normal vertical spacing.
Definition: window_gui.h:61