mirror of https://github.com/OpenTTD/OpenTTD.git
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: {
|
case WID_RV_START_REPLACE: {
|
||||||
Dimension d = GetStringBoundingBox(STR_REPLACE_VEHICLES_START);
|
Dimension d = GetStringBoundingBox(STR_REPLACE_VEHICLES_START);
|
||||||
for (int i = 0; _start_replace_dropdown[i] != INVALID_STRING_ID; i++) {
|
maxdim(d, GetStringListBoundingBox(_start_replace_dropdown));
|
||||||
d = maxdim(d, GetStringBoundingBox(_start_replace_dropdown[i]));
|
|
||||||
}
|
|
||||||
d.width += padding.width;
|
d.width += padding.width;
|
||||||
d.height += padding.height;
|
d.height += padding.height;
|
||||||
size = maxdim(size, d);
|
size = maxdim(size, d);
|
||||||
|
|
|
@ -190,10 +190,7 @@ public:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case WID_BBS_DROPDOWN_CRITERIA: {
|
case WID_BBS_DROPDOWN_CRITERIA: {
|
||||||
Dimension d = {0, 0};
|
Dimension d = GetStringListBoundingBox(BuildBridgeWindow::sorter_names);
|
||||||
for (const StringID *str = BuildBridgeWindow::sorter_names; *str != INVALID_STRING_ID; str++) {
|
|
||||||
d = maxdim(d, GetStringBoundingBox(*str));
|
|
||||||
}
|
|
||||||
d.width += padding.width;
|
d.width += padding.width;
|
||||||
d.height += padding.height;
|
d.height += padding.height;
|
||||||
size = maxdim(size, d);
|
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
|
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)};
|
Dimension d{0, (uint)GetCharacterHeight(FS_NORMAL)};
|
||||||
const StringID *strs = nullptr;
|
std::span<const StringID> strs;
|
||||||
switch (widget) {
|
switch (widget) {
|
||||||
case WID_GL_TEMPERATE: case WID_GL_ARCTIC:
|
case WID_GL_TEMPERATE: case WID_GL_ARCTIC:
|
||||||
case WID_GL_TROPICAL: case WID_GL_TOYLAND:
|
case WID_GL_TROPICAL: case WID_GL_TOYLAND:
|
||||||
|
@ -660,11 +660,7 @@ struct GenerateLandscapeWindow : public Window {
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (strs != nullptr) {
|
maxdim(d, GetStringListBoundingBox(strs));
|
||||||
while (*strs != INVALID_STRING_ID) {
|
|
||||||
d = maxdim(d, GetStringBoundingBox(*strs++));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
d.width += padding.width;
|
d.width += padding.width;
|
||||||
d.height += padding.height;
|
d.height += padding.height;
|
||||||
size = maxdim(size, d);
|
size = maxdim(size, d);
|
||||||
|
|
16
src/gfx.cpp
16
src/gfx.cpp
|
@ -22,6 +22,7 @@
|
||||||
#include "newgrf_debug.h"
|
#include "newgrf_debug.h"
|
||||||
#include "core/backup_type.hpp"
|
#include "core/backup_type.hpp"
|
||||||
#include "core/container_func.hpp"
|
#include "core/container_func.hpp"
|
||||||
|
#include "core/geometry_func.hpp"
|
||||||
#include "viewport_func.h"
|
#include "viewport_func.h"
|
||||||
|
|
||||||
#include "table/string_colours.h"
|
#include "table/string_colours.h"
|
||||||
|
@ -877,6 +878,21 @@ uint GetStringListWidth(std::span<const StringID> list, FontSize fontsize)
|
||||||
return width;
|
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
|
* Get the leading corner of a character in a single-line string relative
|
||||||
* to the start of the string.
|
* 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(std::string_view str, FontSize start_fontsize = FS_NORMAL);
|
||||||
Dimension GetStringBoundingBox(StringID strid, 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);
|
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(std::string_view str, int maxw, FontSize fontsize = FS_NORMAL);
|
||||||
int GetStringHeight(StringID str, int maxw);
|
int GetStringHeight(StringID str, int maxw);
|
||||||
int GetStringLineCount(StringID str, int maxw);
|
int GetStringLineCount(StringID str, int maxw);
|
||||||
|
|
|
@ -1739,10 +1739,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
case WID_ID_DROPDOWN_CRITERIA: {
|
case WID_ID_DROPDOWN_CRITERIA: {
|
||||||
Dimension d = {0, 0};
|
Dimension d = GetStringListBoundingBox(IndustryDirectoryWindow::sorter_names);
|
||||||
for (uint i = 0; IndustryDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
|
|
||||||
d = maxdim(d, GetStringBoundingBox(IndustryDirectoryWindow::sorter_names[i]));
|
|
||||||
}
|
|
||||||
d.width += padding.width;
|
d.width += padding.width;
|
||||||
d.height += padding.height;
|
d.height += padding.height;
|
||||||
size = maxdim(size, d);
|
size = maxdim(size, d);
|
||||||
|
|
|
@ -854,10 +854,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
case WID_O_COND_COMPARATOR: {
|
case WID_O_COND_COMPARATOR: {
|
||||||
Dimension d = {0, 0};
|
Dimension d = GetStringListBoundingBox(_order_conditional_condition);
|
||||||
for (int i = 0; _order_conditional_condition[i] != INVALID_STRING_ID; i++) {
|
|
||||||
d = maxdim(d, GetStringBoundingBox(_order_conditional_condition[i]));
|
|
||||||
}
|
|
||||||
d.width += padding.width;
|
d.width += padding.width;
|
||||||
d.height += padding.height;
|
d.height += padding.height;
|
||||||
size = maxdim(size, d);
|
size = maxdim(size, d);
|
||||||
|
|
|
@ -430,10 +430,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
case WID_STL_SORTDROPBTN: {
|
case WID_STL_SORTDROPBTN: {
|
||||||
Dimension d = {0, 0};
|
Dimension d = GetStringListBoundingBox(CompanyStationsWindow::sorter_names);
|
||||||
for (int i = 0; CompanyStationsWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
|
|
||||||
d = maxdim(d, GetStringBoundingBox(CompanyStationsWindow::sorter_names[i]));
|
|
||||||
}
|
|
||||||
d.width += padding.width;
|
d.width += padding.width;
|
||||||
d.height += padding.height;
|
d.height += padding.height;
|
||||||
size = maxdim(size, d);
|
size = maxdim(size, d);
|
||||||
|
|
|
@ -894,10 +894,7 @@ public:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case WID_TD_SORT_CRITERIA: {
|
case WID_TD_SORT_CRITERIA: {
|
||||||
Dimension d = {0, 0};
|
Dimension d = GetStringListBoundingBox(TownDirectoryWindow::sorter_names);
|
||||||
for (uint i = 0; TownDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
|
|
||||||
d = maxdim(d, GetStringBoundingBox(TownDirectoryWindow::sorter_names[i]));
|
|
||||||
}
|
|
||||||
d.width += padding.width;
|
d.width += padding.width;
|
||||||
d.height += padding.height;
|
d.height += padding.height;
|
||||||
size = maxdim(size, d);
|
size = maxdim(size, d);
|
||||||
|
|
|
@ -2487,12 +2487,7 @@ struct VehicleDetailsWindow : Window {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WID_VD_SERVICE_INTERVAL_DROPDOWN: {
|
case WID_VD_SERVICE_INTERVAL_DROPDOWN: {
|
||||||
Dimension d{0, 0};
|
Dimension d = maxdim(GetStringListBoundingBox(_service_interval_dropdown_calendar), GetStringListBoundingBox(_service_interval_dropdown_wallclock));
|
||||||
for (const StringID *strs : {_service_interval_dropdown_calendar, _service_interval_dropdown_wallclock}) {
|
|
||||||
while (*strs != INVALID_STRING_ID) {
|
|
||||||
d = maxdim(d, GetStringBoundingBox(*strs++));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
d.width += padding.width;
|
d.width += padding.width;
|
||||||
d.height += padding.height;
|
d.height += padding.height;
|
||||||
size = maxdim(size, d);
|
size = maxdim(size, d);
|
||||||
|
|
Loading…
Reference in New Issue