1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-06 06:32:56 +01:00

Implement centred labels

This commit is contained in:
Ted John
2021-01-28 23:51:56 +00:00
parent 180a41686c
commit 2fdf91874c
4 changed files with 65 additions and 4 deletions

View File

@@ -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";

View File

@@ -357,8 +357,16 @@ static void WidgetTextCentred(rct_drawpixelinfo* dpi, rct_window* w, rct_widgeti
stringId = STR_STRING;
ft.Add<utf8*>(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<utf8*>(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);
}
}
/**

View File

@@ -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<std::string> 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<std::vector<ListViewColumn>>(desc["columns"]);
@@ -976,6 +985,10 @@ namespace OpenRCT2::Ui::Windows
widget.type = WindowWidgetType::Label;
widget.string = const_cast<utf8*>(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")

View File

@@ -652,6 +652,33 @@ namespace OpenRCT2::Scripting
{
dukglue_set_base_class<ScWidget, ScLabelWidget>(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<ScGroupBoxWidget>(c, n, widgetIndex));
case WindowWidgetType::Label:
case WindowWidgetType::LabelCentred:
return GetObjectAsDukValue(ctx, std::make_shared<ScLabelWidget>(c, n, widgetIndex));
case WindowWidgetType::Scroll:
return GetObjectAsDukValue(ctx, std::make_shared<ScListViewWidget>(c, n, widgetIndex));