diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp index 891e9febc6..db58477657 100644 --- a/src/autoreplace_gui.cpp +++ b/src/autoreplace_gui.cpp @@ -369,9 +369,7 @@ public: case WID_RV_START_REPLACE: { Dimension d = GetStringBoundingBox(STR_REPLACE_VEHICLES_START); - for (int i = 0; _start_replace_dropdown[i] != INVALID_STRING_ID; i++) { - d = maxdim(d, GetStringBoundingBox(_start_replace_dropdown[i])); - } + maxdim(d, GetStringListBoundingBox(_start_replace_dropdown)); d.width += padding.width; d.height += padding.height; size = maxdim(size, d); diff --git a/src/bridge_gui.cpp b/src/bridge_gui.cpp index c983433b7f..9683b59d33 100644 --- a/src/bridge_gui.cpp +++ b/src/bridge_gui.cpp @@ -190,10 +190,7 @@ public: break; } case WID_BBS_DROPDOWN_CRITERIA: { - Dimension d = {0, 0}; - for (const StringID *str = BuildBridgeWindow::sorter_names; *str != INVALID_STRING_ID; str++) { - d = maxdim(d, GetStringBoundingBox(*str)); - } + Dimension d = GetStringListBoundingBox(BuildBridgeWindow::sorter_names); d.width += padding.width; d.height += padding.height; size = maxdim(size, d); diff --git a/src/genworld_gui.cpp b/src/genworld_gui.cpp index 5cd4112061..4ac2bbae36 100644 --- a/src/genworld_gui.cpp +++ b/src/genworld_gui.cpp @@ -574,7 +574,7 @@ struct GenerateLandscapeWindow : public Window { void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override { Dimension d{0, (uint)GetCharacterHeight(FS_NORMAL)}; - const StringID *strs = nullptr; + std::span strs; switch (widget) { case WID_GL_TEMPERATE: case WID_GL_ARCTIC: case WID_GL_TROPICAL: case WID_GL_TOYLAND: @@ -660,11 +660,7 @@ struct GenerateLandscapeWindow : public Window { default: return; } - if (strs != nullptr) { - while (*strs != INVALID_STRING_ID) { - d = maxdim(d, GetStringBoundingBox(*strs++)); - } - } + maxdim(d, GetStringListBoundingBox(strs)); d.width += padding.width; d.height += padding.height; size = maxdim(size, d); diff --git a/src/gfx.cpp b/src/gfx.cpp index b3bdc79998..64bfc4a807 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -22,6 +22,7 @@ #include "newgrf_debug.h" #include "core/backup_type.hpp" #include "core/container_func.hpp" +#include "core/geometry_func.hpp" #include "viewport_func.h" #include "table/string_colours.h" @@ -877,6 +878,21 @@ uint GetStringListWidth(std::span list, FontSize fontsize) return width; } +/** + * Get maximum dimension of a list of strings. + * @param list List of strings, terminated by INVALID_STRING_ID. + * @param fontsize Font size to use. + * @return Dimension of highest and longest string within the list. + */ +Dimension GetStringListBoundingBox(std::span list, FontSize fontsize) +{ + Dimension d{0, 0}; + for (const StringID *str = list.data(); *str != INVALID_STRING_ID; str++) { + d = maxdim(d, GetStringBoundingBox(*str, fontsize)); + } + return d; +} + /** * Get the leading corner of a character in a single-line string relative * to the start of the string. diff --git a/src/gfx_func.h b/src/gfx_func.h index 592bcc5a8d..c6c1a46a4c 100644 --- a/src/gfx_func.h +++ b/src/gfx_func.h @@ -134,6 +134,7 @@ inline void GfxFillRect(const Rect &r, int colour, FillRectMode mode = FILLRECT_ Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize = FS_NORMAL); Dimension GetStringBoundingBox(StringID strid, FontSize start_fontsize = FS_NORMAL); uint GetStringListWidth(std::span list, FontSize fontsize = FS_NORMAL); +Dimension GetStringListBoundingBox(std::span list, FontSize fontsize = FS_NORMAL); int GetStringHeight(std::string_view str, int maxw, FontSize fontsize = FS_NORMAL); int GetStringHeight(StringID str, int maxw); int GetStringLineCount(StringID str, int maxw); diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index ebde5b921d..863789f250 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1739,10 +1739,7 @@ public: } case WID_ID_DROPDOWN_CRITERIA: { - Dimension d = {0, 0}; - for (uint i = 0; IndustryDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) { - d = maxdim(d, GetStringBoundingBox(IndustryDirectoryWindow::sorter_names[i])); - } + Dimension d = GetStringListBoundingBox(IndustryDirectoryWindow::sorter_names); d.width += padding.width; d.height += padding.height; size = maxdim(size, d); diff --git a/src/order_gui.cpp b/src/order_gui.cpp index 397502cd32..06fb5318af 100644 --- a/src/order_gui.cpp +++ b/src/order_gui.cpp @@ -854,10 +854,7 @@ public: } case WID_O_COND_COMPARATOR: { - Dimension d = {0, 0}; - for (int i = 0; _order_conditional_condition[i] != INVALID_STRING_ID; i++) { - d = maxdim(d, GetStringBoundingBox(_order_conditional_condition[i])); - } + Dimension d = GetStringListBoundingBox(_order_conditional_condition); d.width += padding.width; d.height += padding.height; size = maxdim(size, d); diff --git a/src/station_gui.cpp b/src/station_gui.cpp index d3b04b2749..d538fa33a2 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -430,10 +430,7 @@ public: } case WID_STL_SORTDROPBTN: { - Dimension d = {0, 0}; - for (int i = 0; CompanyStationsWindow::sorter_names[i] != INVALID_STRING_ID; i++) { - d = maxdim(d, GetStringBoundingBox(CompanyStationsWindow::sorter_names[i])); - } + Dimension d = GetStringListBoundingBox(CompanyStationsWindow::sorter_names); d.width += padding.width; d.height += padding.height; size = maxdim(size, d); diff --git a/src/town_gui.cpp b/src/town_gui.cpp index 8e38d7c1d1..e030111cdb 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -894,10 +894,7 @@ public: break; } case WID_TD_SORT_CRITERIA: { - Dimension d = {0, 0}; - for (uint i = 0; TownDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) { - d = maxdim(d, GetStringBoundingBox(TownDirectoryWindow::sorter_names[i])); - } + Dimension d = GetStringListBoundingBox(TownDirectoryWindow::sorter_names); d.width += padding.width; d.height += padding.height; size = maxdim(size, d); diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 6d85048134..2b7e9c00ff 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -2487,12 +2487,7 @@ struct VehicleDetailsWindow : Window { break; case WID_VD_SERVICE_INTERVAL_DROPDOWN: { - Dimension d{0, 0}; - for (const StringID *strs : {_service_interval_dropdown_calendar, _service_interval_dropdown_wallclock}) { - while (*strs != INVALID_STRING_ID) { - d = maxdim(d, GetStringBoundingBox(*strs++)); - } - } + Dimension d = maxdim(GetStringListBoundingBox(_service_interval_dropdown_calendar), GetStringListBoundingBox(_service_interval_dropdown_wallclock)); d.width += padding.width; d.height += padding.height; size = maxdim(size, d);