mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-24 00:03:11 +01:00
Merge pull request #12154 from tupaschoal/coords-text
Use only ScreenCoordsXY for Text.cpp
This commit is contained in:
@@ -14,8 +14,9 @@
|
||||
|
||||
static TextPaint _legacyPaint;
|
||||
|
||||
static void DrawText(rct_drawpixelinfo* dpi, int32_t x, int32_t y, TextPaint* paint, const_utf8string text);
|
||||
static void DrawText(rct_drawpixelinfo* dpi, int32_t x, int32_t y, TextPaint* paint, rct_string_id format, const void* args);
|
||||
static void DrawText(rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, TextPaint* paint, const_utf8string text);
|
||||
static void DrawText(
|
||||
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, TextPaint* paint, rct_string_id format, const void* args);
|
||||
|
||||
StaticLayout::StaticLayout(utf8string source, TextPaint paint, int32_t width)
|
||||
{
|
||||
@@ -30,7 +31,7 @@ StaticLayout::StaticLayout(utf8string source, TextPaint paint, int32_t width)
|
||||
LineHeight = font_get_line_height(fontSpriteBase);
|
||||
}
|
||||
|
||||
void StaticLayout::Draw(rct_drawpixelinfo* dpi, int32_t x, int32_t y)
|
||||
void StaticLayout::Draw(rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords)
|
||||
{
|
||||
gCurrentFontFlags = 0;
|
||||
gCurrentFontSpriteBase = Paint.SpriteBase;
|
||||
@@ -38,27 +39,25 @@ void StaticLayout::Draw(rct_drawpixelinfo* dpi, int32_t x, int32_t y)
|
||||
TextPaint tempPaint = Paint;
|
||||
|
||||
gCurrentFontFlags = 0;
|
||||
int32_t lineY = y;
|
||||
int32_t lineX = x;
|
||||
auto lineCoords = coords;
|
||||
switch (Paint.Alignment)
|
||||
{
|
||||
case TextAlignment::LEFT:
|
||||
lineX = x;
|
||||
break;
|
||||
case TextAlignment::CENTRE:
|
||||
lineX = x + MaxWidth / 2;
|
||||
lineCoords.x += MaxWidth / 2;
|
||||
break;
|
||||
case TextAlignment::RIGHT:
|
||||
lineX = x + MaxWidth;
|
||||
lineCoords.x += MaxWidth;
|
||||
break;
|
||||
}
|
||||
utf8* buffer = Buffer;
|
||||
for (int32_t line = 0; line < LineCount; ++line)
|
||||
{
|
||||
DrawText(dpi, lineX, lineY, &tempPaint, buffer);
|
||||
DrawText(dpi, lineCoords, &tempPaint, buffer);
|
||||
tempPaint.Colour = TEXT_COLOUR_254;
|
||||
buffer = get_string_end(buffer) + 1;
|
||||
lineY += LineHeight;
|
||||
lineCoords.y += LineHeight;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,43 +76,48 @@ int32_t StaticLayout::GetLineCount()
|
||||
return LineCount;
|
||||
}
|
||||
|
||||
static void DrawText(rct_drawpixelinfo* dpi, int32_t x, int32_t y, TextPaint* paint, const_utf8string text)
|
||||
static void DrawText(rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, TextPaint* paint, const_utf8string text)
|
||||
{
|
||||
int32_t width = gfx_get_string_width(text);
|
||||
|
||||
auto alignedCoords = coords;
|
||||
switch (paint->Alignment)
|
||||
{
|
||||
case TextAlignment::LEFT:
|
||||
break;
|
||||
case TextAlignment::CENTRE:
|
||||
x -= (width - 1) / 2;
|
||||
alignedCoords.x -= (width - 1) / 2;
|
||||
break;
|
||||
case TextAlignment::RIGHT:
|
||||
x -= width;
|
||||
alignedCoords.x -= width;
|
||||
break;
|
||||
}
|
||||
|
||||
ttf_draw_string(dpi, text, paint->Colour, { x, y });
|
||||
ttf_draw_string(dpi, text, paint->Colour, alignedCoords);
|
||||
|
||||
if (paint->UnderlineText)
|
||||
{
|
||||
gfx_fill_rect(dpi, x, y + 11, x + width, y + 11, text_palette[1]);
|
||||
gfx_fill_rect(
|
||||
dpi, alignedCoords.x, alignedCoords.y + 11, alignedCoords.x + width, alignedCoords.y + 11, text_palette[1]);
|
||||
if (text_palette[2] != 0)
|
||||
{
|
||||
gfx_fill_rect(dpi, x + 1, y + 12, x + width + 1, y + 12, text_palette[2]);
|
||||
gfx_fill_rect(
|
||||
dpi, alignedCoords.x + 1, alignedCoords.y + 12, alignedCoords.x + width + 1, alignedCoords.y + 12,
|
||||
text_palette[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void DrawText(rct_drawpixelinfo* dpi, int32_t x, int32_t y, TextPaint* paint, rct_string_id format, const void* args)
|
||||
static void DrawText(
|
||||
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, TextPaint* paint, rct_string_id format, const void* args)
|
||||
{
|
||||
utf8 buffer[512];
|
||||
format_string(buffer, sizeof(buffer), format, args);
|
||||
DrawText(dpi, x, y, paint, buffer);
|
||||
DrawText(dpi, coords, paint, buffer);
|
||||
}
|
||||
|
||||
static void DrawTextCompat(
|
||||
rct_drawpixelinfo* dpi, int32_t x, int32_t y, rct_string_id format, const void* args, uint8_t colour,
|
||||
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, rct_string_id format, const void* args, uint8_t colour,
|
||||
TextAlignment alignment, bool underline = false)
|
||||
{
|
||||
_legacyPaint.UnderlineText = underline;
|
||||
@@ -121,11 +125,11 @@ static void DrawTextCompat(
|
||||
_legacyPaint.Alignment = alignment;
|
||||
_legacyPaint.SpriteBase = FONT_SPRITE_BASE_MEDIUM;
|
||||
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
|
||||
DrawText(dpi, x, y, &_legacyPaint, format, args);
|
||||
DrawText(dpi, coords, &_legacyPaint, format, args);
|
||||
}
|
||||
|
||||
static void DrawTextEllipsisedCompat(
|
||||
rct_drawpixelinfo* dpi, int32_t x, int32_t y, int32_t width, rct_string_id format, void* args, uint8_t colour,
|
||||
rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords, int32_t width, rct_string_id format, void* args, uint8_t colour,
|
||||
TextAlignment alignment, bool underline = false)
|
||||
{
|
||||
_legacyPaint.UnderlineText = underline;
|
||||
@@ -138,7 +142,7 @@ static void DrawTextEllipsisedCompat(
|
||||
format_string(buffer, sizeof(buffer), format, args);
|
||||
gfx_clip_string(buffer, width);
|
||||
|
||||
DrawText(dpi, x, y, &_legacyPaint, buffer);
|
||||
DrawText(dpi, coords, &_legacyPaint, buffer);
|
||||
}
|
||||
|
||||
void gfx_draw_string(rct_drawpixelinfo* dpi, const_utf8string buffer, uint8_t colour, const ScreenCoordsXY& coords)
|
||||
@@ -147,63 +151,63 @@ void gfx_draw_string(rct_drawpixelinfo* dpi, const_utf8string buffer, uint8_t co
|
||||
_legacyPaint.Colour = colour;
|
||||
_legacyPaint.Alignment = TextAlignment::LEFT;
|
||||
_legacyPaint.SpriteBase = gCurrentFontSpriteBase;
|
||||
DrawText(dpi, coords.x, coords.y, &_legacyPaint, buffer);
|
||||
DrawText(dpi, coords, &_legacyPaint, buffer);
|
||||
}
|
||||
|
||||
// Basic
|
||||
void gfx_draw_string_left(
|
||||
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, const ScreenCoordsXY& coords)
|
||||
{
|
||||
DrawTextCompat(dpi, coords.x, coords.y, format, args, colour, TextAlignment::LEFT);
|
||||
DrawTextCompat(dpi, coords, format, args, colour, TextAlignment::LEFT);
|
||||
}
|
||||
|
||||
void gfx_draw_string_centred(
|
||||
rct_drawpixelinfo* dpi, rct_string_id format, const ScreenCoordsXY& coords, uint8_t colour, const void* args)
|
||||
{
|
||||
DrawTextCompat(dpi, coords.x, coords.y, format, args, colour, TextAlignment::CENTRE);
|
||||
DrawTextCompat(dpi, coords, format, args, colour, TextAlignment::CENTRE);
|
||||
}
|
||||
|
||||
void gfx_draw_string_right(
|
||||
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, const ScreenCoordsXY& coords)
|
||||
{
|
||||
DrawTextCompat(dpi, coords.x, coords.y, format, args, colour, TextAlignment::RIGHT);
|
||||
DrawTextCompat(dpi, coords, format, args, colour, TextAlignment::RIGHT);
|
||||
}
|
||||
// Underline
|
||||
void draw_string_left_underline(
|
||||
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, const ScreenCoordsXY& coords)
|
||||
{
|
||||
DrawTextCompat(dpi, coords.x, coords.y, format, args, colour, TextAlignment::LEFT, true);
|
||||
DrawTextCompat(dpi, coords, format, args, colour, TextAlignment::LEFT, true);
|
||||
}
|
||||
|
||||
void draw_string_centred_underline(
|
||||
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, const ScreenCoordsXY& coords)
|
||||
{
|
||||
DrawTextCompat(dpi, coords.x, coords.y, format, args, colour, TextAlignment::CENTRE, true);
|
||||
DrawTextCompat(dpi, coords, format, args, colour, TextAlignment::CENTRE, true);
|
||||
}
|
||||
|
||||
void draw_string_right_underline(
|
||||
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, const ScreenCoordsXY& coords)
|
||||
{
|
||||
DrawTextCompat(dpi, coords.x, coords.y, format, args, colour, TextAlignment::RIGHT, true);
|
||||
DrawTextCompat(dpi, coords, format, args, colour, TextAlignment::RIGHT, true);
|
||||
}
|
||||
|
||||
// Ellipsised
|
||||
void gfx_draw_string_left_clipped(
|
||||
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, const ScreenCoordsXY& coords, int32_t width)
|
||||
{
|
||||
DrawTextEllipsisedCompat(dpi, coords.x, coords.y, width, format, args, colour, TextAlignment::LEFT);
|
||||
DrawTextEllipsisedCompat(dpi, coords, width, format, args, colour, TextAlignment::LEFT);
|
||||
}
|
||||
|
||||
void gfx_draw_string_centred_clipped(
|
||||
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, const ScreenCoordsXY& coords, int32_t width)
|
||||
{
|
||||
DrawTextEllipsisedCompat(dpi, coords.x, coords.y, width, format, args, colour, TextAlignment::CENTRE);
|
||||
DrawTextEllipsisedCompat(dpi, coords, width, format, args, colour, TextAlignment::CENTRE);
|
||||
}
|
||||
|
||||
void gfx_draw_string_right_clipped(
|
||||
rct_drawpixelinfo* dpi, rct_string_id format, void* args, uint8_t colour, const ScreenCoordsXY& coords, int32_t width)
|
||||
{
|
||||
DrawTextEllipsisedCompat(dpi, coords.x, coords.y, width, format, args, colour, TextAlignment::RIGHT);
|
||||
DrawTextEllipsisedCompat(dpi, coords, width, format, args, colour, TextAlignment::RIGHT);
|
||||
}
|
||||
|
||||
// Wrapping
|
||||
@@ -221,7 +225,7 @@ int32_t gfx_draw_string_left_wrapped(
|
||||
_legacyPaint.SpriteBase = gCurrentFontSpriteBase;
|
||||
|
||||
StaticLayout layout(buffer, _legacyPaint, width);
|
||||
layout.Draw(dpi, coords.x, coords.y);
|
||||
layout.Draw(dpi, coords);
|
||||
|
||||
return layout.GetHeight();
|
||||
}
|
||||
@@ -246,7 +250,7 @@ int32_t gfx_draw_string_centred_wrapped(
|
||||
int32_t lineHeight = layout.GetHeight() / lineCount;
|
||||
int32_t yOffset = (lineCount - 1) * lineHeight / 2;
|
||||
|
||||
layout.Draw(dpi, coords.x - layout.GetWidth() / 2, coords.y - yOffset);
|
||||
layout.Draw(dpi, coords - ScreenCoordsXY{ layout.GetWidth() / 2, yOffset });
|
||||
|
||||
return layout.GetHeight();
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "../common.h"
|
||||
|
||||
struct ScreenCoordsXY;
|
||||
struct rct_drawpixelinfo;
|
||||
|
||||
enum class TextAlignment
|
||||
@@ -42,7 +43,7 @@ private:
|
||||
|
||||
public:
|
||||
StaticLayout(utf8string source, TextPaint paint, int32_t width);
|
||||
void Draw(rct_drawpixelinfo* dpi, int32_t x, int32_t y);
|
||||
void Draw(rct_drawpixelinfo* dpi, const ScreenCoordsXY& coords);
|
||||
int32_t GetHeight();
|
||||
int32_t GetWidth();
|
||||
int32_t GetLineCount();
|
||||
|
||||
Reference in New Issue
Block a user