From 2c78146da598886c30e5a9c0159253b276b20be0 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 2 Dec 2023 21:22:27 +0000 Subject: [PATCH] Change: Limit cargo types in station cargo filter, with "More..." option to show all. --- src/cargo_type.h | 5 +++-- src/lang/english.txt | 1 + src/station_gui.cpp | 32 ++++++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/cargo_type.h b/src/cargo_type.h index 1fc8a0ede2..a5dbdefcb9 100644 --- a/src/cargo_type.h +++ b/src/cargo_type.h @@ -96,8 +96,9 @@ namespace CargoFilterCriteria { static constexpr CargoID CF_ENGINES = NUM_CARGO + 2; ///< Show only engines (for rail vehicles only) static constexpr CargoID CF_FREIGHT = NUM_CARGO + 3; ///< Show only vehicles which carry any freight (non-passenger) cargo - static constexpr CargoID CF_NO_RATING = NUM_CARGO + 4; ///< Show items with no rating (station list) - static constexpr CargoID CF_SELECT_ALL = NUM_CARGO + 5; ///< Select all items (station list) + static constexpr CargoID CF_NO_RATING = NUM_CARGO + 4; ///< Show items with no rating (station list) + static constexpr CargoID CF_SELECT_ALL = NUM_CARGO + 5; ///< Select all items (station list) + static constexpr CargoID CF_EXPAND_LIST = NUM_CARGO + 6; ///< Expand list to show all items (station list) }; /** Test whether cargo type is not CT_INVALID */ diff --git a/src/lang/english.txt b/src/lang/english.txt index e9733a1784..35f969d5a0 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -3750,6 +3750,7 @@ STR_STATION_LIST_CARGO_FILTER_NO_CARGO_TYPES :No cargo types STR_STATION_LIST_CARGO_FILTER_ONLY_NO_RATING :Only no cargo rating STR_STATION_LIST_CARGO_FILTER_SELECT_ALL :Select all and no rating STR_STATION_LIST_CARGO_FILTER_NO_RATING :No cargo rating +STR_STATION_LIST_CARGO_FILTER_EXPAND :Show more... # Station view window STR_STATION_VIEW_CAPTION :{WHITE}{STATION} {STATION_FEATURES} diff --git a/src/station_gui.cpp b/src/station_gui.cpp index e45a998afe..d9e4f6eb05 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -240,6 +240,7 @@ protected: GUIStationList stations{filter.cargoes}; Scrollbar *vscroll; uint rating_width; + bool filter_expanded; std::array stations_per_cargo_type; ///< Number of stations with a rating for each cargo type. uint16_t stations_per_cargo_type_no_rating; ///< Number of stations without a rating. @@ -526,7 +527,7 @@ public: } } - DropDownList BuildCargoDropDownList() const + DropDownList BuildCargoDropDownList(bool expanded) const { /* Define a custom item consisting of check mark, count string, icon and name string. */ using DropDownListCargoItem = DropDownCheck>; @@ -535,13 +536,28 @@ public: list.push_back(std::make_unique(STR_STATION_LIST_CARGO_FILTER_SELECT_ALL, CargoFilterCriteria::CF_SELECT_ALL)); list.push_back(std::make_unique(-1)); + bool any_hidden = false; + uint16_t count = this->stations_per_cargo_type_no_rating; - list.push_back(std::make_unique>(fmt::format("{}", count), this->filter.include_no_rating, STR_STATION_LIST_CARGO_FILTER_NO_RATING, CargoFilterCriteria::CF_NO_RATING, false, count == 0)); + if (count == 0 && !expanded) { + any_hidden = true; + } else { + list.push_back(std::make_unique>(fmt::format("{}", count), this->filter.include_no_rating, STR_STATION_LIST_CARGO_FILTER_NO_RATING, CargoFilterCriteria::CF_NO_RATING, false, count == 0)); + } Dimension d = GetLargestCargoIconSize(); for (const CargoSpec *cs : _sorted_cargo_specs) { count = this->stations_per_cargo_type[cs->Index()]; - list.push_back(std::make_unique(HasBit(this->filter.cargoes, cs->Index()), fmt::format("{}", count), d, cs->GetCargoIcon(), PAL_NONE, cs->name, cs->Index(), false, count == 0)); + if (count == 0 && !expanded) { + any_hidden = true; + } else { + list.push_back(std::make_unique(HasBit(this->filter.cargoes, cs->Index()), fmt::format("{}", count), d, cs->GetCargoIcon(), PAL_NONE, cs->name, cs->Index(), false, count == 0)); + } + } + + if (!expanded && any_hidden) { + if (list.size() > 2) list.push_back(std::make_unique(-1)); + list.push_back(std::make_unique(STR_STATION_LIST_CARGO_FILTER_EXPAND, CargoFilterCriteria::CF_EXPAND_LIST)); } return list; @@ -605,8 +621,8 @@ public: break; case WID_STL_CARGODROPDOWN: - /* Multiple-choice list should not have a default row selected, so use a non-present value. */ - ShowDropDownList(this, this->BuildCargoDropDownList(), -1, widget, 0, false, true); + this->filter_expanded = false; + ShowDropDownList(this, this->BuildCargoDropDownList(this->filter_expanded), -1, widget, 0, false, true); break; } } @@ -644,6 +660,10 @@ public: } else if (index == CargoFilterCriteria::CF_SELECT_ALL) { this->filter.cargoes = _cargo_mask; this->filter.include_no_rating = true; + } else if (index == CargoFilterCriteria::CF_EXPAND_LIST) { + this->filter_expanded = true; + ReplaceDropDownList(this, this->BuildCargoDropDownList(this->filter_expanded)); + return; } if (oldstate.cargoes != this->filter.cargoes || oldstate.include_no_rating != this->filter.include_no_rating) { @@ -651,7 +671,7 @@ public: this->SetDirty(); /* Only refresh the list if it's changed. */ - if (_ctrl_pressed) ReplaceDropDownList(this, this->BuildCargoDropDownList()); + if (_ctrl_pressed) ReplaceDropDownList(this, this->BuildCargoDropDownList(this->filter_expanded)); } /* Always close the list if ctrl is not pressed. */