(svn r25097) -Feature[FS#5288]: Add sorting on rating for the town directory window (based on work by sbr).

pull/155/head
alberth 11 years ago
parent b84f833b94
commit b803d0c1dd

@ -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

@ -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(

Loading…
Cancel
Save