aboutsummaryrefslogtreecommitdiff
path: root/src/animate.c
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-03-14 23:42:39 +0100
committerFederico Angelilli <code@fedang.net>2024-03-14 23:42:39 +0100
commit5765bea99e1d497063ab312d879390b0dd3d2efd (patch)
tree474befd6fa140801c5d48ce857eb4dc2462616aa /src/animate.c
parent164a2559cbc9012194484fba767dcd127b36a993 (diff)
Add layout animations and refactor
Diffstat (limited to 'src/animate.c')
-rw-r--r--src/animate.c32
1 files changed, 22 insertions, 10 deletions
diff --git a/src/animate.c b/src/animate.c
index 7a6f77c..73ca0eb 100644
--- a/src/animate.c
+++ b/src/animate.c
@@ -6,6 +6,18 @@
#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);
@@ -31,8 +43,6 @@ double cubic_bezier(double x, double a, double b, double c, double d)
typedef struct {
Animation anim;
- gint64 start;
- gint64 duration;
int width;
cairo_pattern_t *gradient;
} AnimationShine;
@@ -40,14 +50,11 @@ typedef struct {
// FIXME: Not working for button group
bool shine_paint(AnimationShine *shine, cairo_t *cr, const Layout *layout)
{
+ gint64 end = shine->anim.start + shine->anim.duration;
gint64 now = g_get_monotonic_time();
- if (shine->start == 0)
- shine->start = now;
-
- gint64 end = shine->start + shine->duration;
if (now > end) return false;
- double t = (double)(now - shine->start) / (end - shine->start);
+ 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);
@@ -81,8 +88,8 @@ Animation *animation_shine_create(gint64 duration)
// Note the 0 initialization
AnimationShine *shine = g_malloc0(sizeof(AnimationShine));
shine->anim.type = ANIM_SHINE;
- shine->anim.paint = (DrawFunc)shine_paint;
- shine->duration = duration;
+ shine->anim.paint_func = (DrawFunc)shine_paint;
+ shine->anim.duration = duration;
// TODO: Change it depending on container size
shine->width = 20;
@@ -95,7 +102,12 @@ Animation *animation_shine_create(gint64 duration)
return (gpointer)shine;
}
-//XXX
+Animation *animation_pulse_create(gint64 duration)
+{
+ // TODO
+ return NULL;
+}
+
void animation_destroy(Animation *anim)
{
if (anim == NULL) return;