OpenTTD Source 20250218-master-g53dd1258a7
aircraft_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 "aircraft.h"
12#include "vehicle_gui.h"
13#include "newgrf_engine.h"
14#include "strings_func.h"
15#include "vehicle_func.h"
16#include "window_gui.h"
17#include "spritecache.h"
18#include "zoom_func.h"
19
20#include "table/strings.h"
21
22#include "safeguards.h"
23
30void DrawAircraftDetails(const Aircraft *v, const Rect &r)
31{
32 Money feeder_share = 0;
33
34 int y = r.top;
35 for (const Aircraft *u = v; u != nullptr; u = u->Next()) {
36 if (u->IsNormalAircraft()) {
38 SetDParam(1, u->build_year);
39 SetDParam(2, u->value);
40 DrawString(r.left, r.right, y, STR_VEHICLE_INFO_BUILT_VALUE);
42
43 SetDParam(0, u->cargo_type);
44 SetDParam(1, u->cargo_cap);
45 SetDParam(2, u->Next()->cargo_type);
46 SetDParam(3, u->Next()->cargo_cap);
48 DrawString(r.left, r.right, y, (u->Next()->cargo_cap != 0) ? STR_VEHICLE_INFO_CAPACITY_CAPACITY : STR_VEHICLE_INFO_CAPACITY);
50 }
51
52 if (u->cargo_cap != 0) {
53 uint cargo_count = u->cargo.StoredCount();
54
55 if (cargo_count != 0) {
56 /* Cargo names (fix pluralness) */
57 SetDParam(0, u->cargo_type);
58 SetDParam(1, cargo_count);
59 SetDParam(2, u->cargo.GetFirstStation());
60 DrawString(r.left, r.right, y, STR_VEHICLE_DETAILS_CARGO_FROM);
62 feeder_share += u->cargo.GetFeederShare();
63 }
64 }
65 }
66
68 SetDParam(0, feeder_share);
69 DrawString(r.left, r.right, y, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);
70}
71
72
79void DrawAircraftImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type)
80{
81 bool rtl = _current_text_dir == TD_RTL;
82
84 v->GetImage(rtl ? DIR_E : DIR_W, image_type, &seq);
85
86 Rect rect;
87 seq.GetBounds(&rect);
88
89 int width = UnScaleGUI(rect.Width());
90 int x_offs = UnScaleGUI(rect.left);
91 int x = rtl ? r.right - width - x_offs : r.left - x_offs;
92 /* This magic -1 offset is related to the sprite_y_offsets in build_vehicle_gui.cpp */
93 int y = ScaleSpriteTrad(-1) + CenterBounds(r.top, r.bottom, 0);
94 bool helicopter = v->subtype == AIR_HELICOPTER;
95
96 int heli_offs = 0;
97
99 seq.Draw(x, y, pal, (v->vehstatus & VS_CRASHED) != 0);
100
101 /* Aircraft can store cargo in their shadow, show this if present. */
102 const Vehicle *u = v->Next();
103 assert(u != nullptr);
104 int dx = 0;
105 if (u->cargo_cap > 0 && u->cargo_type != v->cargo_type) {
106 dx = GetLargestCargoIconSize().width / 2;
107 DrawCargoIconOverlay(x + dx, y, u->cargo_type);
108 }
109 if (v->cargo_cap > 0) DrawCargoIconOverlay(x - dx, y, v->cargo_type);
110
111 if (helicopter) {
112 const Aircraft *a = Aircraft::From(v);
113 VehicleSpriteSeq rotor_seq;
114 GetCustomRotorSprite(a, image_type, &rotor_seq);
115 if (!rotor_seq.IsValid()) rotor_seq.Set(SPR_ROTOR_STOPPED);
116 heli_offs = ScaleSpriteTrad(5);
117 rotor_seq.Draw(x, y - heli_offs, PAL_NONE, false);
118 }
119 if (v->index == selection) {
120 x += x_offs;
121 y += UnScaleGUI(rect.top) - heli_offs;
122 Rect hr = {x, y, x + width - 1, y + UnScaleGUI(rect.Height()) + heli_offs - 1};
124 }
125}
Base for aircraft.
@ AIR_HELICOPTER
an helicopter
Definition aircraft.h:29
void DrawAircraftDetails(const Aircraft *v, const Rect &r)
Draw the details for the given vehicle at the given position.
void DrawAircraftImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type)
Draws an image of an aircraft.
Dimension GetLargestCargoIconSize()
Get dimensions of largest cargo icon.
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:28
int vsep_normal
Normal vertical spacing.
Definition window_gui.h:58
RectPadding bevel
Bevel thickness, affected by "scaled bevels" game option.
Definition window_gui.h:38
@ DIR_W
West.
@ DIR_E
East.
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.
@ VehicleDetails
Name is shown in the vehicle details GUI.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:77
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:657
int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
Definition gfx_func.h:166
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:243
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
Functions for NewGRF engines.
A number of safeguards to prevent using unsafe methods.
Functions to cache sprites in memory.
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition sprites.h:1605
Definition of base types and functions in a cross-platform compatible way.
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:163
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
Functions related to OTTD's strings.
@ TD_RTL
Text is written right-to-left by default.
Aircraft, helicopters, rotors and their shadows belong to this class.
Definition aircraft.h:72
Tindex index
Index of this pool item.
Specification of a rectangle with absolute coordinates of all edges.
int Width() const
Get width of Rect.
int Height() const
Get height of Rect.
Rect Expand(int s) const
Copy and expand Rect by s pixels.
T * Next() const
Get next vehicle in the chain.
static Aircraft * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Sprite sequence for a vehicle part.
bool IsValid() const
Check whether the sequence contains any sprites.
void GetBounds(Rect *bounds) const
Determine shared bounds of all sprites.
Definition vehicle.cpp:103
void Set(SpriteID sprite)
Assign a single sprite to the sequence.
void Draw(int x, int y, PaletteID default_pal, bool force_pal) const
Draw the sprite sequence.
Definition vehicle.cpp:131
Vehicle data structure.
uint16_t cargo_cap
total capacity
uint8_t subtype
subtype (Filled with values from AircraftSubType/DisasterSubType/EffectVehicleType/GroundVehicleSubty...
CargoType cargo_type
type of cargo this vehicle is carrying
Vehicle * Next() const
Get the next vehicle of this vehicle.
uint8_t vehstatus
Status.
virtual void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
Gets the sprite to show for the given direction.
PaletteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition vehicle.cpp:2149
@ VS_CRASHED
Vehicle is crashed.
Functions related to vehicles.
StringID GetCargoSubtypeText(const Vehicle *v)
Get the cargo subtype text from NewGRF for the vehicle details window.
void DrawCargoIconOverlay(int x, int y, CargoType cargo_type)
Draw a cargo icon overlaying an existing sprite, with a black contrast outline.
Functions related to the vehicle's GUIs.
EngineImageType
Visualisation contexts of vehicles and engines.
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition widget.cpp:283
Functions, definitions and such used only by the GUI.
@ BorderOnly
Draw border only, no background.
Functions related to zooming.
int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition zoom_func.h:107
int UnScaleGUI(int value)
Short-hand to apply GUI zoom level.
Definition zoom_func.h:77