From bec575505537847cd8b128182bf9f81c7ac01319 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Sun, 27 Jan 2019 22:45:02 -0800 Subject: Animate with easing function --- src/animation.c | 14 +++++++++++++- src/animation.h | 11 +++++++---- src/render.c | 10 +++------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/animation.c b/src/animation.c index ecefb97..0ec403b 100644 --- a/src/animation.c +++ b/src/animation.c @@ -11,6 +11,11 @@ * Easing functions */ +double anim_identity(double p) +{ + return p; +} + double anim_qubic_ease_out(double p) { double f = (p - 1.0); @@ -35,13 +40,20 @@ anim_time_interval_t anim_now() return (ms / 1000.0); } -double anim_progress(animation_t *anim, const double duration) +double anim_progress_ease(animation_t *anim, const double duration, AnimationEasingFunc easing_f) { const anim_time_interval_t now = anim_now(); double progress = (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; diff --git a/src/animation.h b/src/animation.h index 54adb70..e51771d 100644 --- a/src/animation.h +++ b/src/animation.h @@ -70,14 +70,17 @@ typedef struct { // 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); -// Easing functions -double anim_qubic_ease_out(double p); -double anim_quad_ease_out(double p); - diff --git a/src/render.c b/src/render.c index b6a70d4..ab1e701 100644 --- a/src/render.c +++ b/src/render.c @@ -47,7 +47,7 @@ static void update_single_animation(saver_state_t *state, animation_t *anim) if (ca->cursor_animating) { if (!state->is_processing) { const double fade_duration = 0.5; - double progress = anim_progress(anim, fade_duration); + const double progress = anim_progress(anim, fade_duration); state->cursor_opacity = progress; @@ -64,9 +64,7 @@ static void update_single_animation(saver_state_t *state, animation_t *anim) // Logo animation else if (anim->type == ALogoAnimation) { const double logo_duration = 0.6; - - double progress = anim_progress(anim, logo_duration); - progress = anim_qubic_ease_out(progress); + const double progress = anim_progress_ease(anim, logo_duration, anim_qubic_ease_out); state->logo_fill_progress = progress; state->password_opacity = progress; @@ -77,9 +75,7 @@ static void update_single_animation(saver_state_t *state, animation_t *anim) // Background red flash animation else if (anim->type == ARedFlashAnimation) { const double duration = 0.1; - - double progress = anim_progress(anim, duration); - progress = anim_qubic_ease_out(progress); + const double progress = anim_progress_ease(anim, duration, anim_qubic_ease_out); bool completed = false; if (anim_complete(anim, progress)) { -- cgit v1.2.3