OpenTTD Source: src/vehicle_gui_base.h Source File

OpenTTD Source 1.11.0-RC1

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 "date_type.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 "widgets/dropdown_type.h"
21
22 #include <iterator>
23 #include <numeric>
24
25 typedef GUIList<const Vehicle*> GUIVehicleList;
26
27 struct GUIVehicleGroup {
28 VehicleList::const_iterator vehicles_begin;
29 VehicleList::const_iterator vehicles_end;
30
31 GUIVehicleGroup(VehicleList::const_iterator vehicles_begin, VehicleList::const_iterator vehicles_end)
32 : vehicles_begin(vehicles_begin), vehicles_end(vehicles_end) {}
33
34 std::ptrdiff_t NumVehicles() const
35 {
36 return std::distance(this->vehicles_begin, this->vehicles_end);
37 }
38
39 const Vehicle *GetSingleVehicle() const
40 {
41 assert(this->NumVehicles() == 1);
42 return this->vehicles_begin[0];
43 }
44
45 Money GetDisplayProfitThisYear() const
46 {
47 return std::accumulate(this->vehicles_begin, this->vehicles_end, (Money)0, [](Money acc, const Vehicle *v) {
48 return acc + v->GetDisplayProfitThisYear();
49 });
50 }
51
52 Money GetDisplayProfitLastYear() const
53 {
54 return std::accumulate(this->vehicles_begin, this->vehicles_end, (Money)0, [](Money acc, const Vehicle *v) {
55 return acc + v->GetDisplayProfitLastYear();
56 });
57 }
58
59 Date GetOldestVehicleAge() const
60 {
61 const Vehicle *oldest = *std::max_element(this->vehicles_begin, this->vehicles_end, [](const Vehicle *v_a, const Vehicle *v_b) {
62 return v_a->age < v_b->age;
63 });
64 return oldest->age;
65 }
66 };
67
68 typedef GUIList<GUIVehicleGroup> GUIVehicleGroupList;
69
70 struct BaseVehicleListWindow : public Window {
71
72 enum GroupBy : byte {
73 GB_NONE,
74 GB_SHARED_ORDERS,
75
76 GB_END,
77 };
78
79 GroupBy grouping;
80 VehicleList vehicles;
81 GUIVehicleGroupList vehgroups;
82 Listing *sorting;
83 byte unitnumber_digits;
84 Scrollbar *vscroll;
85 VehicleListIdentifier vli;
86
87 typedef GUIVehicleGroupList::SortFunction VehicleGroupSortFunction;
88 typedef GUIVehicleList::SortFunction VehicleIndividualSortFunction;
89
90 enum ActionDropdownItem {
91 ADI_REPLACE,
92 ADI_SERVICE,
93 ADI_DEPOT,
94 ADI_ADD_SHARED,
95 ADI_REMOVE_ALL,
96 };
97
98 static const StringID vehicle_depot_name[];
99 static const StringID vehicle_group_by_names[];
100 static const StringID vehicle_group_none_sorter_names[];
101 static const StringID vehicle_group_shared_orders_sorter_names[];
102 static VehicleGroupSortFunction * const vehicle_group_none_sorter_funcs[];
103 static VehicleGroupSortFunction * const vehicle_group_shared_orders_sorter_funcs[];
104
105 BaseVehicleListWindow(WindowDesc *desc, WindowNumber wno);
106
107 void UpdateSortingFromGrouping();
108
109 void DrawVehicleListItems(VehicleID selected_vehicle, int line_height, const Rect &r) const;
110 void UpdateVehicleGroupBy(GroupBy group_by);
111 void SortVehicleList();
112 void BuildVehicleList();
113 Dimension GetActionDropdownSize(bool show_autoreplace, bool show_group);
114 DropDownList BuildActionDropdownList(bool show_autoreplace, bool show_group);
115
116 const StringID *GetVehicleSorterNames()
117 {
118 switch (this->grouping) {
119 case GB_NONE:
120 return vehicle_group_none_sorter_names;
121 case GB_SHARED_ORDERS:
122 return vehicle_group_shared_orders_sorter_names;
123 default:
124 NOT_REACHED();
125 }
126 }
127
128 VehicleGroupSortFunction * const *GetVehicleSorterFuncs()
129 {
130 switch (this->grouping) {
131 case GB_NONE:
132 return vehicle_group_none_sorter_funcs;
133 case GB_SHARED_ORDERS:
134 return vehicle_group_shared_orders_sorter_funcs;
135 default:
136 NOT_REACHED();
137 }
138 }
139 };
140
141 uint GetVehicleListHeight(VehicleType type, uint divisor = 1);
142
143 struct Sorting {
144 Listing aircraft;
145 Listing roadveh;
146 Listing ship;
147 Listing train;
148 };
149
150 extern BaseVehicleListWindow::GroupBy _grouping[VLT_END][VEH_COMPANY_END];
151 extern Sorting _sorting[BaseVehicleListWindow::GB_END];
152
153 #endif /* VEHICLE_GUI_BASE_H */
BaseVehicleListWindow::GetActionDropdownSize
Dimension GetActionDropdownSize(bool show_autoreplace, bool show_group)
Compute the size for the Action dropdown.
Definition: vehicle_gui.cpp:227
VehicleList
std::vector< const Vehicle * > VehicleList
A list of vehicles.
Definition: vehiclelist.h:53
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
BaseVehicleListWindow::grouping
GroupBy grouping
How we want to group the list.
Definition: vehicle_gui_base.h:79
VehicleListIdentifier
The information about a vehicle list.
Definition: vehiclelist.h:29
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:100
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:1485
BaseVehicleListWindow::sorting
Listing * sorting
Pointer to the vehicle type related sorting.
Definition: vehicle_gui_base.h:82
vehicle_base.h
GUIVehicleGroup::vehicles_begin
VehicleList::const_iterator vehicles_begin
Pointer to beginning element of this vehicle group.
Definition: vehicle_gui_base.h:28
WindowNumber
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:711
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:222
Scrollbar
Scrollbar data structure.
Definition: widget_type.h:588
WindowDesc
High level window description.
Definition: window_gui.h:166
window_gui.h
BaseVehicleListWindow::vli
VehicleListIdentifier vli
Identifier of the vehicle list we want to currently show.
Definition: vehicle_gui_base.h:85
BaseVehicleListWindow::BuildActionDropdownList
DropDownList BuildActionDropdownList(bool show_autoreplace, bool show_group)
Display the Action dropdown window.
Definition: vehicle_gui.cpp:249
Vehicle::GetDisplayProfitThisYear
Money GetDisplayProfitThisYear() const
Gets the profit vehicle had this year.
Definition: vehicle_base.h:577
Listing
Data structure describing how to show the list (what sort direction and criteria).
Definition: sortlist_type.h:30
VEH_COMPANY_END
@ VEH_COMPANY_END
Last company-ownable type.
Definition: vehicle_type.h:29
BaseVehicleListWindow
Definition: vehicle_gui_base.h:70
GUIList< GUIVehicleGroup >::SortFunction
bool SortFunction(const GUIVehicleGroup &, const GUIVehicleGroup &)
Signature of sort function.
Definition: sortlist_type.h:48
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:1505
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:143
GUIVehicleGroup
Definition: vehicle_gui_base.h:27
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:81
Vehicle::age
Date age
Age in days.
Definition: vehicle_base.h:268
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:80
OverflowSafeInt< int64, INT64_MAX, INT64_MIN >
BaseVehicleListWindow::unitnumber_digits
byte unitnumber_digits
The number of digits of the highest unit number.
Definition: vehicle_gui_base.h:83
Window
Data structure for an opened window.
Definition: window_gui.h:276
economy_type.h
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
Vehicle::GetDisplayProfitLastYear
Money GetDisplayProfitLastYear() const
Gets the profit vehicle had last year.
Definition: vehicle_base.h:583
GUIVehicleGroup::vehicles_end
VehicleList::const_iterator vehicles_end
Pointer to past-the-end element of this vehicle group.
Definition: vehicle_gui_base.h:29