Mercurial > hg > openttd
changeset 14934:da359ccd927b draft
(svn r19536) -Feature: Sort cargos at the filter by cargo dropdown in the build vehicle window.
author | terkhen <terkhen@openttd.org> |
---|---|
date | Thu, 01 Apr 2010 19:52:13 +0000 |
parents | 7127e155ed79 |
children | a64a273253c0 |
files | src/build_vehicle_gui.cpp |
diffstat | 1 files changed, 52 insertions(+), 36 deletions(-) [+] |
line wrap: on
line diff
--- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -99,9 +99,9 @@ }; static bool _internal_sort_order; // descending/ascending -static byte _last_sort_criteria[] = {0, 0, 0, 0}; -static bool _last_sort_order[] = {false, false, false, false}; -static byte _last_filter_criteria[] = {0, 0, 0, 0}; +static byte _last_sort_criteria[] = {0, 0, 0, 0}; +static bool _last_sort_order[] = {false, false, false, false}; +static CargoID _last_filter_criteria[] = {CF_ANY, CF_ANY, CF_ANY, CF_ANY}; static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b) { @@ -790,38 +790,6 @@ this->sort_criteria = _last_sort_criteria[type]; this->descending_sort_order = _last_sort_order[type]; - this->cargo_filter_criteria = _last_filter_criteria[type]; - - /* Populate filter list */ - uint filter_items = 0; - - /* Add item for disabling filtering */ - this->cargo_filter[filter_items] = CF_ANY; - this->cargo_filter_texts[filter_items] = STR_PURCHASE_INFO_ALL_TYPES; - filter_items++; - - /* Add item for vehicles not carrying anything, e.g. train engines. - * This could also be useful for eyecandy vehicles of other types, but is likely too confusing for joe, */ - if (type == VEH_TRAIN) { - this->cargo_filter[filter_items] = CF_NONE; - this->cargo_filter_texts[filter_items] = STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE; - filter_items++; - } - - /* Collect available cargo types for filtering */ - const CargoSpec *cargo; - FOR_ALL_CARGOSPECS(cargo) { - if (IsCargoInClass(cargo->Index(), CC_SPECIAL)) continue; // exclude fake cargo types - this->cargo_filter[filter_items] = cargo->Index(); - this->cargo_filter_texts[filter_items] = cargo->name; - filter_items++; - } - - this->cargo_filter_texts[filter_items] = INVALID_STRING_ID; - if (this->cargo_filter_criteria >= filter_items) this->cargo_filter_criteria = 0; - - this->eng_list.SetFilterFuncs(_filter_funcs); - this->eng_list.SetFilterState(this->cargo_filter[this->cargo_filter_criteria] != CF_ANY); switch (type) { default: NOT_REACHED(); @@ -866,6 +834,54 @@ if (this->eng_list.Length() > 0) this->sel_engine = this->eng_list[0]; } + /** Populate the filter list and set the cargo filter criteria. */ + void SetCargoFilterArray() + { + uint filter_items = 0; + + /* Add item for disabling filtering. */ + this->cargo_filter[filter_items] = CF_ANY; + this->cargo_filter_texts[filter_items] = STR_PURCHASE_INFO_ALL_TYPES; + filter_items++; + + /* Add item for vehicles not carrying anything, e.g. train engines. + * This could also be useful for eyecandy vehicles of other types, but is likely too confusing for joe, */ + if (this->vehicle_type == VEH_TRAIN) { + this->cargo_filter[filter_items] = CF_NONE; + this->cargo_filter_texts[filter_items] = STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE; + filter_items++; + } + + /* Collect available cargo types for filtering. */ + for (int i = 0; i < _sorted_cargo_specs_size; i++) { + this->cargo_filter[filter_items] = _sorted_cargo_specs[i]->Index(); + this->cargo_filter_texts[filter_items] = _sorted_cargo_specs[i]->name; + filter_items++; + } + + /* Terminate the filter list. */ + this->cargo_filter_texts[filter_items] = INVALID_STRING_ID; + + /* If not found, the cargo criteria will be set to all cargos. */ + this->cargo_filter_criteria = 0; + + /* Find the last cargo filter criteria. */ + for (uint i = 0; i < filter_items; i++) { + if (this->cargo_filter[i] == _last_filter_criteria[this->vehicle_type]) { + this->cargo_filter_criteria = i; + break; + } + } + + this->eng_list.SetFilterFuncs(_filter_funcs); + this->eng_list.SetFilterState(this->cargo_filter[this->cargo_filter_criteria] != CF_ANY); + } + + void OnInit() + { + this->SetCargoFilterArray(); + } + /** Filter the engine list against the currently selected cargo filter */ void FilterEngineList() { @@ -1190,7 +1206,7 @@ case BUILD_VEHICLE_WIDGET_CARGO_FILTER_DROPDOWN: // Select a cargo filter criteria if (this->cargo_filter_criteria != index) { this->cargo_filter_criteria = index; - _last_filter_criteria[this->vehicle_type] = this->cargo_filter_criteria; + _last_filter_criteria[this->vehicle_type] = this->cargo_filter[this->cargo_filter_criteria]; /* deactivate filter if criteria is 'Show All', activate it otherwise */ this->eng_list.SetFilterState(this->cargo_filter[this->cargo_filter_criteria] != CF_ANY); this->eng_list.ForceRebuild();