OpenTTD Source: src/aircraft_gui.cpp Source File

OpenTTD Source 15.3

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
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
29 void 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));
37 y += GetCharacterHeight(FS_NORMAL);
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 }
44 y += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal;
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()));
53 y += GetCharacterHeight(FS_NORMAL);
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
70 void DrawAircraftImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type)
71{
72 bool rtl = _current_text_dir == TD_RTL;
73
74 VehicleSpriteSeq seq;
75 v->GetImage(rtl ? DIR_E : DIR_W, image_type, &seq);
76
77 Rect rect;
78 seq.GetBounds(&rect);
79
80 int width = UnScaleGUI(rect.Width());
81 int x_offs = UnScaleGUI(rect.left);
82 int x = rtl ? r.right - width - x_offs : r.left - x_offs;
83 /* This magic -1 offset is related to the sprite_y_offsets in build_vehicle_gui.cpp */
84 int y = ScaleSpriteTrad(-1) + CentreBounds(r.top, r.bottom, 0);
85 bool helicopter = v->subtype == AIR_HELICOPTER;
86
87 int heli_offs = 0;
88
89 PaletteID pal = v->vehstatus.Test(VehState::Crashed) ? PALETTE_CRASH : GetVehiclePalette(v);
90 seq.Draw(x, y, pal, v->vehstatus.Test(VehState::Crashed));
91
92 /* Aircraft can store cargo in their shadow, show this if present. */
93 const Vehicle *u = v->Next();
94 assert(u != nullptr);
95 int dx = 0;
96 if (u->cargo_cap > 0 && u->cargo_type != v->cargo_type) {
97 dx = GetLargestCargoIconSize().width / 2;
98 DrawCargoIconOverlay(x + dx, y, u->cargo_type);
99 }
100 if (v->cargo_cap > 0) DrawCargoIconOverlay(x - dx, y, v->cargo_type);
101
102 if (helicopter) {
103 const Aircraft *a = Aircraft::From(v);
104 VehicleSpriteSeq rotor_seq;
105 GetCustomRotorSprite(a, image_type, &rotor_seq);
106 if (!rotor_seq.IsValid()) rotor_seq.Set(SPR_ROTOR_STOPPED);
107 heli_offs = ScaleSpriteTrad(5);
108 rotor_seq.Draw(x, y - heli_offs, PAL_NONE, false);
109 }
110 if (v->index == selection) {
111 x += x_offs;
112 y += UnScaleGUI(rect.top) - heli_offs;
113 Rect hr = {x, y, x + width - 1, y + UnScaleGUI(rect.Height()) + heli_offs - 1};
114 DrawFrameRect(hr.Expand(WidgetDimensions::scaled.bevel), COLOUR_WHITE, FrameFlag::BorderOnly);
115 }
116}
aircraft.h
Base for aircraft.
AIR_HELICOPTER
@ AIR_HELICOPTER
an helicopter
Definition aircraft.h:29
DrawAircraftDetails
void DrawAircraftDetails(const Aircraft *v, const Rect &r)
Draw the details for the given vehicle at the given position.
Definition aircraft_gui.cpp:29
DrawAircraftImage
void DrawAircraftImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type)
Draws an image of an aircraft.
Definition aircraft_gui.cpp:70
GetLargestCargoIconSize
Dimension GetLargestCargoIconSize()
Get dimensions of largest cargo icon.
Definition cargotype.cpp:163
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 >
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
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
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
newgrf_engine.h
Functions for NewGRF engines.
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.
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.
TD_RTL
@ TD_RTL
Text is written right-to-left by default.
Definition strings_type.h:27
Aircraft
Aircraft, helicopters, rotors and their shadows belong to this class.
Definition aircraft.h:72
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::Expand
Rect Expand(int s) const
Copy and expand Rect by s pixels.
Definition geometry_type.hpp:179
SpecializedVehicle::Next
T * Next() const
Get next vehicle in the chain.
Definition vehicle_base.h:1049
SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::From
static Aircraft * 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::IsValid
bool IsValid() const
Check whether the sequence contains any sprites.
Definition vehicle_base.h:114
VehicleSpriteSeq::GetBounds
void GetBounds(Rect *bounds) const
Determine shared bounds of all sprites.
Definition vehicle.cpp:114
VehicleSpriteSeq::Set
void Set(SpriteID sprite)
Assign a single sprite to the sequence.
Definition vehicle_base.h:130
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::cargo_cap
uint16_t cargo_cap
total capacity
Definition vehicle_base.h:299
Vehicle::subtype
uint8_t subtype
subtype (Filled with values from AircraftSubType/DisasterSubType/EffectVehicleType/GroundVehicleSubty...
Definition vehicle_base.h:310
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::GetImage
virtual void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
Gets the sprite to show for the given direction.
Definition vehicle_base.h:441
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
DrawCargoIconOverlay
void DrawCargoIconOverlay(int x, int y, CargoType cargo_type)
Draw a cargo icon overlaying an existing sprite, with a black contrast outline.
Definition vehicle_gui.cpp:372
vehicle_gui.h
Functions related to the vehicle's GUIs.
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
UnScaleGUI
int UnScaleGUI(int value)
Short-hand to apply GUI zoom level.
Definition zoom_func.h:77