Fix: Don't allow right-click to close world generation progress window. (#13084)
[openttd-github.git] / src / vehicle_gui_base.h
blob88f76bdc617be75daaedb4fbcdef50b765a4e751
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 */
8 /** @file vehicle_gui_base.h Functions/classes shared between the different vehicle list GUIs. */
10 #ifndef VEHICLE_GUI_BASE_H
11 #define VEHICLE_GUI_BASE_H
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"
22 typedef GUIList<const Vehicle*, std::nullptr_t, CargoID> GUIVehicleList;
24 struct GUIVehicleGroup {
25 VehicleList::const_iterator vehicles_begin; ///< Pointer to beginning element of this vehicle group.
26 VehicleList::const_iterator vehicles_end; ///< Pointer to past-the-end element of this vehicle group.
28 GUIVehicleGroup(VehicleList::const_iterator vehicles_begin, VehicleList::const_iterator vehicles_end)
29 : vehicles_begin(vehicles_begin), vehicles_end(vehicles_end) {}
31 std::ptrdiff_t NumVehicles() const
33 return std::distance(this->vehicles_begin, this->vehicles_end);
36 const Vehicle *GetSingleVehicle() const
38 assert(this->NumVehicles() == 1);
39 return this->vehicles_begin[0];
42 Money GetDisplayProfitThisYear() const
44 return std::accumulate(this->vehicles_begin, this->vehicles_end, (Money)0, [](Money acc, const Vehicle *v) {
45 return acc + v->GetDisplayProfitThisYear();
46 });
49 Money GetDisplayProfitLastYear() const
51 return std::accumulate(this->vehicles_begin, this->vehicles_end, (Money)0, [](Money acc, const Vehicle *v) {
52 return acc + v->GetDisplayProfitLastYear();
53 });
56 TimerGameEconomy::Date GetOldestVehicleAge() const
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;
65 typedef GUIList<GUIVehicleGroup, std::nullptr_t, CargoID> GUIVehicleGroupList;
67 struct BaseVehicleListWindow : public Window {
69 enum GroupBy : uint8_t {
70 GB_NONE,
71 GB_SHARED_ORDERS,
73 GB_END,
76 GroupBy grouping; ///< How we want to group the list.
77 VehicleList vehicles; ///< List of vehicles. This is the buffer for `vehgroups` to point into; if this is structurally modified, `vehgroups` must be rebuilt.
78 GUIVehicleGroupList vehgroups; ///< List of (groups of) vehicles. This stores iterators of `vehicles`, and should be rebuilt if `vehicles` is structurally changed.
79 Listing *sorting; ///< Pointer to the vehicle type related sorting.
80 uint8_t unitnumber_digits; ///< The number of digits of the highest unit number.
81 Scrollbar *vscroll;
82 VehicleListIdentifier vli; ///< Identifier of the vehicle list we want to currently show.
83 VehicleID vehicle_sel; ///< Selected vehicle
84 CargoID cargo_filter_criteria; ///< Selected cargo filter index
85 uint order_arrow_width; ///< Width of the arrow in the small order list.
86 CargoTypes used_cargoes;
88 typedef GUIVehicleGroupList::SortFunction VehicleGroupSortFunction;
89 typedef GUIVehicleList::SortFunction VehicleIndividualSortFunction;
91 enum ActionDropdownItem {
92 ADI_REPLACE,
93 ADI_SERVICE,
94 ADI_DEPOT,
95 ADI_ADD_SHARED,
96 ADI_REMOVE_ALL,
97 ADI_CREATE_GROUP,
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;
109 BaseVehicleListWindow(WindowDesc &desc, WindowNumber wno);
111 void OnInit() override;
113 void UpdateSortingFromGrouping();
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(CargoID cid) 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);
127 std::span<const StringID> GetVehicleSorterNames();
129 std::span<VehicleGroupSortFunction * const> GetVehicleSorterFuncs()
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();
142 struct CargoIconOverlay {
143 int left;
144 int right;
145 CargoID cargo_type;
146 uint cargo_cap;
148 constexpr CargoIconOverlay(int left, int right, CargoID cargo_type, uint cargo_cap)
149 : left(left), right(right), cargo_type(cargo_type), cargo_cap(cargo_cap)
153 bool ShowCargoIconOverlay();
154 void AddCargoIconOverlay(std::vector<CargoIconOverlay> &overlays, int x, int width, const Vehicle *v);
155 void DrawCargoIconOverlay(int x, int y, CargoID cid);
156 void DrawCargoIconOverlays(std::span<const CargoIconOverlay> overlays, int y);
158 uint GetVehicleListHeight(VehicleType type, uint divisor = 1);
160 struct Sorting {
161 Listing aircraft;
162 Listing roadveh;
163 Listing ship;
164 Listing train;
167 extern BaseVehicleListWindow::GroupBy _grouping[VLT_END][VEH_COMPANY_END];
168 extern Sorting _sorting[BaseVehicleListWindow::GB_END];
170 #endif /* VEHICLE_GUI_BASE_H */