From 15086bff2e32bf67f9a093b35b53b81a6b4851e0 Mon Sep 17 00:00:00 2001 From: Arslaan Pathan Date: Sun, 26 Apr 2026 17:24:42 +1200 Subject: QoL stuff and get a bit more of stuff implemented --- include/saffron_widget.h | 5 +++-- meson.build | 3 ++- src/saffron_text.c | 20 +++++++++++++++++--- src/saffron_widget.c | 7 ++++++- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/include/saffron_widget.h b/include/saffron_widget.h index ddf4f78..5c0005c 100644 --- a/include/saffron_widget.h +++ b/include/saffron_widget.h @@ -17,8 +17,8 @@ typedef enum { typedef struct SaffronWidget { int x, y, w, h; - void (*draw)(struct SaffronWidget *self, SDL_Renderer *renderer); - void (*on_click)(struct SaffronWidget *self); + void (*draw)(struct SaffronWidget* self, SDL_Renderer* renderer); + void (*on_click)(struct SaffronWidget* self); struct SaffronWidget* parent; struct SaffronWidget** children; int child_count; @@ -26,6 +26,7 @@ typedef struct SaffronWidget { SaffronSizeMode height_mode; bool pixel_perfect; // Do we scale dynamically (SaffronSizeMode, etc) or use raw x, y, w, h values? If this is true, the SaffronSizeModes from earlier will be ignored. this pretty much tells the engine NOT to layout your widgets, don't expect it to allocate size for it. get better SaffronWidgetType type; + void (*free)(struct SaffronWidget* self); } SaffronWidget; #endif diff --git a/meson.build b/meson.build index c357bdd..9b1e5fa 100644 --- a/meson.build +++ b/meson.build @@ -10,7 +10,8 @@ sources = [ 'src/saffron.c', 'src/saffron_window.c', 'src/saffron_widget.c', - 'src/saffron_layout.c' + 'src/saffron_layout.c', + 'src/saffron_text.c' ] saffron_lib = static_library('saffron', sources, include_directories: inc, dependencies: deps) diff --git a/src/saffron_text.c b/src/saffron_text.c index 8a093ea..e2ac850 100644 --- a/src/saffron_text.c +++ b/src/saffron_text.c @@ -3,6 +3,11 @@ #include #include #include +#include + +static void saffron_text_free(SaffronWidget* self) { + free(((SaffronText*)self)->text); +} static void saffron_text_draw(SaffronWidget* widget, SDL_Renderer* renderer) { SaffronText* text = (SaffronText*)widget; @@ -25,7 +30,16 @@ SaffronText* saffron_text_new(const char* text, int font_size, SDL_Color color) saffron_widget_init((SaffronWidget*)text_widget); - // todo actually do stuff - // and return something - // im tired ill do this later + ((SaffronWidget*)text_widget)->type = SAFFRON_WIDGET_TEXT; + ((SaffronWidget*)text_widget)->draw = saffron_text_draw; + ((SaffronWidget*)text_widget)->free = saffron_text_free; + + text_widget->text = malloc(strlen(text) + 1); + if (text_widget->text) { + strcpy(text_widget->text, text); + } + text_widget->font = NULL; // TODO initialize a font here + text_widget->color = color; + + return text_widget; } diff --git a/src/saffron_widget.c b/src/saffron_widget.c index b673e48..955e265 100644 --- a/src/saffron_widget.c +++ b/src/saffron_widget.c @@ -22,6 +22,7 @@ void saffron_widget_init(SaffronWidget* widget) { widget->pixel_perfect = false; widget->type = SAFFRON_WIDGET_UNKNOWN; + widget->free = NULL; } SaffronWidget* saffron_widget_new(void) { @@ -45,6 +46,10 @@ void saffron_widget_free(SaffronWidget *widget) { saffron_widget_free(widget->children[i]); } + if (widget->free) { + widget->free(widget); + } + free(widget->children); free(widget); } @@ -90,7 +95,7 @@ SaffronWidget* saffron_widget_hit_test(SaffronWidget* widget, int x, int y) { for (int i = widget->child_count - 1; i >= 0; i--) { printf("[Saffron] recursing to child (%i)\n", i); SaffronWidget* hit = saffron_widget_hit_test(widget->children[i], x, y); - if (hit) return hit; + if (hit && hit->on_click) return hit; } return widget; -- cgit v1.2.3