OpenTTD Source: src/vehicle_gui_base.h Source File

OpenTTD Source 13.1

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 <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef VEHICLE_GUI_BASE_H
11 #define VEHICLE_GUI_BASE_H
12
13 #include "core/smallvec_type.hpp"
14 #include "cargo_type.h"
15 #include "date_type.h"
16 #include "economy_type.h"
17 #include "sortlist_type.h"
18 #include "vehicle_base.h"
19 #include "vehiclelist.h"
20 #include "window_gui.h"
21 #include "widgets/dropdown_type.h"
22
23 #include <iterator>
24 #include <numeric>
25
26 typedef GUIList<const Vehicle*, CargoID> GUIVehicleList;
27
28 struct GUIVehicleGroup {
29 VehicleList::const_iterator vehicles_begin;
30 VehicleList::const_iterator vehicles_end;
31
32 GUIVehicleGroup(VehicleList::const_iterator vehicles_begin, VehicleList::const_iterator vehicles_end)
33 : vehicles_begin(vehicles_begin), vehicles_end(vehicles_end) {}
34
35 std::ptrdiff_t NumVehicles() const
36 {
37 return std::distance(this->vehicles_begin, this->vehicles_end);
38 }
39
40 const Vehicle *GetSingleVehicle() const
41 {
42 assert(this->NumVehicles() == 1);
43 return this->vehicles_begin[0];
44 }
45
46 Money GetDisplayProfitThisYear() const
47 {
48 return std::accumulate(this->vehicles_begin, this->vehicles_end, (Money)0, [](Money acc, const Vehicle *v) {
49 return acc + v->GetDisplayProfitThisYear();
50 });
51 }
52
53 Money GetDisplayProfitLastYear() const
54 {
55 return std::accumulate(this->vehicles_begin, this->vehicles_end, (Money)0, [](Money acc, const Vehicle *v) {
56 return acc + v->GetDisplayProfitLastYear();
57 });
58 }
59
60 Date GetOldestVehicleAge() const
61 {
62 const Vehicle *oldest = *std::max_element(this->vehicles_begin, this->vehicles_end, [](const Vehicle *v_a, const Vehicle *v_b) {
63 return v_a->age < v_b->age;
64 });
65 return oldest->age;
66 }
67 };
68
69 typedef GUIList<GUIVehicleGroup, CargoID> GUIVehicleGroupList;
70
71 struct BaseVehicleListWindow : public Window {
72
73 enum GroupBy : byte {
74 GB_NONE,
75 GB_SHARED_ORDERS,
76
77 GB_END,
78 };
79
81 enum CargoFilterSpecialType {
82 CF_NONE = CT_INVALID,
83 CF_ANY = CT_NO_REFIT,
84 CF_FREIGHT = CT_AUTO_REFIT,
85 };
86
87 GroupBy grouping;
88 VehicleList vehicles;
89 GUIVehicleGroupList vehgroups;
90 Listing *sorting;
91 byte unitnumber_digits;
92 Scrollbar *vscroll;
93 VehicleListIdentifier vli;
94 VehicleID vehicle_sel;
95 CargoID cargo_filter[NUM_CARGO + 3];
96 StringID cargo_filter_texts[NUM_CARGO + 4];
97 byte cargo_filter_criteria;
98 uint order_arrow_width;
99
100 typedef GUIVehicleGroupList::SortFunction VehicleGroupSortFunction;
101 typedef GUIVehicleList::SortFunction VehicleIndividualSortFunction;
102
103 enum ActionDropdownItem {
104 ADI_REPLACE,
105 ADI_SERVICE,
106 ADI_DEPOT,
107 ADI_ADD_SHARED,
108 ADI_REMOVE_ALL,
109 };
110
111 static const StringID vehicle_depot_name[];
112 static const StringID vehicle_group_by_names[];
113 static const StringID vehicle_group_none_sorter_names[];
114 static const StringID vehicle_group_shared_orders_sorter_names[];
115 static VehicleGroupSortFunction * const vehicle_group_none_sorter_funcs[];
116 static VehicleGroupSortFunction * const vehicle_group_shared_orders_sorter_funcs[];
117
118 BaseVehicleListWindow(WindowDesc *desc, WindowNumber wno);
119
120 void OnInit() override;
121
122 void UpdateSortingFromGrouping();
123
124 void DrawVehicleListItems(VehicleID selected_vehicle, int line_height, const Rect &r) const;
125 void UpdateVehicleGroupBy(GroupBy group_by);
126 void SortVehicleList();
127 void BuildVehicleList();
128 void SetCargoFilterIndex(byte index);
129 void SetCargoFilterArray();
130 void FilterVehicleList();
131 Dimension GetActionDropdownSize(bool show_autoreplace, bool show_group);
132 DropDownList BuildActionDropdownList(bool show_autoreplace, bool show_group);
133
134 const StringID *GetVehicleSorterNames()
135 {
136 switch (this->grouping) {
137 case GB_NONE:
138 return vehicle_group_none_sorter_names;
139 case GB_SHARED_ORDERS:
140 return vehicle_group_shared_orders_sorter_names;
141 default:
142 NOT_REACHED();
143 }
144 }
145
146 VehicleGroupSortFunction * const *GetVehicleSorterFuncs()
147 {
148 switch (this->grouping) {
149 case GB_NONE:
150 return vehicle_group_none_sorter_funcs;
151 case GB_SHARED_ORDERS:
152 return vehicle_group_shared_orders_sorter_funcs;
153 default:
154 NOT_REACHED();
155 }
156 }
157 };
158
159 uint GetVehicleListHeight(VehicleType type, uint divisor = 1);
160
161 struct Sorting {
162 Listing aircraft;
163 Listing roadveh;
164 Listing ship;
165 Listing train;
166 };
167
168 extern BaseVehicleListWindow::GroupBy _grouping[VLT_END][VEH_COMPANY_END];
169 extern Sorting _sorting[BaseVehicleListWindow::GB_END];
170
171 #endif /* VEHICLE_GUI_BASE_H */
BaseVehicleListWindow::cargo_filter_criteria
byte cargo_filter_criteria
Selected cargo filter index.
Definition: vehicle_gui_base.h:97
BaseVehicleListWindow::GetActionDropdownSize
Dimension GetActionDropdownSize(bool show_autoreplace, bool show_group)
Compute the size for the Action dropdown.
Definition: vehicle_gui.cpp:348
VehicleList
std::vector< const Vehicle * > VehicleList
A list of vehicles.
Definition: vehiclelist.h:55
BaseVehicleListWindow::CF_ANY
@ CF_ANY
Show all vehicles independent of carried cargo (i.e. no filtering)
Definition: vehicle_gui_base.h:83
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
BaseVehicleListWindow::CF_FREIGHT
@ CF_FREIGHT
Show only vehicles which carry any freight (non-passenger) cargo.
Definition: vehicle_gui_base.h:84
BaseVehicleListWindow::grouping
GroupBy grouping
How we want to group the list.
Definition: vehicle_gui_base.h:87
VehicleListIdentifier
The information about a vehicle list.
Definition: vehiclelist.h:29
BaseVehicleListWindow::order_arrow_width
uint order_arrow_width
Width of the arrow in the small order list.
Definition: vehicle_gui_base.h:98
GUIList
List template of 'things' T to sort in a GUI.
Definition: sortlist_type.h:46
vehiclelist.h
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:99
smallvec_type.hpp
GetVehicleListHeight
uint GetVehicleListHeight(VehicleType type, uint divisor=1)
Get the height of a vehicle in the vehicle list GUIs.
Definition: vehicle_gui.cpp:1625
BaseVehicleListWindow::sorting
Listing * sorting
Pointer to the vehicle type related sorting.
Definition: vehicle_gui_base.h:90
vehicle_base.h
GUIVehicleGroup::vehicles_begin
VehicleList::const_iterator vehicles_begin
Pointer to beginning element of this vehicle group.
Definition: vehicle_gui_base.h:29
WindowNumber
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:713
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:224
Scrollbar
Scrollbar data structure.
Definition: widget_type.h:634
BaseVehicleListWindow::SetCargoFilterIndex
void SetCargoFilterIndex(byte index)
Set cargo filter list item index.
Definition: vehicle_gui.cpp:280
BaseVehicleListWindow::SetCargoFilterArray
void SetCargoFilterArray()
Populate the filter list and set the cargo filter criteria.
Definition: vehicle_gui.cpp:294
BaseVehicleListWindow::OnInit
void OnInit() override
Notification that the nested widget tree gets initialized.
Definition: vehicle_gui.cpp:364
WindowDesc
High level window description.
Definition: window_gui.h:102
BaseVehicleListWindow::FilterVehicleList
void FilterVehicleList()
Filter the engine list against the currently selected cargo filter.
Definition: vehicle_gui.cpp:331
window_gui.h
BaseVehicleListWindow::vli
VehicleListIdentifier vli
Identifier of the vehicle list we want to currently show.
Definition: vehicle_gui_base.h:93
BaseVehicleListWindow::BuildActionDropdownList
DropDownList BuildActionDropdownList(bool show_autoreplace, bool show_group)
Display the Action dropdown window.
Definition: vehicle_gui.cpp:376
Vehicle::GetDisplayProfitThisYear
Money GetDisplayProfitThisYear() const
Gets the profit vehicle had this year.
Definition: vehicle_base.h:595
Listing
Data structure describing how to show the list (what sort direction and criteria).
Definition: sortlist_type.h:30
BaseVehicleListWindow::cargo_filter
CargoID cargo_filter[NUM_CARGO+3]
Available cargo filters; CargoID or CF_ANY or CF_FREIGHT or CF_NONE.
Definition: vehicle_gui_base.h:95
VEH_COMPANY_END
@ VEH_COMPANY_END
Last company-ownable type.
Definition: vehicle_type.h:29
BaseVehicleListWindow
Definition: vehicle_gui_base.h:71
GUIList< GUIVehicleGroup, CargoID >::SortFunction
bool SortFunction(const GUIVehicleGroup &, const GUIVehicleGroup &)
Signature of sort function.
Definition: sortlist_type.h:48
cargo_type.h
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:14
dropdown_type.h
sortlist_type.h
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
date_type.h
BaseVehicleListWindow::DrawVehicleListItems
void DrawVehicleListItems(VehicleID selected_vehicle, int line_height, const Rect &r) const
Draw all the vehicle list items.
Definition: vehicle_gui.cpp:1645
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Sorting
Definition: vehicle_gui_base.h:161
GUIVehicleGroup
Definition: vehicle_gui_base.h:28
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:89
BaseVehicleListWindow::vehicle_sel
VehicleID vehicle_sel
Selected vehicle.
Definition: vehicle_gui_base.h:94
CT_AUTO_REFIT
@ CT_AUTO_REFIT
Automatically choose cargo type when doing auto refitting.
Definition: cargo_type.h:67
Vehicle::age
Date age
Age in days.
Definition: vehicle_base.h:273
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:65
VehicleID
uint32 VehicleID
The type all our vehicle IDs have.
Definition: vehicle_type.h:16
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:88
OverflowSafeInt< int64 >
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
BaseVehicleListWindow::CargoFilterSpecialType
CargoFilterSpecialType
Special cargo filter criteria.
Definition: vehicle_gui_base.h:81
BaseVehicleListWindow::unitnumber_digits
byte unitnumber_digits
The number of digits of the highest unit number.
Definition: vehicle_gui_base.h:91
BaseVehicleListWindow::cargo_filter_texts
StringID cargo_filter_texts[NUM_CARGO+4]
Texts for filter_cargo, terminated by INVALID_STRING_ID.
Definition: vehicle_gui_base.h:96
BaseVehicleListWindow::CF_NONE
@ CF_NONE
Show only vehicles which do not carry cargo (e.g. train engines)
Definition: vehicle_gui_base.h:82
Window
Data structure for an opened window.
Definition: window_gui.h:213
CT_INVALID
@ CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:69
economy_type.h
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:69
CT_NO_REFIT
@ CT_NO_REFIT
Do not refit cargo of a vehicle (used in vehicle orders and auto-replace/auto-new).
Definition: cargo_type.h:68
Vehicle::GetDisplayProfitLastYear
Money GetDisplayProfitLastYear() const
Gets the profit vehicle had last year.
Definition: vehicle_base.h:601
GUIVehicleGroup::vehicles_end
VehicleList::const_iterator vehicles_end
Pointer to past-the-end element of this vehicle group.
Definition: vehicle_gui_base.h:30