OpenTTD Source
20240917-master-g9ab0a47812
|
Go to the documentation of this file.
10 #ifndef SORTLIST_TYPE_H
11 #define SORTLIST_TYPE_H
46 template <
typename T,
typename P = std::
nullptr_t,
typename F = const
char*>
49 using SortFunction = std::conditional_t<std::is_same_v<P, std::nullptr_t>, bool (
const T&,
const T&), bool (
const T&,
const T&,
const P)>;
62 using SortParameterReference = std::conditional_t<std::is_same_v<P, std::nullptr_t>, P, P&>;
63 const SortParameterReference params;
72 return std::vector<T>::size() >= 2;
86 template <
typename T_ = T,
typename P_ = P,
typename _F = F, std::enable_if_t<std::is_same_v<P_, std::
nullptr_t>>* =
nullptr>
98 template <
typename T_ = T,
typename P_ = P,
typename _F = F, std::enable_if_t<!std::is_same_v<P_, std::
nullptr_t>>* =
nullptr>
126 assert(n_type < std::size(this->sort_func_list));
127 if (this->sort_type != n_type) {
129 this->sort_type = n_type;
179 assert(n_type < std::size(this->filter_func_list));
180 if (this->filter_type != n_type) {
181 this->filter_type = n_type;
224 if (--this->resort_timer == 0) {
248 return (this->flags &
VL_DESC) != 0;
260 if (this->
IsSortable()) std::reverse(std::vector<T>::begin(), std::vector<T>::end());
269 template <
typename Comp>
273 if (!(this->flags &
VL_RESORT))
return false;
282 const bool desc = (this->flags &
VL_DESC) != 0;
284 if constexpr (std::is_same_v<P, std::nullptr_t>) {
285 std::sort(std::vector<T>::begin(), std::vector<T>::end(), [&](
const T &a,
const T &b) {
return desc ? compare(b, a) : compare(a, b); });
287 std::sort(std::vector<T>::begin(), std::vector<T>::end(), [&](
const T &a,
const T &b) {
return desc ? compare(b, a, params) : compare(a, b, params); });
299 this->sort_func_list = n_funcs;
310 if (this->sort_func_list.empty())
return false;
312 return this->
Sort(this->sort_func_list[this->sort_type]);
349 if (!(this->flags &
VL_FILTER))
return false;
351 bool changed =
false;
352 for (
auto it = std::vector<T>::begin(); it != std::vector<T>::end(); ) {
353 if (!decide(&*it, filter_data)) {
354 it = std::vector<T>::erase(it);
371 this->filter_func_list = n_funcs;
382 if (this->filter_func_list.empty())
return false;
384 return this->
Filter(this->filter_func_list[this->filter_type], filter_data);
uint8_t criteria
Filtering criteria.
bool Filter(F filter_data)
Filter the data with the currently selected filter.
bool IsSortable() const
Check if the list is sortable.
void ResetResortTimer()
Reset the resort timer.
void SetFilterState(bool state)
Enable or disable the filter.
std::span< SortFunction *const > sort_func_list
the sort criteria functions
List template of 'things' T to sort in a GUI.
@ VL_RESORT
instruct the code to resort the list in the next loop
SortListFlags
Flags of the sort list.
uint8_t filter_type
what criteria to filter on
void SetSortType(uint8_t n_type)
Set the sorttype of the list.
void SetFilterFuncs(std::span< FilterFunction *const > n_funcs)
Hand the filter function pointers to the GUIList.
uint16_t resort_timer
resort list after a given amount of ticks if set
bool NeedRebuild() const
Check if a rebuild is needed.
#define SETBITS(x, y)
Sets several bits in a variable.
bool Sort()
Overload of #Sort(SortFunction *compare) Overloaded to reduce external code.
Filtering GetFiltering() const
Export current filter conditions.
@ VL_FILTER
filter disabled/enabled
bool NeedResort()
Check if a resort is needed next loop If used the resort timer will decrease every call till 0.
Data structure describing how to show the list (what sort direction and criteria).
SortListFlags flags
used to control sorting/resorting/etc.
bool Filter(FilterFunction *decide, F filter_data)
Filter the list.
std::conditional_t< std::is_same_v< std::nullptr_t, std::nullptr_t >, bool(const BuildBridgeData &, const BuildBridgeData &), bool(const BuildBridgeData &, const BuildBridgeData &, const std::nullptr_t)> SortFunction
Signature of sort function.
void ForceResort()
Force a resort next Sort call Reset the resort timer if used too.
bool order
Ascending/descending.
void ToggleSortOrder()
Toggle the sort order Since that is the worst condition for the sort function reverse the list here.
uint8_t FilterType() const
Get the filtertype of the list.
Data structure describing what to show in the list (filter criteria).
bool IsDescSortOrder() const
Check if the sort order is descending.
bool(const BuildBridgeData *, const char *) FilterFunction
Signature of filter function.
uint8_t criteria
Sorting criteria.
void ForceRebuild()
Force that a rebuild is needed.
@ VL_DESC
sort descending or ascending
Listing GetListing() const
Export current sort conditions.
std::span< FilterFunction *const > filter_func_list
the filter criteria functions
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
uint8_t SortType() const
Get the sorttype of the list.
void RebuildDone()
Notify the sortlist that the rebuild is done.
bool IsFilterEnabled() const
Check if the filter is enabled.
static constexpr TimerGameTick::Ticks DAY_TICKS
1 day is 74 ticks; TimerGameCalendar::date_fract used to be uint16_t and incremented by 885.
@ VL_REBUILD
rebuild the sort list
bool Sort(Comp compare)
Sort the list.
uint8_t sort_type
what criteria to sort on
#define CLRBITS(x, y)
Clears several bits in a variable.
void SetFiltering(Filtering f)
Import filter conditions.
void SetFilterType(uint8_t n_type)
Set the filtertype of the list.
void SetSortFuncs(std::span< SortFunction *const > n_funcs)
Hand the sort function pointers to the GUIList.
void SetListing(Listing l)
Import sort conditions.