OpenTTD Source: src/vehicle_gui_base.h Source File

OpenTTD Source 15.0-RC2

vehicle_gui_base.h
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#ifndef VEHICLE_GUI_BASE_H
11#define VEHICLE_GUI_BASE_H
12
13#include "cargo_type.h"
14#include "timer/timer_game_calendar.h"
15#include "economy_type.h"
16#include "sortlist_type.h"
17#include "vehicle_base.h"
18#include "vehiclelist.h"
19#include "window_gui.h"
20#include "dropdown_type.h"
21
22typedef GUIList<const Vehicle*, std::nullptr_t, CargoType> GUIVehicleList;
23
24 struct GUIVehicleGroup {
25 VehicleList::const_iterator vehicles_begin;
26 VehicleList::const_iterator vehicles_end;
27
28 GUIVehicleGroup(VehicleList::const_iterator vehicles_begin, VehicleList::const_iterator vehicles_end)
29 : vehicles_begin(vehicles_begin), vehicles_end(vehicles_end) {}
30
31 std::ptrdiff_t NumVehicles() const
32 {
33 return std::distance(this->vehicles_begin, this->vehicles_end);
34 }
35
36 const Vehicle *GetSingleVehicle() const
37 {
38 assert(this->NumVehicles() == 1);
39 return this->vehicles_begin[0];
40 }
41
42 Money GetDisplayProfitThisYear() const
43 {
44 return std::accumulate(this->vehicles_begin, this->vehicles_end, (Money)0, [](Money acc, const Vehicle *v) {
45 return acc + v->GetDisplayProfitThisYear();
46 });
47 }
48
49 Money GetDisplayProfitLastYear() const
50 {
51 return std::accumulate(this->vehicles_begin, this->vehicles_end, (Money)0, [](Money acc, const Vehicle *v) {
52 return acc + v->GetDisplayProfitLastYear();
53 });
54 }
55
56 TimerGameEconomy::Date GetOldestVehicleAge() const
57 {
58 const Vehicle *oldest = *std::max_element(this->vehicles_begin, this->vehicles_end, [](const Vehicle *v_a, const Vehicle *v_b) {
59 return v_a->economy_age < v_b->economy_age;
60 });
61 return oldest->economy_age;
62 }
63};
64
65typedef GUIList<GUIVehicleGroup, std::nullptr_t, CargoType> GUIVehicleGroupList;
66
67 struct BaseVehicleListWindow : public Window {
68
69 enum GroupBy : uint8_t {
70 GB_NONE,
71 GB_SHARED_ORDERS,
72
73 GB_END,
74 };
75
76 GroupBy grouping{};
77 VehicleList vehicles{};
78 GUIVehicleGroupList vehgroups{};
79 Listing *sorting = nullptr;
80 uint8_t unitnumber_digits = 0;
81 Scrollbar *vscroll = nullptr;
82 VehicleListIdentifier vli{};
83 VehicleID vehicle_sel{};
84 CargoType cargo_filter_criteria{};
85 uint order_arrow_width = 0;
86 CargoTypes used_cargoes{};
87
88 typedef GUIVehicleGroupList::SortFunction VehicleGroupSortFunction;
89 typedef GUIVehicleList::SortFunction VehicleIndividualSortFunction;
90
91 enum ActionDropdownItem : uint8_t {
92 ADI_REPLACE,
93 ADI_SERVICE,
94 ADI_DEPOT,
95 ADI_ADD_SHARED,
96 ADI_REMOVE_ALL,
97 ADI_CREATE_GROUP,
98 };
99
100 static const StringID vehicle_depot_name[];
101 static const std::initializer_list<const StringID> vehicle_group_by_names;
102 static const std::initializer_list<const StringID> vehicle_group_none_sorter_names_calendar;
103 static const std::initializer_list<const StringID> vehicle_group_none_sorter_names_wallclock;
104 static const std::initializer_list<const StringID> vehicle_group_shared_orders_sorter_names_calendar;
105 static const std::initializer_list<const StringID> vehicle_group_shared_orders_sorter_names_wallclock;
106 static const std::initializer_list<VehicleGroupSortFunction * const> vehicle_group_none_sorter_funcs;
107 static const std::initializer_list<VehicleGroupSortFunction * const> vehicle_group_shared_orders_sorter_funcs;
108
109 BaseVehicleListWindow(WindowDesc &desc, const VehicleListIdentifier &vli);
110
111 void OnInit() override;
112
113 void UpdateSortingFromGrouping();
114
115 void DrawVehicleListItems(VehicleID selected_vehicle, int line_height, const Rect &r) const;
116 void UpdateVehicleGroupBy(GroupBy group_by);
117 void SortVehicleList();
118 void BuildVehicleList();
119 void SetCargoFilter(uint8_t index);
120 void SetCargoFilterArray();
121 void FilterVehicleList();
122 StringID GetCargoFilterLabel(CargoType cargo_type) const;
123 DropDownList BuildCargoDropDownList(bool full) const;
124 Dimension GetActionDropdownSize(bool show_autoreplace, bool show_group, bool show_create);
125 DropDownList BuildActionDropdownList(bool show_autoreplace, bool show_group, bool show_create);
126
127 std::span<const StringID> GetVehicleSorterNames() const;
128
129 std::span<VehicleGroupSortFunction * const> GetVehicleSorterFuncs() const
130 {
131 switch (this->grouping) {
132 case GB_NONE:
133 return vehicle_group_none_sorter_funcs;
134 case GB_SHARED_ORDERS:
135 return vehicle_group_shared_orders_sorter_funcs;
136 default:
137 NOT_REACHED();
138 }
139 }
140};
141
142 struct CargoIconOverlay {
143 int left;
144 int right;
145 CargoType cargo_type;
146 uint cargo_cap;
147
148 constexpr CargoIconOverlay(int left, int right, CargoType cargo_type, uint cargo_cap)
149 : left(left), right(right), cargo_type(cargo_type), cargo_cap(cargo_cap)
150 { }
151};
152
153bool ShowCargoIconOverlay();
154void AddCargoIconOverlay(std::vector<CargoIconOverlay> &overlays, int x, int width, const Vehicle *v);
155void DrawCargoIconOverlay(int x, int y, CargoType cargo_type);
156void DrawCargoIconOverlays(std::span<const CargoIconOverlay> overlays, int y);
157
158uint GetVehicleListHeight(VehicleType type, uint divisor = 1);
159
160 struct Sorting {
161 Listing aircraft;
162 Listing roadveh;
163 Listing ship;
164 Listing train;
165};
166
167#endif /* VEHICLE_GUI_BASE_H */
cargo_type.h
Types related to cargoes...
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
GUIList
List template of 'things' T to sort in a GUI.
Definition sortlist_type.h:43
GUIList< GUIVehicleGroup, std::nullptr_t, CargoType >::SortFunction
std::conditional_t< std::is_same_v< std::nullptr_t, std::nullptr_t >, bool(const GUIVehicleGroup &, const GUIVehicleGroup &), bool(const GUIVehicleGroup &, const GUIVehicleGroup &, const std::nullptr_t)> SortFunction
Signature of sort function.
Definition sortlist_type.h:45
OverflowSafeInt< int64_t >
Scrollbar
Scrollbar data structure.
Definition widget_type.h:708
dropdown_type.h
Types related to the drop down widget.
DropDownList
std::vector< std::unique_ptr< const DropDownListItem > > DropDownList
A drop down list is a collection of drop down list items.
Definition dropdown_type.h:56
economy_type.h
Types related to the economy.
sortlist_type.h
Base types for having sorted lists in GUIs.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
BaseVehicleListWindow
Definition vehicle_gui_base.h:67
BaseVehicleListWindow::vli
VehicleListIdentifier vli
Identifier of the vehicle list we want to currently show.
Definition vehicle_gui_base.h:82
BaseVehicleListWindow::DrawVehicleListItems
void DrawVehicleListItems(VehicleID selected_vehicle, int line_height, const Rect &r) const
Draw all the vehicle list items.
Definition vehicle_gui.cpp:1757
BaseVehicleListWindow::vehicle_sel
VehicleID vehicle_sel
Selected vehicle.
Definition vehicle_gui_base.h:83
BaseVehicleListWindow::sorting
Listing * sorting
Pointer to the vehicle type related sorting.
Definition vehicle_gui_base.h:79
BaseVehicleListWindow::SetCargoFilter
void SetCargoFilter(uint8_t index)
Set cargo filter for the vehicle group list.
Definition vehicle_gui.cpp:417
BaseVehicleListWindow::grouping
GroupBy grouping
How we want to group the list.
Definition vehicle_gui_base.h:76
BaseVehicleListWindow::GetActionDropdownSize
Dimension GetActionDropdownSize(bool show_autoreplace, bool show_group, bool show_create)
Compute the size for the Action dropdown.
Definition vehicle_gui.cpp:459
BaseVehicleListWindow::order_arrow_width
uint order_arrow_width
Width of the arrow in the small order list.
Definition vehicle_gui_base.h:85
BaseVehicleListWindow::unitnumber_digits
uint8_t unitnumber_digits
The number of digits of the highest unit number.
Definition vehicle_gui_base.h:80
BaseVehicleListWindow::BuildActionDropdownList
DropDownList BuildActionDropdownList(bool show_autoreplace, bool show_group, bool show_create)
Display the Action dropdown window.
Definition vehicle_gui.cpp:526
BaseVehicleListWindow::vehicles
VehicleList vehicles
List of vehicles. This is the buffer for vehgroups to point into; if this is structurally modified,...
Definition vehicle_gui_base.h:77
BaseVehicleListWindow::vehgroups
GUIVehicleGroupList vehgroups
List of (groups of) vehicles. This stores iterators of vehicles, and should be rebuilt if vehicles is...
Definition vehicle_gui_base.h:78
BaseVehicleListWindow::SetCargoFilterArray
void SetCargoFilterArray()
Populate the filter list and set the cargo filter criteria.
Definition vehicle_gui.cpp:431
BaseVehicleListWindow::BuildCargoDropDownList
DropDownList BuildCargoDropDownList(bool full) const
Build drop down list for cargo filter selection.
Definition vehicle_gui.cpp:498
BaseVehicleListWindow::OnInit
void OnInit() override
Notification that the nested widget tree gets initialized.
Definition vehicle_gui.cpp:477
BaseVehicleListWindow::cargo_filter_criteria
CargoType cargo_filter_criteria
Selected cargo filter index.
Definition vehicle_gui_base.h:84
BaseVehicleListWindow::FilterVehicleList
void FilterVehicleList()
Filter the engine list against the currently selected cargo filter.
Definition vehicle_gui.cpp:441
CargoIconOverlay
Definition vehicle_gui_base.h:142
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:56
GUIVehicleGroup
Definition vehicle_gui_base.h:24
GUIVehicleGroup::vehicles_end
VehicleList::const_iterator vehicles_end
Pointer to past-the-end element of this vehicle group.
Definition vehicle_gui_base.h:26
GUIVehicleGroup::vehicles_begin
VehicleList::const_iterator vehicles_begin
Pointer to beginning element of this vehicle group.
Definition vehicle_gui_base.h:25
Listing
Data structure describing how to show the list (what sort direction and criteria).
Definition sortlist_type.h:26
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF >
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
Sorting
Definition vehicle_gui_base.h:160
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition strong_typedef_type.hpp:130
VehicleListIdentifier
The information about a vehicle list.
Definition vehiclelist.h:32
Vehicle
Vehicle data structure.
Definition vehicle_base.h:205
Vehicle::GetDisplayProfitThisYear
Money GetDisplayProfitThisYear() const
Gets the profit vehicle had this year.
Definition vehicle_base.h:572
Vehicle::economy_age
TimerGameEconomy::Date economy_age
Age in economy days.
Definition vehicle_base.h:254
Vehicle::GetDisplayProfitLastYear
Money GetDisplayProfitLastYear() const
Gets the profit vehicle had last year.
Definition vehicle_base.h:578
WindowDesc
High level window description.
Definition window_gui.h:168
Window
Data structure for an opened window.
Definition window_gui.h:274
timer_game_calendar.h
Definition of the game-calendar-timer.
vehicle_base.h
Base class for all vehicles.
GetVehicleListHeight
uint GetVehicleListHeight(VehicleType type, uint divisor=1)
Get the height of a vehicle in the vehicle list GUIs.
Definition vehicle_gui.cpp:1727
DrawCargoIconOverlays
void DrawCargoIconOverlays(std::span< const CargoIconOverlay > overlays, int y)
Draw a list of cargo icon overlays.
Definition vehicle_gui.cpp:401
AddCargoIconOverlay
void AddCargoIconOverlay(std::vector< CargoIconOverlay > &overlays, int x, int width, const Vehicle *v)
Add a cargo icon to the list of overlays.
Definition vehicle_gui.cpp:349
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
ShowCargoIconOverlay
bool ShowCargoIconOverlay()
Test if cargo icon overlays should be drawn.
Definition vehicle_gui.cpp:337
VehicleType
VehicleType
Available vehicle types.
Definition vehicle_type.h:22
vehiclelist.h
Functions and type for generating vehicle lists.
VehicleList
std::vector< const Vehicle * > VehicleList
A list of vehicles.
Definition vehiclelist.h:68
window_gui.h
Functions, definitions and such used only by the GUI.