[FL-2150] Dolphin animation refactoring (#938)

* Dolphin Animation Refactoring, part 1
* Remove animations from desktop
* Remove excess, first start
* Split animation_manager with callbacks
* allocate view inside animation_view
* Work on ViewComposed
* Draw white rectangles under bubble corners
* Fix bubbles sequence
* RPC: remove obsolete include "status.pb.h"
* Add animations manifest decoding
* Flipper file: add strict mode
* FFF: Animation structures parsing
* Assembling structure of animation
* Lot of view fixes:
  Add multi-line bubbles
  Add support for passive bubbles (frame_order values starts from passive now)
  Add hard-coded delay (active_shift) for active state enabling
  Fix active state handling
  Fix leaks
  Fix parsing uncorrect bubble_animation meta file
  Fix bubble rules of showing
* Animation load/unload & view freeze/unfreeze
* Blocking & system animations, fixes:
  View correct activation
  Refactoring + blocking animation
  Freeze first passive/active frames
  Many insert/eject SD tests fixes
  Add system animations
  Add Loader events app started/finished
  Add system no_sd animation
* Assets: dolphin packer. Scripts: minor refactoring.
* Desktop: update logging tags. Scripts: add metadata to dolphin bundling process, extra sorting for fs traversing. Make: phony assets rules.
* Github: rebuild assets on build
* Docker: add missing dependencies for assets compilation
* Docker: fix run command syntax
* ReadMe: update naming rules with link to source
* Assets: recompile icons
* Loader: add loader event
* Desktop, Gui, Furi Core: const shenanigans macros

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
Albert Kharisov
2022-01-03 01:39:56 +04:00
committed by GitHub
parent 065241fe5b
commit a39002ce22
349 changed files with 3531 additions and 1912 deletions

View File

@@ -0,0 +1,436 @@
#include "animation_manager.h"
#include "furi-hal-delay.h"
#include "portmacro.h"
#include "views/bubble_animation_view.h"
#include "animation_storage.h"
#include <cmsis_os2.h>
#include <dolphin/dolphin.h>
#include <furi/check.h>
#include <furi/pubsub.h>
#include <furi/record.h>
#include <m-string.h>
#include <power/power_service/power.h>
#include <stdint.h>
#include <storage/storage.h>
#include <dolphin/dolphin_i.h>
#include <storage/filesystem-api-defines.h>
#define TAG "AnimationManager"
typedef enum {
AnimationManagerStateIdle,
AnimationManagerStateBlocked,
AnimationManagerStateFreezedIdle,
AnimationManagerStateFreezedBlocked,
} AnimationManagerState;
struct AnimationManager {
bool sd_show_url;
bool sd_shown_no_db;
bool sd_shown_sd_ok;
AnimationManagerState state;
FuriPubSubSubscription* pubsub_subscription_storage;
FuriPubSubSubscription* pubsub_subscription_dolphin;
BubbleAnimationView* animation_view;
osTimerId_t idle_animation_timer;
StorageAnimation* current_animation;
AnimationManagerInteractCallback interact_callback;
AnimationManagerSetNewIdleAnimationCallback new_idle_callback;
AnimationManagerSetNewIdleAnimationCallback check_blocking_callback;
void* context;
string_t freezed_animation_name;
int32_t freezed_animation_time_left;
};
static StorageAnimation*
animation_manager_select_idle_animation(AnimationManager* animation_manager);
static void animation_manager_replace_current_animation(
AnimationManager* animation_manager,
StorageAnimation* storage_animation);
static void animation_manager_start_new_idle(AnimationManager* animation_manager);
static bool animation_manager_check_blocking(AnimationManager* animation_manager);
void animation_manager_set_context(AnimationManager* animation_manager, void* context) {
furi_assert(animation_manager);
animation_manager->context = context;
}
void animation_manager_set_new_idle_callback(
AnimationManager* animation_manager,
AnimationManagerSetNewIdleAnimationCallback callback) {
furi_assert(animation_manager);
animation_manager->new_idle_callback = callback;
}
void animation_manager_set_check_callback(
AnimationManager* animation_manager,
AnimationManagerCheckBlockingCallback callback) {
furi_assert(animation_manager);
animation_manager->check_blocking_callback = callback;
}
void animation_manager_set_interact_callback(
AnimationManager* animation_manager,
AnimationManagerInteractCallback callback) {
furi_assert(animation_manager);
animation_manager->interact_callback = callback;
}
static void animation_manager_check_blocking_callback(const void* message, void* context) {
furi_assert(context);
AnimationManager* animation_manager = context;
if(animation_manager->check_blocking_callback) {
animation_manager->check_blocking_callback(animation_manager->context);
}
}
static void animation_manager_timer_callback(void* context) {
furi_assert(context);
AnimationManager* animation_manager = context;
if(animation_manager->new_idle_callback) {
animation_manager->new_idle_callback(animation_manager->context);
}
}
static void animation_manager_interact_callback(void* context) {
furi_assert(context);
AnimationManager* animation_manager = context;
if(animation_manager->interact_callback) {
animation_manager->interact_callback(animation_manager->context);
}
}
/* reaction to animation_manager->interact_callback() */
void animation_manager_check_blocking_process(AnimationManager* animation_manager) {
furi_assert(animation_manager);
if(animation_manager->state == AnimationManagerStateIdle) {
animation_manager_check_blocking(animation_manager);
}
}
/* reaction to animation_manager->new_idle_callback() */
void animation_manager_new_idle_process(AnimationManager* animation_manager) {
furi_assert(animation_manager);
if(animation_manager->state == AnimationManagerStateIdle) {
animation_manager_start_new_idle(animation_manager);
}
}
/* reaction to animation_manager->check_blocking_callback() */
void animation_manager_interact_process(AnimationManager* animation_manager) {
furi_assert(animation_manager);
if(animation_manager->state == AnimationManagerStateBlocked) {
/* check if new blocking animation has to be displayed */
bool blocked = animation_manager_check_blocking(animation_manager);
if(!blocked) {
animation_manager_start_new_idle(animation_manager);
}
}
}
static void animation_manager_start_new_idle(AnimationManager* animation_manager) {
furi_assert(animation_manager);
StorageAnimation* new_animation = animation_manager_select_idle_animation(animation_manager);
animation_manager_replace_current_animation(animation_manager, new_animation);
const BubbleAnimation* bubble_animation =
animation_storage_get_bubble_animation(animation_manager->current_animation);
animation_manager->state = AnimationManagerStateIdle;
osTimerStart(animation_manager->idle_animation_timer, bubble_animation->duration * 1000);
}
static bool animation_manager_check_blocking(AnimationManager* animation_manager) {
furi_assert(animation_manager);
StorageAnimation* blocking_animation = NULL;
Storage* storage = furi_record_open("storage");
FS_Error sd_status = storage_sd_status(storage);
if(sd_status == FSE_INTERNAL) {
blocking_animation = animation_storage_find_animation(BAD_SD_ANIMATION_NAME);
} else if(sd_status == FSE_NOT_READY) {
animation_manager->sd_shown_sd_ok = false;
animation_manager->sd_shown_no_db = false;
} else if(sd_status == FSE_OK) {
if(!animation_manager->sd_shown_sd_ok) {
blocking_animation = animation_storage_find_animation(SD_OK_ANIMATION_NAME);
animation_manager->sd_shown_sd_ok = true;
} else if(!animation_manager->sd_shown_no_db) {
bool db_exists = storage_common_stat(storage, "/ext/Manifest", NULL) == FSE_OK;
if(!db_exists) {
blocking_animation = animation_storage_find_animation(NO_DB_ANIMATION_NAME);
animation_manager->sd_shown_no_db = true;
animation_manager->sd_show_url = true;
}
} else if(animation_manager->sd_show_url) {
blocking_animation = animation_storage_find_animation(URL_ANIMATION_NAME);
animation_manager->sd_show_url = false;
}
}
Dolphin* dolphin = furi_record_open("dolphin");
DolphinStats stats = dolphin_stats(dolphin);
furi_record_close("dolphin");
if(!blocking_animation && stats.level_up_is_pending) {
blocking_animation = animation_storage_find_animation(LEVELUP_ANIMATION_NAME);
}
if(blocking_animation) {
osTimerStop(animation_manager->idle_animation_timer);
animation_manager_replace_current_animation(animation_manager, blocking_animation);
/* no starting timer because its blocking animation */
animation_manager->state = AnimationManagerStateBlocked;
}
furi_record_close("storage");
return !!blocking_animation;
}
static void animation_manager_replace_current_animation(
AnimationManager* animation_manager,
StorageAnimation* storage_animation) {
furi_assert(storage_animation);
StorageAnimation* previous_animation = animation_manager->current_animation;
const BubbleAnimation* animation = animation_storage_get_bubble_animation(storage_animation);
bubble_animation_view_set_animation(animation_manager->animation_view, animation);
const char* new_name = string_get_cstr(animation_storage_get_meta(storage_animation)->name);
FURI_LOG_I(TAG, "Select \'%s\' animation", new_name);
animation_manager->current_animation = storage_animation;
if(previous_animation) {
animation_storage_free_storage_animation(&previous_animation);
}
}
AnimationManager* animation_manager_alloc(void) {
animation_storage_initialize_internal_animations();
AnimationManager* animation_manager = furi_alloc(sizeof(AnimationManager));
animation_manager->animation_view = bubble_animation_view_alloc();
string_init(animation_manager->freezed_animation_name);
animation_manager->idle_animation_timer =
osTimerNew(animation_manager_timer_callback, osTimerOnce, animation_manager, NULL);
bubble_animation_view_set_interact_callback(
animation_manager->animation_view, animation_manager_interact_callback, animation_manager);
Storage* storage = furi_record_open("storage");
animation_manager->pubsub_subscription_storage = furi_pubsub_subscribe(
storage_get_pubsub(storage), animation_manager_check_blocking_callback, animation_manager);
furi_record_close("storage");
Dolphin* dolphin = furi_record_open("dolphin");
animation_manager->pubsub_subscription_dolphin = furi_pubsub_subscribe(
dolphin_get_pubsub(dolphin), animation_manager_check_blocking_callback, animation_manager);
furi_record_close("dolphin");
animation_manager->sd_shown_sd_ok = true;
if(!animation_manager_check_blocking(animation_manager)) {
animation_manager_start_new_idle(animation_manager);
}
return animation_manager;
}
void animation_manager_free(AnimationManager* animation_manager) {
furi_assert(animation_manager);
Dolphin* dolphin = furi_record_open("dolphin");
furi_pubsub_unsubscribe(
dolphin_get_pubsub(dolphin), animation_manager->pubsub_subscription_dolphin);
furi_record_close("dolphin");
Storage* storage = furi_record_open("storage");
furi_pubsub_unsubscribe(
storage_get_pubsub(storage), animation_manager->pubsub_subscription_storage);
furi_record_close("storage");
string_clear(animation_manager->freezed_animation_name);
bubble_animation_view_free(animation_manager->animation_view);
osTimerDelete(animation_manager->idle_animation_timer);
}
View* animation_manager_get_animation_view(AnimationManager* animation_manager) {
furi_assert(animation_manager);
return bubble_animation_get_view(animation_manager->animation_view);
}
static StorageAnimation*
animation_manager_select_idle_animation(AnimationManager* animation_manager) {
StorageAnimationList_t animation_list;
StorageAnimationList_init(animation_list);
animation_storage_fill_animation_list(&animation_list);
Power* power = furi_record_open("power");
PowerInfo info;
power_get_info(power, &info);
bool battery_is_well = power_is_battery_well(&info);
furi_record_close("power");
Storage* storage = furi_record_open("storage");
FS_Error sd_status = storage_sd_status(storage);
furi_record_close("storage");
Dolphin* dolphin = furi_record_open("dolphin");
DolphinStats stats = dolphin_stats(dolphin);
uint32_t whole_weight = 0;
StorageAnimationList_it_t it;
for(StorageAnimationList_it(it, animation_list); !StorageAnimationList_end_p(it);) {
StorageAnimation* storage_animation = *StorageAnimationList_ref(it);
const StorageAnimationMeta* meta = animation_storage_get_meta(storage_animation);
bool skip_animation = false;
if(battery_is_well && !string_cmp_str(meta->name, BAD_BATTERY_ANIMATION_NAME)) {
skip_animation = true;
} else if((sd_status != FSE_NOT_READY) && !string_cmp_str(meta->name, NO_SD_ANIMATION_NAME)) {
skip_animation = true;
} else if((stats.butthurt < meta->min_butthurt) || (stats.butthurt > meta->max_butthurt)) {
skip_animation = true;
} else if((stats.level < meta->min_level) || (stats.level > meta->max_level)) {
skip_animation = true;
}
if(skip_animation) {
animation_storage_free_storage_animation(&storage_animation);
/* remove and increase iterator */
StorageAnimationList_remove(animation_list, it);
} else {
whole_weight += meta->weight;
StorageAnimationList_next(it);
}
}
uint32_t lucky_number = random() % whole_weight;
uint32_t weight = 0;
StorageAnimation* selected = NULL;
for
M_EACH(item, animation_list, StorageAnimationList_t) {
if(lucky_number < weight) {
break;
}
weight += animation_storage_get_meta(*item)->weight;
selected = *item;
}
for
M_EACH(item, animation_list, StorageAnimationList_t) {
if(*item != selected) {
animation_storage_free_storage_animation(item);
}
}
StorageAnimationList_clear(animation_list);
furi_record_close("dolphin");
/* cache animation, if failed - choose reliable animation */
if(!animation_storage_get_bubble_animation(selected)) {
const char* name = string_get_cstr(animation_storage_get_meta(selected)->name);
FURI_LOG_E(TAG, "Can't upload animation described in manifest: \'%s\'", name);
animation_storage_free_storage_animation(&selected);
selected = animation_storage_find_animation(HARDCODED_ANIMATION_NAME);
}
furi_assert(selected);
return selected;
}
void animation_manager_unload_and_stall_animation(AnimationManager* animation_manager) {
furi_assert(animation_manager);
furi_assert(animation_manager->current_animation);
furi_assert(!string_size(animation_manager->freezed_animation_name));
furi_assert(
(animation_manager->state == AnimationManagerStateIdle) ||
(animation_manager->state == AnimationManagerStateBlocked));
if(animation_manager->state == AnimationManagerStateBlocked) {
animation_manager->state = AnimationManagerStateFreezedBlocked;
} else if(animation_manager->state == AnimationManagerStateIdle) {
animation_manager->state = AnimationManagerStateFreezedIdle;
animation_manager->freezed_animation_time_left =
xTimerGetExpiryTime(animation_manager->idle_animation_timer) - xTaskGetTickCount();
if(animation_manager->freezed_animation_time_left < 0) {
animation_manager->freezed_animation_time_left = 0;
}
osTimerStop(animation_manager->idle_animation_timer);
} else {
furi_assert(0);
}
StorageAnimationMeta* meta = animation_storage_get_meta(animation_manager->current_animation);
/* copy str, not move, because it can be internal animation */
string_set(animation_manager->freezed_animation_name, meta->name);
bubble_animation_freeze(animation_manager->animation_view);
animation_storage_free_storage_animation(&animation_manager->current_animation);
}
void animation_manager_load_and_continue_animation(AnimationManager* animation_manager) {
furi_assert(animation_manager);
furi_assert(!animation_manager->current_animation);
furi_assert(string_size(animation_manager->freezed_animation_name));
furi_assert(
(animation_manager->state == AnimationManagerStateFreezedIdle) ||
(animation_manager->state == AnimationManagerStateFreezedBlocked));
if(animation_manager->state == AnimationManagerStateFreezedBlocked) {
StorageAnimation* restore_animation = animation_storage_find_animation(
string_get_cstr(animation_manager->freezed_animation_name));
/* all blocked animations must be in flipper -> we can
* always find blocking animation */
furi_assert(restore_animation);
animation_manager_replace_current_animation(animation_manager, restore_animation);
animation_manager->state = AnimationManagerStateBlocked;
} else if(animation_manager->state == AnimationManagerStateFreezedIdle) {
/* check if we missed some system notifications, and set current_animation */
bool blocked = animation_manager_check_blocking(animation_manager);
if(!blocked) {
/* if no blocking - try restore last one idle */
StorageAnimation* restore_animation = animation_storage_find_animation(
string_get_cstr(animation_manager->freezed_animation_name));
if(restore_animation) {
animation_manager_replace_current_animation(animation_manager, restore_animation);
animation_manager->state = AnimationManagerStateIdle;
if(animation_manager->freezed_animation_time_left) {
osTimerStart(
animation_manager->idle_animation_timer,
animation_manager->freezed_animation_time_left);
} else {
const BubbleAnimation* animation = animation_storage_get_bubble_animation(
animation_manager->current_animation);
osTimerStart(
animation_manager->idle_animation_timer, animation->duration * 1000);
}
} else {
FURI_LOG_E(
TAG,
"Failed to restore \'%s\'",
string_get_cstr(animation_manager->freezed_animation_name));
}
}
} else {
/* Unknown state is an error. But not in release version.*/
furi_assert(0);
}
/* if can't restore previous animation - select new */
if(!animation_manager->current_animation) {
animation_manager_start_new_idle(animation_manager);
}
FURI_LOG_D(
TAG,
"Load & Continue with \'%s\'",
string_get_cstr(animation_storage_get_meta(animation_manager->current_animation)->name));
bubble_animation_unfreeze(animation_manager->animation_view);
string_reset(animation_manager->freezed_animation_name);
furi_assert(animation_manager->current_animation);
}

View File

@@ -0,0 +1,151 @@
#pragma once
#include "dolphin/dolphin.h"
#include <gui/view.h>
#include <stdint.h>
typedef struct AnimationManager AnimationManager;
typedef struct {
uint8_t x;
uint8_t y;
const char* str;
Align horizontal;
Align vertical;
} Bubble;
typedef struct FrameBubble {
Bubble bubble;
uint8_t starts_at_frame;
uint8_t ends_at_frame;
struct FrameBubble* next_bubble;
} FrameBubble;
typedef struct {
FrameBubble** frame_bubbles;
uint8_t frame_bubbles_count;
const Icon** icons;
uint8_t passive_frames;
uint8_t active_frames;
uint8_t active_cycles;
uint8_t frame_rate;
uint16_t duration;
uint16_t active_cooldown;
} BubbleAnimation;
typedef void (*AnimationManagerSetNewIdleAnimationCallback)(void* context);
typedef void (*AnimationManagerCheckBlockingCallback)(void* context);
typedef void (*AnimationManagerInteractCallback)(void*);
/**
* Allocate Animation Manager
*
* @return animation manager instance
*/
AnimationManager* animation_manager_alloc(void);
/**
* Free Animation Manager
*
* @animation_manager instance
*/
void animation_manager_free(AnimationManager* animation_manager);
/**
* Get View of Animation Manager
*
* @animation_manager instance
* @return view
*/
View* animation_manager_get_animation_view(AnimationManager* animation_manager);
/**
* Set context for all callbacks for Animation Manager
*
* @animation_manager instance
* @context context
*/
void animation_manager_set_context(AnimationManager* animation_manager, void* context);
/**
* Set callback for Animation Manager for defered calls
* for animation_manager_new_idle_process().
* Animation Manager doesn't have it's own thread, so main thread gives
* callbacks to A.M. to call when it should perform some inner manipulations.
* This callback is called from other threads and should notify main thread
* when to call animation_manager_new_idle_process().
* So scheme is this:
* A.M. sets callbacks,
* callbacks notifies main thread
* main thread in its own context calls appropriate *_process() function.
*
* @animation_manager instance
* @callback callback
*/
void animation_manager_set_new_idle_callback(
AnimationManager* animation_manager,
AnimationManagerSetNewIdleAnimationCallback callback);
/**
* Function to call in main thread as a response to
* set_new_idle_callback's call.
*
* @animation_manager instance
*/
void animation_manager_new_idle_process(AnimationManager* animation_manager);
/**
* Set callback for Animation Manager for defered calls
* for animation_manager_check_blocking_process().
*
* @animation_manager instance
* @callback callback
*/
void animation_manager_set_check_callback(
AnimationManager* animation_manager,
AnimationManagerCheckBlockingCallback callback);
/**
* Function to call in main thread as a response to
* set_new_idle_callback's call.
*
* @animation_manager instance
*/
void animation_manager_check_blocking_process(AnimationManager* animation_manager);
/**
* Set callback for Animation Manager for defered calls
* for animation_manager_interact_process().
*
* @animation_manager instance
* @callback callback
*/
void animation_manager_set_interact_callback(
AnimationManager* animation_manager,
AnimationManagerInteractCallback callback);
/**
* Function to call in main thread as a response to
* set_new_idle_callback's call.
*
* @animation_manager instance
*/
void animation_manager_interact_process(AnimationManager* animation_manager);
/**
* Unload and Stall animation actions. Draw callback in view
* paints first frame of current animation until
* animation_manager_load_and_continue_animation() is called.
* Can't be called multiple times. Every Stall has to be finished
* with Continue.
*
* @animation_manager instance
*/
void animation_manager_unload_and_stall_animation(AnimationManager* animation_manager);
/**
* Load and Contunue execution of animation manager.
*
* @animation_manager instance
*/
void animation_manager_load_and_continue_animation(AnimationManager* animation_manager);

View File

@@ -0,0 +1,482 @@
#include "animation_manager.h"
#include "file-worker.h"
#include "flipper_file.h"
#include "furi/common_defines.h"
#include "furi/memmgr.h"
#include "furi/record.h"
#include "animation_storage.h"
#include "gui/canvas.h"
#include "m-string.h"
#include "pb.h"
#include "pb_decode.h"
#include "storage/filesystem-api-defines.h"
#include "storage/storage.h"
#include "animation_storage_i.h"
#include <stdint.h>
#include <gui/icon_i.h>
#define ANIMATION_META_FILE "meta.txt"
#define ANIMATION_DIR "/ext/dolphin/animations"
#define ANIMATION_MANIFEST_FILE ANIMATION_DIR "/manifest.txt"
#define TAG "AnimationStorage"
#define DEBUG_PB 0
static void animation_storage_free_bubbles(BubbleAnimation* animation);
static void animation_storage_free_frames(BubbleAnimation* animation);
static void animation_storage_free_animation(BubbleAnimation** storage_animation);
static BubbleAnimation* animation_storage_load_animation(const char* name);
void animation_storage_fill_animation_list(StorageAnimationList_t* animation_list) {
furi_assert(sizeof(StorageAnimationList_t) == sizeof(void*));
furi_assert(!StorageAnimationList_size(*animation_list));
Storage* storage = furi_record_open("storage");
FlipperFile* file = flipper_file_alloc(storage);
/* Forbid skipping fields */
flipper_file_set_strict_mode(file, true);
string_t header;
string_init(header);
do {
uint32_t u32value;
StorageAnimation* storage_animation = NULL;
if(FSE_OK != storage_sd_status(storage)) break;
if(!flipper_file_open_existing(file, ANIMATION_MANIFEST_FILE)) break;
if(!flipper_file_read_header(file, header, &u32value)) break;
if(string_cmp_str(header, "Flipper Animation Manifest")) break;
do {
storage_animation = furi_alloc(sizeof(StorageAnimation));
storage_animation->external = true;
storage_animation->animation = NULL;
if(!flipper_file_read_string(file, "Name", storage_animation->meta.name)) break;
if(!flipper_file_read_uint32(file, "Min butthurt", &u32value, 1)) break;
storage_animation->meta.min_butthurt = u32value;
if(!flipper_file_read_uint32(file, "Max butthurt", &u32value, 1)) break;
storage_animation->meta.max_butthurt = u32value;
if(!flipper_file_read_uint32(file, "Min level", &u32value, 1)) break;
storage_animation->meta.min_level = u32value;
if(!flipper_file_read_uint32(file, "Max level", &u32value, 1)) break;
storage_animation->meta.max_level = u32value;
if(!flipper_file_read_uint32(file, "Weight", &u32value, 1)) break;
storage_animation->meta.weight = u32value;
StorageAnimationList_push_back(*animation_list, storage_animation);
} while(1);
animation_storage_free_storage_animation(&storage_animation);
} while(0);
string_clear(header);
flipper_file_close(file);
flipper_file_free(file);
// add hard-coded animations
for(int i = 0; i < COUNT_OF(StorageAnimationInternal); ++i) {
StorageAnimationList_push_back(*animation_list, &StorageAnimationInternal[i]);
}
furi_record_close("storage");
}
StorageAnimation* animation_storage_find_animation(const char* name) {
furi_assert(name);
furi_assert(strlen(name));
StorageAnimation* storage_animation = NULL;
/* look through internal animations */
for(int i = 0; i < COUNT_OF(StorageAnimationInternal); ++i) {
if(!string_cmp_str(StorageAnimationInternal[i].meta.name, name)) {
storage_animation = &StorageAnimationInternal[i];
break;
}
}
/* look through external animations */
if(!storage_animation) {
BubbleAnimation* animation = animation_storage_load_animation(name);
if(animation != NULL) {
storage_animation = furi_alloc(sizeof(StorageAnimation));
storage_animation->animation = animation;
storage_animation->external = true;
/* meta data takes part in random animation selection, so it
* doesn't need here as we exactly know which animation we need,
* that's why we can ignore reading manifest.txt file
* filling meta data by zeroes */
storage_animation->meta.min_butthurt = 0;
storage_animation->meta.max_butthurt = 0;
storage_animation->meta.min_level = 0;
storage_animation->meta.max_level = 0;
storage_animation->meta.weight = 0;
string_init_set_str(storage_animation->meta.name, name);
}
}
return storage_animation;
}
StorageAnimationMeta* animation_storage_get_meta(StorageAnimation* storage_animation) {
furi_assert(storage_animation);
return &storage_animation->meta;
}
const BubbleAnimation*
animation_storage_get_bubble_animation(StorageAnimation* storage_animation) {
furi_assert(storage_animation);
animation_storage_cache_animation(storage_animation);
return storage_animation->animation;
}
void animation_storage_cache_animation(StorageAnimation* storage_animation) {
furi_assert(storage_animation);
if(storage_animation->external) {
if(!storage_animation->animation) {
storage_animation->animation =
animation_storage_load_animation(string_get_cstr(storage_animation->meta.name));
}
}
}
static void animation_storage_free_animation(BubbleAnimation** animation) {
furi_assert(animation);
if(*animation) {
animation_storage_free_bubbles(*animation);
animation_storage_free_frames(*animation);
free(*animation);
*animation = NULL;
}
}
void animation_storage_free_storage_animation(StorageAnimation** storage_animation) {
furi_assert(storage_animation);
furi_assert(*storage_animation);
if((*storage_animation)->external) {
animation_storage_free_animation((BubbleAnimation**)&(*storage_animation)->animation);
string_clear((*storage_animation)->meta.name);
free(*storage_animation);
}
*storage_animation = NULL;
}
static bool animation_storage_cast_align(string_t align_str, Align* align) {
if(!string_cmp_str(align_str, "Bottom")) {
*align = AlignBottom;
} else if(!string_cmp_str(align_str, "Top")) {
*align = AlignTop;
} else if(!string_cmp_str(align_str, "Left")) {
*align = AlignLeft;
} else if(!string_cmp_str(align_str, "Right")) {
*align = AlignRight;
} else if(!string_cmp_str(align_str, "Center")) {
*align = AlignCenter;
} else {
return false;
}
return true;
}
static void animation_storage_free_frames(BubbleAnimation* animation) {
furi_assert(animation);
furi_assert(animation->icons);
const Icon** icons = animation->icons;
uint16_t frames = animation->active_frames + animation->passive_frames;
furi_assert(frames > 0);
for(int i = 0; i < frames; ++i) {
if(!icons[i]) continue;
const Icon* icon = icons[i];
free((void*)icon->frames[0]);
free(icon->frames);
free((void*)icon);
for(int j = i; j < frames; ++j) {
if(icons[j] == icon) {
icons[j] = NULL;
}
}
}
free(animation->icons);
animation->icons = NULL;
}
static Icon* animation_storage_alloc_icon(size_t frame_size) {
Icon* icon = furi_alloc(sizeof(Icon));
icon->frames = furi_alloc(sizeof(const uint8_t*));
icon->frames[0] = furi_alloc(frame_size);
return icon;
}
static void animation_storage_free_icon(Icon* icon) {
free((void*)icon->frames[0]);
free(icon->frames);
free(icon);
}
static bool animation_storage_load_frames(
Storage* storage,
const char* name,
BubbleAnimation* animation,
uint32_t* frame_order,
uint32_t width,
uint32_t height) {
furi_assert(!animation->icons);
uint16_t frame_order_size = animation->passive_frames + animation->active_frames;
bool frames_ok = false;
animation->icons = furi_alloc(sizeof(const Icon*) * frame_order_size);
File* file = storage_file_alloc(storage);
FileInfo file_info;
string_t filename;
string_init(filename);
size_t max_filesize = ROUND_UP_TO(width, 8) * height + 1;
for(int i = 0; i < frame_order_size; ++i) {
if(animation->icons[i]) continue;
frames_ok = false;
string_printf(filename, ANIMATION_DIR "/%s/frame_%d.bm", name, frame_order[i]);
if(storage_common_stat(storage, string_get_cstr(filename), &file_info) != FSE_OK) break;
if(file_info.size > max_filesize) {
FURI_LOG_E(
TAG,
"Filesize %d, max: %d (width %d, height %d)",
file_info.size,
max_filesize,
width,
height);
break;
}
if(!storage_file_open(file, string_get_cstr(filename), FSAM_READ, FSOM_OPEN_EXISTING)) {
FURI_LOG_E(TAG, "Can't open file \'%s\'", string_get_cstr(filename));
break;
}
Icon* icon = animation_storage_alloc_icon(file_info.size);
if(storage_file_read(file, (void*)icon->frames[0], file_info.size) != file_info.size) {
FURI_LOG_E(TAG, "Read failed: \'%s\'", string_get_cstr(filename));
animation_storage_free_icon(icon);
break;
}
storage_file_close(file);
FURI_CONST_ASSIGN(icon->frame_count, 1);
FURI_CONST_ASSIGN(icon->frame_rate, 0);
FURI_CONST_ASSIGN(icon->height, height);
FURI_CONST_ASSIGN(icon->width, width);
/* Claim 1 allocation for 1 files blob and several links to it */
for(int j = i; j < frame_order_size; ++j) {
if(frame_order[i] == frame_order[j]) {
animation->icons[j] = icon;
}
}
frames_ok = true;
}
if(!frames_ok) {
FURI_LOG_E(
TAG,
"Load \'%s\' failed, %dx%d, size: %d",
string_get_cstr(filename),
width,
height,
file_info.size);
animation_storage_free_frames(animation);
animation->icons = NULL;
} else {
for(int i = 0; i < frame_order_size; ++i) {
furi_check(animation->icons[i]);
furi_check(animation->icons[i]->frames[0]);
}
}
storage_file_free(file);
string_clear(filename);
return frames_ok;
}
static bool animation_storage_load_bubbles(BubbleAnimation* animation, FlipperFile* ff) {
uint32_t u32value;
string_t str;
string_init(str);
bool success = false;
furi_assert(!animation->frame_bubbles);
do {
if(!flipper_file_read_uint32(ff, "Bubble slots", &u32value, 1)) break;
if(u32value > 20) break;
animation->frame_bubbles_count = u32value;
if(animation->frame_bubbles_count == 0) {
animation->frame_bubbles = NULL;
success = true;
break;
}
animation->frame_bubbles =
furi_alloc(sizeof(FrameBubble*) * animation->frame_bubbles_count);
uint32_t current_slot = 0;
for(int i = 0; i < animation->frame_bubbles_count; ++i) {
animation->frame_bubbles[i] = furi_alloc(sizeof(FrameBubble));
}
FrameBubble* bubble = animation->frame_bubbles[0];
int8_t index = -1;
for(;;) {
if(!flipper_file_read_uint32(ff, "Slot", &current_slot, 1)) break;
if((current_slot != 0) && (index == -1)) break;
if(current_slot == index) {
bubble->next_bubble = furi_alloc(sizeof(FrameBubble));
bubble = bubble->next_bubble;
} else if(current_slot == index + 1) {
++index;
bubble = animation->frame_bubbles[index];
} else {
/* slots have to start from 0, be ascending sorted, and
* have exact number of slots as specified in "Bubble slots" */
break;
}
if(index >= animation->frame_bubbles_count) break;
if(!flipper_file_read_uint32(ff, "X", &u32value, 1)) break;
bubble->bubble.x = u32value;
if(!flipper_file_read_uint32(ff, "Y", &u32value, 1)) break;
bubble->bubble.y = u32value;
if(!flipper_file_read_string(ff, "Text", str)) break;
if(string_size(str) > 100) break;
string_replace_all_str(str, "\\n", "\n");
bubble->bubble.str = furi_alloc(string_size(str) + 1);
strcpy((char*)bubble->bubble.str, string_get_cstr(str));
if(!flipper_file_read_string(ff, "AlignH", str)) break;
if(!animation_storage_cast_align(str, &bubble->bubble.horizontal)) break;
if(!flipper_file_read_string(ff, "AlignV", str)) break;
if(!animation_storage_cast_align(str, &bubble->bubble.vertical)) break;
if(!flipper_file_read_uint32(ff, "StartFrame", &u32value, 1)) break;
bubble->starts_at_frame = u32value;
if(!flipper_file_read_uint32(ff, "EndFrame", &u32value, 1)) break;
bubble->ends_at_frame = u32value;
}
success = (index + 1) == animation->frame_bubbles_count;
} while(0);
if(!success) {
if(animation->frame_bubbles) {
FURI_LOG_E(TAG, "Failed to load animation bubbles");
animation_storage_free_bubbles(animation);
}
}
string_clear(str);
return success;
}
static BubbleAnimation* animation_storage_load_animation(const char* name) {
furi_assert(name);
BubbleAnimation* animation = furi_alloc(sizeof(BubbleAnimation));
uint32_t height = 0;
uint32_t width = 0;
uint32_t* u32array = NULL;
Storage* storage = furi_record_open("storage");
FlipperFile* ff = flipper_file_alloc(storage);
/* Forbid skipping fields */
flipper_file_set_strict_mode(ff, true);
string_t str;
string_init(str);
animation->frame_bubbles = NULL;
bool success = false;
do {
uint32_t u32value;
if(FSE_OK != storage_sd_status(storage)) break;
string_printf(str, ANIMATION_DIR "/%s/" ANIMATION_META_FILE, name);
if(!flipper_file_open_existing(ff, string_get_cstr(str))) break;
if(!flipper_file_read_header(ff, str, &u32value)) break;
if(string_cmp_str(str, "Flipper Animation")) break;
if(!flipper_file_read_uint32(ff, "Width", &width, 1)) break;
if(!flipper_file_read_uint32(ff, "Height", &height, 1)) break;
if(!flipper_file_read_uint32(ff, "Passive frames", &u32value, 1)) break;
animation->passive_frames = u32value;
if(!flipper_file_read_uint32(ff, "Active frames", &u32value, 1)) break;
animation->active_frames = u32value;
uint8_t frames = animation->passive_frames + animation->active_frames;
u32array = furi_alloc(sizeof(uint32_t) * frames);
if(!flipper_file_read_uint32(ff, "Frames order", u32array, frames)) break;
/* passive and active frames must be loaded up to this point */
if(!animation_storage_load_frames(storage, name, animation, u32array, width, height))
break;
if(!flipper_file_read_uint32(ff, "Active cycles", &u32value, 1)) break;
animation->active_cycles = u32value;
if(!flipper_file_read_uint32(ff, "Frame rate", &u32value, 1)) break;
animation->frame_rate = u32value;
if(!flipper_file_read_uint32(ff, "Duration", &u32value, 1)) break;
animation->duration = u32value;
if(!flipper_file_read_uint32(ff, "Active cooldown", &u32value, 1)) break;
animation->active_cooldown = u32value;
if(!animation_storage_load_bubbles(animation, ff)) break;
success = true;
} while(0);
string_clear(str);
flipper_file_close(ff);
flipper_file_free(ff);
if(u32array) {
free(u32array);
}
if(!success) {
free(animation);
animation = NULL;
}
return animation;
}
static void animation_storage_free_bubbles(BubbleAnimation* animation) {
if(!animation->frame_bubbles) return;
for(int i = 0; i < animation->frame_bubbles_count;) {
FrameBubble** bubble = &animation->frame_bubbles[i];
if((*bubble) == NULL) break;
while((*bubble)->next_bubble != NULL) {
bubble = &(*bubble)->next_bubble;
}
if((*bubble)->bubble.str) {
free((void*)(*bubble)->bubble.str);
}
if((*bubble) == animation->frame_bubbles[i]) {
++i;
}
free(*bubble);
*bubble = NULL;
}
free(animation->frame_bubbles);
animation->frame_bubbles = NULL;
}

View File

@@ -0,0 +1,98 @@
#pragma once
#include <stdint.h>
#include <m-list.h>
#include "views/bubble_animation_view.h"
#include <m-string.h>
#define HARDCODED_ANIMATION_NAME "tv"
#define NO_SD_ANIMATION_NAME "no_sd"
#define BAD_BATTERY_ANIMATION_NAME "bad_battery"
#define NO_DB_ANIMATION_NAME "no_db"
#define BAD_SD_ANIMATION_NAME "bad_sd"
#define SD_OK_ANIMATION_NAME "sd_ok"
#define URL_ANIMATION_NAME "url"
#define LEVELUP_ANIMATION_NAME "level"
/** Main structure to handle animation data.
* Contains all, including animation playing data (BubbleAnimation),
* data for random animation selection (StorageAnimationMeta) and
* flag of location internal/external */
typedef struct StorageAnimation StorageAnimation;
typedef struct {
string_t name;
uint8_t min_butthurt;
uint8_t max_butthurt;
uint8_t min_level;
uint8_t max_level;
uint8_t weight;
} StorageAnimationMeta;
/** Container to return available animations list */
LIST_DEF(StorageAnimationList, StorageAnimation*, M_PTR_OPLIST)
#define M_OPL_StorageAnimationList_t() LIST_OPLIST(StorageAnimationList)
/**
* Fill list of available animations.
* List will contain all idle animations on inner flash
* and all available on SD-card, mentioned in manifest.txt.
* Performs caching of animation. If fail - falls back to
* inner animation.
* List has to be initialized.
*
* @list list to fill with animations data
*/
void animation_storage_fill_animation_list(StorageAnimationList_t* list);
/**
* Get bubble animation of storage animation.
* Bubble Animation is a structure which describes animation
* independent of it's place of storage and meta data.
* It contain all what is need to be played.
* If storage_animation is not cached - caches it.
*
* @storage_animation animation from which extract bubble animation
* @return bubble_animation, NULL if failed to cache data.
*/
const BubbleAnimation* animation_storage_get_bubble_animation(StorageAnimation* storage_animation);
/**
* Performs caching animation data (Bubble Animation)
* if this is not done yet.
*
* @storage_animation animation to cache
*/
void animation_storage_cache_animation(StorageAnimation* storage_animation);
/**
* Find animation by name.
* Search through the inner flash, and SD-card if has.
*
* @name name of animation
* @return found animation. NULL if nothing found.
*/
StorageAnimation* animation_storage_find_animation(const char* name);
/**
* Get meta information of storage animation.
* This information allows to randomly select animation.
* Also it contains name. Never returns NULL.
*
* @storage_animation item of whom we have to extract meta.
* @return meta itself
*/
StorageAnimationMeta* animation_storage_get_meta(StorageAnimation* storage_animation);
/**
* Free storage_animation, which previously acquired
* by Animation Storage.
*
* @storage_animation item to free. NULL-ed after all.
*/
void animation_storage_free_storage_animation(StorageAnimation** storage_animation);
/**
* Has to be called at least 1 time to initialize runtime structures
* of animations in inner flash.
*/
void animation_storage_initialize_internal_animations(void);

View File

@@ -0,0 +1,195 @@
#pragma once
#include "animation_storage.h"
#include "assets_icons.h"
#include "animation_manager.h"
#include "gui/canvas.h"
struct StorageAnimation {
const BubbleAnimation* animation;
bool external;
StorageAnimationMeta meta;
};
// Hard-coded, always available idle animation
FrameBubble tv_bubble1 = {
.bubble =
{.x = 1,
.y = 23,
.str = "Take the red pill",
.horizontal = AlignRight,
.vertical = AlignBottom},
.starts_at_frame = 7,
.ends_at_frame = 9,
.next_bubble = NULL,
};
FrameBubble tv_bubble2 = {
.bubble =
{.x = 1,
.y = 23,
.str = "I can joke better",
.horizontal = AlignRight,
.vertical = AlignBottom},
.starts_at_frame = 7,
.ends_at_frame = 9,
.next_bubble = NULL,
};
FrameBubble* tv_bubbles[] = {&tv_bubble1, &tv_bubble2};
const Icon* tv_icons[] = {
&I_tv1,
&I_tv2,
&I_tv3,
&I_tv4,
&I_tv5,
&I_tv6,
&I_tv7,
&I_tv8,
};
const BubbleAnimation tv_bubble_animation = {
.icons = tv_icons,
.frame_bubbles = tv_bubbles,
.frame_bubbles_count = COUNT_OF(tv_bubbles),
.passive_frames = 6,
.active_frames = 2,
.active_cycles = 2,
.frame_rate = 2,
.duration = 3600,
.active_cooldown = 5,
};
// System animation - no SD card
const Icon* no_sd_icons[] = {
&I_no_sd1,
&I_no_sd2,
&I_no_sd1,
&I_no_sd2,
&I_no_sd1,
&I_no_sd3,
&I_no_sd4,
&I_no_sd5,
&I_no_sd4,
&I_no_sd6,
};
FrameBubble no_sd_bubble = {
.bubble =
{.x = 40,
.y = 18,
.str = "Need an\nSD card",
.horizontal = AlignRight,
.vertical = AlignBottom},
.starts_at_frame = 0,
.ends_at_frame = 9,
.next_bubble = NULL,
};
FrameBubble* no_sd_bubbles[] = {&no_sd_bubble};
const BubbleAnimation no_sd_bubble_animation = {
.icons = no_sd_icons,
.frame_bubbles = no_sd_bubbles,
.frame_bubbles_count = COUNT_OF(no_sd_bubbles),
.passive_frames = 10,
.active_frames = 0,
.frame_rate = 2,
.duration = 3600,
.active_cooldown = 0,
.active_cycles = 0,
};
// BLOCKING ANIMATION - no_db, bad_sd, sd_ok, url
const Icon* no_db_icons[] = {
&I_no_databases1,
&I_no_databases2,
&I_no_databases3,
&I_no_databases4,
};
const BubbleAnimation no_db_bubble_animation = {
.icons = no_db_icons,
.passive_frames = COUNT_OF(no_db_icons),
.frame_rate = 2,
};
const Icon* bad_sd_icons[] = {
&I_card_bad1,
&I_card_bad2,
};
const BubbleAnimation bad_sd_bubble_animation = {
.icons = bad_sd_icons,
.passive_frames = COUNT_OF(bad_sd_icons),
.frame_rate = 2,
};
const Icon* url_icons[] = {
&I_url1,
&I_url2,
&I_url3,
&I_url4,
};
const BubbleAnimation url_bubble_animation = {
.icons = url_icons,
.passive_frames = COUNT_OF(url_icons),
.frame_rate = 2,
};
const Icon* sd_ok_icons[] = {
&I_card_ok1,
&I_card_ok2,
&I_card_ok3,
&I_card_ok4,
};
const BubbleAnimation sd_ok_bubble_animation = {
.icons = sd_ok_icons,
.passive_frames = COUNT_OF(sd_ok_icons),
.frame_rate = 2,
};
static StorageAnimation StorageAnimationInternal[] = {
{.animation = &tv_bubble_animation,
.external = false,
.meta =
{
.min_butthurt = 0,
.max_butthurt = 11,
.min_level = 1,
.max_level = 3,
.weight = 3,
}},
{.animation = &no_sd_bubble_animation,
.external = false,
.meta =
{
.min_butthurt = 0,
.max_butthurt = 14,
.min_level = 1,
.max_level = 3,
.weight = 6,
}},
{
.animation = &no_db_bubble_animation,
.external = false,
},
{
.animation = &bad_sd_bubble_animation,
.external = false,
},
{
.animation = &sd_ok_bubble_animation,
.external = false,
},
{
.animation = &url_bubble_animation,
.external = false,
},
};
void animation_storage_initialize_internal_animations(void) {
/* not in constructor - no memory pool yet */
/* called in 1 thread - no need in double check */
static bool initialized = false;
if(!initialized) {
initialized = true;
string_init_set_str(StorageAnimationInternal[0].meta.name, HARDCODED_ANIMATION_NAME);
string_init_set_str(StorageAnimationInternal[1].meta.name, NO_SD_ANIMATION_NAME);
string_init_set_str(StorageAnimationInternal[2].meta.name, NO_DB_ANIMATION_NAME);
string_init_set_str(StorageAnimationInternal[3].meta.name, BAD_SD_ANIMATION_NAME);
string_init_set_str(StorageAnimationInternal[4].meta.name, SD_OK_ANIMATION_NAME);
string_init_set_str(StorageAnimationInternal[5].meta.name, URL_ANIMATION_NAME);
}
}

View File

@@ -0,0 +1,410 @@
#include "cmsis_os2.h"
#include "../animation_manager.h"
#include "../animation_storage.h"
#include "furi-hal-delay.h"
#include "furi-hal-resources.h"
#include "furi/check.h"
#include "furi/memmgr.h"
#include "gui/canvas.h"
#include "gui/elements.h"
#include "gui/view.h"
#include "input/input.h"
#include <furi.h>
#include "portmacro.h"
#include <gui/icon.h>
#include <stdint.h>
#include <FreeRTOS.h>
#include <timers.h>
#include "bubble_animation_view.h"
#include <gui/icon_i.h>
#define ACTIVE_SHIFT 2
typedef struct {
const BubbleAnimation* current;
const FrameBubble* current_bubble;
uint8_t current_frame;
uint8_t active_cycle;
uint8_t active_bubbles;
uint8_t passive_bubbles;
uint8_t active_shift;
TickType_t active_ended_at;
Icon* freeze_frame;
} BubbleAnimationViewModel;
struct BubbleAnimationView {
View* view;
osTimerId_t timer;
BubbleAnimationInteractCallback interact_callback;
void* interact_callback_context;
};
static void bubble_animation_activate(BubbleAnimationView* view, bool force);
static void bubble_animation_activate_right_now(BubbleAnimationView* view);
static uint8_t bubble_animation_get_icon_index(BubbleAnimationViewModel* model) {
furi_assert(model);
uint8_t icon_index = 0;
const BubbleAnimation* animation = model->current;
if(model->current_frame < animation->passive_frames) {
icon_index = model->current_frame;
} else {
icon_index =
(model->current_frame - animation->passive_frames) % animation->active_frames +
animation->passive_frames;
}
furi_assert(icon_index < (animation->passive_frames + animation->active_frames));
return icon_index;
}
static void bubble_animation_draw_callback(Canvas* canvas, void* model_) {
furi_assert(model_);
furi_assert(canvas);
BubbleAnimationViewModel* model = model_;
const BubbleAnimation* animation = model->current;
if(model->freeze_frame) {
uint8_t y_offset = canvas_height(canvas) - icon_get_height(model->freeze_frame);
canvas_draw_icon(canvas, 0, y_offset, model->freeze_frame);
return;
}
if(!animation) {
return;
}
furi_assert(model->current_frame < 255);
const Icon* icon = animation->icons[bubble_animation_get_icon_index(model)];
furi_assert(icon);
uint8_t y_offset = canvas_height(canvas) - icon_get_height(icon);
canvas_draw_icon(canvas, 0, y_offset, icon);
const FrameBubble* bubble = model->current_bubble;
if(bubble) {
if((model->current_frame >= bubble->starts_at_frame) &&
(model->current_frame <= bubble->ends_at_frame)) {
const Bubble* b = &bubble->bubble;
elements_bubble_str(canvas, b->x, b->y, b->str, b->horizontal, b->vertical);
}
}
}
static FrameBubble* bubble_animation_pick_bubble(BubbleAnimationViewModel* model, bool active) {
FrameBubble* bubble = NULL;
if((model->active_bubbles == 0) && (model->passive_bubbles == 0)) {
return NULL;
}
uint8_t index = random() % (active ? model->active_bubbles : model->passive_bubbles);
const BubbleAnimation* animation = model->current;
for(int i = 0; i < animation->frame_bubbles_count; ++i) {
if((animation->frame_bubbles[i]->starts_at_frame < animation->passive_frames) ^ active) {
if(!index) {
bubble = animation->frame_bubbles[i];
}
--index;
}
}
return bubble;
}
static bool bubble_animation_input_callback(InputEvent* event, void* context) {
furi_assert(context);
furi_assert(event);
BubbleAnimationView* animation_view = context;
bool consumed = false;
if(event->type == InputTypePress) {
bubble_animation_activate(animation_view, false);
}
if(event->key == InputKeyRight) {
/* Right button reserved for animation activation, so consume */
consumed = true;
if(event->type == InputTypeShort) {
if(animation_view->interact_callback) {
animation_view->interact_callback(animation_view->interact_callback_context);
}
}
} else if(event->key == InputKeyBack) {
/* Prevent back button to fall down to common handler - leaving
* application, so consume */
consumed = true;
}
return consumed;
}
static void bubble_animation_activate(BubbleAnimationView* view, bool force) {
furi_assert(view);
bool activate = true;
BubbleAnimationViewModel* model = view_get_model(view->view);
if(!model->current) {
activate = false;
} else if(model->freeze_frame) {
activate = false;
} else if(model->current->active_frames == 0) {
activate = false;
}
if(!force) {
if((model->active_ended_at + model->current->active_cooldown * 1000) >
xTaskGetTickCount()) {
activate = false;
} else if(model->active_shift) {
activate = false;
} else if(model->current_frame >= model->current->passive_frames) {
activate = false;
}
}
view_commit_model(view->view, false);
if(!activate && !force) {
return;
}
if(ACTIVE_SHIFT > 0) {
BubbleAnimationViewModel* model = view_get_model(view->view);
model->active_shift = ACTIVE_SHIFT;
view_commit_model(view->view, false);
} else {
bubble_animation_activate_right_now(view);
}
}
static void bubble_animation_activate_right_now(BubbleAnimationView* view) {
furi_assert(view);
uint8_t frame_rate = 0;
BubbleAnimationViewModel* model = view_get_model(view->view);
if(model->current && (model->current->active_frames > 0) && (!model->freeze_frame)) {
model->current_frame = model->current->passive_frames;
model->current_bubble = bubble_animation_pick_bubble(model, true);
frame_rate = model->current->frame_rate;
}
view_commit_model(view->view, true);
if(frame_rate) {
osTimerStart(view->timer, 1000 / frame_rate);
}
}
static void bubble_animation_next_frame(BubbleAnimationViewModel* model) {
furi_assert(model);
if(!model->current) {
return;
}
if(model->current_frame < model->current->passive_frames) {
model->current_frame = (model->current_frame + 1) % model->current->passive_frames;
} else {
++model->current_frame;
model->active_cycle +=
!((model->current_frame - model->current->passive_frames) %
model->current->active_frames);
if(model->active_cycle >= model->current->active_cycles) {
// switch to passive
model->active_cycle = 0;
model->current_frame = 0;
model->current_bubble = bubble_animation_pick_bubble(model, false);
model->active_ended_at = xTaskGetTickCount();
}
if(model->current_bubble) {
if(model->current_frame > model->current_bubble->ends_at_frame) {
model->current_bubble = model->current_bubble->next_bubble;
}
}
}
}
static void bubble_animation_timer_callback(void* context) {
furi_assert(context);
BubbleAnimationView* view = context;
bool activate = false;
BubbleAnimationViewModel* model = view_get_model(view->view);
if(model->active_shift > 0) {
activate = (--model->active_shift == 0);
}
if(!model->freeze_frame && !activate) {
bubble_animation_next_frame(model);
}
view_commit_model(view->view, !activate);
if(activate) {
bubble_animation_activate_right_now(view);
}
}
static Icon* bubble_animation_clone_frame(const Icon* icon_orig) {
furi_assert(icon_orig);
furi_assert(icon_orig->frames);
furi_assert(icon_orig->frames[0]);
Icon* icon_clone = furi_alloc(sizeof(Icon));
memcpy(icon_clone, icon_orig, sizeof(Icon));
icon_clone->frames = furi_alloc(sizeof(uint8_t*));
/* icon bitmap can be either compressed or not. It is compressed if
* compressed size is less than original, so max size for bitmap is
* uncompressed (width * height) + 1 byte (in uncompressed case)
* for compressed header
*/
size_t max_bitmap_size = ROUND_UP_TO(icon_orig->width, 8) * icon_orig->height + 1;
icon_clone->frames[0] = furi_alloc(max_bitmap_size);
memcpy((void*)icon_clone->frames[0], icon_orig->frames[0], max_bitmap_size);
return icon_clone;
}
static void bubble_animation_release_frame(Icon** icon) {
furi_assert(icon);
furi_assert(*icon);
free((void*)(*icon)->frames[0]);
free((*icon)->frames);
free(*icon);
*icon = NULL;
}
static void bubble_animation_enter(void* context) {
furi_assert(context);
BubbleAnimationView* view = context;
bubble_animation_activate(view, false);
BubbleAnimationViewModel* model = view_get_model(view->view);
uint8_t frame_rate = model->current->frame_rate;
view_commit_model(view->view, false);
if(frame_rate) {
osTimerStart(view->timer, 1000 / frame_rate);
}
}
static void bubble_animation_exit(void* context) {
furi_assert(context);
BubbleAnimationView* view = context;
osTimerStop(view->timer);
}
BubbleAnimationView* bubble_animation_view_alloc(void) {
BubbleAnimationView* view = furi_alloc(sizeof(BubbleAnimationView));
view->view = view_alloc();
view->interact_callback = NULL;
view->timer = osTimerNew(bubble_animation_timer_callback, osTimerPeriodic, view, NULL);
view_allocate_model(view->view, ViewModelTypeLocking, sizeof(BubbleAnimationViewModel));
view_set_context(view->view, view);
view_set_draw_callback(view->view, bubble_animation_draw_callback);
view_set_input_callback(view->view, bubble_animation_input_callback);
view_set_enter_callback(view->view, bubble_animation_enter);
view_set_exit_callback(view->view, bubble_animation_exit);
return view;
}
void bubble_animation_view_free(BubbleAnimationView* view) {
furi_assert(view);
view_set_draw_callback(view->view, NULL);
view_set_input_callback(view->view, NULL);
view_set_context(view->view, NULL);
view_free(view->view);
view->view = NULL;
free(view);
}
void bubble_animation_view_set_interact_callback(
BubbleAnimationView* view,
BubbleAnimationInteractCallback callback,
void* context) {
furi_assert(view);
view->interact_callback_context = context;
view->interact_callback = callback;
}
void bubble_animation_view_set_animation(
BubbleAnimationView* view,
const BubbleAnimation* new_animation) {
furi_assert(view);
furi_assert(new_animation);
BubbleAnimationViewModel* model = view_get_model(view->view);
furi_assert(model);
model->current = new_animation;
model->active_ended_at = xTaskGetTickCount() - (model->current->active_cooldown * 1000);
model->active_bubbles = 0;
model->passive_bubbles = 0;
for(int i = 0; i < new_animation->frame_bubbles_count; ++i) {
if(new_animation->frame_bubbles[i]->starts_at_frame < new_animation->passive_frames) {
++model->passive_bubbles;
} else {
++model->active_bubbles;
}
}
/* select bubble sequence */
model->current_bubble = bubble_animation_pick_bubble(model, false);
model->current_frame = 0;
model->active_cycle = 0;
view_commit_model(view->view, true);
osTimerStart(view->timer, 1000 / new_animation->frame_rate);
}
void bubble_animation_freeze(BubbleAnimationView* view) {
furi_assert(view);
BubbleAnimationViewModel* model = view_get_model(view->view);
furi_assert(model->current);
furi_assert(!model->freeze_frame);
/* always freeze first passive frame, because
* animation is always activated at unfreezing and played
* passive frame first, and 2 frames after - active
*/
uint8_t icon_index = 0;
model->freeze_frame = bubble_animation_clone_frame(model->current->icons[icon_index]);
model->current = NULL;
view_commit_model(view->view, false);
osTimerStop(view->timer);
}
void bubble_animation_unfreeze(BubbleAnimationView* view) {
furi_assert(view);
uint8_t frame_rate;
BubbleAnimationViewModel* model = view_get_model(view->view);
furi_assert(model->freeze_frame);
bubble_animation_release_frame(&model->freeze_frame);
furi_assert(model->current);
furi_assert(model->current->icons);
frame_rate = model->current->frame_rate;
view_commit_model(view->view, true);
osTimerStart(view->timer, 1000 / frame_rate);
bubble_animation_activate(view, false);
}
View* bubble_animation_get_view(BubbleAnimationView* view) {
furi_assert(view);
return view->view;
}

View File

@@ -0,0 +1,89 @@
#pragma once
#include <gui/view.h>
#include "../animation_manager.h"
/** Bubble Animation instance */
typedef struct BubbleAnimationView BubbleAnimationView;
/** Callback type to be called when interact button pressed */
typedef void (*BubbleAnimationInteractCallback)(void*);
/**
* Allocate bubble animation view.
* This is animation with bubbles, and 2 phases:
* active and passive.
*
* @return instance of new bubble animation
*/
BubbleAnimationView* bubble_animation_view_alloc(void);
/**
* Free bubble animation view.
*
* @view bubble animation view instance
*/
void bubble_animation_view_free(BubbleAnimationView* view);
/**
* Set callback for interact action for animation.
* Currently this is right button.
*
* @view bubble animation view instance
* @callback callback to call when button pressed
* @context context
*/
void bubble_animation_view_set_interact_callback(
BubbleAnimationView* view,
BubbleAnimationInteractCallback callback,
void* context);
/**
* Set new animation.
* BubbleAnimation doesn't posses Bubble Animation object
* so it doesn't handle any memory manipulation on Bubble Animations.
*
* @view bubble animation view instance
* @new_bubble_animation new animation to set
*/
void bubble_animation_view_set_animation(
BubbleAnimationView* view,
const BubbleAnimation* new_bubble_animation);
/**
* Get view of bubble animation.
*
* @view bubble animation view instance
* @return view
*/
View* bubble_animation_get_view(BubbleAnimationView* view);
/**
* Freeze current playing animation. Saves a frame to be shown
* during next unfreeze called.
* bubble_animation_freeze() stops any reference to 'current' animation
* so it can be freed. Therefore lock unfreeze should be preceeded with
* new animation set.
*
* Freeze/Unfreeze usage example:
*
* animation_view_alloc()
* set_animation()
* ...
* freeze_animation()
* // release animation
* ...
* // allocate animation
* set_animation()
* unfreeze()
*
* @view bubble animation view instance
*/
void bubble_animation_freeze(BubbleAnimationView* view);
/**
* Starts bubble animation after freezing.
*
* @view bubble animation view instance
*/
void bubble_animation_unfreeze(BubbleAnimationView* view);