Added ability to choose type of text alignment
This commit is contained in:
parent
9f8a037f69
commit
82c35a6636
@ -84,6 +84,7 @@ static SDL_Rect find_visible_area(const SDL_Surface* txt_surface)
|
|||||||
WidgetText::WidgetText(int x, int y, int width, int height, string text,
|
WidgetText::WidgetText(int x, int y, int width, int height, string text,
|
||||||
TextFit fit, bool should_wrap,
|
TextFit fit, bool should_wrap,
|
||||||
HorizontalAlign halign, VerticalAlign valign,
|
HorizontalAlign halign, VerticalAlign valign,
|
||||||
|
bool halign_via_visible, bool valign_via_visible,
|
||||||
SDL_Color text_color,
|
SDL_Color text_color,
|
||||||
int size, std::string font)
|
int size, std::string font)
|
||||||
: Widget(x, y, width, height),
|
: Widget(x, y, width, height),
|
||||||
@ -94,7 +95,9 @@ m_fit(fit),
|
|||||||
m_should_wrap(should_wrap),
|
m_should_wrap(should_wrap),
|
||||||
m_halign(halign),
|
m_halign(halign),
|
||||||
m_valign(valign),
|
m_valign(valign),
|
||||||
m_text_color(text_color)
|
m_text_color(text_color),
|
||||||
|
m_halign_via_visible(halign_via_visible),
|
||||||
|
m_valign_via_visible(valign_via_visible)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +112,9 @@ m_fit(fit),
|
|||||||
m_should_wrap(should_wrap),
|
m_should_wrap(should_wrap),
|
||||||
m_halign(HALIGN_CENTER),
|
m_halign(HALIGN_CENTER),
|
||||||
m_valign(VALIGN_CENTER),
|
m_valign(VALIGN_CENTER),
|
||||||
m_text_color{.r = 0, .g = 0, .b = 0, .a = SDL_ALPHA_OPAQUE}
|
m_text_color{.r = 0, .g = 0, .b = 0, .a = SDL_ALPHA_OPAQUE},
|
||||||
|
m_halign_via_visible(true),
|
||||||
|
m_valign_via_visible(true)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,7 +127,9 @@ m_size(size),
|
|||||||
m_fit(FIT_NONE),
|
m_fit(FIT_NONE),
|
||||||
m_halign(HALIGN_CENTER),
|
m_halign(HALIGN_CENTER),
|
||||||
m_valign(VALIGN_CENTER),
|
m_valign(VALIGN_CENTER),
|
||||||
m_text_color{.r = 0, .g = 0, .b = 0, .a = SDL_ALPHA_OPAQUE}
|
m_text_color{.r = 0, .g = 0, .b = 0, .a = SDL_ALPHA_OPAQUE},
|
||||||
|
m_halign_via_visible(true),
|
||||||
|
m_valign_via_visible(true)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -238,6 +245,18 @@ void WidgetText::draw()
|
|||||||
|
|
||||||
// Now we have the final rendered text surface
|
// Now we have the final rendered text surface
|
||||||
SDL_Rect hint = find_visible_area(txt_surface);
|
SDL_Rect hint = find_visible_area(txt_surface);
|
||||||
|
if (!m_halign_via_visible)
|
||||||
|
{
|
||||||
|
hint.x = 0;
|
||||||
|
hint.w = txt_surface->w;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!m_valign_via_visible)
|
||||||
|
{
|
||||||
|
hint.y = 0;
|
||||||
|
hint.h = txt_surface->h;
|
||||||
|
}
|
||||||
|
|
||||||
SDL_Rect align = surface_align(m_surface, txt_surface, m_halign, m_valign, &hint);
|
SDL_Rect align = surface_align(m_surface, txt_surface, m_halign, m_valign, &hint);
|
||||||
SDL_BlitSurface(txt_surface, NULL, m_surface, &align);
|
SDL_BlitSurface(txt_surface, NULL, m_surface, &align);
|
||||||
SDL_FreeSurface(txt_surface);
|
SDL_FreeSurface(txt_surface);
|
||||||
@ -254,6 +273,8 @@ Widget* WidgetText::builder(const nlohmann::json& j)
|
|||||||
bool should_wrap = false;
|
bool should_wrap = false;
|
||||||
HorizontalAlign halign = HALIGN_CENTER;
|
HorizontalAlign halign = HALIGN_CENTER;
|
||||||
VerticalAlign valign = VALIGN_CENTER;
|
VerticalAlign valign = VALIGN_CENTER;
|
||||||
|
bool halign_via_visible = true;
|
||||||
|
bool valign_via_visible = true;
|
||||||
SDL_Color text_color = {.r = 0, .g = 0, .b = 0, .a = SDL_ALPHA_OPAQUE};
|
SDL_Color text_color = {.r = 0, .g = 0, .b = 0, .a = SDL_ALPHA_OPAQUE};
|
||||||
int size = 0;
|
int size = 0;
|
||||||
string font = "";
|
string font = "";
|
||||||
@ -267,6 +288,8 @@ Widget* WidgetText::builder(const nlohmann::json& j)
|
|||||||
json_extract(j, "should_wrap", should_wrap);
|
json_extract(j, "should_wrap", should_wrap);
|
||||||
json_extract(j, "halign", halign);
|
json_extract(j, "halign", halign);
|
||||||
json_extract(j, "valign", valign);
|
json_extract(j, "valign", valign);
|
||||||
|
json_extract(j, "halign_via_visible", halign_via_visible);
|
||||||
|
json_extract(j, "valign_via_visible", valign_via_visible);
|
||||||
json_extract(j, "color", text_color);
|
json_extract(j, "color", text_color);
|
||||||
json_extract(j, "size", size);
|
json_extract(j, "size", size);
|
||||||
json_extract(j, "font", font);
|
json_extract(j, "font", font);
|
||||||
@ -276,5 +299,9 @@ Widget* WidgetText::builder(const nlohmann::json& j)
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new WidgetText(x, y, width, height, text, fit, should_wrap, halign, valign, text_color, size, font);
|
return new WidgetText(x, y, width, height,
|
||||||
|
text, fit, should_wrap,
|
||||||
|
halign, valign,
|
||||||
|
halign_via_visible, valign_via_visible,
|
||||||
|
text_color, size, font);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,10 +40,19 @@ protected:
|
|||||||
// Default - black
|
// Default - black
|
||||||
SDL_Color m_text_color;
|
SDL_Color m_text_color;
|
||||||
|
|
||||||
|
// Whether to H-align via visible pixels or rendering surface
|
||||||
|
// Default - true
|
||||||
|
bool m_halign_via_visible;
|
||||||
|
|
||||||
|
// Whether to V-align via visible pixels or rendering surface
|
||||||
|
// Default - true
|
||||||
|
bool m_valign_via_visible;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
WidgetText(int x, int y, int width, int height, std::string text,
|
WidgetText(int x, int y, int width, int height, std::string text,
|
||||||
TextFit fit, bool should_wrap,
|
TextFit fit, bool should_wrap,
|
||||||
HorizontalAlign halign, VerticalAlign valign,
|
HorizontalAlign halign, VerticalAlign valign,
|
||||||
|
bool halign_via_visible, bool valign_via_visible,
|
||||||
SDL_Color text_color,
|
SDL_Color text_color,
|
||||||
int size, std::string font = "");
|
int size, std::string font = "");
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user