Changeset - r14924:3292467c6a9b
[Not reviewed]
master
0 1 0
terkhen - 15 years ago 2010-04-01 19:52:13
terkhen@openttd.org
(svn r19536) -Feature: Sort cargos at the filter by cargo dropdown in the build vehicle window.
1 file changed with 52 insertions and 36 deletions:
0 comments (0 inline, 0 general)
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -96,15 +96,15 @@ static const NWidgetPart _nested_build_v
 
enum {
 
	CF_ANY  = CT_NO_REFIT, ///< Show all vehicles independent of carried cargo (i.e. no filtering)
 
	CF_NONE = CT_INVALID,  ///< Show only vehicles which do not carry cargo (e.g. train engines)
 
};
 

	
 
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)
 
{
 
	int r = ListPositionOfEngine(*a) - ListPositionOfEngine(*b);
 

	
 
	return _internal_sort_order ? -r : r;
 
@@ -787,44 +787,12 @@ struct BuildVehicleWindow : Window {
 
		this->window_number = tile == INVALID_TILE ? (int)type : tile;
 

	
 
		this->sel_engine      = INVALID_ENGINE;
 

	
 
		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();
 
			case VEH_TRAIN:
 
				this->filter.railtype = (tile == INVALID_TILE) ? RAILTYPE_END : GetRailType(tile);
 
				break;
 
@@ -863,12 +831,60 @@ struct BuildVehicleWindow : Window {
 
		this->eng_list.ForceRebuild();
 
		this->GenerateBuildList(); // generate the list, since we need it in the next line
 
		/* Select the first engine in the list as default when opening the window */
 
		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()
 
	{
 
		this->eng_list.Filter(this->cargo_filter[this->cargo_filter_criteria]);
 
		if (0 == this->eng_list.Length()) { // no engine passed through the filter, invalidate the previously selected engine
 
			this->sel_engine = INVALID_ENGINE;
 
@@ -1187,13 +1203,13 @@ struct BuildVehicleWindow : Window {
 
				}
 
				break;
 

	
 
			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();
 
				}
 
				break;
 
		}
0 comments (0 inline, 0 general)