1
0
Fork 0

Codechange: inline sorter name definitions and use proper static accessor instead of (implying) this->

pull/12590/head
Rubidium 2024-04-28 14:56:52 +02:00 committed by rubidium42
parent ef55cc7979
commit ad50c4f298
4 changed files with 61 additions and 75 deletions

View File

@ -75,7 +75,12 @@ private:
static Listing last_sorting; ///< Last setting of the sort. static Listing last_sorting; ///< Last setting of the sort.
/* Constants for sorting the bridges */ /* Constants for sorting the bridges */
static const StringID sorter_names[]; static inline const StringID sorter_names[] = {
STR_SORT_BY_NUMBER,
STR_SORT_BY_COST,
STR_SORT_BY_MAX_SPEED,
INVALID_STRING_ID
};
static const std::initializer_list<GUIBridgeList::SortFunction * const> sorter_funcs; static const std::initializer_list<GUIBridgeList::SortFunction * const> sorter_funcs;
/* Internal variables */ /* Internal variables */
@ -122,7 +127,7 @@ private:
this->bridges.Sort(); this->bridges.Sort();
/* Display the current sort variant */ /* Display the current sort variant */
this->GetWidget<NWidgetCore>(WID_BBS_DROPDOWN_CRITERIA)->widget_data = this->sorter_names[this->bridges.SortType()]; this->GetWidget<NWidgetCore>(WID_BBS_DROPDOWN_CRITERIA)->widget_data = BuildBridgeWindow::sorter_names[this->bridges.SortType()];
/* Set the modified widgets dirty */ /* Set the modified widgets dirty */
this->SetWidgetDirty(WID_BBS_DROPDOWN_CRITERIA); this->SetWidgetDirty(WID_BBS_DROPDOWN_CRITERIA);
@ -161,8 +166,8 @@ public:
this->FinishInitNested(transport_type); // Initializes 'this->icon_width'. this->FinishInitNested(transport_type); // Initializes 'this->icon_width'.
this->parent = FindWindowById(WC_BUILD_TOOLBAR, transport_type); this->parent = FindWindowById(WC_BUILD_TOOLBAR, transport_type);
this->bridges.SetListing(this->last_sorting); this->bridges.SetListing(BuildBridgeWindow::last_sorting);
this->bridges.SetSortFuncs(this->sorter_funcs); this->bridges.SetSortFuncs(BuildBridgeWindow::sorter_funcs);
this->bridges.NeedResort(); this->bridges.NeedResort();
this->SortBridgeList(); this->SortBridgeList();
@ -171,7 +176,7 @@ public:
~BuildBridgeWindow() ~BuildBridgeWindow()
{ {
this->last_sorting = this->bridges.GetListing(); BuildBridgeWindow::last_sorting = this->bridges.GetListing();
} }
void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
@ -186,7 +191,7 @@ public:
} }
case WID_BBS_DROPDOWN_CRITERIA: { case WID_BBS_DROPDOWN_CRITERIA: {
Dimension d = {0, 0}; Dimension d = {0, 0};
for (const StringID *str = this->sorter_names; *str != INVALID_STRING_ID; str++) { for (const StringID *str = BuildBridgeWindow::sorter_names; *str != INVALID_STRING_ID; str++) {
d = maxdim(d, GetStringBoundingBox(*str)); d = maxdim(d, GetStringBoundingBox(*str));
} }
d.width += padding.width; d.width += padding.width;
@ -274,7 +279,7 @@ public:
break; break;
case WID_BBS_DROPDOWN_CRITERIA: case WID_BBS_DROPDOWN_CRITERIA:
ShowDropDownMenu(this, this->sorter_names, this->bridges.SortType(), WID_BBS_DROPDOWN_CRITERIA, 0, 0); ShowDropDownMenu(this, BuildBridgeWindow::sorter_names, this->bridges.SortType(), WID_BBS_DROPDOWN_CRITERIA, 0, 0);
break; break;
} }
} }
@ -304,14 +309,6 @@ const std::initializer_list<GUIBridgeList::SortFunction * const> BuildBridgeWind
&BridgeSpeedSorter &BridgeSpeedSorter
}; };
/** Names of the sorting functions. */
const StringID BuildBridgeWindow::sorter_names[] = {
STR_SORT_BY_NUMBER,
STR_SORT_BY_COST,
STR_SORT_BY_MAX_SPEED,
INVALID_STRING_ID
};
/** Widgets of the bridge gui. */ /** Widgets of the bridge gui. */
static constexpr NWidgetPart _nested_build_bridge_widgets[] = { static constexpr NWidgetPart _nested_build_bridge_widgets[] = {
/* Header */ /* Header */

View File

@ -1325,7 +1325,13 @@ protected:
static Listing last_sorting; static Listing last_sorting;
/* Constants for sorting industries */ /* Constants for sorting industries */
static const StringID sorter_names[]; static inline const StringID sorter_names[] = {
STR_SORT_BY_NAME,
STR_SORT_BY_TYPE,
STR_SORT_BY_PRODUCTION,
STR_SORT_BY_TRANSPORTED,
INVALID_STRING_ID
};
static const std::initializer_list<GUIIndustryList::SortFunction * const> sorter_funcs; static const std::initializer_list<GUIIndustryList::SortFunction * const> sorter_funcs;
GUIIndustryList industries{IndustryDirectoryWindow::produced_cargo_filter}; GUIIndustryList industries{IndustryDirectoryWindow::produced_cargo_filter};
@ -1909,15 +1915,6 @@ const std::initializer_list<GUIIndustryList::SortFunction * const> IndustryDirec
&IndustryTransportedCargoSorter &IndustryTransportedCargoSorter
}; };
/* Names of the sorting functions */
const StringID IndustryDirectoryWindow::sorter_names[] = {
STR_SORT_BY_NAME,
STR_SORT_BY_TYPE,
STR_SORT_BY_PRODUCTION,
STR_SORT_BY_TRANSPORTED,
INVALID_STRING_ID
};
CargoID IndustryDirectoryWindow::produced_cargo_filter = CargoFilterCriteria::CF_ANY; CargoID IndustryDirectoryWindow::produced_cargo_filter = CargoFilterCriteria::CF_ANY;

