2009-08-21 22:21:05 +02:00
/*
* This file is part of OpenTTD .
* 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.
* 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 .
* 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/>.
*/
2008-09-13 12:04:36 +02:00
/** @file vehicle_gui_base.h Functions/classes shared between the different vehicle list GUIs. */
# ifndef VEHICLE_GUI_BASE_H
# define VEHICLE_GUI_BASE_H
2022-11-08 21:11:16 +01:00
# include "cargo_type.h"
2023-04-24 20:33:18 +02:00
# include "timer/timer_game_calendar.h"
2019-01-11 09:09:37 +01:00
# include "economy_type.h"
2008-09-13 12:04:36 +02:00
# include "sortlist_type.h"
2021-02-21 16:35:15 +01:00
# include "vehicle_base.h"
2010-09-08 23:28:50 +02:00
# include "vehiclelist.h"
2010-01-15 17:41:15 +01:00
# include "window_gui.h"
2010-07-17 16:36:36 +02:00
# include "widgets/dropdown_type.h"
2008-09-13 12:04:36 +02:00
2023-12-03 00:28:01 +01:00
typedef GUIList < const Vehicle * , std : : nullptr_t , CargoID > GUIVehicleList ;
2008-09-13 12:04:36 +02:00
2019-01-11 09:09:37 +01:00
struct GUIVehicleGroup {
VehicleList : : const_iterator vehicles_begin ; ///< Pointer to beginning element of this vehicle group.
VehicleList : : const_iterator vehicles_end ; ///< Pointer to past-the-end element of this vehicle group.
2021-02-21 16:35:15 +01:00
GUIVehicleGroup ( VehicleList : : const_iterator vehicles_begin , VehicleList : : const_iterator vehicles_end )
: vehicles_begin ( vehicles_begin ) , vehicles_end ( vehicles_end ) { }
2019-01-11 09:09:37 +01:00
std : : ptrdiff_t NumVehicles ( ) const
{
2021-02-21 16:35:15 +01:00
return std : : distance ( this - > vehicles_begin , this - > vehicles_end ) ;
2019-01-11 09:09:37 +01:00
}
2021-02-21 16:35:15 +01:00
2019-01-11 09:09:37 +01:00
const Vehicle * GetSingleVehicle ( ) const
{
2021-02-21 16:35:15 +01:00
assert ( this - > NumVehicles ( ) = = 1 ) ;
return this - > vehicles_begin [ 0 ] ;
}
Money GetDisplayProfitThisYear ( ) const
{
return std : : accumulate ( this - > vehicles_begin , this - > vehicles_end , ( Money ) 0 , [ ] ( Money acc , const Vehicle * v ) {
return acc + v - > GetDisplayProfitThisYear ( ) ;
} ) ;
}
Money GetDisplayProfitLastYear ( ) const
{
return std : : accumulate ( this - > vehicles_begin , this - > vehicles_end , ( Money ) 0 , [ ] ( Money acc , const Vehicle * v ) {
return acc + v - > GetDisplayProfitLastYear ( ) ;
} ) ;
}
2023-04-24 20:33:18 +02:00
TimerGameCalendar : : Date GetOldestVehicleAge ( ) const
2021-02-21 16:35:15 +01:00
{
const Vehicle * oldest = * std : : max_element ( this - > vehicles_begin , this - > vehicles_end , [ ] ( const Vehicle * v_a , const Vehicle * v_b ) {
return v_a - > age < v_b - > age ;
} ) ;
return oldest - > age ;
2019-01-11 09:09:37 +01:00
}
} ;
2023-12-03 00:28:01 +01:00
typedef GUIList < GUIVehicleGroup , std : : nullptr_t , CargoID > GUIVehicleGroupList ;
2019-01-11 09:09:37 +01:00
2009-11-17 12:36:36 +01:00
struct BaseVehicleListWindow : public Window {
2019-01-11 09:09:37 +01:00
enum GroupBy : byte {
GB_NONE ,
2019-01-11 09:50:38 +01:00
GB_SHARED_ORDERS ,
2019-01-11 09:09:37 +01:00
GB_END ,
} ;
2022-11-08 21:11:16 +01:00
GroupBy grouping ; ///< How we want to group the list.
VehicleList vehicles ; ///< List of vehicles. This is the buffer for `vehgroups` to point into; if this is structurally modified, `vehgroups` must be rebuilt.
GUIVehicleGroupList vehgroups ; ///< List of (groups of) vehicles. This stores iterators of `vehicles`, and should be rebuilt if `vehicles` is structurally changed.
Listing * sorting ; ///< Pointer to the vehicle type related sorting.
byte unitnumber_digits ; ///< The number of digits of the highest unit number.
2010-08-12 10:37:01 +02:00
Scrollbar * vscroll ;
2022-11-08 21:11:16 +01:00
VehicleListIdentifier vli ; ///< Identifier of the vehicle list we want to currently show.
VehicleID vehicle_sel ; ///< Selected vehicle
2023-11-26 20:30:22 +01:00
CargoID cargo_filter_criteria ; ///< Selected cargo filter index
2022-11-08 21:11:16 +01:00
uint order_arrow_width ; ///< Width of the arrow in the small order list.
2023-12-03 19:11:08 +01:00
CargoTypes used_cargoes ;
2019-01-11 09:09:37 +01:00
typedef GUIVehicleGroupList : : SortFunction VehicleGroupSortFunction ;
typedef GUIVehicleList : : SortFunction VehicleIndividualSortFunction ;
2008-09-13 12:04:36 +02:00
2010-07-17 16:36:36 +02:00
enum ActionDropdownItem {
ADI_REPLACE ,
ADI_SERVICE ,
ADI_DEPOT ,
ADI_ADD_SHARED ,
ADI_REMOVE_ALL ,
2023-06-18 21:48:04 +02:00
ADI_CREATE_GROUP ,
2010-07-17 16:36:36 +02:00
} ;
2010-07-17 16:53:46 +02:00
static const StringID vehicle_depot_name [ ] ;
2019-01-11 09:09:37 +01:00
static const StringID vehicle_group_by_names [ ] ;
static const StringID vehicle_group_none_sorter_names [ ] ;
static const StringID vehicle_group_shared_orders_sorter_names [ ] ;
static VehicleGroupSortFunction * const vehicle_group_none_sorter_funcs [ ] ;
static VehicleGroupSortFunction * const vehicle_group_shared_orders_sorter_funcs [ ] ;
2008-09-13 12:04:36 +02:00
2019-01-11 09:09:37 +01:00
BaseVehicleListWindow ( WindowDesc * desc , WindowNumber wno ) ;
2021-04-25 18:51:03 +02:00
void OnInit ( ) override ;
2019-01-11 09:09:37 +01:00
void UpdateSortingFromGrouping ( ) ;
2009-10-25 15:52:46 +01:00
void DrawVehicleListItems ( VehicleID selected_vehicle , int line_height , const Rect & r ) const ;
2019-01-11 09:09:37 +01:00
void UpdateVehicleGroupBy ( GroupBy group_by ) ;
2008-09-13 12:04:36 +02:00
void SortVehicleList ( ) ;
2010-09-09 16:40:39 +02:00
void BuildVehicleList ( ) ;
2023-12-14 02:48:45 +01:00
void SetCargoFilter ( byte index ) ;
2022-11-08 21:11:16 +01:00
void SetCargoFilterArray ( ) ;
void FilterVehicleList ( ) ;
2023-11-12 22:32:12 +01:00
StringID GetCargoFilterLabel ( CargoID cid ) const ;
2023-12-03 19:11:08 +01:00
DropDownList BuildCargoDropDownList ( bool full ) const ;
2023-06-18 21:48:04 +02:00
Dimension GetActionDropdownSize ( bool show_autoreplace , bool show_group , bool show_create ) ;
DropDownList BuildActionDropdownList ( bool show_autoreplace , bool show_group , bool show_create ) ;
2019-01-11 09:09:37 +01:00
const StringID * GetVehicleSorterNames ( )
{
2019-01-11 09:50:38 +01:00
switch ( this - > grouping ) {
case GB_NONE :
return vehicle_group_none_sorter_names ;
case GB_SHARED_ORDERS :
return vehicle_group_shared_orders_sorter_names ;
default :
NOT_REACHED ( ) ;
}
2019-01-11 09:09:37 +01:00
}
VehicleGroupSortFunction * const * GetVehicleSorterFuncs ( )
{
2019-01-11 09:50:38 +01:00
switch ( this - > grouping ) {
case GB_NONE :
return vehicle_group_none_sorter_funcs ;
case GB_SHARED_ORDERS :
return vehicle_group_shared_orders_sorter_funcs ;
default :
NOT_REACHED ( ) ;
}
2019-01-11 09:09:37 +01:00
}
2008-09-13 12:04:36 +02:00
} ;
2009-11-16 22:28:12 +01:00
uint GetVehicleListHeight ( VehicleType type , uint divisor = 1 ) ;
2008-09-13 12:04:36 +02:00
struct Sorting {
Listing aircraft ;
Listing roadveh ;
Listing ship ;
Listing train ;
} ;
2019-01-11 09:09:37 +01:00
extern BaseVehicleListWindow : : GroupBy _grouping [ VLT_END ] [ VEH_COMPANY_END ] ;
extern Sorting _sorting [ BaseVehicleListWindow : : GB_END ] ;
2008-09-13 12:04:36 +02:00
# endif /* VEHICLE_GUI_BASE_H */