diff --git a/Widgets/Widget.cpp b/Widgets/Widget.cpp index ff23bb3..e8b2d5f 100644 --- a/Widgets/Widget.cpp +++ b/Widgets/Widget.cpp @@ -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); } diff --git a/Widgets/Widget.h b/Widgets/Widget.h index 2a3c2ed..07bb912 100644 --- a/Widgets/Widget.h +++ b/Widgets/Widget.h @@ -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 diff --git a/Widgets/WidgetText.cpp b/Widgets/WidgetText.cpp index 4a3e441..5ba3615 100644 --- a/Widgets/WidgetText.cpp +++ b/Widgets/WidgetText.cpp @@ -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); } diff --git a/Widgets/WidgetText.h b/Widgets/WidgetText.h index 5ff8b0b..4135626 100644 --- a/Widgets/WidgetText.h +++ b/Widgets/WidgetText.h @@ -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);