2021-07-07 11:57:49 +03:00
|
|
|
#include "icon_animation_i.h"
|
2024-06-03 16:43:23 +03:00
|
|
|
#include "icon_i.h" // IWYU pragma: keep
|
2021-07-07 11:57:49 +03:00
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
|
|
|
|
IconAnimation* icon_animation_alloc(const Icon* icon) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(icon);
|
|
|
|
|
2022-02-18 22:53:46 +03:00
|
|
|
IconAnimation* instance = malloc(sizeof(IconAnimation));
|
2021-07-07 11:57:49 +03:00
|
|
|
instance->icon = icon;
|
2022-07-20 13:56:33 +03:00
|
|
|
instance->timer =
|
|
|
|
furi_timer_alloc(icon_animation_timer_callback, FuriTimerTypePeriodic, instance);
|
2024-03-19 17:43:52 +03:00
|
|
|
|
2021-07-07 11:57:49 +03:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void icon_animation_free(IconAnimation* instance) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(instance);
|
|
|
|
|
2021-12-02 01:13:21 +03:00
|
|
|
icon_animation_stop(instance);
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_timer_free(instance->timer);
|
2024-03-19 17:43:52 +03:00
|
|
|
|
2021-07-07 11:57:49 +03:00
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
2021-10-02 20:00:56 +03:00
|
|
|
void icon_animation_set_update_callback(
|
|
|
|
IconAnimation* instance,
|
|
|
|
IconAnimationCallback callback,
|
|
|
|
void* context) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(instance);
|
|
|
|
|
2021-10-02 20:00:56 +03:00
|
|
|
instance->callback = callback;
|
|
|
|
instance->callback_context = context;
|
|
|
|
}
|
|
|
|
|
2023-02-09 07:58:01 +03:00
|
|
|
const uint8_t* icon_animation_get_data(const IconAnimation* instance) {
|
2021-07-07 11:57:49 +03:00
|
|
|
return instance->icon->frames[instance->frame];
|
|
|
|
}
|
|
|
|
|
|
|
|
void icon_animation_next_frame(IconAnimation* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
instance->frame = (instance->frame + 1) % instance->icon->frame_count;
|
|
|
|
}
|
|
|
|
|
2021-10-02 20:00:56 +03:00
|
|
|
void icon_animation_timer_callback(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
IconAnimation* instance = context;
|
|
|
|
|
|
|
|
if(!instance->animating) return;
|
|
|
|
|
|
|
|
icon_animation_next_frame(instance);
|
|
|
|
if(instance->callback) {
|
|
|
|
instance->callback(instance, instance->callback_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 07:58:01 +03:00
|
|
|
uint8_t icon_animation_get_width(const IconAnimation* instance) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(instance);
|
|
|
|
|
2021-07-07 11:57:49 +03:00
|
|
|
return instance->icon->width;
|
|
|
|
}
|
|
|
|
|
2023-02-09 07:58:01 +03:00
|
|
|
uint8_t icon_animation_get_height(const IconAnimation* instance) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(instance);
|
|
|
|
|
2021-07-07 11:57:49 +03:00
|
|
|
return instance->icon->height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void icon_animation_start(IconAnimation* instance) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(instance);
|
|
|
|
|
2021-10-02 20:00:56 +03:00
|
|
|
if(!instance->animating) {
|
|
|
|
instance->animating = true;
|
2021-11-24 19:21:12 +03:00
|
|
|
furi_assert(instance->icon->frame_rate);
|
2021-10-02 20:00:56 +03:00
|
|
|
furi_check(
|
2023-11-01 10:24:11 +03:00
|
|
|
furi_timer_start(
|
2021-12-02 01:13:21 +03:00
|
|
|
instance->timer,
|
2023-11-01 10:24:11 +03:00
|
|
|
(furi_kernel_get_tick_frequency() / instance->icon->frame_rate)) == FuriStatusOk);
|
2021-10-02 20:00:56 +03:00
|
|
|
}
|
2021-07-07 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void icon_animation_stop(IconAnimation* instance) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(instance);
|
|
|
|
|
2021-10-02 20:00:56 +03:00
|
|
|
if(instance->animating) {
|
|
|
|
instance->animating = false;
|
2023-11-01 10:24:11 +03:00
|
|
|
furi_timer_stop(instance->timer);
|
2021-10-02 20:00:56 +03:00
|
|
|
instance->frame = 0;
|
|
|
|
}
|
2021-07-07 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 07:58:01 +03:00
|
|
|
bool icon_animation_is_last_frame(const IconAnimation* instance) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(instance);
|
|
|
|
|
2021-07-07 11:57:49 +03:00
|
|
|
return instance->icon->frame_count - instance->frame <= 1;
|
2021-10-02 20:00:56 +03:00
|
|
|
}
|