From a5b4127b08126d48071e32c7875d722a8c12399e Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 21 Jan 2024 21:38:36 -0500 Subject: [PATCH] Add animation step callback --- include/hexagram/anim.h | 17 +++++++++++++---- src/anim.c | 16 ++++++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/include/hexagram/anim.h b/include/hexagram/anim.h index e5c735c..131c9c0 100644 --- a/include/hexagram/anim.h +++ b/include/hexagram/anim.h @@ -20,7 +20,11 @@ typedef enum { typedef struct _hexagram_anim_stop hexagram_anim_stop; -typedef double (hexagram_anim_progress_fn)(hexagram_anim_stop *, double); +typedef struct _hexagram_anim hexagram_anim; + +typedef int (hexagram_anim_callback)(hexagram_anim *, int, void *); + +typedef double (hexagram_anim_progress_fn)(double); typedef struct _hexagram_anim_action { int flags; @@ -47,14 +51,19 @@ struct _hexagram_anim_stop { hexagram_anim_progress_fn *fn; }; -typedef struct _hexagram_anim { +struct _hexagram_anim { hexagram_anim_stop *stops; size_t count; + + hexagram_anim_callback *callback; + void *ctx; hexagram_anim_state state; struct timeval start, now; -} hexagram_anim; +}; -void hexagram_anim_init(hexagram_anim *anim); +void hexagram_anim_init(hexagram_anim *anim, + hexagram_anim_callback *callback, + void *ctx); int hexagram_anim_step(hexagram_anim *anim); diff --git a/src/anim.c b/src/anim.c index 9250b8a..1429899 100644 --- a/src/anim.c +++ b/src/anim.c @@ -3,8 +3,13 @@ #include -void hexagram_anim_init(hexagram_anim *anim) { - anim->state = HEXAGRAM_ANIM_PLAYING; +void hexagram_anim_init(hexagram_anim *anim, + hexagram_anim_callback *callback, + void *ctx) { + anim->callback = callback; + anim->ctx = ctx; + anim->state = HEXAGRAM_ANIM_PLAYING; + gettimeofday(&anim->start, NULL); } @@ -60,12 +65,15 @@ int hexagram_anim_step(hexagram_anim *anim) { progress = 1.0; anim->state = HEXAGRAM_ANIM_STOPPED; } else if (anim->stops[i].fn) { - progress = anim->stops[i].fn(&anim->stops[i], - (interval - offset) / anim->stops[i].duration); + progress = anim->stops[i].fn((interval - offset) / anim->stops[i].duration); } else { progress = (interval - offset) / anim->stops[i].duration; } + if (anim->callback) { + anim->callback(anim, i, anim->ctx); + } + for (a=0; astops[i].count; a++) { hexagram_anim_action *action = &anim->stops[i].actions[a];