From 44a9ccb0ca18314016ef10b4b1b1c7c958d9eabc Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sun, 17 Mar 2024 15:20:03 +0100 Subject: Add group grow animation --- src/animate.c | 35 +++++++++++++++++++++++++++++++++++ src/animate.h | 4 +++- src/comet.c | 18 ++++++++---------- 3 files changed, 46 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/animate.c b/src/animate.c index 313cfa7..387c9b8 100644 --- a/src/animate.c +++ b/src/animate.c @@ -179,6 +179,41 @@ Animation *animation_group_shrink_create(gint64 duration) return shrink; } +static bool grow_func(Animation *grow, Layout *layout) +{ + g_assert_false(layout->btn->simple); + ButtonGroup *group = (gpointer)layout->btn; + + gint64 end = grow->start + grow->duration; + gint64 now = g_get_monotonic_time(); + if (now > end) return false; + + double t = (double)(now - grow->start) / (end - grow->start); + double pos = smoothstep(t, 0, 1); + + int start_w = CAST(layout->children->data, Layout)->width; + int width = start_w + pos * (layout->width - start_w); + layout->width = width; + + for (GList *it = layout->children, *next = it->next; it; it = next, next = it ? it->next : NULL) { + Layout *child = it->data; + if (child->x + child->width > layout->x + width) + layout->children = g_list_delete_link(layout->children, it); + } + + return true; +} + +Animation *animation_group_grow_create(gint64 duration) +{ + // Note the 0 initialization + Animation *grow = g_malloc0(sizeof(Animation)); + grow->type = ANIM_GROUP_GROW; + grow->layout_func = (LayoutFunc)grow_func; + grow->duration = duration; + return grow; +} + void animation_destroy(Animation *anim) { if (anim == NULL) return; diff --git a/src/animate.h b/src/animate.h index 2323017..79b6174 100644 --- a/src/animate.h +++ b/src/animate.h @@ -18,7 +18,7 @@ struct Animation { ANIM_SHINE, ANIM_PULSE, ANIM_GROUP_SHRINK, - //ANIM_GROUP_GROW, + ANIM_GROUP_GROW, } type; gint64 start; gint64 duration; @@ -42,6 +42,8 @@ Animation *animation_pulse_create(gint64 duration); Animation *animation_group_shrink_create(gint64 duration); +Animation *animation_group_grow_create(gint64 duration); + void animation_destroy(Animation *anim); #endif diff --git a/src/comet.c b/src/comet.c index b417489..b6c40fd 100644 --- a/src/comet.c +++ b/src/comet.c @@ -210,16 +210,14 @@ static void menu_action(Button *btn) button_simple_set_text(btn, g_strdup(menu_ctx->strings[to_open]), CAST(btn, ButtonSimple)->text_color); - if (!to_open) { - Animation *shrink = animation_group_shrink_create(MILLIS(600)); - - if (button_set_animation((gpointer)menu_ctx->group, shrink)) - state_request_animation(menu_ctx->state); - else - animation_destroy(shrink); - } else { - state_request_redraw(menu_ctx->state, true); - } + Animation *anim = to_open + ? animation_group_grow_create(MILLIS(600)) + : animation_group_shrink_create(MILLIS(600)); + + if (button_set_animation((gpointer)menu_ctx->group, anim)) + state_request_animation(menu_ctx->state); + else + animation_destroy(anim); } static void register_buttons(State *state, Color color, Color text_color, Color line_color, int line_w) -- cgit v1.2.3