aboutsummaryrefslogtreecommitdiff
path: root/src/animate.c
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-03-17 14:55:36 +0100
committerFederico Angelilli <code@fedang.net>2024-03-17 15:01:59 +0100
commitabacdaccb6a1ef55bd85b66e10caaa217458400b (patch)
tree029e80b94670150064b74880add4c44e318a0ca3 /src/animate.c
parent8419a503313f4e734465b726f897833e094c91b3 (diff)
Add group shrink animation
Diffstat (limited to 'src/animate.c')
-rw-r--r--src/animate.c48
1 files changed, 47 insertions, 1 deletions
diff --git a/src/animate.c b/src/animate.c
index 8bd09b5..313cfa7 100644
--- a/src/animate.c
+++ b/src/animate.c
@@ -129,8 +129,54 @@ Animation *animation_pulse_create(gint64 duration)
pulse->type = ANIM_PULSE;
pulse->before_func = (DrawFunc)pulse_func;
pulse->duration = duration;
+ return pulse;
+}
+
+static bool shrink_func(Animation *shrink, Layout *layout)
+{
+ g_assert_false(layout->btn->simple);
+ ButtonGroup *group = (gpointer)layout->btn;
+
+ gint64 end = shrink->start + shrink->duration;
+ gint64 now = g_get_monotonic_time();
+
+ if (now > end) {
+ // NOTE: Actually remove the buttons from the group after the animation's end
+ // This part is quite rough around the edges...
+ if (group->children->next) {
+ g_list_free(g_list_remove_link(group->children, group->children));
+ g_list_free(g_list_remove_link(layout->children, layout->children));
+ layout->width = CAST(layout->children->data, Layout)->width;
+ return true;
+ }
+
+ return false;
+ }
+
+ double t = (double)(now - shrink->start) / (end - shrink->start);
+ double pos = smoothstep(t, 0, 1);
- return (gpointer)pulse;
+ int target_w = CAST(layout->children->data, Layout)->width;
+ int width = layout->width - pos * (layout->width - target_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_shrink_create(gint64 duration)
+{
+ // Note the 0 initialization
+ Animation *shrink = g_malloc0(sizeof(Animation));
+ shrink->type = ANIM_GROUP_SHRINK;
+ shrink->layout_func = (LayoutFunc)shrink_func;
+ shrink->duration = duration;
+ return shrink;
}
void animation_destroy(Animation *anim)