From d5307c9722cf87c0553d34c2281255f6c9e01e75 Mon Sep 17 00:00:00 2001 From: alberth Date: Sun, 17 Mar 2013 15:45:24 +0000 Subject: (svn r25097) -Feature[FS#5288]: Add sorting on rating for the town directory window (based on work by sbr). --- src/lang/english.txt | 1 + src/town_gui.cpp | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/lang/english.txt b/src/lang/english.txt index 2558bb016..75bddcd51 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -289,6 +289,7 @@ STR_SORT_BY_POWER_VS_RUNNING_COST :Power/Running C STR_SORT_BY_CARGO_CAPACITY :Cargo Capacity STR_SORT_BY_RANGE :Range STR_SORT_BY_POPULATION :Population +STR_SORT_BY_RATING :Rating # Tooltips for the main toolbar STR_TOOLBAR_TOOLTIP_PAUSE_GAME :{BLACK}Pause game diff --git a/src/town_gui.cpp b/src/town_gui.cpp index d8ffc8262..35e2e9ff7 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -691,6 +691,25 @@ private: return (a_population > b_population) ? -1 : 1; } + /** Sort by town rating */ + static int CDECL TownRatingSorter(const Town * const *a, const Town * const *b) + { + int before = TownDirectoryWindow::last_sorting.order ? 1 : -1; // Value to get 'a' before 'b'. + + /* Towns without rating are always after towns with rating. */ + if (HasBit((*a)->have_ratings, _local_company)) { + if (HasBit((*b)->have_ratings, _local_company)) { + int16 a_rating = (*a)->ratings[_local_company]; + int16 b_rating = (*b)->ratings[_local_company]; + if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b); + return (a_rating > b_rating) ? -1 : 1; + } + return before; + } + if (HasBit((*b)->have_ratings, _local_company)) return -before; + return -before * TownDirectoryWindow::TownNameSorter(a, b); // Sort unrated towns always on ascending town name. + } + public: TownDirectoryWindow(const WindowDesc *desc) : Window() { @@ -823,8 +842,16 @@ public: { switch (widget) { case WID_TD_SORT_ORDER: // Click on sort order button - this->towns.ToggleSortOrder(); - this->last_sorting = this->towns.GetListing(); // Store new sorting order. + if (this->towns.SortType() != 2) { // A different sort than by rating. + this->towns.ToggleSortOrder(); + this->last_sorting = this->towns.GetListing(); // Store new sorting order. + } else { + /* Some parts are always sorted ascending on name. */ + this->last_sorting.order = !this->last_sorting.order; + this->towns.SetListing(this->last_sorting); + this->towns.ForceResort(); + this->towns.Sort(); + } this->SetDirty(); break; @@ -899,6 +926,7 @@ const Town *TownDirectoryWindow::last_town = NULL; const StringID TownDirectoryWindow::sorter_names[] = { STR_SORT_BY_NAME, STR_SORT_BY_POPULATION, + STR_SORT_BY_RATING, INVALID_STRING_ID }; @@ -906,6 +934,7 @@ const StringID TownDirectoryWindow::sorter_names[] = { GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = { &TownNameSorter, &TownPopulationSorter, + &TownRatingSorter, }; static const WindowDesc _town_directory_desc( -- cgit v1.2.3-54-g00ecf