OpenTTD Source 20241224-master-gf74b0cf984
ship_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 "vehicle_base.h"
12#include "window_gui.h"
13#include "gfx_func.h"
14#include "vehicle_gui.h"
15#include "strings_func.h"
16#include "vehicle_func.h"
17#include "spritecache.h"
18#include "zoom_func.h"
19
20#include "table/strings.h"
21
22#include "safeguards.h"
23
30void DrawShipImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type)
31{
32 bool rtl = _current_text_dir == TD_RTL;
33
35 v->GetImage(rtl ? DIR_E : DIR_W, image_type, &seq);
36
37 Rect rect;
38 seq.GetBounds(&rect);
39
40 int width = UnScaleGUI(rect.Width());
41 int x_offs = UnScaleGUI(rect.left);
42 int x = rtl ? r.right - width - x_offs : r.left - x_offs;
43 /* This magic -1 offset is related to the sprite_y_offsets in build_vehicle_gui.cpp */
44 int y = ScaleSpriteTrad(-1) + CenterBounds(r.top, r.bottom, 0);
45
46 seq.Draw(x, y, GetVehiclePalette(v), false);
47 if (v->cargo_cap > 0) DrawCargoIconOverlay(x, y, v->cargo_type);
48
49 if (v->index == selection) {
50 x += x_offs;
51 y += UnScaleGUI(rect.top);
52 Rect hr = {x, y, x + width - 1, y + UnScaleGUI(rect.Height()) - 1};
54 }
55}
56
63void DrawShipDetails(const Vehicle *v, const Rect &r)
64{
65 int y = r.top;
66
68 SetDParam(1, v->build_year);
69 SetDParam(2, v->value);
70 DrawString(r.left, r.right, y, STR_VEHICLE_INFO_BUILT_VALUE);
72
73 SetDParam(0, v->cargo_type);
74 SetDParam(1, v->cargo_cap);
76 DrawString(r.left, r.right, y, STR_VEHICLE_INFO_CAPACITY);
78
79 StringID str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
80 if (v->cargo.StoredCount() > 0) {
81 SetDParam(0, v->cargo_type);
84 str = STR_VEHICLE_DETAILS_CARGO_FROM;
85 }
86 DrawString(r.left, r.right, y, str);
88
89 /* Draw Transfer credits text */
91 DrawString(r.left, r.right, y, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);
92}
Money GetFeederShare() const
Returns total sum of the feeder share for all packets.
StationID GetFirstStation() const
Returns the first station of the first cargo packet in this list.
uint StoredCount() const
Returns sum of cargo on board the vehicle (ie not only reserved).
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: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.
@ 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
Functions related to the gfx engine.
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
A number of safeguards to prevent using unsafe methods.
void DrawShipDetails(const Vehicle *v, const Rect &r)
Draw the details for the given vehicle at the given position.
Definition ship_gui.cpp:63
void DrawShipImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type)
Draws an image of a ship.
Definition ship_gui.cpp:30
Functions to cache sprites in memory.
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.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
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.
Sprite sequence for a vehicle part.
void GetBounds(Rect *bounds) const
Determine shared bounds of all sprites.
Definition vehicle.cpp:103
void Draw(int x, int y, PaletteID default_pal, bool force_pal) const
Draw the sprite sequence.
Definition vehicle.cpp:131
Vehicle data structure.
EngineID engine_type
The type of engine used for this vehicle.
VehicleCargoList cargo
The cargo this vehicle is carrying.
uint16_t cargo_cap
total capacity
CargoID cargo_type
type of cargo this vehicle is carrying
Money value
Value of the vehicle.
TimerGameCalendar::Year build_year
Year the vehicle has been built.
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:2152
Base class for all vehicles.
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.
uint32_t 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:283
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