Changeset - r27939:8e2e1ddcb130
[Not reviewed]
master
0 3 0
Peter Nelson - 15 months ago 2023-09-18 21:34:55
peter1138@openttd.org
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.
3 files changed with 15 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/cargotype.cpp
Show inline comments
 
@@ -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;
src/cargotype.h
Show inline comments
 
@@ -187,6 +187,7 @@ CargoID GetCargoIDByBitnum(uint8_t bitnu
 
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;
 

	
src/industry_gui.cpp
Show inline comments
 
@@ -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 Wi
 
		/* 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 Wi
 
		/* 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;
 

	
0 comments (0 inline, 0 general)