aboutsummaryrefslogtreecommitdiff
path: root/src/animate.c
blob: 839e7486953e63f0f45fa0cc1141c3c00c0ebe20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <glib.h>
#include <stdbool.h>
#include <math.h>

#include "animate.h"
#include "state.h"
#include "log.h"

double clamp(double x, double min, double max)
{
    const double t = x < min ? min : x;
    return t > max ? max : t;
}

double smoothstep(double x, double edge0, double edge1)
{
   x = clamp((x - edge0) / (edge1 - edge0), 0, 1);
   return x * x * (3.0 - 2.0 * x);
}

double quadratic_bezier(double x, double a, double b, double c)
{
	g_assert(x >= 0 && x <= 1);
	const double t = 1 - x;
	return a * t * t + 2 * b * t * x + c * x * x;
}

double cubic_bezier(double x, double a, double b, double c, double d)
{
	g_assert(x >= 0 && x <= 1);
	const double t = 1 - x;
	return a * (t * t * t) + 3 * b * (t * t * x) + 3 * c * (t * x * x) + d * (x * x * x);
}

typedef struct {
    Animation anim;
    int width;
    cairo_pattern_t *gradient;
} AnimationShine;

static bool shine_func(AnimationShine *shine, Layout *layout, cairo_t *cr)
{
    gint64 end = shine->anim.start + shine->anim.duration;
    gint64 now = g_get_monotonic_time();
    if (now > end) return false;

    double t = (double)(now - shine->anim.start) / (end - shine->anim.start);
    double pos = cubic_bezier(t, 0.19, 1.0, 0.22, 1.0);

    double angle = atan((double)layout->height / layout->width);

    // Make it double just to be sure
    int h = 2 * (double)layout->height / cos(angle);
    int x = layout->x + pos * layout->width - shine->width;
    int y = layout->y;

    cairo_matrix_t matrix;
    cairo_matrix_init_translate(&matrix, -x, -y);
    cairo_pattern_set_matrix(shine->gradient, &matrix);

    int dx = layout->x + layout->width / 2;
    int dy = layout->y + layout->height / 2;

    cairo_translate(cr, dx, dy);
    cairo_rotate(cr, -angle);
    cairo_translate(cr, -dx, -dy);

    cairo_set_operator(cr, CAIRO_OPERATOR_ATOP);
    cairo_set_source(cr, shine->gradient);
    cairo_rectangle(cr, x, y - (h - layout->height) / 2, shine->width, h);
    cairo_fill(cr);

    return true;
}

Animation *animation_shine_create(gint64 duration)
{
    // Note the 0 initialization
    AnimationShine *shine = g_malloc0(sizeof(AnimationShine));
    shine->anim.type = ANIM_SHINE;
    shine->anim.after_func = (DrawFunc)shine_func;
    shine->anim.duration = duration;

    // TODO: Change it depending on container size
    shine->width = 25;

    shine->gradient = cairo_pattern_create_linear(0, 0, shine->width, 0);
    cairo_pattern_add_color_stop_rgba(shine->gradient, 0, 1, 1, 1, 0);
    cairo_pattern_add_color_stop_rgba(shine->gradient, 0.5, 1, 1, 1, 0.2);
    cairo_pattern_add_color_stop_rgba(shine->gradient, 1, 1, 1, 1, 0);

    return (gpointer)shine;
}

static bool pulse_func(Animation *pulse, Layout *layout, cairo_t *cr)
{
    gint64 end = pulse->start + pulse->duration;
    gint64 now = g_get_monotonic_time();
    if (now > end) return false;

    // After half the duration we invert direction
    gint64 mid = pulse->start + pulse->duration / 2;
    double t = now <= mid
        ? (double)(now - pulse->start) / (mid - pulse->start)
        : 1.0 - (double)(now - mid) / (end - mid);

    double pos = cubic_bezier(t, 0.19, 1.0, 0.22, 1.0);
    int max = 0.15 * layout->height;

    layout->x_pad = layout->btn->x_pad + max * pos;
    layout->y_pad = layout->btn->y_pad + max * pos;

    return true;
}

Animation *animation_pulse_create(gint64 duration)
{
    // Note the 0 initialization
    Animation *pulse = g_malloc0(sizeof(Animation));
    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_full(g_list_remove_link(layout->children, layout->children), (GDestroyNotify)layout_destroy);
            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);

    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) continue;

        layout_destroy(it->data);
        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;
}

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, *next = it->next; it; it = next, next = it ? it->next : NULL) {
        Layout *child = it->data;
        if (child->x + child->width <= layout->x + width) continue;

        layout_destroy(it->data);
        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;

    if (anim->type == ANIM_SHINE)
        cairo_pattern_destroy(CAST(anim, AnimationShine)->gradient);

    g_free(anim);
}

// vim: ts=4 sw=4 et