Skip to content

Commit

Permalink
improve chained animation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Mar 4, 2024
1 parent 0d0f413 commit 0cc53dc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
36 changes: 20 additions & 16 deletions src/animation.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,11 @@ void animation_setup(struct animation* animation, void* target, animator_functio
}

static bool animation_update(struct animation* animation, uint64_t time, uint64_t clock) {
if (!animation->target || !animation->update_function) {
if (!animation->target
|| !animation->update_function
|| animation->waiting ) {
return false;
}
double dt = animation->last_time > 0
? ((double)(time - animation->last_time) / clock)
: 0.0;
animation->last_time = time;
if (animation->offset > 0) {
animation->offset -= dt;
return false;
}

if (!animation->initial_time) animation->initial_time = time;
double t = animation->duration > 0
Expand Down Expand Up @@ -100,8 +94,6 @@ static bool animation_update(struct animation* animation, uint64_t time, uint64_
needs_update = animation->update_function(animation->target, value);
}

animation->counter += dt;

bool found_item = false;
for (int i = 0; i < g_bar_manager.bar_item_count; i++) {
if (needs_update
Expand All @@ -117,6 +109,11 @@ static bool animation_update(struct animation* animation, uint64_t time, uint64_
if (!found_item && needs_update) g_bar_manager.bar_needs_update = true;

animation->finished = final_frame;
if (animation->finished && animation->next) {
animation->next->previous = NULL;
animation->next->waiting = false;
animation->next = NULL;
}
return needs_update;
}

Expand Down Expand Up @@ -159,18 +156,22 @@ void animator_lock(struct animator* animator) {
static void animator_calculate_offset_for_animation(struct animator* animator, struct animation* animation) {
if (animator->animation_count < 1) return;

double offset = 0;
struct animation* previous = NULL;
for (uint32_t i = 0; i < animator->animation_count; i++) {
for (int i = animator->animation_count - 1; i >= 0; i--) {
struct animation* current = animator->animations[i];
if (current->target == animation->target
&& current->update_function == animation->update_function) {
offset += current->duration - current->counter;
previous = current;
break;
}
}
animation->offset = offset;
if (previous) animation->initial_value = previous->final_value;

if (previous) {
animation->initial_value = previous->final_value;
previous->next = animation;
animation->previous = previous;
animation->waiting = true;
}
}

void animator_add(struct animator* animator, struct animation* animation) {
Expand Down Expand Up @@ -205,6 +206,9 @@ static void animator_remove(struct animator* animator, struct animation* animati
sizeof(struct animation*)*animator->animation_count);
}

if (animation->previous) animation->previous->next = NULL;
if (animation->next) animation->next->previous = NULL;

animation_destroy(animation);
}

Expand Down
7 changes: 4 additions & 3 deletions src/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,20 @@ struct animation {
bool as_float;
bool locked;
bool finished;
bool waiting;

uint64_t initial_time;
uint64_t last_time;
double duration;
double counter;
double offset;

int initial_value;
int final_value;
animation_function* interp_function;

void* target;
animator_function* update_function;

struct animation* next;
struct animation* previous;
};

struct animation* animation_create();
Expand Down

0 comments on commit 0cc53dc

Please sign in to comment.