OpenTTD Source: src/roadveh_gui.cpp Source File

OpenTTD Source 14.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 "core/backup_type.hpp"
12 #include "roadveh.h"
13 #include "window_gui.h"
14 #include "strings_func.h"
15 #include "vehicle_func.h"
16 #include "string_func.h"
17 #include "zoom_func.h"
18
19 #include "table/strings.h"
20
21 #include "safeguards.h"
22
29 void DrawRoadVehDetails(const Vehicle *v, const Rect &r)
30 {
31 int y = r.top + (v->HasArticulatedPart() ? ScaleSpriteTrad(15) : 0); // Draw the first line below the sprite of an articulated RV instead of after it.
32 StringID str;
33 Money feeder_share = 0;
34
35 SetDParam(0, PackEngineNameDParam(v->engine_type, EngineNameContext::VehicleDetails));
36 SetDParam(1, v->build_year);
37 SetDParam(2, v->value);
38 DrawString(r.left, r.right, y, STR_VEHICLE_INFO_BUILT_VALUE);
39 y += GetCharacterHeight(FS_NORMAL);
40
41 if (v->HasArticulatedPart()) {
42 CargoArray max_cargo{};
43 std::array<StringID, NUM_CARGO> subtype_text{};
44
45 for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
46 max_cargo[u->cargo_type] += u->cargo_cap;
47 if (u->cargo_cap > 0) {
48 StringID text = GetCargoSubtypeText(u);
49 if (text != STR_EMPTY) subtype_text[u->cargo_type] = text;
50 }
51 }
52
53 std::string capacity = GetString(STR_VEHICLE_DETAILS_TRAIN_ARTICULATED_RV_CAPACITY);
54
55 bool first = true;
56 for (const CargoSpec *cs : _sorted_cargo_specs) {
57 CargoID cid = cs->Index();
58 if (max_cargo[cid] > 0) {
59 if (!first) capacity += ", ";
60
61 SetDParam(0, cid);
62 SetDParam(1, max_cargo[cid]);
63 capacity += GetString(STR_JUST_CARGO);
64
65 if (subtype_text[cid] != STR_NULL) {
66 capacity += GetString(subtype_text[cid]);
67 }
68
69 first = false;
70 }
71 }
72
73 DrawString(r.left, r.right, y, capacity, TC_BLUE);
74 y += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal;
75
76 for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
77 if (u->cargo_cap == 0) continue;
78
79 str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
80 if (u->cargo.StoredCount() > 0) {
81 SetDParam(0, u->cargo_type);
82 SetDParam(1, u->cargo.StoredCount());
83 SetDParam(2, u->cargo.GetFirstStation());
84 str = STR_VEHICLE_DETAILS_CARGO_FROM;
85 feeder_share += u->cargo.GetFeederShare();
86 }
87 DrawString(r.left, r.right, y, str);
88 y += GetCharacterHeight(FS_NORMAL);
89 }
90 y += WidgetDimensions::scaled.vsep_normal;
91 } else {
92 SetDParam(0, v->cargo_type);
93 SetDParam(1, v->cargo_cap);
94 SetDParam(4, GetCargoSubtypeText(v));
95 DrawString(r.left, r.right, y, STR_VEHICLE_INFO_CAPACITY);
96 y += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal;
97
98 str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
99 if (v->cargo.StoredCount() > 0) {
100 SetDParam(0, v->cargo_type);
101 SetDParam(1, v->cargo.StoredCount());
102 SetDParam(2, v->cargo.GetFirstStation());
103 str = STR_VEHICLE_DETAILS_CARGO_FROM;
104 feeder_share += v->cargo.GetFeederShare();
105 }
106 DrawString(r.left, r.right, y, str);
107 y += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal;
108 }
109
110 /* Draw Transfer credits text */
111 SetDParam(0, feeder_share);
112 DrawString(r.left, r.right, y, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);
113 }
114
122 void DrawRoadVehImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type, int skip)
123 {
124 bool rtl = _current_text_dir == TD_RTL;
125 Direction dir = rtl ? DIR_E : DIR_W;
126 const RoadVehicle *u = RoadVehicle::From(v);
127
128 DrawPixelInfo tmp_dpi;
129 int max_width = r.Width();
130
131 if (!FillDrawPixelInfo(&tmp_dpi, r)) return;
132
133 AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi);
134
135 int px = rtl ? max_width + skip : -skip;
136 int y = r.Height() / 2;
137 for (; u != nullptr && (rtl ? px > 0 : px < max_width); u = u->Next())
138 {
139 Point offset;
140 int width = u->GetDisplayImageWidth(&offset);
141
142 if (rtl ? px + width > 0 : px - width < max_width) {
143 PaletteID pal = (u->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(u);
144 VehicleSpriteSeq seq;
145 u->GetImage(dir, image_type, &seq);
146 seq.Draw(px + (rtl ? -offset.x : offset.x), y + offset.y, pal, (u->vehstatus & VS_CRASHED) != 0);
147 }
148
149 px += rtl ? -width : width;
150 }
151
152 if (v->index == selection) {
153 int height = ScaleSpriteTrad(12);
154 Rect hr = {(rtl ? px : 0), 0, (rtl ? max_width : px) - 1, height - 1};
155 DrawFrameRect(hr.Translate(r.left, CenterBounds(r.top, r.bottom, height)).Expand(WidgetDimensions::scaled.bevel), COLOUR_WHITE, FR_BORDERONLY);
156 }
157 }
RoadVehicle
Buses, trucks and trams belong to this class.
Definition: roadveh.h:106
VehicleCargoList::StoredCount
uint StoredCount() const
Returns sum of cargo on board the vehicle (ie not only reserved).
Definition: cargopacket.h:434
Rect::Height
int Height() const
Get height of Rect.
Definition: geometry_type.hpp:91
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:271
WidgetDimensions::scaled
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition: window_gui.h:68
Vehicle::cargo_cap
uint16_t cargo_cap
total capacity
Definition: vehicle_base.h:339
_sorted_cargo_specs
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
Definition: cargotype.cpp:168
Vehicle::Next
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition: vehicle_base.h:628
SpecializedVehicle::Next
T * Next() const
Get next vehicle in the chain.
Definition: vehicle_base.h:1126
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Vehicle::vehstatus
byte vehstatus
Status.
Definition: vehicle_base.h:349
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:238
CargoArray
Class for storing amounts of cargo.
Definition: cargo_type.h:114
zoom_func.h
CargoSpec
Specification of a cargo type.
Definition: cargotype.h:68
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:153
EngineImageType
EngineImageType
Visualisation contexts of vehicles and engines.
Definition: vehicle_type.h:85
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:240
VehicleSpriteSeq::Draw
void Draw(int x, int y, PaletteID default_pal, bool force_pal) const
Draw the sprite sequence.
Definition: vehicle.cpp:131
VehicleCargoList::GetFirstStation
StationID GetFirstStation() const
Returns the first station of the first cargo packet in this list.
Definition: cargopacket.h:405
PaletteID
uint32_t PaletteID
The number of the palette.
Definition: gfx_type.h:18
window_gui.h
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:203
Rect::Translate
Rect Translate(int x, int y) const
Copy and translate Rect by x,y pixels.
Definition: geometry_type.hpp:174
DIR_E
@ DIR_E
East.
Definition: direction_type.h:28
PackEngineNameDParam
uint64_t PackEngineNameDParam(EngineID engine_id, EngineNameContext context, uint32_t extra_data=0)
Combine an engine ID and a name context to an engine name dparam.
Definition: engine_type.h:199
Vehicle::engine_type
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:319
VS_CRASHED
@ VS_CRASHED
Vehicle is crashed.
Definition: vehicle_base.h:40
VehicleSpriteSeq
Sprite sequence for a vehicle part.
Definition: vehicle_base.h:131
Vehicle::cargo
VehicleCargoList cargo
The cargo this vehicle is carrying.
Definition: vehicle_base.h:341
Vehicle::build_year
TimerGameCalendar::Year build_year
Year the vehicle has been built.
Definition: vehicle_base.h:287
safeguards.h
VehicleID
uint32_t VehicleID
The type all our vehicle IDs have.
Definition: vehicle_type.h:16
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
CenterBounds
int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
Definition: gfx_func.h:166
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:122
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:1567
GetCargoSubtypeText
StringID GetCargoSubtypeText(const Vehicle *v)
Get the cargo subtype text from NewGRF for the vehicle details window.
Definition: vehicle_gui.cpp:1325
DrawRoadVehDetails
void DrawRoadVehDetails(const Vehicle *v, const Rect &r)
Draw the details for the given vehicle at the given position.
Definition: roadveh_gui.cpp:29
string_func.h
vehicle_func.h
PALETTE_CRASH
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition: sprites.h:1602
strings_func.h
VehicleCargoList::GetFeederShare
Money GetFeederShare() const
Returns total sum of the feeder share for all packets.
Definition: cargopacket.h:414
RoadVehicle::GetDisplayImageWidth
int GetDisplayImageWidth(Point *offset=nullptr) const
Get the width of a road vehicle image in the GUI.
Definition: roadveh_cmd.cpp:94
SpecializedVehicle< RoadVehicle, Type >::From
static RoadVehicle * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition: vehicle_base.h:1211
GetVehiclePalette
PaletteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition: vehicle.cpp:2145
SetDParam
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings.cpp:104
WidgetDimensions::vsep_normal
int vsep_normal
Normal vertical spacing.
Definition: window_gui.h:60
AutoRestoreBackup
Class to backup a specific variable and restore it upon destruction of this object to prevent stack v...
Definition: backup_type.hpp:153
ScaleSpriteTrad
int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition: zoom_func.h:107
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition: strings.cpp:327
VehicleDetails
@ VehicleDetails
Name is shown in the vehicle details GUI.
Definition: engine_type.h:192
Vehicle::HasArticulatedPart
bool HasArticulatedPart() const
Check if an engine has an articulated part.
Definition: vehicle_base.h:959
DrawFrameRect
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:281
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:78
OverflowSafeInt< int64_t >
DrawString
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:654
Vehicle::cargo_type
CargoID cargo_type
type of cargo this vehicle is carrying
Definition: vehicle_base.h:337
WidgetDimensions::bevel
RectPadding bevel
Bevel thickness, affected by "scaled bevels" game option.
Definition: window_gui.h:40
Rect::Width
int Width() const
Get width of Rect.
Definition: geometry_type.hpp:85
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:75
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:56
FR_BORDERONLY
@ FR_BORDERONLY
Draw border only, no background.
Definition: window_gui.h:27
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:151
roadveh.h
backup_type.hpp