#include #include #include static void saffron_button_draw(SaffronWidget* widget, SDL_Renderer* renderer) { SaffronButton* btn = (SaffronButton*)widget; SaffronTheme* theme = widget->theme; if (!theme) return; SaffronColor bg = theme->bg; SaffronColor tertiary = theme->tertiary; 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); SDL_SetRenderDrawColor(renderer, tertiary.r, tertiary.g, tertiary.b, tertiary.a); 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)); 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* 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; 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; }