Compare commits
3 Commits
17f73211a9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 7c5cec7d8a | |||
| 3f4e42d7ce | |||
| 40df0f6e0c |
9
.gitignore
vendored
9
.gitignore
vendored
@@ -1,4 +1,13 @@
|
||||
# Exclude the executable
|
||||
trmnl_sdl
|
||||
|
||||
# Exclude configuration files
|
||||
*.json
|
||||
|
||||
# Exclude images
|
||||
*.bmp
|
||||
*.jpg
|
||||
*.png
|
||||
|
||||
# Exclude fonts
|
||||
*.ttf
|
||||
|
||||
25
README.md
25
README.md
@@ -63,13 +63,35 @@ Controls vertical alignment of elements. **String** type. Has several options:
|
||||
* center - Align objects to be centered
|
||||
* 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
|
||||
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
|
||||
* 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
|
||||
|
||||
# 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
|
||||
Renders a rectangle with optional rounded corners using either fill or internal stroke.
|
||||
| Name | Type | Required | Default | Description |
|
||||
@@ -84,7 +106,6 @@ Renders a rectangle with optional rounded corners using either fill or internal
|
||||
|
||||
### text
|
||||
Renders text within a specified box
|
||||
|
||||
| Name | Type | Required | Default | Description |
|
||||
| ---: | :--: | :------: | ------: | :---------- |
|
||||
| x | int | | 0 | Horizontal position in pixels (from left to right) |
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
if (nullptr == m_surface)
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "../sdl_helpers.h"
|
||||
|
||||
// Renders an image with optional scaling
|
||||
class WidgetImage : public Widget
|
||||
{
|
||||
protected:
|
||||
@@ -39,6 +40,12 @@ public:
|
||||
VerticalAlign valign = VALIGN_CENTER,
|
||||
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;
|
||||
|
||||
static std::unique_ptr<Widget> builder(const nlohmann::json& j);
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
if (nullptr == m_surface)
|
||||
|
||||
@@ -27,6 +27,10 @@ public:
|
||||
WidgetRect(int x, int y, int width, int height,
|
||||
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;
|
||||
|
||||
static std::unique_ptr<Widget> builder(const nlohmann::json& j);
|
||||
|
||||
@@ -168,6 +168,15 @@ void WidgetText::set_color(SDL_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()
|
||||
{
|
||||
if (nullptr == m_surface)
|
||||
|
||||
@@ -70,6 +70,8 @@ public:
|
||||
void set_halign(HorizontalAlign halign);
|
||||
void set_valign(VerticalAlign valign);
|
||||
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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user