# HG changeset patch # Parent 10446cea1b1533a5217b97b6d4cb11155934bcc5 # User sbr # Date 1348305882 -7200 Add sort by rating capabilities to the TownDirectoryWindow. diff -r 10446cea1b15 src/lang/english.txt --- a/src/lang/english.txt Sun Sep 02 11:19:45 2012 +0200 +++ b/src/lang/english.txt Sat Sep 22 11:25:45 2012 +0200 @@ -288,6 +288,7 @@ 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 -r 10446cea1b15 src/town_gui.cpp --- a/src/town_gui.cpp Sun Sep 02 11:19:45 2012 +0200 +++ b/src/town_gui.cpp Sat Sep 22 11:25:45 2012 +0200 @@ -688,6 +688,19 @@ return (*a)->cache.population - (*b)->cache.population; } + /** Sort by town rating */ + static int CDECL TownRatingSorter(const Town * const *a, const Town * const *b) + { + /* Towns without rating are always sorted after towns with rating. + * Depending of the sort order in use, not rated towns are given a different value to compare to. */ + int16 no_rating = TownDirectoryWindow::last_sorting.order ? RATING_MINIMUM - 1 : RATING_MAXIMUM + 1; + + int16 a_rating = !HasBit((*a)->have_ratings, _local_company) ? no_rating : (*a)->ratings[_local_company]; + int16 b_rating = !HasBit((*b)->have_ratings, _local_company) ? no_rating : (*b)->ratings[_local_company]; + + return a_rating - b_rating; + } + public: TownDirectoryWindow(const WindowDesc *desc) : Window() { @@ -821,6 +834,12 @@ 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) { // Sort by town rating + /* Towns without ratings aren't sorted in the same manner if the sort order is ascending or is descending. */ + this->towns.ForceResort(); + this->towns.Sort(); + } this->SetDirty(); break; @@ -894,6 +913,7 @@ const StringID TownDirectoryWindow::sorter_names[] = { STR_SORT_BY_NAME, STR_SORT_BY_POPULATION, + STR_SORT_BY_RATING, INVALID_STRING_ID }; @@ -901,6 +921,7 @@ GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = { &TownNameSorter, &TownPopulationSorter, + &TownRatingSorter, }; static const WindowDesc _town_directory_desc(