summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lang/english.txt1
-rw-r--r--src/town_gui.cpp33
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(