Fix #11315: Sort industries and cargoes by name* in industry chain window.

*Cargo types are sorted by the normal method so it's not strictly alphabetical.
This commit is contained in:
Peter Nelson 2023-09-18 22:34:55 +01:00 committed by PeterN
parent 106f29f761
commit 6fb89b189f
3 changed files with 15 additions and 2 deletions

View File

@ -142,6 +142,7 @@ SpriteID CargoSpec::GetCargoIcon() const
return sprite;
}
std::array<uint8_t, NUM_CARGO> _sorted_cargo_types; ///< Sort order of cargoes by cargo ID.
std::vector<const CargoSpec *> _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name.
span<const CargoSpec *> _sorted_standard_cargo_specs; ///< Standard cargo specifications sorted alphabetically by name.
@ -186,6 +187,11 @@ void InitializeSortedCargoSpecs()
/* Sort cargo specifications by cargo class and name. */
std::sort(_sorted_cargo_specs.begin(), _sorted_cargo_specs.end(), &CargoSpecClassSorter);
/* Populate */
for (auto it = std::begin(_sorted_cargo_specs); it != std::end(_sorted_cargo_specs); ++it) {
_sorted_cargo_types[(*it)->Index()] = static_cast<uint8_t>(it - std::begin(_sorted_cargo_specs));
}
/* Count the number of standard cargos and fill the mask. */
_standard_cargo_mask = 0;
uint8_t nb_standard_cargo = 0;

View File

@ -187,6 +187,7 @@ CargoID GetCargoIDByBitnum(uint8_t bitnum);
CargoID GetDefaultCargoID(LandscapeID l, CargoType ct);
void InitializeSortedCargoSpecs();
extern std::array<uint8_t, NUM_CARGO> _sorted_cargo_types;
extern std::vector<const CargoSpec *> _sorted_cargo_specs;
extern span<const CargoSpec *> _sorted_standard_cargo_specs;

View File

@ -1920,6 +1920,11 @@ enum CargoesFieldType {
static const uint MAX_CARGOES = 16; ///< Maximum number of cargoes carried in a #CFT_CARGO field in #CargoesField.
static bool CargoIDSorter(const CargoID &a, const CargoID &b)
{
return _sorted_cargo_types[a] < _sorted_cargo_types[b];
}
/** Data about a single field in the #IndustryCargoesWindow panel. */
struct CargoesField {
static int vert_inter_industry_space;
@ -2049,6 +2054,7 @@ struct CargoesField {
}
}
this->u.cargo.num_cargoes = (count < 0) ? static_cast<uint8_t>(insert - std::begin(this->u.cargo.vertical_cargoes)) : count;
std::sort(std::begin(this->u.cargo.vertical_cargoes), insert, &CargoIDSorter);
std::fill(insert, std::end(this->u.cargo.vertical_cargoes), CT_INVALID);
this->u.cargo.top_end = top_end;
this->u.cargo.bottom_end = bottom_end;
@ -2813,7 +2819,7 @@ struct IndustryCargoesWindow : public Window {
/* Add suppliers and customers of the 'it' industry. */
int supp_count = 0;
int cust_count = 0;
for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
for (IndustryType it : _sorted_industry_types) {
const IndustrySpec *indsp = GetIndustrySpec(it);
if (!indsp->enabled) continue;
@ -2881,7 +2887,7 @@ struct IndustryCargoesWindow : public Window {
/* Add suppliers and customers of the cargo. */
int supp_count = 0;
int cust_count = 0;
for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
for (IndustryType it : _sorted_industry_types) {
const IndustrySpec *indsp = GetIndustrySpec(it);
if (!indsp->enabled) continue;