Added being able to set position via JSON. Text now always wraps on newlines.
This commit is contained in:
parent
56e07e1080
commit
69a50b0584
@ -1,7 +1,7 @@
|
||||
#include "Widget.h"
|
||||
|
||||
Widget::Widget(int width, int height)
|
||||
: m_surface(nullptr), m_rect{.x = 0, .y = 0, .w = width, .h = height}
|
||||
Widget::Widget(int x, int y, int width, int height)
|
||||
: m_surface(nullptr), m_rect{.x = x, .y = y, .w = width, .h = height}
|
||||
{
|
||||
m_surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, SDL_PIXELFORMAT_RGBA8888);
|
||||
}
|
||||
|
||||
@ -13,7 +13,7 @@ protected:
|
||||
SDL_Rect m_rect;
|
||||
|
||||
public:
|
||||
Widget(int width, int height);
|
||||
Widget(int x, int y, int width, int height);
|
||||
virtual ~Widget();
|
||||
|
||||
// Method which updates the internal surface with the latest image
|
||||
|
||||
@ -5,24 +5,22 @@ using std::string;
|
||||
static SDL_Surface* render_text(bool should_wrap, TTF_Font* font, const string& text, SDL_Color color, Uint32 wrap_size)
|
||||
{
|
||||
SDL_Surface* txt_surface = nullptr;
|
||||
if (should_wrap)
|
||||
if (!should_wrap)
|
||||
{
|
||||
txt_surface = TTF_RenderUTF8_Solid_Wrapped(font, text.c_str(), color, wrap_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
txt_surface = TTF_RenderUTF8_Solid(font, text.c_str(), color);
|
||||
// When wrap_size is 0 - the text only wraps on newlines
|
||||
wrap_size = 0;
|
||||
}
|
||||
txt_surface = TTF_RenderUTF8_Solid_Wrapped(font, text.c_str(), color, wrap_size);
|
||||
|
||||
return txt_surface;
|
||||
}
|
||||
|
||||
WidgetText::WidgetText(int width, int height, string text,
|
||||
WidgetText::WidgetText(int x, int y, int width, int height, string text,
|
||||
TextFit fit, bool should_wrap,
|
||||
HorizontalAlign halign, VerticalAlign valign,
|
||||
SDL_Color text_color,
|
||||
int size, std::string font)
|
||||
: Widget(width, height),
|
||||
: Widget(x, y, width, height),
|
||||
m_text(text),
|
||||
m_font_file(font),
|
||||
m_size(size),
|
||||
@ -38,10 +36,10 @@ m_text_color(text_color)
|
||||
}
|
||||
}
|
||||
|
||||
WidgetText::WidgetText(int width, int height, string text,
|
||||
WidgetText::WidgetText(int x, int y, int width, int height, string text,
|
||||
TextFit fit, bool should_wrap,
|
||||
int size, string font)
|
||||
: Widget(width, height),
|
||||
: Widget(x, y, width, height),
|
||||
m_text(text),
|
||||
m_font_file(font),
|
||||
m_size(size),
|
||||
@ -57,9 +55,9 @@ m_text_color{.r = 0, .g = 0, .b = 0, .a = SDL_ALPHA_OPAQUE}
|
||||
}
|
||||
}
|
||||
|
||||
WidgetText::WidgetText(int width, int height, string text,
|
||||
WidgetText::WidgetText(int x, int y, int width, int height, string text,
|
||||
int size, string font)
|
||||
: Widget(width, height),
|
||||
: Widget(x, y, width, height),
|
||||
m_text(text),
|
||||
m_font_file(font),
|
||||
m_size(size),
|
||||
@ -192,6 +190,8 @@ void WidgetText::draw()
|
||||
|
||||
Widget* WidgetText::builder(const nlohmann::json& j)
|
||||
{
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
string text = "";
|
||||
@ -203,6 +203,8 @@ Widget* WidgetText::builder(const nlohmann::json& j)
|
||||
int size = 0;
|
||||
string font = "";
|
||||
|
||||
json_extract(j, "x", x);
|
||||
json_extract(j, "y", y);
|
||||
json_extract(j, "width", width);
|
||||
json_extract(j, "height", height);
|
||||
json_extract(j, "text", text);
|
||||
@ -219,5 +221,5 @@ Widget* WidgetText::builder(const nlohmann::json& j)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return new WidgetText(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, text_color, size, font);
|
||||
}
|
||||
|
||||
@ -42,17 +42,17 @@ protected:
|
||||
SDL_Color m_text_color;
|
||||
|
||||
public:
|
||||
WidgetText(int width, int height, std::string text,
|
||||
WidgetText(int x, int y, int width, int height, std::string text,
|
||||
TextFit fit, bool should_wrap,
|
||||
HorizontalAlign halign, VerticalAlign valign,
|
||||
SDL_Color text_color,
|
||||
int size, std::string font = "");
|
||||
|
||||
WidgetText(int width, int height, std::string text,
|
||||
WidgetText(int x, int y, int width, int height, std::string text,
|
||||
TextFit fit, bool should_wrap,
|
||||
int size, std::string font = "");
|
||||
|
||||
WidgetText(int width, int height, std::string text,
|
||||
WidgetText(int x, int y, int width, int height, std::string text,
|
||||
int size, std::string font = "");
|
||||
|
||||
void set_text(const std::string& text);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user