From ad50c4f2983b454bf5cef7546f4c4d5ec3240295 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 28 Apr 2024 14:56:52 +0200 Subject: [PATCH] Codechange: inline sorter name definitions and use proper static accessor instead of (implying) this-> --- src/bridge_gui.cpp | 27 +++++++--------- src/industry_gui.cpp | 17 ++++------ src/station_gui.cpp | 77 +++++++++++++++++++++----------------------- src/town_gui.cpp | 15 ++++----- 4 files changed, 61 insertions(+), 75 deletions(-) diff --git a/src/bridge_gui.cpp b/src/bridge_gui.cpp index 6510355f8c..c983433b7f 100644 --- a/src/bridge_gui.cpp +++ b/src/bridge_gui.cpp @@ -75,7 +75,12 @@ private: static Listing last_sorting; ///< Last setting of the sort. /* 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 sorter_funcs; /* Internal variables */ @@ -122,7 +127,7 @@ private: this->bridges.Sort(); /* Display the current sort variant */ - this->GetWidget(WID_BBS_DROPDOWN_CRITERIA)->widget_data = this->sorter_names[this->bridges.SortType()]; + this->GetWidget(WID_BBS_DROPDOWN_CRITERIA)->widget_data = BuildBridgeWindow::sorter_names[this->bridges.SortType()]; /* Set the modified widgets dirty */ this->SetWidgetDirty(WID_BBS_DROPDOWN_CRITERIA); @@ -161,8 +166,8 @@ public: this->FinishInitNested(transport_type); // Initializes 'this->icon_width'. this->parent = FindWindowById(WC_BUILD_TOOLBAR, transport_type); - this->bridges.SetListing(this->last_sorting); - this->bridges.SetSortFuncs(this->sorter_funcs); + this->bridges.SetListing(BuildBridgeWindow::last_sorting); + this->bridges.SetSortFuncs(BuildBridgeWindow::sorter_funcs); this->bridges.NeedResort(); this->SortBridgeList(); @@ -171,7 +176,7 @@ public: ~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 @@ -186,7 +191,7 @@ public: } case WID_BBS_DROPDOWN_CRITERIA: { 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.width += padding.width; @@ -274,7 +279,7 @@ public: break; 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; } } @@ -304,14 +309,6 @@ const std::initializer_list BuildBridgeWind &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. */ static constexpr NWidgetPart _nested_build_bridge_widgets[] = { /* Header */ diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 25f48a3ed5..ebde5b921d 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1325,7 +1325,13 @@ protected: static Listing last_sorting; /* 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 sorter_funcs; GUIIndustryList industries{IndustryDirectoryWindow::produced_cargo_filter}; @@ -1909,15 +1915,6 @@ const std::initializer_list IndustryDirec &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; diff --git a/src/station_gui.cpp b/src/station_gui.cpp index 241a512b12..d3b04b2749 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -244,7 +244,15 @@ protected: }; /* 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 sorter_funcs; FilterState filter; @@ -400,7 +408,7 @@ public: if (HasBit(this->filter.facilities, i)) this->LowerWidget(i + WID_STL_TRAIN); } - this->GetWidget(WID_STL_SORTDROPBTN)->widget_data = this->sorter_names[this->stations.SortType()]; + this->GetWidget(WID_STL_SORTDROPBTN)->widget_data = CompanyStationsWindow::sorter_names[this->stations.SortType()]; } ~CompanyStationsWindow() @@ -627,7 +635,7 @@ public: break; 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; case WID_STL_CARGODROPDOWN: @@ -644,7 +652,7 @@ public: this->stations.SetSortType(index); /* Display the current sort variant */ - this->GetWidget(WID_STL_SORTDROPBTN)->widget_data = this->sorter_names[this->stations.SortType()]; + this->GetWidget(WID_STL_SORTDROPBTN)->widget_data = CompanyStationsWindow::sorter_names[this->stations.SortType()]; this->SetDirty(); } @@ -728,17 +736,6 @@ const std::initializer_list CompanyStation &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[] = { NWidget(NWID_HORIZONTAL), NWidget(WWT_CLOSEBOX, COLOUR_GREY), @@ -1291,8 +1288,24 @@ struct StationViewWindow : public Window { ALH_ACCEPTS = 3, ///< Height of the accepted cargo view. }; - static const StringID _sort_names[]; ///< Names of the sorting options in the dropdown. - static const StringID _group_names[]; ///< Names of the grouping options in the dropdown. + /** Names of the sorting 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'. @@ -1977,14 +1990,14 @@ struct StationViewWindow : public Window { * sorting criteria for columns 1, 2, and 3. Column 0 is always * sorted by cargo ID. The others can theoretically be sorted * 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), WID_SV_SORT_BY, 0, 0); break; } 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; } @@ -2015,7 +2028,7 @@ struct StationViewWindow : public Window { void SelectSortBy(int index) { _settings_client.gui.station_gui_sort_by = index; - switch (_sort_names[index]) { + switch (StationViewWindow::sort_names[index]) { case STR_STATION_VIEW_WAITING_STATION: this->current_mode = MODE_WAITING; this->sortings[1] = this->sortings[2] = this->sortings[3] = CargoSortType::AsGrouping; @@ -2036,7 +2049,7 @@ struct StationViewWindow : public Window { NOT_REACHED(); } /* Display the current sort variant */ - this->GetWidget(WID_SV_SORT_BY)->widget_data = _sort_names[index]; + this->GetWidget(WID_SV_SORT_BY)->widget_data = StationViewWindow::sort_names[index]; this->SetDirty(); } @@ -2048,8 +2061,8 @@ struct StationViewWindow : public Window { { this->grouping_index = index; _settings_client.gui.station_gui_group_order = index; - this->GetWidget(WID_SV_GROUP_BY)->widget_data = _group_names[index]; - switch (_group_names[index]) { + this->GetWidget(WID_SV_GROUP_BY)->widget_data = StationViewWindow::group_names[index]; + switch (StationViewWindow::group_names[index]) { case STR_STATION_VIEW_GROUP_S_V_D: this->groupings[1] = GR_SOURCE; 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( WDP_AUTO, "view_station", 249, 117, WC_STATION_VIEW, WC_NONE, diff --git a/src/town_gui.cpp b/src/town_gui.cpp index 0b5e729f99..8e38d7c1d1 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -720,7 +720,12 @@ private: static Listing last_sorting; /* 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 sorter_funcs; StringFilter string_filter; ///< Filter for towns @@ -1045,14 +1050,6 @@ public: 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. */ const std::initializer_list TownDirectoryWindow::sorter_funcs = { &TownNameSorter,