10#ifndef SORTLIST_TYPE_H
11#define SORTLIST_TYPE_H
42template <
typename T,
typename P = std::
nullptr_t,
typename F = std::
string_view>
45 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)>;
58 using SortParameterReference = std::conditional_t<std::is_same_v<P, std::nullptr_t>, P, P&>;
59 const SortParameterReference params;
68 return std::vector<T>::size() >= 2;
82 template <
typename T_ = T,
typename P_ = P,
typename _F = F, std::enable_if_t<std::is_same_v<P_, std::
nullptr_t>>* =
nullptr>
94 template <
typename T_ = T,
typename P_ = P,
typename _F = F, std::enable_if_t<!std::is_same_v<P_, std::
nullptr_t>>* =
nullptr>
122 assert(n_type < std::size(this->sort_func_list));
123 if (this->sort_type != n_type) {
125 this->sort_type = n_type;
175 assert(n_type < std::size(this->filter_func_list));
176 if (this->filter_type != n_type) {
177 this->filter_type = n_type;
220 if (--this->resort_timer == 0) {
256 if (this->
IsSortable()) std::reverse(std::vector<T>::begin(), std::vector<T>::end());
265 template <
typename Comp>
280 if constexpr (std::is_same_v<P, std::nullptr_t>) {
281 std::sort(std::vector<T>::begin(), std::vector<T>::end(), [&](
const T &a,
const T &b) {
return desc ? compare(b, a) : compare(a, b); });
283 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); });
295 this->sort_func_list = n_funcs;
306 if (this->sort_func_list.empty())
return false;
308 return this->
Sort(this->sort_func_list[this->sort_type]);
347 bool changed =
false;
348 for (
auto it = std::vector<T>::begin(); it != std::vector<T>::end(); ) {
349 if (!decide(&*it, filter_data)) {
350 it = std::vector<T>::erase(it);
367 this->filter_func_list = n_funcs;
378 if (this->filter_func_list.empty())
return false;
380 return this->
Filter(this->filter_func_list[this->filter_type], filter_data);
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Flip(Tvalue_type value)
Flip the value-th bit.
constexpr Timpl & Set()
Set all bits.
List template of 'things' T to sort in a GUI.
bool Filter(FilterFunction *decide, F filter_data)
Filter the list.
void RebuildDone()
Notify the sortlist that the rebuild is done.
void SetFiltering(Filtering f)
Import filter conditions.
void SetListing(Listing l)
Import sort conditions.
bool IsSortable() const
Check if the list is sortable.
uint16_t resort_timer
resort list after a given amount of ticks if set
void SetFilterState(bool state)
Enable or disable the filter.
bool IsDescSortOrder() const
Check if the sort order is descending.
void ToggleSortOrder()
Toggle the sort order Since that is the worst condition for the sort function reverse the list here.
bool(const T *, F) FilterFunction
Signature of filter function.
void SetFilterFuncs(std::span< FilterFunction *const > n_funcs)
Hand the filter function pointers to the GUIList.
Filtering GetFiltering() const
Export current filter conditions.
bool NeedRebuild() const
Check if a rebuild is needed.
void SetFilterType(uint8_t n_type)
Set the filtertype of the list.
std::span< FilterFunction *const > filter_func_list
the filter criteria functions
void ResetResortTimer()
Reset the resort timer.
void ForceRebuild()
Force that a rebuild is needed.
bool IsFilterEnabled() const
Check if the filter is enabled.
std::conditional_t< std::is_same_v< P, std::nullptr_t >, bool(const T &, const T &), bool(const T &, const T &, const P)> SortFunction
Signature of sort function.
std::span< SortFunction *const > sort_func_list
the sort criteria functions
bool Sort(Comp compare)
Sort the list.
void ForceResort()
Force a resort next Sort call Reset the resort timer if used too.
uint8_t SortType() const
Get the sorttype of the list.
bool Filter(F filter_data)
Filter the data with the currently selected filter.
bool Sort()
Overload of #Sort(SortFunction *compare) Overloaded to reduce external code.
Listing GetListing() const
Export current sort conditions.
uint8_t sort_type
what criteria to sort on
void SetSortFuncs(std::span< SortFunction *const > n_funcs)
Hand the sort function pointers to the GUIList.
bool NeedResort()
Check if a resort is needed next loop If used the resort timer will decrease every call till 0.
void SetSortType(uint8_t n_type)
Set the sorttype of the list.
uint8_t filter_type
what criteria to filter on
SortListFlags flags
used to control sorting/resorting/etc.
uint8_t FilterType() const
Get the filtertype of the list.
static constexpr TimerGameTick::Ticks DAY_TICKS
1 day is 74 ticks; TimerGameCalendar::date_fract used to be uint16_t and incremented by 885.
Type (helpers) for enums.
SortListFlag
Flags of the sort list.
@ Rebuild
rebuild the sort list
@ Desc
sort descending or ascending
@ Resort
instruct the code to resort the list in the next loop
@ Filter
filter disabled/enabled
Data structure describing what to show in the list (filter criteria).
uint8_t criteria
Filtering criteria.
Data structure describing how to show the list (what sort direction and criteria).
uint8_t criteria
Sorting criteria.
bool order
Ascending/descending.
Definition of the tick-based game-timer.