OpenTTD Source: src/roadveh_gui.cpp Source File

OpenTTD Source 1.11.0-RC1

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);
101 SetDParam(4, GetCargoSubtypeText(v));
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) {
149 PaletteID pal = (u->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(u);
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 }
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:351
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:251
Vehicle::Next
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition: vehicle_base.h:592
SpecializedVehicle::Next
T * Next() const
Get next vehicle in the chain.
Definition: vehicle_base.h:1077
Vehicle::vehstatus
byte vehstatus
Status.
Definition: vehicle_base.h:326
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:227
CargoArray
Class for storing amounts of cargo.
Definition: cargo_type.h:81
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:640
DIR_W
@ DIR_W
West.
Definition: direction_type.h:32
EngineImageType
EngineImageType
Visualisation contexts of vehicles and engines.
Definition: vehicle_type.h:85
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:222
VehicleSpriteSeq::Draw
void Draw(int x, int y, PaletteID default_pal, bool force_pal) const
Draw the sprite sequence.
Definition: vehicle.cpp:126
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:199
DrawRoadVehDetails
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
window_gui.h
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:297
VS_CRASHED
@ VS_CRASHED
Vehicle is crashed.
Definition: vehicle_base.h:37
VehicleSpriteSeq
Sprite sequence for a vehicle part.
Definition: vehicle_base.h:128
Vehicle::cargo
VehicleCargoList cargo
The cargo this vehicle is carrying.
Definition: vehicle_base.h:318
safeguards.h
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
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:1616
GetCargoSubtypeText
StringID GetCargoSubtypeText(const Vehicle *v)
Get the cargo subtype text from NewGRF for the vehicle details window.
Definition: vehicle_gui.cpp:1152
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:1594
strings_func.h
DrawRoadVehImage
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.
Definition: roadveh_gui.cpp:129
Vehicle::cargo_cap
uint16 cargo_cap
total capacity
Definition: vehicle_base.h:316
RoadVehicle::GetDisplayImageWidth
int GetDisplayImageWidth(Point *offset=nullptr) const
Get the width of a road vehicle image in the GUI.
Definition: roadveh_cmd.cpp:90
SpecializedVehicle< RoadVehicle, Type >::From
static RoadVehicle * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition: vehicle_base.h:1162
GetVehiclePalette
PaletteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition: vehicle.cpp:2041
FONT_HEIGHT_NORMAL
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:179
ScaleGUITrad
static int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition: zoom_func.h:76
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:64
Vehicle::HasArticulatedPart
bool HasArticulatedPart() const
Check if an engine has an articulated part.
Definition: vehicle_base.h:913
Vehicle::build_year
Year build_year
Year the vehicle has been built.
Definition: vehicle_base.h:267
VehicleCargoList::Source
StationID Source() const
Returns source of the first cargo packet in this list.
Definition: cargopacket.h:322
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:175
RoadVehicle::GetImage
void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
Gets the sprite to show for the given direction.
Definition: roadveh_cmd.cpp:117
OverflowSafeInt< int64, INT64_MAX, INT64_MIN >
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:314
strecat
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: string.cpp:84
VehicleCargoList::FeederShare
Money FeederShare() const
Returns total sum of the feeder share for all packets.
Definition: cargopacket.h:331
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
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:48
FR_BORDERONLY
@ FR_BORDERONLY
Draw border only, no background.
Definition: window_gui.h:28
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:155
roadveh.h