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 
8 /** @file vehicle_gui_base.h Functions/classes shared between the different vehicle list GUIs. */
9 
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;    ///< Pointer to beginning element of this vehicle group.
29 	VehicleList::const_iterator vehicles_end;      ///< Pointer to past-the-end element of this vehicle group.
30 
GUIVehicleGroupGUIVehicleGroup31 	GUIVehicleGroup(VehicleList::const_iterator vehicles_begin, VehicleList::const_iterator vehicles_end)
32 		: vehicles_begin(vehicles_begin), vehicles_end(vehicles_end) {}
33 
NumVehiclesGUIVehicleGroup34 	std::ptrdiff_t NumVehicles() const
35 	{
36 		return std::distance(this->vehicles_begin, this->vehicles_end);
37 	}
38 
GetSingleVehicleGUIVehicleGroup39 	const Vehicle *GetSingleVehicle() const
40 	{
41 		assert(this->NumVehicles() == 1);
42 		return this->vehicles_begin[0];
43 	}
44 
GetDisplayProfitThisYearGUIVehicleGroup45 	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 
GetDisplayProfitLastYearGUIVehicleGroup52 	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 
GetOldestVehicleAgeGUIVehicleGroup59 	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;                         ///< How we want to group the list.
80 	VehicleList vehicles;                     ///< List of vehicles.  This is the buffer for `vehgroups` to point into; if this is structurally modified, `vehgroups` must be rebuilt.
81 	GUIVehicleGroupList vehgroups;            ///< List of (groups of) vehicles.  This stores iterators of `vehicles`, and should be rebuilt if `vehicles` is structurally changed.
82 	Listing *sorting;                         ///< Pointer to the vehicle type related sorting.
83 	byte unitnumber_digits;                   ///< The number of digits of the highest unit number.
84 	Scrollbar *vscroll;
85 	VehicleListIdentifier vli;                ///< Identifier of the vehicle list we want to currently show.
86 	uint order_arrow_width;                   ///< Width of the arrow in the small order list.
87 
88 	typedef GUIVehicleGroupList::SortFunction VehicleGroupSortFunction;
89 	typedef GUIVehicleList::SortFunction VehicleIndividualSortFunction;
90 
91 	enum ActionDropdownItem {
92 		ADI_REPLACE,
93 		ADI_SERVICE,
94 		ADI_DEPOT,
95 		ADI_ADD_SHARED,
96 		ADI_REMOVE_ALL,
97 	};
98 
99 	static const StringID vehicle_depot_name[];
100 	static const StringID vehicle_group_by_names[];
101 	static const StringID vehicle_group_none_sorter_names[];
102 	static const StringID vehicle_group_shared_orders_sorter_names[];
103 	static VehicleGroupSortFunction * const vehicle_group_none_sorter_funcs[];
104 	static VehicleGroupSortFunction * const vehicle_group_shared_orders_sorter_funcs[];
105 
106 	BaseVehicleListWindow(WindowDesc *desc, WindowNumber wno);
107 
108 	void OnInit() override;
109 
110 	void UpdateSortingFromGrouping();
111 
112 	void DrawVehicleListItems(VehicleID selected_vehicle, int line_height, const Rect &r) const;
113 	void UpdateVehicleGroupBy(GroupBy group_by);
114 	void SortVehicleList();
115 	void BuildVehicleList();
116 	Dimension GetActionDropdownSize(bool show_autoreplace, bool show_group);
117 	DropDownList BuildActionDropdownList(bool show_autoreplace, bool show_group);
118 
GetVehicleSorterNamesBaseVehicleListWindow119 	const StringID *GetVehicleSorterNames()
120 	{
121 		switch (this->grouping) {
122 			case GB_NONE:
123 				return vehicle_group_none_sorter_names;
124 			case GB_SHARED_ORDERS:
125 				return vehicle_group_shared_orders_sorter_names;
126 			default:
127 				NOT_REACHED();
128 		}
129 	}
130 
GetVehicleSorterFuncsBaseVehicleListWindow131 	VehicleGroupSortFunction * const *GetVehicleSorterFuncs()
132 	{
133 		switch (this->grouping) {
134 			case GB_NONE:
135 				return vehicle_group_none_sorter_funcs;
136 			case GB_SHARED_ORDERS:
137 				return vehicle_group_shared_orders_sorter_funcs;
138 			default:
139 				NOT_REACHED();
140 		}
141 	}
142 };
143 
144 uint GetVehicleListHeight(VehicleType type, uint divisor = 1);
145 
146 struct Sorting {
147 	Listing aircraft;
148 	Listing roadveh;
149 	Listing ship;
150 	Listing train;
151 };
152 
153 extern BaseVehicleListWindow::GroupBy _grouping[VLT_END][VEH_COMPANY_END];
154 extern Sorting _sorting[BaseVehicleListWindow::GB_END];
155 
156 #endif /* VEHICLE_GUI_BASE_H */
157