# HG changeset patch # User adf88@interia.pl # Date 1503136308 -7200 # Sat Aug 19 11:51:48 2017 +0200 # Branch trunk # Node ID bd3a434f73bad30fccc3bbbd0a1ae3687817a2e8 # Parent 27d7b11d4da36924c1d32db7b6afe37c1b760403 Remove duplicated code and use GUIList class for sorting engine lists. diff -r 27d7b11d4da3 -r bd3a434f73ba src/autoreplace_gui.cpp --- a/src/autoreplace_gui.cpp Sat Aug 19 11:51:37 2017 +0200 +++ b/src/autoreplace_gui.cpp Sat Aug 19 11:51:48 2017 +0200 @@ -32,12 +32,11 @@ void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count, GroupID selected_group); -static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b) -{ - int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position; +extern int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b); // build_vehicle_gui.cpp - return r; -} +static GUIEngineList::SortFunction * const _engine_replace_list_sort_functions[] = { + EngineNumberSorter, +}; /** * Rebuild the left autoreplace list if an engine is removed or added @@ -84,8 +83,6 @@ bool reset_sel_engine; ///< Also reset #sel_engine while updating left and/or right (#update_left and/or #update_right) and no valid engine selected. GroupID sel_group; ///< Group selected to replace. int details_height; ///< Minimal needed height of the details panels (found so far). - byte sort_criteria; ///< Criteria of sorting vehicles. - bool descending_sort_order; ///< Order of sorting vehicles. bool show_hidden_engines; ///< Whether to show the hidden engines. RailType sel_railtype; ///< Type of rail tracks selected. #INVALID_RAILTYPE to show all. Scrollbar *vscroll[2]; @@ -144,12 +141,6 @@ if (eid == this->sel_engine[side]) selected_engine = eid; // The selected engine is still in the list } this->sel_engine[side] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore) - if (draw_left) { - EngList_Sort(list, &EngineNumberSorter); - } else { - _engine_sort_direction = this->descending_sort_order; - EngList_Sort(list, _engine_sort_functions[this->window_number][this->sort_criteria]); - } } /** Generate the lists */ @@ -164,6 +155,7 @@ if (this->reset_sel_engine && this->sel_engine[0] == INVALID_ENGINE && this->engines[0].Length() != 0) { this->sel_engine[0] = this->engines[0][0]; } + this->engines[0].RebuildDone(); } if (this->engines[1].NeedRebuild() || e != this->sel_engine[0]) { @@ -190,10 +182,9 @@ this->vscroll[1]->ScrollTowards(position); } } + this->engines[1].RebuildDone(); } /* Reset the flags about needed updates */ - this->engines[0].RebuildDone(); - this->engines[1].RebuildDone(); this->reset_sel_engine = false; } @@ -213,7 +204,11 @@ { this->sel_railtype = INVALID_RAILTYPE; this->replace_engines = true; // start with locomotives (all other vehicles will not read this bool) + + this->engines[0].SetSortFuncs(_engine_replace_list_sort_functions); this->engines[0].ForceRebuild(); + this->engines[1].SetSortFuncs(_engine_sort_functions[vehicletype]); + this->engines[1].SetListing(_engine_sort_last_sorting[vehicletype]); this->engines[1].ForceRebuild(); this->reset_sel_engine = true; this->details_height = ((vehicletype == VEH_TRAIN) ? 10 : 9) * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM; @@ -231,12 +226,16 @@ widget->SetLowered(this->show_hidden_engines); this->FinishInitNested(vehicletype); - this->sort_criteria = _engine_sort_last_criteria[vehicletype]; - this->descending_sort_order = _engine_sort_last_order[vehicletype]; this->owner = _local_company; this->sel_group = id_g; } + ~ReplaceVehicleWindow() + { + _engine_sort_last_sorting[this->window_number] = this->engines[1].GetListing(); + _engine_sort_show_hidden_engines[this->window_number] = this->show_hidden_engines; + } + virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) { switch (widget) { @@ -338,7 +337,7 @@ break; case WID_RV_SORT_DROPDOWN: - SetDParam(0, _engine_sort_listing[this->window_number][this->sort_criteria]); + SetDParam(0, _engine_sort_listing[this->window_number][this->engines[1].SortType()]); break; case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: { @@ -357,7 +356,7 @@ { switch (widget) { case WID_RV_SORT_ASCENDING_DESCENDING: - this->DrawSortButtonState(WID_RV_SORT_ASCENDING_DESCENDING, this->descending_sort_order ? SBS_DOWN : SBS_UP); + this->DrawSortButtonState(WID_RV_SORT_ASCENDING_DESCENDING, this->engines[1].IsDescSortOrder() ? SBS_DOWN : SBS_UP); break; case WID_RV_INFO_TAB: { @@ -397,6 +396,8 @@ virtual void OnPaint() { if (this->engines[0].NeedRebuild() || this->engines[1].NeedRebuild()) this->GenerateLists(); + this->engines[0].Sort(); + this->engines[1].Sort(); Company *c = Company::Get(_local_company); @@ -441,22 +442,19 @@ { switch (widget) { case WID_RV_SORT_ASCENDING_DESCENDING: - this->descending_sort_order ^= true; - _engine_sort_last_order[this->window_number] = this->descending_sort_order; - this->engines[1].ForceRebuild(); + this->engines[1].ToggleSortOrder(); this->SetDirty(); break; case WID_RV_SHOW_HIDDEN_ENGINES: this->show_hidden_engines ^= true; - _engine_sort_show_hidden_engines[this->window_number] = this->show_hidden_engines; this->engines[1].ForceRebuild(); this->SetWidgetLoweredState(widget, this->show_hidden_engines); this->SetDirty(); break; case WID_RV_SORT_DROPDOWN: - DisplayVehicleSortDropDown(this, static_cast(this->window_number), this->sort_criteria, WID_RV_SORT_DROPDOWN); + DisplayVehicleSortDropDown(this, static_cast(this->window_number), this->engines[1].SortType(), WID_RV_SORT_DROPDOWN); break; case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN: { @@ -520,10 +518,8 @@ { switch (widget) { case WID_RV_SORT_DROPDOWN: - if (this->sort_criteria != index) { - this->sort_criteria = index; - _engine_sort_last_criteria[this->window_number] = this->sort_criteria; - this->engines[1].ForceRebuild(); + if (this->engines[1].SortType() != index) { + this->engines[1].SetSortType(index); this->SetDirty(); } break; diff -r 27d7b11d4da3 -r bd3a434f73ba src/build_vehicle_gui.cpp --- a/src/build_vehicle_gui.cpp Sat Aug 19 11:51:37 2017 +0200 +++ b/src/build_vehicle_gui.cpp Sat Aug 19 11:51:48 2017 +0200 @@ -91,11 +91,9 @@ static const CargoID CF_ANY = CT_NO_REFIT; ///< Show all vehicles independent of carried cargo (i.e. no filtering) static const CargoID CF_NONE = CT_INVALID; ///< Show only vehicles which do not carry cargo (e.g. train engines) -bool _engine_sort_direction; ///< \c false = descending, \c true = ascending. -byte _engine_sort_last_criteria[] = {0, 0, 0, 0}; ///< Last set sort criteria, for each vehicle type. -bool _engine_sort_last_order[] = {false, false, false, false}; ///< Last set direction of the sort order, for each vehicle type. -bool _engine_sort_show_hidden_engines[] = {false, false, false, false}; ///< Last set 'show hidden engines' setting for each vehicle type. -static CargoID _engine_sort_last_cargo_criteria[] = {CF_ANY, CF_ANY, CF_ANY, CF_ANY}; ///< Last set filter criteria, for each vehicle type. +Listing _engine_sort_last_sorting[VEH_COMPANY_END] = { { 0, false }, { 0, false }, { 0, false }, { 0, false } }; +bool _engine_sort_show_hidden_engines[VEH_COMPANY_END] = {false, false, false, false}; ///< Last set 'show hidden engines' setting for each vehicle type. +static CargoID _engine_sort_last_cargo_criteria[VEH_COMPANY_END] = {CF_ANY, CF_ANY, CF_ANY, CF_ANY}; ///< Last set filter criteria, for each vehicle type. /** * Determines order of engines by engineID @@ -103,11 +101,9 @@ * @param *b second engine to compare * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal */ -static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b) +int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b) { - int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position; - - return _engine_sort_direction ? -r : r; + return Engine::Get(*a)->list_position - Engine::Get(*b)->list_position; } /** @@ -120,11 +116,11 @@ { const int va = Engine::Get(*a)->intro_date; const int vb = Engine::Get(*b)->intro_date; - const int r = va - vb; + int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (r == 0) r = EngineNumberSorter(a, b); + return r; } /** @@ -156,8 +152,8 @@ int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting). /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (r == 0) r = EngineNumberSorter(a, b); + return r; } /** @@ -170,11 +166,11 @@ { const int va = Engine::Get(*a)->reliability; const int vb = Engine::Get(*b)->reliability; - const int r = va - vb; + int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (r == 0) r = EngineNumberSorter(a, b); + return r; } /** @@ -190,8 +186,8 @@ int r = ClampToI32(va - vb); /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (r == 0) r = EngineNumberSorter(a, b); + return r; } /** @@ -207,8 +203,8 @@ int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (r == 0) r = EngineNumberSorter(a, b); + return r; } /** @@ -224,8 +220,8 @@ int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (r == 0) r = EngineNumberSorter(a, b); + return r; } /** @@ -241,8 +237,8 @@ int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (r == 0) r = EngineNumberSorter(a, b); + return r; } /** @@ -258,8 +254,8 @@ int r = ClampToI32(va - vb); /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (r == 0) r = EngineNumberSorter(a, b); + return r; } /** @@ -284,33 +280,13 @@ int r = ClampToI32(vb - va); /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (r == 0) r = EngineNumberSorter(a, b); + return r; } /* Train sorting functions */ /** - * Determines order of train engines by capacity - * @param *a first engine to compare - * @param *b second engine to compare - * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal - */ -static int CDECL TrainEngineCapacitySorter(const EngineID *a, const EngineID *b) -{ - const RailVehicleInfo *rvi_a = RailVehInfo(*a); - const RailVehicleInfo *rvi_b = RailVehInfo(*b); - - int va = GetTotalCapacityOfArticulatedParts(*a) * (rvi_a->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1); - int vb = GetTotalCapacityOfArticulatedParts(*b) * (rvi_b->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1); - int r = va - vb; - - /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; -} - -/** * Determines order of train engines by engine / wagon * @param *a first engine to compare * @param *b second engine to compare @@ -320,11 +296,39 @@ { int val_a = (RailVehInfo(*a)->railveh_type == RAILVEH_WAGON ? 1 : 0); int val_b = (RailVehInfo(*b)->railveh_type == RAILVEH_WAGON ? 1 : 0); - int r = val_a - val_b; + return val_a - val_b; +} + +/** @copydoc TrainEnginesThenWagonsSorter */ +template +static int CDECL TrainEnginesThenWagonsSorter(const EngineID *a, const EngineID *b) +{ + int r = TrainEnginesThenWagonsSorter(a, b); + if (r == 0) r = TfallbackSorter(a, b); + return r; +} + +/** + * Determines order of train engines by capacity + * @param *a first engine to compare + * @param *b second engine to compare + * @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal + */ +static int CDECL TrainEngineCapacitySorter(const EngineID *a, const EngineID *b) +{ + int r = TrainEnginesThenWagonsSorter(a, b); + if (r != 0) return r; + + const RailVehicleInfo *rvi_a = RailVehInfo(*a); + const RailVehicleInfo *rvi_b = RailVehInfo(*b); + + int va = GetTotalCapacityOfArticulatedParts(*a) * (rvi_a->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1); + int vb = GetTotalCapacityOfArticulatedParts(*b) * (rvi_b->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1); + r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + return r; } /* Road vehicle sorting functions */ @@ -342,8 +346,8 @@ int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (r == 0) r = EngineNumberSorter(a, b); + return r; } /* Ship vehicle sorting functions */ @@ -364,8 +368,8 @@ int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (r == 0) r = EngineNumberSorter(a, b); + return r; } /* Aircraft sorting functions */ @@ -386,16 +390,12 @@ int vb = e_b->GetDisplayDefaultCapacity(&mail_b); int r = va - vb; - if (r == 0) { - /* The planes have the same passenger capacity. Check mail capacity instead */ - r = mail_a - mail_b; + /* The planes have the same passenger capacity. Check mail capacity instead */ + if (r == 0) r = mail_a - mail_b; - if (r == 0) { - /* Use EngineID to sort instead since we want consistent sorting */ - return EngineNumberSorter(a, b); - } - } - return _engine_sort_direction ? -r : r; + /* Use EngineID to sort instead since we want consistent sorting */ + if (r == 0) r = EngineNumberSorter(a, b); + return r; } /** @@ -412,23 +412,23 @@ int r = r_a - r_b; /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (r == 0) r = EngineNumberSorter(a, b); + return r; } /** Sort functions for the vehicle sort criteria, for each vehicle type. */ -EngList_SortTypeFunction * const _engine_sort_functions[][11] = {{ +GUIEngineList::SortFunction * const _engine_sort_functions[VEH_COMPANY_END][11] = {{ /* Trains */ - &EngineNumberSorter, - &EngineCostSorter, - &EngineSpeedSorter, - &EnginePowerSorter, - &EngineTractiveEffortSorter, - &EngineIntroDateSorter, - &EngineNameSorter, - &EngineRunningCostSorter, - &EnginePowerVsRunningCostSorter, - &EngineReliabilitySorter, + &TrainEnginesThenWagonsSorter<&EngineNumberSorter>, + &TrainEnginesThenWagonsSorter<&EngineCostSorter>, + &TrainEnginesThenWagonsSorter<&EngineSpeedSorter>, + &TrainEnginesThenWagonsSorter<&EnginePowerSorter>, + &TrainEnginesThenWagonsSorter<&EngineTractiveEffortSorter>, + &TrainEnginesThenWagonsSorter<&EngineIntroDateSorter>, + &TrainEnginesThenWagonsSorter<&EngineNameSorter>, + &TrainEnginesThenWagonsSorter<&EngineRunningCostSorter>, + &TrainEnginesThenWagonsSorter<&EnginePowerVsRunningCostSorter>, + &TrainEnginesThenWagonsSorter<&EngineReliabilitySorter>, &TrainEngineCapacitySorter, }, { /* Road vehicles */ @@ -467,7 +467,7 @@ }}; /** Dropdown menu strings for the vehicle sort criteria. */ -const StringID _engine_sort_listing[][12] = {{ +const StringID _engine_sort_listing[VEH_COMPANY_END][12] = {{ /* Trains */ STR_SORT_BY_ENGINE_ID, STR_SORT_BY_COST, @@ -976,8 +976,6 @@ RailTypeByte railtype; ///< Rail type to show, or #RAILTYPE_END. RoadTypes roadtypes; ///< Road type to show, or #ROADTYPES_ALL. } filter; ///< Filter to apply. - bool descending_sort_order; ///< Sort direction, @see _engine_sort_direction - byte sort_criteria; ///< Current sort criterium. bool show_hidden_engines; ///< State of the 'show hidden engines' button. bool listview_mode; ///< If set, only display the available vehicles and do not show a 'build' button. EngineID sel_engine; ///< Currently selected engine, or #INVALID_ENGINE @@ -995,10 +993,14 @@ this->window_number = tile == INVALID_TILE ? (int)type : tile; this->sel_engine = INVALID_ENGINE; + this->cargo_filter_criteria = INVALID_CARGO; // init from _engine_sort_last_cargo_criteria - this->sort_criteria = _engine_sort_last_criteria[type]; - this->descending_sort_order = _engine_sort_last_order[type]; - this->show_hidden_engines = _engine_sort_show_hidden_engines[type]; + this->eng_list.SetSortFuncs(_engine_sort_functions[type]); + this->eng_list.SetFilterFuncs(_filter_funcs); + this->eng_list.SetListing(_engine_sort_last_sorting[type]); + this->show_hidden_engines = _engine_sort_show_hidden_engines[type]; + this->eng_list.SetFilterState(true); + this->eng_list.ForceRebuild(); switch (type) { default: NOT_REACHED(); @@ -1049,16 +1051,22 @@ this->FinishInitNested(tile == INVALID_TILE ? (int)type : tile); this->owner = (tile != INVALID_TILE) ? GetTileOwner(tile) : _local_company; + } - 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]; + ~BuildVehicleWindow() + { + _engine_sort_last_sorting[this->vehicle_type] = this->eng_list.GetListing(); + _engine_sort_show_hidden_engines[this->vehicle_type] = this->show_hidden_engines; + _engine_sort_last_cargo_criteria[this->vehicle_type] = this->cargo_filter[this->cargo_filter_criteria]; } /** Populate the filter list and set the cargo filter criteria. */ void SetCargoFilterArray() { + CargoID last_criteria = this->cargo_filter_criteria == INVALID_CARGO ? + _engine_sort_last_cargo_criteria[this->vehicle_type] : + this->cargo_filter[this->cargo_filter_criteria]; + uint filter_items = 0; /* Add item for disabling filtering. */ @@ -1090,14 +1098,11 @@ /* Find the last cargo filter criteria. */ for (uint i = 0; i < filter_items; i++) { - if (this->cargo_filter[i] == _engine_sort_last_cargo_criteria[this->vehicle_type]) { + if (this->cargo_filter[i] == last_criteria) { 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() @@ -1117,19 +1122,6 @@ } } - /** Filter the engine list against the currently selected cargo filter */ - void SortEngineList() - { - _engine_sort_direction = this->descending_sort_order; - EngList_Sort(&this->eng_list, _engine_sort_functions[this->vehicle_type][this->sort_criteria]); - - if (this->vehicle_type == VEH_TRAIN) { - /* make engines first, then wagons */ - _engine_sort_direction = false; - EngList_Sort(&this->eng_list, TrainEnginesThenWagonsSorter); - } - } - /* Figure out what train EngineIDs to put in the list */ void GenerateBuildTrainList() { @@ -1219,7 +1211,6 @@ } this->FilterEngineList(); - this->SortEngineList(); this->eng_list.Compact(); this->eng_list.RebuildDone(); } @@ -1228,15 +1219,12 @@ { switch (widget) { case WID_BV_SORT_ASCENDING_DESCENDING: - this->descending_sort_order ^= true; - _engine_sort_last_order[this->vehicle_type] = this->descending_sort_order; - this->eng_list.ForceRebuild(); + this->eng_list.ToggleSortOrder(); this->SetDirty(); break; case WID_BV_SHOW_HIDDEN_ENGINES: this->show_hidden_engines ^= true; - _engine_sort_show_hidden_engines[this->vehicle_type] = this->show_hidden_engines; this->eng_list.ForceRebuild(); this->SetWidgetLoweredState(widget, this->show_hidden_engines); this->SetDirty(); @@ -1256,7 +1244,7 @@ } case WID_BV_SORT_DROPDOWN: // Select sorting criteria dropdown menu - DisplayVehicleSortDropDown(this, this->vehicle_type, this->sort_criteria, WID_BV_SORT_DROPDOWN); + DisplayVehicleSortDropDown(this, this->vehicle_type, this->eng_list.SortType(), WID_BV_SORT_DROPDOWN); break; case WID_BV_CARGO_FILTER_DROPDOWN: // Select cargo filtering criteria dropdown menu @@ -1303,9 +1291,8 @@ /* When switching to original acceleration model for road vehicles, clear the selected sort criteria if it is not available now. */ if (this->vehicle_type == VEH_ROAD && _settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL && - this->sort_criteria > 7) { - this->sort_criteria = 0; - _engine_sort_last_criteria[VEH_ROAD] = 0; + this->eng_list.SortType() > 7) { + this->eng_list.SetSortType(0); } this->eng_list.ForceRebuild(); } @@ -1323,7 +1310,7 @@ break; case WID_BV_SORT_DROPDOWN: - SetDParam(0, _engine_sort_listing[this->vehicle_type][this->sort_criteria]); + SetDParam(0, _engine_sort_listing[this->vehicle_type][this->eng_list.SortType()]); break; case WID_BV_CARGO_FILTER_DROPDOWN: @@ -1380,7 +1367,7 @@ break; case WID_BV_SORT_ASCENDING_DESCENDING: - this->DrawSortButtonState(WID_BV_SORT_ASCENDING_DESCENDING, this->descending_sort_order ? SBS_DOWN : SBS_UP); + this->DrawSortButtonState(WID_BV_SORT_ASCENDING_DESCENDING, this->eng_list.IsDescSortOrder() ? SBS_DOWN : SBS_UP); break; } } @@ -1388,6 +1375,7 @@ virtual void OnPaint() { this->GenerateBuildList(); + this->eng_list.Sort(); this->vscroll->SetCount(this->eng_list.Length()); this->SetWidgetDisabledState(WID_BV_SHOW_HIDE, this->sel_engine == INVALID_ENGINE); @@ -1424,19 +1412,12 @@ { switch (widget) { case WID_BV_SORT_DROPDOWN: - if (this->sort_criteria != index) { - this->sort_criteria = index; - _engine_sort_last_criteria[this->vehicle_type] = this->sort_criteria; - this->eng_list.ForceRebuild(); - } + this->eng_list.SetSortType(index); break; case WID_BV_CARGO_FILTER_DROPDOWN: // Select a cargo filter criteria if (this->cargo_filter_criteria != index) { this->cargo_filter_criteria = index; - _engine_sort_last_cargo_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; diff -r 27d7b11d4da3 -r bd3a434f73ba src/engine_gui.cpp --- a/src/engine_gui.cpp Sat Aug 19 11:51:37 2017 +0200 +++ b/src/engine_gui.cpp Sat Aug 19 11:51:48 2017 +0200 @@ -317,17 +317,3 @@ default: NOT_REACHED(); } } - -/** - * Sort all items using quick sort and given 'CompareItems' function - * @param el list to be sorted - * @param compare function for evaluation of the quicksort - */ -void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare) -{ - uint size = el->Length(); - /* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems) - * generally, do not sort if there are less than 2 items */ - if (size < 2) return; - QSortT(el->Begin(), size, compare); -} diff -r 27d7b11d4da3 -r bd3a434f73ba src/engine_gui.h --- a/src/engine_gui.h Sat Aug 19 11:51:37 2017 +0200 +++ b/src/engine_gui.h Sat Aug 19 11:51:48 2017 +0200 @@ -19,9 +19,6 @@ typedef GUIList GUIEngineList; -typedef int CDECL EngList_SortTypeFunction(const EngineID*, const EngineID*); ///< argument type for #EngList_Sort. -void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare); - StringID GetEngineCategoryName(EngineID engine); StringID GetEngineInfoString(EngineID engine); @@ -31,12 +28,10 @@ void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type); void DrawAircraftEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type); -extern bool _engine_sort_direction; -extern byte _engine_sort_last_criteria[]; -extern bool _engine_sort_last_order[]; -extern bool _engine_sort_show_hidden_engines[]; -extern const StringID _engine_sort_listing[][12]; -extern EngList_SortTypeFunction * const _engine_sort_functions[][11]; +extern Listing _engine_sort_last_sorting[VEH_COMPANY_END]; +extern bool _engine_sort_show_hidden_engines[VEH_COMPANY_END]; +extern const StringID _engine_sort_listing[VEH_COMPANY_END][12]; +extern GUIEngineList::SortFunction * const _engine_sort_functions[VEH_COMPANY_END][11]; uint GetEngineListHeight(VehicleType type); void DisplayVehicleSortDropDown(Window *w, VehicleType vehicle_type, int selected, int button);