Add animation step callback
This commit is contained in:
parent
68b9ade03e
commit
a5b4127b08
2 changed files with 25 additions and 8 deletions
|
@ -20,7 +20,11 @@ typedef enum {
|
||||||
|
|
||||||
typedef struct _hexagram_anim_stop hexagram_anim_stop;
|
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 {
|
typedef struct _hexagram_anim_action {
|
||||||
int flags;
|
int flags;
|
||||||
|
@ -47,14 +51,19 @@ struct _hexagram_anim_stop {
|
||||||
hexagram_anim_progress_fn *fn;
|
hexagram_anim_progress_fn *fn;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct _hexagram_anim {
|
struct _hexagram_anim {
|
||||||
hexagram_anim_stop *stops;
|
hexagram_anim_stop *stops;
|
||||||
size_t count;
|
size_t count;
|
||||||
|
|
||||||
|
hexagram_anim_callback *callback;
|
||||||
|
void *ctx;
|
||||||
hexagram_anim_state state;
|
hexagram_anim_state state;
|
||||||
struct timeval start, now;
|
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);
|
int hexagram_anim_step(hexagram_anim *anim);
|
||||||
|
|
||||||
|
|
14
src/anim.c
14
src/anim.c
|
@ -3,8 +3,13 @@
|
||||||
|
|
||||||
#include <hexagram/anim.h>
|
#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;
|
anim->state = HEXAGRAM_ANIM_PLAYING;
|
||||||
|
|
||||||
gettimeofday(&anim->start, NULL);
|
gettimeofday(&anim->start, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,12 +65,15 @@ int hexagram_anim_step(hexagram_anim *anim) {
|
||||||
progress = 1.0;
|
progress = 1.0;
|
||||||
anim->state = HEXAGRAM_ANIM_STOPPED;
|
anim->state = HEXAGRAM_ANIM_STOPPED;
|
||||||
} else if (anim->stops[i].fn) {
|
} else if (anim->stops[i].fn) {
|
||||||
progress = anim->stops[i].fn(&anim->stops[i],
|
progress = anim->stops[i].fn((interval - offset) / anim->stops[i].duration);
|
||||||
(interval - offset) / anim->stops[i].duration);
|
|
||||||
} else {
|
} else {
|
||||||
progress = (interval - offset) / anim->stops[i].duration;
|
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++) {
|
for (a=0; a<anim->stops[i].count; a++) {
|
||||||
hexagram_anim_action *action = &anim->stops[i].actions[a];
|
hexagram_anim_action *action = &anim->stops[i].actions[a];
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue