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

#pragma once
#include <stdbool.h>
#include <time.h>

typedef double anim_time_interval_t;

struct animation_t;
typedef void(*AnimationCompletion)(struct animation_t *anim, void *context);

typedef enum {
    IN,
    OUT
} AnimationDirection;

typedef enum {
    _EmptyAnimationType,
    ACursorAnimation,
    ALogoAnimation,
    ARedFlashAnimation,
    ASpinnerAnimation,
} AnimationType;

// Cursor flash animation
typedef struct {
    bool cursor_animating;
} CursorAnimation;

// Logo transition in/out animation
typedef struct {
} LogoAnimation;

// Red flash for incorrect password
typedef struct {
    unsigned           flash_count;
} RedFlashAnimation;

// Spinner shown when checking password
typedef struct {
    double rotation;
} SpinnerAnimation;

typedef union {
    CursorAnimation   cursor_anim;
    LogoAnimation     logo_anim;
    RedFlashAnimation redflash_anim;
    SpinnerAnimation  spinner_anim;
} Animation;

typedef struct {
    AnimationType        type;
    Animation            anim;

    bool                 completed;
    anim_time_interval_t start_time;

    AnimationDirection   direction;

    AnimationCompletion  completion_func;
    void                *completion_func_context;
} animation_t;

// Convenience functions

// returns current time as anim_time_interval_t
anim_time_interval_t anim_now();

// Easing functions
typedef double(*AnimationEasingFunc)(double in);
double anim_identity(double p);
double anim_qubic_ease_out(double p);
double anim_quad_ease_out(double p);

// Returns normalized progress based on start time of `anim` and `duration`
double anim_progress(animation_t *anim, const double duration);
double anim_progress_ease(animation_t *anim, const double duration, AnimationEasingFunc easing_func);

// Returns true if `anim` is complete depending on direction
bool anim_complete(animation_t *anim, const double progress);