OpenTTD Source 20260208-master-g43af8e94d0
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
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 "zoom_func.h"
18
19#include "table/strings.h"
20
21#include "safeguards.h"
22
29void DrawAircraftDetails(const Aircraft *v, const Rect &r)
30{
31 Money feeder_share = 0;
32
33 int y = r.top;
34 for (const Aircraft *u = v; u != nullptr; u = u->Next()) {
35 if (u->IsNormalAircraft()) {
36 DrawString(r.left, r.right, y, GetString(STR_VEHICLE_INFO_BUILT_VALUE, PackEngineNameDParam(u->engine_type, EngineNameContext::VehicleDetails), u->build_year, u->value));
38
39 if (u->Next()->cargo_cap != 0) {
40 DrawString(r.left, r.right, y, GetString(STR_VEHICLE_INFO_CAPACITY_CAPACITY, u->cargo_type, u->cargo_cap, u->Next()->cargo_type, u->Next()->cargo_cap, GetCargoSubtypeText(u)));
41 } else {
42 DrawString(r.left, r.right, y, GetString(STR_VEHICLE_INFO_CAPACITY, u->cargo_type, u->cargo_cap, GetCargoSubtypeText(u)));
43 }
45 }
46
47 if (u->cargo_cap != 0) {
48 uint cargo_count = u->cargo.StoredCount();
49
50 if (cargo_count != 0) {
51 /* Cargo names (fix pluralness) */
52 DrawString(r.left, r.right, y, GetString(STR_VEHICLE_DETAILS_CARGO_FROM, u->cargo_type, cargo_count, u->cargo.GetFirstStation()));
54 feeder_share += u->cargo.GetFeederShare();
55 }
56 }
57 }
58
59 y += WidgetDimensions::scaled.vsep_normal;
60 DrawString(r.left, r.right, y, GetString(STR_VEHICLE_INFO_FEEDER_CARGO_VALUE, feeder_share));
61}
62
63
71void DrawAircraftImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type)
72{
73 bool rtl = _current_text_dir == TD_RTL;
74
76 v->GetImage(rtl ? DIR_E : DIR_W, image_type, &seq);
77
78 Rect rect;
79 seq.GetBounds(&rect);
80
81 int width = UnScaleGUI(rect.Width());
82 int x_offs = UnScaleGUI(rect.left);
83 int x = rtl ? r.right - width - x_offs : r.left - x_offs;
84 /* This magic -1 offset is related to the sprite_y_offsets in build_vehicle_gui.cpp */
85 int y = ScaleSpriteTrad(-1) + CentreBounds(r.top, r.bottom, 0);
86 bool helicopter = v->subtype == AIR_HELICOPTER;
87
88 int heli_offs = 0;
89
91 seq.Draw(x, y, pal, v->vehstatus.Test(VehState::Crashed));
92
93 /* Aircraft can store cargo in their shadow, show this if present. */
94 const Vehicle *u = v->Next();
95 assert(u != nullptr);
96 int dx = 0;
97 if (u->cargo_cap > 0 && u->cargo_type != v->cargo_type) {
98 dx = GetLargestCargoIconSize().width / 2;
99 DrawCargoIconOverlay(x + dx, y, u->cargo_type);
100 }
101 if (v->cargo_cap > 0) DrawCargoIconOverlay(x - dx, y, v->cargo_type);
102
103 if (helicopter) {
104 const Aircraft *a = Aircraft::From(v);
105 VehicleSpriteSeq rotor_seq;
106 GetCustomRotorSprite(a, image_type, &rotor_seq);
107 if (!rotor_seq.IsValid()) rotor_seq.Set(SPR_ROTOR_STOPPED);
108 heli_offs = ScaleSpriteTrad(5);
109 rotor_seq.Draw(x, y - heli_offs, PAL_NONE, false);
110 }
111 if (v->index == selection) {
112 x += x_offs;
113 y += UnScaleGUI(rect.top) - heli_offs;
114 Rect hr = {x, y, x + width - 1, y + UnScaleGUI(rect.Height()) + heli_offs - 1};
116 }
117}
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.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:30
@ 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:87
int CentreBounds(int min, int max, int size)
Determine where to position a centred object.
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:669
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:249
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
#define Rect
Macro that prevents name conflicts between included headers.
Functions for NewGRF engines.
A number of safeguards to prevent using unsafe methods.
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition sprites.h:1619
Definition of base types and functions in a cross-platform compatible way.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
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:73
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.
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:114
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:142
Vehicle data structure.
uint16_t cargo_cap
total capacity
uint8_t subtype
subtype (Filled with values from AircraftSubType/DisasterSubType/EffectVehicleType/GroundVehicleSubty...
VehStates vehstatus
Status.
CargoType cargo_type
type of cargo this vehicle is carrying
Vehicle * Next() const
Get the next vehicle of this vehicle.
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:2146
@ 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.
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF > VehicleID
The type all our vehicle IDs have.
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition widget.cpp:289
Functions, definitions and such used only by the GUI.
@ BorderOnly
Draw border only, no background.
Definition window_gui.h:26
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