Add animation step callback

This commit is contained in:
XANTRONIX Development 2024-01-21 21:38:36 -05:00
parent 68b9ade03e
commit a5b4127b08
2 changed files with 25 additions and 8 deletions

View file

@ -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);

View file

@ -3,8 +3,13 @@
#include <hexagram/anim.h>
void hexagram_anim_init(hexagram_anim *anim) {
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; a<anim->stops[i].count; a++) {
hexagram_anim_action *action = &anim->stops[i].actions[a];