From 2d636266f59e8f5e88c3460b9adb8889442108d8 Mon Sep 17 00:00:00 2001 From: frosch Date: Fri, 30 Oct 2015 17:24:30 +0000 Subject: [PATCH] (svn r27427) -Fix: Use the NewGRF railtype sorting order in the infrastructure window. --- src/company_gui.cpp | 6 ++++-- src/rail.h | 9 +++++++++ src/rail_cmd.cpp | 21 +++++++++++++++++++++ src/rail_gui.cpp | 17 ++--------------- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/company_gui.cpp b/src/company_gui.cpp index 1343acb91b..0be6679583 100644 --- a/src/company_gui.cpp +++ b/src/company_gui.cpp @@ -1764,7 +1764,8 @@ struct CompanyInfrastructureWindow : Window if (this->railtypes != RAILTYPES_NONE) { /* Draw name of each valid railtype. */ - for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) { + RailType rt; + FOR_ALL_SORTED_RAILTYPES(rt) { if (HasBit(this->railtypes, rt)) { SetDParam(0, GetRailTypeInfo(rt)->strings.name); DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_WHITE_STRING); @@ -1781,7 +1782,8 @@ struct CompanyInfrastructureWindow : Window case WID_CI_RAIL_COUNT: { /* Draw infrastructure count for each valid railtype. */ uint32 rail_total = c->infrastructure.GetRailTotal(); - for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) { + RailType rt; + FOR_ALL_SORTED_RAILTYPES(rt) { if (HasBit(this->railtypes, rt)) { this->DrawCountLine(r, y, c->infrastructure.rail[rt], RailMaintenanceCost(rt, c->infrastructure.rail[rt], rail_total)); } diff --git a/src/rail.h b/src/rail.h index 539a162b8e..320d24a9a0 100644 --- a/src/rail.h +++ b/src/rail.h @@ -431,4 +431,13 @@ void ResetRailTypes(); void InitRailTypes(); RailType AllocateRailType(RailTypeLabel label); +extern RailType _sorted_railtypes[RAILTYPE_END]; +extern uint8 _sorted_railtypes_size; + +/** + * Loop header for iterating over railtypes, sorted by sortorder. + * @param var Railtype. + */ +#define FOR_ALL_SORTED_RAILTYPES(var) for (uint8 index = 0; index < _sorted_railtypes_size && (var = _sorted_railtypes[index], true) ; index++) + #endif /* RAIL_H */ diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp index 743e6e193c..2010f9b305 100644 --- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -44,6 +44,8 @@ typedef SmallVector TrainList; RailtypeInfo _railtypes[RAILTYPE_END]; +RailType _sorted_railtypes[RAILTYPE_END]; +uint8 _sorted_railtypes_size; assert_compile(sizeof(_original_railtypes) <= sizeof(_railtypes)); @@ -109,6 +111,17 @@ void ResolveRailTypeGUISprites(RailtypeInfo *rti) } } +/** + * Compare railtypes based on their sorting order. + * @param first The railtype to compare to. + * @param second The railtype to compare. + * @return True iff the first should be sorted before the second. + */ +static int CDECL CompareRailTypes(const RailType *first, const RailType *second) +{ + return GetRailTypeInfo(*first)->sorting_order - GetRailTypeInfo(*second)->sorting_order; +} + /** * Resolve sprites of custom rail types */ @@ -118,6 +131,14 @@ void InitRailTypes() RailtypeInfo *rti = &_railtypes[rt]; ResolveRailTypeGUISprites(rti); } + + _sorted_railtypes_size = 0; + for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) { + if (_railtypes[rt].label != 0) { + _sorted_railtypes[_sorted_railtypes_size++] = rt; + } + } + QSortT(_sorted_railtypes, _sorted_railtypes_size, CompareRailTypes); } /** diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index a8c2fc6b33..a48abd29cc 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1978,17 +1978,6 @@ void InitializeRailGUI() ResetSignalVariant(); } -/** - * Compare railtypes based on their sorting order. - * @param first The railtype to compare to. - * @param second The railtype to compare. - * @return True iff the first should be sorted before the second. - */ -static int CDECL CompareRailTypes(const DropDownListItem * const *first, const DropDownListItem * const *second) -{ - return GetRailTypeInfo((RailType)(*first)->result)->sorting_order - GetRailTypeInfo((RailType)(*second)->result)->sorting_order; -} - /** * Create a drop down list for all the rail types of the local company. * @param for_replacement Whether this list is for the replacement window. @@ -2011,13 +2000,12 @@ DropDownList *GetRailTypeDropDownList(bool for_replacement) const Company *c = Company::Get(_local_company); DropDownList *list = new DropDownList(); - for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) { + RailType rt; + FOR_ALL_SORTED_RAILTYPES(rt) { /* If it's not used ever, don't show it to the user. */ if (!HasBit(used_railtypes, rt)) continue; const RailtypeInfo *rti = GetRailTypeInfo(rt); - /* Skip rail type if it has no label */ - if (rti->label == 0) continue; StringID str = for_replacement ? rti->strings.replace_text : (rti->max_speed > 0 ? STR_TOOLBAR_RAILTYPE_VELOCITY : STR_JUST_STRING); DropDownListParamStringItem *item = new DropDownListParamStringItem(str, rt, !HasBit(c->avail_railtypes, rt)); @@ -2025,6 +2013,5 @@ DropDownList *GetRailTypeDropDownList(bool for_replacement) item->SetParam(1, rti->max_speed); *list->Append() = item; } - QSortT(list->Begin(), list->Length(), CompareRailTypes); return list; }