diff --git a/distribution/openrct2.d.ts b/distribution/openrct2.d.ts index 86711e844e..a1c79735ca 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -1997,9 +1997,12 @@ declare global { interface LabelWidget extends WidgetBase { type: 'label'; text?: string; + textAlign?: TextAlignment; onChange?: (index: number) => void; } + type TextAlignment = "left" | "centred"; + type SortOrder = "none" | "ascending" | "descending"; type ScrollbarType = "none" | "horizontal" | "vertical" | "both"; diff --git a/src/openrct2-ui/interface/Widget.cpp b/src/openrct2-ui/interface/Widget.cpp index 6d117741ac..189cb44cac 100644 --- a/src/openrct2-ui/interface/Widget.cpp +++ b/src/openrct2-ui/interface/Widget.cpp @@ -357,8 +357,16 @@ static void WidgetTextCentred(rct_drawpixelinfo* dpi, rct_window* w, rct_widgeti stringId = STR_STRING; ft.Add(widget->string); } - DrawTextEllipsised( - dpi, { (topLeft.x + r + 1) / 2 - 1, topLeft.y }, widget->width() - 2, stringId, ft, colour, TextAlignment::CENTRE); + + ScreenCoordsXY coords = { (topLeft.x + r + 1) / 2 - 1, topLeft.y }; + if (widget->type == WindowWidgetType::LabelCentred) + { + gfx_draw_string_centred_wrapped(dpi, ft.Data(), coords, widget->width() - 2, stringId, colour); + } + else + { + DrawTextEllipsised(dpi, coords, widget->width() - 2, stringId, ft, colour, TextAlignment::CENTRE); + } } /** @@ -398,7 +406,16 @@ static void WidgetText(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex wi stringId = STR_STRING; ft.Add(widget->string); } - DrawTextEllipsised(dpi, { l + 1, t }, r - l, stringId, ft, colour); + + ScreenCoordsXY coords = { l + 1, t }; + if (widget->type == WindowWidgetType::LabelCentred) + { + gfx_draw_string_centred_wrapped(dpi, ft.Data(), coords, r - l, stringId, colour); + } + else + { + DrawTextEllipsised(dpi, coords, r - l, stringId, ft, colour); + } } /** diff --git a/src/openrct2-ui/scripting/CustomWindow.cpp b/src/openrct2-ui/scripting/CustomWindow.cpp index 750b8c1744..7848ba63df 100644 --- a/src/openrct2-ui/scripting/CustomWindow.cpp +++ b/src/openrct2-ui/scripting/CustomWindow.cpp @@ -93,6 +93,7 @@ namespace OpenRCT2::Ui::Windows std::string Name; ImageId Image; std::string Text; + TextAlignment TextAlign; colour_t Colour; std::string Tooltip; std::vector Items; @@ -175,10 +176,18 @@ namespace OpenRCT2::Ui::Windows result.SelectedIndex = AsOrDefault(desc["selectedIndex"], 0); result.OnChange = desc["onChange"]; } - else if (result.Type == "groupbox" || result.Type == "label") + else if (result.Type == "groupbox") { result.Text = ProcessString(desc["text"]); } + else if (result.Type == "label") + { + result.Text = ProcessString(desc["text"]); + if (ProcessString(desc["textAlign"]) == "centred") + { + result.TextAlign = TextAlignment::CENTRE; + } + } else if (result.Type == "listview") { result.ListViewColumns = FromDuk>(desc["columns"]); @@ -976,6 +985,10 @@ namespace OpenRCT2::Ui::Windows widget.type = WindowWidgetType::Label; widget.string = const_cast(desc.Text.c_str()); widget.flags |= WIDGET_FLAGS::TEXT_IS_STRING; + if (desc.TextAlign == TextAlignment::CENTRE) + { + widget.type = WindowWidgetType::LabelCentred; + } widgetList.push_back(widget); } else if (desc.Type == "listview") diff --git a/src/openrct2-ui/scripting/ScWidget.hpp b/src/openrct2-ui/scripting/ScWidget.hpp index 74c1c0cc26..2973d7c7b2 100644 --- a/src/openrct2-ui/scripting/ScWidget.hpp +++ b/src/openrct2-ui/scripting/ScWidget.hpp @@ -652,6 +652,33 @@ namespace OpenRCT2::Scripting { dukglue_set_base_class(ctx); dukglue_register_property(ctx, &ScLabelWidget::text_get, &ScLabelWidget::text_set, "text"); + dukglue_register_property(ctx, &ScLabelWidget::textAlign_get, &ScLabelWidget::textAlign_set, "textAlign"); + } + + private: + std::string textAlign_get() const + { + auto* widget = GetWidget(); + if (widget != nullptr) + { + if (widget->type == WindowWidgetType::LabelCentred) + { + return "centred"; + } + } + return "left"; + } + + void textAlign_set(const std::string& value) + { + auto* widget = GetWidget(); + if (widget != nullptr) + { + if (value == "centred") + widget->type = WindowWidgetType::LabelCentred; + else + widget->type = WindowWidgetType::Label; + } } }; @@ -946,6 +973,7 @@ namespace OpenRCT2::Scripting case WindowWidgetType::Groupbox: return GetObjectAsDukValue(ctx, std::make_shared(c, n, widgetIndex)); case WindowWidgetType::Label: + case WindowWidgetType::LabelCentred: return GetObjectAsDukValue(ctx, std::make_shared(c, n, widgetIndex)); case WindowWidgetType::Scroll: return GetObjectAsDukValue(ctx, std::make_shared(c, n, widgetIndex));