From 398fdbaa5c02bbd138c59004053d2e8ad0082ead Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Thu, 14 Mar 2024 00:58:05 +0100 Subject: Add animation scaffolding --- src/button.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/button.c') diff --git a/src/button.c b/src/button.c index 7bee576..01d4e36 100644 --- a/src/button.c +++ b/src/button.c @@ -1,6 +1,7 @@ #include #include "button.h" +#include "log.h" Button *button_simple_create(PangoAlignment align, Color color, Color line_color) { @@ -63,8 +64,41 @@ void button_set_padding(Button *btn, int padding) btn->padding = padding; } +bool button_set_animation(Button *btn, Animation *anim) +{ + if (btn->anim) return false; + btn->anim = anim; + return true; +} + +// Wrapper for animation sources +static gboolean anim_handler(gpointer data) +{ + Button *btn = data; + if (btn->anim->handler(btn->anim)) + return G_SOURCE_CONTINUE; + + g_free(btn->anim); + btn->anim = NULL; + return G_SOURCE_REMOVE; +} + +void button_start_animation(Button *btn) +{ + // Maybe error out + if (btn->anim == NULL) { + log_warning("Button animation was not set"); + return; + } + + const int fps = 60; + if (btn->anim->handler(btn->anim)) + g_timeout_add(1000 / 60, anim_handler, btn); +} + void button_destroy(Button *btn) { if (btn->simple) g_free(CAST(btn, ButtonSimple)->text); + animation_destroy(btn->anim); g_free(btn); } -- cgit v1.2.3