mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-06 14:27:16 +00:00
Codechange: refactor string list dimension finding into a separate function
This commit is contained in:
parent
546a996d95
commit
37a03b513f
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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<const StringID> 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);
|
||||
|
16
src/gfx.cpp
16
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<const StringID> 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<const StringID> 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.
|
||||
|
@ -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<const StringID> list, FontSize fontsize = FS_NORMAL);
|
||||
Dimension GetStringListBoundingBox(std::span<const StringID> 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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user