View File

@ -244,7 +244,15 @@ protected:
}; };
/* Constants for sorting stations */ /* Constants for sorting stations */
static const StringID sorter_names[]; static inline const StringID sorter_names[] = {
STR_SORT_BY_NAME,
STR_SORT_BY_FACILITY,
STR_SORT_BY_WAITING_TOTAL,
STR_SORT_BY_WAITING_AVAILABLE,
STR_SORT_BY_RATING_MAX,
STR_SORT_BY_RATING_MIN,
INVALID_STRING_ID
};
static const std::initializer_list<GUIStationList::SortFunction * const> sorter_funcs; static const std::initializer_list<GUIStationList::SortFunction * const> sorter_funcs;
FilterState filter; FilterState filter;
@ -400,7 +408,7 @@ public:
if (HasBit(this->filter.facilities, i)) this->LowerWidget(i + WID_STL_TRAIN); if (HasBit(this->filter.facilities, i)) this->LowerWidget(i + WID_STL_TRAIN);
} }
this->GetWidget<NWidgetCore>(WID_STL_SORTDROPBTN)->widget_data = this->sorter_names[this->stations.SortType()]; this->GetWidget<NWidgetCore>(WID_STL_SORTDROPBTN)->widget_data = CompanyStationsWindow::sorter_names[this->stations.SortType()];
} }
~CompanyStationsWindow() ~CompanyStationsWindow()
@ -627,7 +635,7 @@ public:
break; break;
case WID_STL_SORTDROPBTN: // select sorting criteria dropdown menu case WID_STL_SORTDROPBTN: // select sorting criteria dropdown menu
ShowDropDownMenu(this, this->sorter_names, this->stations.SortType(), WID_STL_SORTDROPBTN, 0, 0); ShowDropDownMenu(this, CompanyStationsWindow::sorter_names, this->stations.SortType(), WID_STL_SORTDROPBTN, 0, 0);
break; break;
case WID_STL_CARGODROPDOWN: case WID_STL_CARGODROPDOWN:
@ -644,7 +652,7 @@ public:
this->stations.SetSortType(index); this->stations.SetSortType(index);
/* Display the current sort variant */ /* Display the current sort variant */
this->GetWidget<NWidgetCore>(WID_STL_SORTDROPBTN)->widget_data = this->sorter_names[this->stations.SortType()]; this->GetWidget<NWidgetCore>(WID_STL_SORTDROPBTN)->widget_data = CompanyStationsWindow::sorter_names[this->stations.SortType()];
this->SetDirty(); this->SetDirty();
} }
@ -728,17 +736,6 @@ const std::initializer_list<GUIStationList::SortFunction * const> CompanyStation
&StationRatingMinSorter &StationRatingMinSorter
}; };
/* Names of the sorting functions */
const StringID CompanyStationsWindow::sorter_names[] = {
STR_SORT_BY_NAME,
STR_SORT_BY_FACILITY,
STR_SORT_BY_WAITING_TOTAL,
STR_SORT_BY_WAITING_AVAILABLE,
STR_SORT_BY_RATING_MAX,
STR_SORT_BY_RATING_MIN,
INVALID_STRING_ID
};
static constexpr NWidgetPart _nested_company_stations_widgets[] = { static constexpr NWidgetPart _nested_company_stations_widgets[] = {
NWidget(NWID_HORIZONTAL), NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_GREY), NWidget(WWT_CLOSEBOX, COLOUR_GREY),
@ -1291,8 +1288,24 @@ struct StationViewWindow : public Window {
ALH_ACCEPTS = 3, ///< Height of the accepted cargo view. ALH_ACCEPTS = 3, ///< Height of the accepted cargo view.
}; };
static const StringID _sort_names[]; ///< Names of the sorting options in the dropdown. /** Names of the sorting options in the dropdown. */
static const StringID _group_names[]; ///< Names of the grouping options in the dropdown. static inline const StringID sort_names[] = {
STR_STATION_VIEW_WAITING_STATION,
STR_STATION_VIEW_WAITING_AMOUNT,
STR_STATION_VIEW_PLANNED_STATION,
STR_STATION_VIEW_PLANNED_AMOUNT,
INVALID_STRING_ID
};
/** Names of the grouping options in the dropdown. */
static inline const StringID group_names[] = {
STR_STATION_VIEW_GROUP_S_V_D,
STR_STATION_VIEW_GROUP_S_D_V,
STR_STATION_VIEW_GROUP_V_S_D,
STR_STATION_VIEW_GROUP_V_D_S,
STR_STATION_VIEW_GROUP_D_S_V,
STR_STATION_VIEW_GROUP_D_V_S,
INVALID_STRING_ID
};
/** /**
* Sort types of the different 'columns'. * Sort types of the different 'columns'.
@ -1977,14 +1990,14 @@ struct StationViewWindow : public Window {
* sorting criteria for columns 1, 2, and 3. Column 0 is always * sorting criteria for columns 1, 2, and 3. Column 0 is always
* sorted by cargo ID. The others can theoretically be sorted * sorted by cargo ID. The others can theoretically be sorted
* by different things but there is no UI for that. */ * by different things but there is no UI for that. */
ShowDropDownMenu(this, _sort_names, ShowDropDownMenu(this, StationViewWindow::sort_names,
this->current_mode * 2 + (this->sortings[1] == CargoSortType::Count ? 1 : 0), this->current_mode * 2 + (this->sortings[1] == CargoSortType::Count ? 1 : 0),
WID_SV_SORT_BY, 0, 0); WID_SV_SORT_BY, 0, 0);
break; break;
} }
case WID_SV_GROUP_BY: { case WID_SV_GROUP_BY: {
ShowDropDownMenu(this, _group_names, this->grouping_index, WID_SV_GROUP_BY, 0, 0); ShowDropDownMenu(this, StationViewWindow::group_names, this->grouping_index, WID_SV_GROUP_BY, 0, 0);
break; break;
} }
@ -2015,7 +2028,7 @@ struct StationViewWindow : public Window {
void SelectSortBy(int index) void SelectSortBy(int index)
{ {
_settings_client.gui.station_gui_sort_by = index; _settings_client.gui.station_gui_sort_by = index;
switch (_sort_names[index]) { switch (StationViewWindow::sort_names[index]) {
case STR_STATION_VIEW_WAITING_STATION: case STR_STATION_VIEW_WAITING_STATION:
this->current_mode = MODE_WAITING; this->current_mode = MODE_WAITING;
this->sortings[1] = this->sortings[2] = this->sortings[3] = CargoSortType::AsGrouping; this->sortings[1] = this->sortings[2] = this->sortings[3] = CargoSortType::AsGrouping;
@ -2036,7 +2049,7 @@ struct StationViewWindow : public Window {
NOT_REACHED(); NOT_REACHED();
} }
/* Display the current sort variant */ /* Display the current sort variant */
this->GetWidget<NWidgetCore>(WID_SV_SORT_BY)->widget_data = _sort_names[index]; this->GetWidget<NWidgetCore>(WID_SV_SORT_BY)->widget_data = StationViewWindow::sort_names[index];
this->SetDirty(); this->SetDirty();
} }
@ -2048,8 +2061,8 @@ struct StationViewWindow : public Window {
{ {
this->grouping_index = index; this->grouping_index = index;
_settings_client.gui.station_gui_group_order = index; _settings_client.gui.station_gui_group_order = index;
this->GetWidget<NWidgetCore>(WID_SV_GROUP_BY)->widget_data = _group_names[index]; this->GetWidget<NWidgetCore>(WID_SV_GROUP_BY)->widget_data = StationViewWindow::group_names[index];
switch (_group_names[index]) { switch (StationViewWindow::group_names[index]) {
case STR_STATION_VIEW_GROUP_S_V_D: case STR_STATION_VIEW_GROUP_S_V_D:
this->groupings[1] = GR_SOURCE; this->groupings[1] = GR_SOURCE;
this->groupings[2] = GR_NEXT; this->groupings[2] = GR_NEXT;
@ -2122,24 +2135,6 @@ struct StationViewWindow : public Window {
} }
}; };
const StringID StationViewWindow::_sort_names[] = {
STR_STATION_VIEW_WAITING_STATION,
STR_STATION_VIEW_WAITING_AMOUNT,
STR_STATION_VIEW_PLANNED_STATION,
STR_STATION_VIEW_PLANNED_AMOUNT,
INVALID_STRING_ID
};
const StringID StationViewWindow::_group_names[] = {
STR_STATION_VIEW_GROUP_S_V_D,
STR_STATION_VIEW_GROUP_S_D_V,
STR_STATION_VIEW_GROUP_V_S_D,
STR_STATION_VIEW_GROUP_V_D_S,
STR_STATION_VIEW_GROUP_D_S_V,
STR_STATION_VIEW_GROUP_D_V_S,
INVALID_STRING_ID
};
static WindowDesc _station_view_desc( static WindowDesc _station_view_desc(
WDP_AUTO, "view_station", 249, 117, WDP_AUTO, "view_station", 249, 117,
WC_STATION_VIEW, WC_NONE, WC_STATION_VIEW, WC_NONE,

View File

@ -720,7 +720,12 @@ private:
static Listing last_sorting; static Listing last_sorting;
/* Constants for sorting towns */ /* Constants for sorting towns */
static const StringID sorter_names[]; static inline const StringID sorter_names[] = {
STR_SORT_BY_NAME,
STR_SORT_BY_POPULATION,
STR_SORT_BY_RATING,
INVALID_STRING_ID
};
static const std::initializer_list<GUITownList::SortFunction * const> sorter_funcs; static const std::initializer_list<GUITownList::SortFunction * const> sorter_funcs;
StringFilter string_filter; ///< Filter for towns StringFilter string_filter; ///< Filter for towns
@ -1045,14 +1050,6 @@ public:
Listing TownDirectoryWindow::last_sorting = {false, 0}; Listing TownDirectoryWindow::last_sorting = {false, 0};
/** Names of the sorting functions. */
const StringID TownDirectoryWindow::sorter_names[] = {
STR_SORT_BY_NAME,
STR_SORT_BY_POPULATION,
STR_SORT_BY_RATING,
INVALID_STRING_ID
};
/** Available town directory sorting functions. */ /** Available town directory sorting functions. */
const std::initializer_list<GUITownList::SortFunction * const> TownDirectoryWindow::sorter_funcs = { const std::initializer_list<GUITownList::SortFunction * const> TownDirectoryWindow::sorter_funcs = {
&TownNameSorter, &TownNameSorter,