aboutsummaryrefslogtreecommitdiff
path: root/src/saffron_button.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/saffron_button.c')
-rw-r--r--src/saffron_button.c37
1 files changed, 31 insertions, 6 deletions
diff --git a/src/saffron_button.c b/src/saffron_button.c
index bef0018..b7db204 100644
--- a/src/saffron_button.c
+++ b/src/saffron_button.c
@@ -7,9 +7,9 @@ static void saffron_button_draw(SaffronWidget* widget, SDL_Renderer* renderer) {
SaffronTheme* theme = widget->theme;
if (!theme) return;
- SaffronColor secondary = theme->secondary;
+ SaffronColor bg = theme->bg;
SaffronColor tertiary = theme->tertiary;
- SDL_SetRenderDrawColor(renderer, secondary.r, secondary.g, secondary.b, secondary.a);
+ SDL_SetRenderDrawColor(renderer, bg.r, bg.g, bg.b, bg.a);
SDL_FRect rect = {widget->x, widget->y, widget->w, widget->h};
SDL_RenderFillRect(renderer, &rect);
@@ -17,16 +17,41 @@ static void saffron_button_draw(SaffronWidget* widget, SDL_Renderer* renderer) {
SDL_RenderRect(renderer, &rect);
}
+static void saffron_button_onclick(SaffronWidget* self) {
+ SaffronButton* btn = (SaffronButton*)self;
+ btn->callback(btn);
+}
+
SaffronButton* saffron_button_new(bool enabled, void (*callback)(SaffronButton* self), int width, int height) {
SaffronButton* button = malloc(sizeof(SaffronButton));
- saffron_widget_init((SaffronWidget*)button);
+ if (!button) return NULL;
+
+ SaffronBox* box = saffron_box_new(SAFFRON_ORIENTATION_HORIZONTAL, SAFFRON_HALIGN_CENTER, SAFFRON_VALIGN_CENTER, 5, 5, 0, width, height);
+ if (!box) {
+ free(button);
+ return NULL;
+ }
+
+ memcpy(&button->base, box, sizeof(SaffronBox));
+ free(box);
- ((SaffronWidget*)button)->type = SAFFRON_WIDGET_BUTTON;
- ((SaffronWidget*)button)->draw = saffron_button_draw;
+ SaffronWidget* widget = (SaffronWidget*)button;
+ // We set it to type BOX because the layout engine needs to treat it as one: I'll remove the BUTTON enum later or make the layout engine check for both
+ widget->type = SAFFRON_WIDGET_BOX;
+ widget->draw = saffron_button_draw;
+ widget->on_click = saffron_button_onclick;
+ widget->children = NULL;
+ widget->child_count = 0;
button->callback = callback;
button->enabled = enabled;
- button->base = *saffron_box_new(SAFFRON_ORIENTATION_HORIZONTAL, SAFFRON_HALIGN_CENTER, SAFFRON_VALIGN_CENTER, 5, 5, 0, width, height);
+
+ return button;
+}
+
+SaffronButton* saffron_button_new_with_text(SaffronText* text, bool enabled, void (*callback)(SaffronButton* self), int width, int height) {
+ SaffronButton* button = saffron_button_new(enabled, callback, width, height);
+ saffron_widget_add_child((SaffronWidget*)button, (SaffronWidget*)text);
return button;
}