Town directory window class. */
| 716 | |
| 717 | /** Town directory window class. */ |
| 718 | struct TownDirectoryWindow : public Window { |
| 719 | private: |
| 720 | /* Runtime saved values */ |
| 721 | static Listing last_sorting; |
| 722 | |
| 723 | /* Constants for sorting towns */ |
| 724 | static inline const StringID sorter_names[] = { |
| 725 | STR_SORT_BY_NAME, |
| 726 | STR_SORT_BY_POPULATION, |
| 727 | STR_SORT_BY_RATING, |
| 728 | }; |
| 729 | static const std::initializer_list<GUITownList::SortFunction * const> sorter_funcs; |
| 730 | |
| 731 | StringFilter string_filter{}; ///< Filter for towns |
| 732 | QueryString townname_editbox; ///< Filter editbox |
| 733 | |
| 734 | GUITownList towns{TownDirectoryWindow::last_sorting.order}; |
| 735 | |
| 736 | Scrollbar *vscroll = nullptr; |
| 737 | |
| 738 | void BuildSortTownList() |
| 739 | { |
| 740 | if (this->towns.NeedRebuild()) { |
| 741 | this->towns.clear(); |
| 742 | this->towns.reserve(Town::GetNumItems()); |
| 743 | |
| 744 | for (const Town *t : Town::Iterate()) { |
| 745 | if (this->string_filter.IsEmpty()) { |
| 746 | this->towns.push_back(t); |
| 747 | continue; |
| 748 | } |
| 749 | this->string_filter.ResetState(); |
| 750 | this->string_filter.AddLine(t->GetCachedName()); |
| 751 | if (this->string_filter.GetState()) this->towns.push_back(t); |
| 752 | } |
| 753 | |
| 754 | this->towns.RebuildDone(); |
| 755 | this->vscroll->SetCount(this->towns.size()); // Update scrollbar as well. |
| 756 | } |
| 757 | /* Always sort the towns. */ |
| 758 | this->towns.Sort(); |
| 759 | this->SetWidgetDirty(WID_TD_LIST); // Force repaint of the displayed towns. |
| 760 | } |
| 761 | |
| 762 | /** Sort by town name */ |
| 763 | static bool TownNameSorter(const Town * const &a, const Town * const &b, const bool &) |
| 764 | { |
| 765 | return StrNaturalCompare(a->GetCachedName(), b->GetCachedName()) < 0; // Sort by name (natural sorting). |
| 766 | } |
| 767 | |
| 768 | /** Sort by population (default descending, as big towns are of the most interest). */ |
| 769 | static bool TownPopulationSorter(const Town * const &a, const Town * const &b, const bool &order) |
| 770 | { |
| 771 | uint32_t a_population = a->cache.population; |
| 772 | uint32_t b_population = b->cache.population; |
| 773 | if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b, order); |
| 774 | return a_population < b_population; |
| 775 | } |
nothing calls this directly
no test coverage detected