mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2026-07-24 03:56:43 +00:00
Codechange: simplify the FiosItem sorter logic
This commit is contained in:
+31
-9
@@ -43,6 +43,28 @@ LoadCheckData _load_check_data; ///< Data loaded from save during SL_LOAD_CHE
|
||||
static bool _fios_path_changed;
|
||||
static bool _savegame_sort_dirty;
|
||||
|
||||
/** The available sorters for FiosItems. */
|
||||
enum class SavegameSorter : uint8_t {
|
||||
Date, ///< Sort by date.
|
||||
Name, ///< Sort by name.
|
||||
};
|
||||
|
||||
static SavegameSorter _savegame_sorter = SavegameSorter::Date; ///< Sorter for savegames.
|
||||
static bool _savegame_sorter_ascending = false; ///< Sorter for savegames.
|
||||
|
||||
/** Sorts the FiosItems based on the savegame sorter and order. @copydoc GUIList::Sorter */
|
||||
bool FiosItemSorter(const FiosItem &a, const FiosItem &b)
|
||||
{
|
||||
switch (_savegame_sorter) {
|
||||
case SavegameSorter::Date:
|
||||
return _savegame_sorter_ascending ? FiosItemModificationDateSorter(a, b) : FiosItemModificationDateSorter(b, a);
|
||||
case SavegameSorter::Name:
|
||||
return _savegame_sorter_ascending ? FiosItemNameSorter(a, b) : FiosItemNameSorter(b, a);
|
||||
default:
|
||||
NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset read data.
|
||||
*/
|
||||
@@ -317,7 +339,7 @@ static void SortSaveGameList(FileList &file_list)
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(file_list.begin() + sort_start, file_list.end() - sort_end);
|
||||
std::sort(file_list.begin() + sort_start, file_list.end() - sort_end, FiosItemSorter);
|
||||
}
|
||||
|
||||
struct SaveLoadWindow : public Window {
|
||||
@@ -491,8 +513,8 @@ public:
|
||||
switch (widget) {
|
||||
case WID_SL_SORT_BYNAME:
|
||||
case WID_SL_SORT_BYDATE:
|
||||
if (((_savegame_sort_order & SORT_BY_NAME) != 0) == (widget == WID_SL_SORT_BYNAME)) {
|
||||
this->DrawSortButtonState(widget, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
|
||||
if ((_savegame_sorter == SavegameSorter::Name) == (widget == WID_SL_SORT_BYNAME)) {
|
||||
this->DrawSortButtonState(widget, _savegame_sorter_ascending ? SBS_UP : SBS_DOWN);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -667,16 +689,16 @@ public:
|
||||
void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
|
||||
{
|
||||
switch (widget) {
|
||||
case WID_SL_SORT_BYNAME: // Sort save names by name
|
||||
_savegame_sort_order = (_savegame_sort_order == SORT_BY_NAME) ?
|
||||
SORT_BY_NAME | SORT_DESCENDING : SORT_BY_NAME;
|
||||
case WID_SL_SORT_BYNAME: // Sort save games by name
|
||||
_savegame_sorter_ascending = _savegame_sorter != SavegameSorter::Name || !_savegame_sorter_ascending;
|
||||
_savegame_sorter = SavegameSorter::Name;
|
||||
_savegame_sort_dirty = true;
|
||||
this->SetDirty();
|
||||
break;
|
||||
|
||||
case WID_SL_SORT_BYDATE: // Sort save names by date
|
||||
_savegame_sort_order = (_savegame_sort_order == SORT_BY_DATE) ?
|
||||
SORT_BY_DATE | SORT_DESCENDING : SORT_BY_DATE;
|
||||
case WID_SL_SORT_BYDATE: // Sort save games by date
|
||||
_savegame_sorter_ascending = _savegame_sorter != SavegameSorter::Date || !_savegame_sorter_ascending;
|
||||
_savegame_sorter = SavegameSorter::Date;
|
||||
_savegame_sort_dirty = true;
|
||||
this->SetDirty();
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user