aboutsummaryrefslogtreecommitdiff
path: root/src/animation.c
blob: 930c8236992ddd659a40f23a19c668ebab19eeca (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
/*
 * animation.c
 *
 * Created by buzzert <buzzert@buzzert.net> 2019-01-20
 */

#include "animation.h"
#include <stdlib.h>
#include <sys/param.h>

/*
 * Easing functions
 */

double anim_identity(double p)
{
    return p;
}

double anim_qubic_ease_out(double p)
{
	double f = (p - 1.0);
    return (f * f * f) + 1;
}

double anim_quad_ease_out(double p)
{
    return -(p * (p - 2.0));
}

/*
 * Convenience calculation functions
 */

anim_time_interval_t anim_now()
{
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC_RAW, &ts);

    long ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
    return (ms / 1000.0);
}

double anim_progress_ease(animation_t *anim, const double duration, AnimationEasingFunc easing_f)
{
    const anim_time_interval_t now = anim_now();
    double progress = MAX(0.0, now - anim->start_time) / duration;
    progress = easing_f(progress);

    return (anim->direction == IN) ? progress : (1.0 - progress);
}

double anim_progress(animation_t *anim, const double duration)
{
    return anim_progress_ease(anim, duration, anim_identity);
}

bool anim_complete(animation_t *anim, const double progress)
{
   return (anim->direction == IN) ? progress >= 1.0 : progress <= 0.0;
}