OpenTTD Source  20241120-master-g6d3adc6169
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 
30 void 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 
79 void DrawAircraftImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type)
80 {
81  bool rtl = _current_text_dir == TD_RTL;
82 
83  VehicleSpriteSeq seq;
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.
Definition: cargotype.cpp:156
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition: window_gui.h:68
int vsep_normal
Normal vertical spacing.
Definition: window_gui.h:60
RectPadding bevel
Bevel thickness, affected by "scaled bevels" game option.
Definition: window_gui.h:40
@ 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.
Definition: engine_type.h:199
@ VehicleDetails
Name is shown in the vehicle details GUI.
Definition: engine_type.h:192
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:209
uint32_t PaletteID
The number of the palette.
Definition: gfx_type.h:19
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:104
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.
Definition: strings_type.h:24
Aircraft, helicopters, rotors and their shadows belong to this class.
Definition: aircraft.h:72
Tindex index
Index of this pool item.
Definition: pool_type.hpp:238
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.
static Aircraft * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
T * Next() const
Get next vehicle in the chain.
Sprite sequence for a vehicle part.
Definition: vehicle_base.h:135
bool IsValid() const
Check whether the sequence contains any sprites.
Definition: vehicle_base.h:152
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.
Definition: vehicle_base.h:168
void Draw(int x, int y, PaletteID default_pal, bool force_pal) const
Draw the sprite sequence.
Definition: vehicle.cpp:131
Vehicle data structure.
Definition: vehicle_base.h:244
uint16_t cargo_cap
total capacity
Definition: vehicle_base.h:344
uint8_t subtype
subtype (Filled with values from AircraftSubType/DisasterSubType/EffectVehicleType/GroundVehicleSubty...
Definition: vehicle_base.h:355
virtual void GetImage([[maybe_unused]] Direction direction, [[maybe_unused]] EngineImageType image_type, [[maybe_unused]] VehicleSpriteSeq *result) const
Gets the sprite to show for the given direction.
Definition: vehicle_base.h:486
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition: vehicle_base.h:632
CargoID cargo_type
type of cargo this vehicle is carrying
Definition: vehicle_base.h:342
uint8_t vehstatus
Status.
Definition: vehicle_base.h:354
PaletteID GetVehiclePalette(const Vehicle *v)
Get the colour map for a vehicle.
Definition: vehicle.cpp:2152
@ VS_CRASHED
Vehicle is crashed.
Definition: vehicle_base.h:40
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, CargoID cid)
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.
Definition: vehicle_type.h:78
uint32_t VehicleID
The type all our vehicle IDs have.
Definition: vehicle_type.h:16
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:281
Functions, definitions and such used only by the GUI.
@ FR_BORDERONLY
Draw border only, no background.
Definition: window_gui.h:27
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