Compare commits

...

3 Commits

Author SHA1 Message Date
7c5cec7d8a Updated README. Fixed gitignore 2026-01-19 16:00:16 +02:00
3f4e42d7ce Added setters for Widget properties 2025-12-18 12:24:19 +02:00
40df0f6e0c Added info to README for WidgetImage 2025-12-15 17:07:44 +02:00
8 changed files with 106 additions and 2 deletions

9
.gitignore vendored
View File

@@ -1,4 +1,13 @@
# Exclude the executable
trmnl_sdl trmnl_sdl
# Exclude configuration files
*.json *.json
# Exclude images
*.bmp
*.jpg
*.png *.png
# Exclude fonts
*.ttf *.ttf

View File

@@ -63,13 +63,35 @@ Controls vertical alignment of elements. **String** type. Has several options:
* center - Align objects to be centered * center - Align objects to be centered
* bottom - Align objects to the bottom * bottom - Align objects to the bottom
### ImageResize
Controls image resizing. **String** type. Has several options:
* none - Image is not resized and is only clipped by the bounding box
* fit - Image is uniformly scaled to fit inside the box
* stretch - Image is scaled (with possible stretching) to fully fill the box
### TextFit ### TextFit
Controls automatic change the text size depending on contents. **String** type. Has several options: Controls the automatic change of the text size depending on contents. **String** type. Has several options:
* none - Text is not changed in any way * none - Text is not changed in any way
* shrink - Renders text with desired size and shrinks it to fit if contents are too large * shrink - Renders text with desired size and shrinks it to fit if contents are too large
* auto - Renders text with desired size and enlarges/shrinks it to fit if contents are too small/large * auto - Renders text with desired size and enlarges/shrinks it to fit if contents are too small/large
# List of Widgets and their parameters # List of Widgets and their parameters
### image
Renders an image with optional scaling
| Name | Type | Required | Default | Description |
| ---: | :--: | :------: | ------: | :---------- |
| x | int | | 0 | Horizontal position in pixels (from left to right) |
| y | int | | 0 | Vertical position in pixels (from top to bottom) |
| width | int | REQ | | Width in pixels |
| height | int | REQ | | Height in pixels |
| filename | string | | | Filename of the image to render |
| resize_type | ImageResize | | fit | Whether to resize the image and how to do so |
| bg_color | color | | 255, 255, 255, 0 (Transparent WHITE) | The background color to fill around the image if it is not fully in the box |
| halign | HAlign | | center | Horizontal alignment of the image |
| valign | VAlign | | center | Vertical alignment of the image |
Supported image formats: **JPG**, **PNG**, **BMP**
### rect ### rect
Renders a rectangle with optional rounded corners using either fill or internal stroke. Renders a rectangle with optional rounded corners using either fill or internal stroke.
| Name | Type | Required | Default | Description | | Name | Type | Required | Default | Description |
@@ -84,7 +106,6 @@ Renders a rectangle with optional rounded corners using either fill or internal
### text ### text
Renders text within a specified box Renders text within a specified box
| Name | Type | Required | Default | Description | | Name | Type | Required | Default | Description |
| ---: | :--: | :------: | ------: | :---------- | | ---: | :--: | :------: | ------: | :---------- |
| x | int | | 0 | Horizontal position in pixels (from left to right) | | x | int | | 0 | Horizontal position in pixels (from left to right) |

View File

@@ -22,6 +22,43 @@ m_bg_color(bg_color)
} }
} }
void WidgetImage::set_filename(const string& filename)
{
if (m_image_surface != nullptr)
{
SDL_FreeSurface(m_image_surface);
m_image_surface = nullptr;
}
m_filename = filename;
m_image_surface = IMG_Load(m_filename.c_str());
if (nullptr == m_image_surface)
{
// TODO: Print errors
}
}
void WidgetImage::set_resize(ImageResize type)
{
m_resize_type = type;
}
void WidgetImage::set_halign(HorizontalAlign halign)
{
m_halign = halign;
}
void WidgetImage::set_valign(VerticalAlign valign)
{
m_valign = valign;
}
void WidgetImage::set_bg_color(SDL_Color bg_color)
{
m_bg_color = bg_color;
}
void WidgetImage::draw() void WidgetImage::draw()
{ {
if (nullptr == m_surface) if (nullptr == m_surface)

View File

@@ -7,6 +7,7 @@
#include "../sdl_helpers.h" #include "../sdl_helpers.h"
// Renders an image with optional scaling
class WidgetImage : public Widget class WidgetImage : public Widget
{ {
protected: protected:
@@ -39,6 +40,12 @@ public:
VerticalAlign valign = VALIGN_CENTER, VerticalAlign valign = VALIGN_CENTER,
SDL_Color bg_color = SDL_Color{.r = 255, .g = 255, .b = 255, .a = SDL_ALPHA_TRANSPARENT}); SDL_Color bg_color = SDL_Color{.r = 255, .g = 255, .b = 255, .a = SDL_ALPHA_TRANSPARENT});
void set_filename(const std::string& filename);
void set_resize(ImageResize type);
void set_halign(HorizontalAlign halign);
void set_valign(VerticalAlign valign);
void set_bg_color(SDL_Color bg_color);
virtual void draw() override; virtual void draw() override;
static std::unique_ptr<Widget> builder(const nlohmann::json& j); static std::unique_ptr<Widget> builder(const nlohmann::json& j);

View File

@@ -24,6 +24,21 @@ m_radius(radius)
{ {
} }
void WidgetRect::set_color(SDL_Color color)
{
m_color = color;
}
void WidgetRect::set_stroke_size(int stroke_size)
{
m_stroke_size = stroke_size
}
void WidgetRect::set_radius(int radius)
{
m_radius = radius;
}
void WidgetRect::draw() void WidgetRect::draw()
{ {
if (nullptr == m_surface) if (nullptr == m_surface)

View File

@@ -27,6 +27,10 @@ public:
WidgetRect(int x, int y, int width, int height, WidgetRect(int x, int y, int width, int height,
int radius = 0, int stroke_size = -1); int radius = 0, int stroke_size = -1);
void set_color(SDL_Color color);
void set_stroke_size(int stroke_size);
void set_radius(int radius);
virtual void draw() override; virtual void draw() override;
static std::unique_ptr<Widget> builder(const nlohmann::json& j); static std::unique_ptr<Widget> builder(const nlohmann::json& j);

View File

@@ -168,6 +168,15 @@ void WidgetText::set_color(SDL_Color text_color)
m_text_color = text_color; m_text_color = text_color;
} }
void WidgetText::set_halign_via_visible(bool value)
{
m_halign_via_visible = value;
}
void WidgetText::set_valign_via_visible(bool value)
{
m_valign_via_visible = value;
}
void WidgetText::draw() void WidgetText::draw()
{ {
if (nullptr == m_surface) if (nullptr == m_surface)

View File

@@ -70,6 +70,8 @@ public:
void set_halign(HorizontalAlign halign); void set_halign(HorizontalAlign halign);
void set_valign(VerticalAlign valign); void set_valign(VerticalAlign valign);
void set_color(SDL_Color text_color); void set_color(SDL_Color text_color);
void set_halign_via_visible(bool value);
void set_valign_via_visible(bool value);
virtual void draw() override; virtual void draw() override;