OpenTTD Source: src/roadveh_gui.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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 Money feeder_share = 0;
33
34 DrawString(r.left, r.right, y, GetString(STR_VEHICLE_INFO_BUILT_VALUE, PackEngineNameDParam(v->engine_type, EngineNameContext::VehicleDetails), v->build_year, v->value));
35 y += GetCharacterHeight(FS_NORMAL);
36
37 if (v->HasArticulatedPart()) {
38 CargoArray max_cargo{};
39 std::array<StringID, NUM_CARGO> subtype_text{};
40
41 for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
42 max_cargo[u->cargo_type] += u->cargo_cap;
43 if (u->cargo_cap > 0) {
44 StringID text = GetCargoSubtypeText(u);
45 if (text != STR_EMPTY) subtype_text[u->cargo_type] = text;
46 }
47 }
48
49 std::string capacity;
50 std::string_view list_separator = GetListSeparator();
51
52 bool first = true;
53 for (const CargoSpec *cs : _sorted_cargo_specs) {
54 CargoType cargo_type = cs->Index();
55 if (max_cargo[cargo_type] > 0) {
56 if (!first) capacity += list_separator;
57
58 auto params = MakeParameters(cargo_type, max_cargo[cargo_type]);
59 AppendStringWithArgsInPlace(capacity, STR_JUST_CARGO, params);
60
61 if (subtype_text[cargo_type] != STR_NULL) {
62 AppendStringInPlace(capacity, subtype_text[cargo_type]);
63 }
64
65 first = false;
66 }
67 }
68
69 DrawString(r.left, r.right, y, GetString(STR_VEHICLE_DETAILS_TRAIN_ARTICULATED_RV_CAPACITY, capacity), TC_BLUE);
70 y += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal;
71
72 for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
73 if (u->cargo_cap == 0) continue;
74
75 std::string str;
76 if (u->cargo.StoredCount() > 0) {
77 str = GetString(STR_VEHICLE_DETAILS_CARGO_FROM, u->cargo_type, u->cargo.StoredCount(), u->cargo.GetFirstStation());
78 feeder_share += u->cargo.GetFeederShare();
79 } else {
80 str = GetString(STR_VEHICLE_DETAILS_CARGO_EMPTY);
81 }
82 DrawString(r.left, r.right, y, str);
83 y += GetCharacterHeight(FS_NORMAL);
84 }
85 y += WidgetDimensions::scaled.vsep_normal;
86 } else {
87 DrawString(r.left, r.right, y, GetString(STR_VEHICLE_INFO_CAPACITY, v->cargo_type, v->cargo_cap, GetCargoSubtypeText(v)));
88 y += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal;
89
90 std::string str;
91 if (v->cargo.StoredCount() > 0) {
92 str = GetString(STR_VEHICLE_DETAILS_CARGO_FROM, v->cargo_type, v->cargo.StoredCount(), v->cargo.GetFirstStation());
93 feeder_share += v->cargo.GetFeederShare();
94 } else {
95 str = GetString(STR_VEHICLE_DETAILS_CARGO_EMPTY);
96 }
97 DrawString(r.left, r.right, y, str);
98 y += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal;
99 }
100
101 /* Draw Transfer credits text */
102 DrawString(r.left, r.right, y, GetString(STR_VEHICLE_INFO_FEEDER_CARGO_VALUE, feeder_share));
103}
104
112 void DrawRoadVehImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type, int skip)
113{
114 bool rtl = _current_text_dir == TD_RTL;
115 Direction dir = rtl ? DIR_E : DIR_W;
116 const RoadVehicle *u = RoadVehicle::From(v);
117
118 DrawPixelInfo tmp_dpi;
119 int max_width = r.Width();
120
121 if (!FillDrawPixelInfo(&tmp_dpi, r)) return;
122
123 AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi);
124
125 bool do_overlays = ShowCargoIconOverlay();
126 /* List of overlays, only used if cargo icon overlays are enabled. */
127 static std::vector<CargoIconOverlay> overlays;
128
129 int px = rtl ? max_width + skip : -skip;
130 int y = r.Height() / 2;
131 for (; u != nullptr && (rtl ? px > 0 : px < max_width); u = u->Next())
132 {
133 Point offset;
134 int width = u->GetDisplayImageWidth(&offset);
135
136 if (rtl ? px + width > 0 : px - width < max_width) {
137 PaletteID pal = u->vehstatus.Test(VehState::Crashed) ? PALETTE_CRASH : GetVehiclePalette(u);
138 VehicleSpriteSeq seq;
139 u->GetImage(dir, image_type, &seq);
140 seq.Draw(px + (rtl ? -offset.x : offset.x), y + offset.y, pal, u->vehstatus.Test(VehState::Crashed));
141 }
142
143 if (do_overlays) AddCargoIconOverlay(overlays, px, width, u);
144 px += rtl ? -width : width;
145 }
146
147 if (do_overlays) {
148 DrawCargoIconOverlays(overlays, y);
149 overlays.clear();
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, CentreBounds(r.top, r.bottom, height)).Expand(WidgetDimensions::scaled.bevel), COLOUR_WHITE, FrameFlag::BorderOnly);
156 }
157}
backup_type.hpp
Class for backupping variables and making sure they are restored later.
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
_sorted_cargo_specs
std::vector< const CargoSpec * > _sorted_cargo_specs
Cargo specifications sorted alphabetically by name.
Definition cargotype.cpp:190
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
OverflowSafeInt< int64_t >
VehicleCargoList::GetFeederShare
Money GetFeederShare() const
Returns total sum of the feeder share for all packets.
Definition cargopacket.h:405
VehicleCargoList::GetFirstStation
StationID GetFirstStation() const
Returns the first station of the first cargo packet in this list.
Definition cargopacket.h:396
VehicleCargoList::StoredCount
uint StoredCount() const
Returns sum of cargo on board the vehicle (ie not only reserved).
Definition cargopacket.h:425
WidgetDimensions::scaled
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:30
WidgetDimensions::vsep_normal
int vsep_normal
Normal vertical spacing.
Definition window_gui.h:58
WidgetDimensions::bevel
RectPadding bevel
Bevel thickness, affected by "scaled bevels" game option.
Definition window_gui.h:38
Direction
Direction
Defines the 8 directions on the map.
Definition direction_type.h:24
DIR_W
@ DIR_W
West.
Definition direction_type.h:32
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:239
EngineNameContext::VehicleDetails
@ VehicleDetails
Name is shown in the vehicle details GUI.
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:87
CentreBounds
int CentreBounds(int min, int max, int size)
Determine where to position a centred object.
Definition geometry_type.hpp:26
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:668
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:1568
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:249
PaletteID
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
roadveh.h
Road vehicle states.
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:112
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
safeguards.h
A number of safeguards to prevent using unsafe methods.
PALETTE_CRASH
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition sprites.h:1619
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
string_func.h
Functions related to low-level strings.
GetListSeparator
std::string_view GetListSeparator()
Get the list separator string for the current language.
Definition strings.cpp:299
AppendStringInPlace
void AppendStringInPlace(std::string &result, StringID string)
Resolve the given StringID and append in place into an existing std::string with formatting but no pa...
Definition strings.cpp:434
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
strings_func.h
Functions related to OTTD's strings.
MakeParameters
auto MakeParameters(Args &&... args)
Helper to create the StringParameters with its own buffer with the given parameter values.
Definition strings_func.h:107
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
TD_RTL
@ TD_RTL
Text is written right-to-left by default.
Definition strings_type.h:27
AutoRestoreBackup
Class to backup a specific variable and restore it upon destruction of this object to prevent stack v...
Definition backup_type.hpp:150
CargoArray
Class for storing amounts of cargo.
Definition cargo_type.h:111
CargoSpec
Specification of a cargo type.
Definition cargotype.h:74
Coord2D< int >
Coord2D::y
T y
Y coordinate.
Definition geometry_type.hpp:35
Coord2D::x
T x
X coordinate.
Definition geometry_type.hpp:34
DrawPixelInfo
Data about how and where to blit pixels.
Definition gfx_type.h:157
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF >
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
Rect::Width
int Width() const
Get width of Rect.
Definition geometry_type.hpp:111
Rect::Height
int Height() const
Get height of Rect.
Definition geometry_type.hpp:117
Rect::Translate
Rect Translate(int x, int y) const
Copy and translate Rect by x,y pixels.
Definition geometry_type.hpp:200
Rect::Expand
Rect Expand(int s) const
Copy and expand Rect by s pixels.
Definition geometry_type.hpp:179
RoadVehicle
Buses, trucks and trams belong to this class.
Definition roadveh.h:98
RoadVehicle::GetDisplayImageWidth
int GetDisplayImageWidth(Point *offset=nullptr) const
Get the width of a road vehicle image in the GUI.
Definition roadveh_cmd.cpp:93
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:120
SpecializedVehicle::Next
T * Next() const
Get next vehicle in the chain.
Definition vehicle_base.h:1049
SpecializedVehicle::From
static T * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition vehicle_base.h:1134
VehicleSpriteSeq
Sprite sequence for a vehicle part.
Definition vehicle_base.h:102
VehicleSpriteSeq::Draw
void Draw(int x, int y, PaletteID default_pal, bool force_pal) const
Draw the sprite sequence.
Definition vehicle.cpp:142
Vehicle
Vehicle data structure.
Definition vehicle_base.h:205
Vehicle::engine_type
EngineID engine_type
The type of engine used for this vehicle.
Definition vehicle_base.h:278
Vehicle::cargo
VehicleCargoList cargo
The cargo this vehicle is carrying.
Definition vehicle_base.h:296
Vehicle::cargo_cap
uint16_t cargo_cap
total capacity
Definition vehicle_base.h:299
Vehicle::HasArticulatedPart
bool HasArticulatedPart() const
Check if an engine has an articulated part.
Definition vehicle_base.h:931
Vehicle::vehstatus
VehStates vehstatus
Status.
Definition vehicle_base.h:309
Vehicle::cargo_type
CargoType cargo_type
type of cargo this vehicle is carrying
Definition vehicle_base.h:297
Vehicle::Next
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition vehicle_base.h:587
Vehicle::value
Money value
Value of the vehicle.
Definition vehicle_base.h:236
Vehicle::build_year
TimerGameCalendar::Year build_year
Year the vehicle has been built.
Definition vehicle_base.h:252
GetVehiclePalette
PaletteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition vehicle.cpp:2133
VehState::Crashed
@ Crashed
Vehicle is crashed.
vehicle_func.h
Functions related to vehicles.
GetCargoSubtypeText
StringID GetCargoSubtypeText(const Vehicle *v)
Get the cargo subtype text from NewGRF for the vehicle details window.
Definition vehicle_gui.cpp:1387
DrawCargoIconOverlays
void DrawCargoIconOverlays(std::span< const CargoIconOverlay > overlays, int y)
Draw a list of cargo icon overlays.
Definition vehicle_gui.cpp:401
AddCargoIconOverlay
void AddCargoIconOverlay(std::vector< CargoIconOverlay > &overlays, int x, int width, const Vehicle *v)
Add a cargo icon to the list of overlays.
Definition vehicle_gui.cpp:349
ShowCargoIconOverlay
bool ShowCargoIconOverlay()
Test if cargo icon overlays should be drawn.
Definition vehicle_gui.cpp:337
EngineImageType
EngineImageType
Visualisation contexts of vehicles and engines.
Definition vehicle_type.h:74
DrawFrameRect
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition widget.cpp:307
window_gui.h
Functions, definitions and such used only by the GUI.
FrameFlag::BorderOnly
@ BorderOnly
Draw border only, no background.
zoom_func.h
Functions related to zooming.
ScaleSpriteTrad
int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition zoom_func.h:107