[FL-2183] [FL-2209] Dolphin Deeds, Level up, assets generation, refactoring (#965)

* Desktop: cleanup headers
* Get loader pubsub via record
* [FL-2183] Dolphin refactoring 2022.01
* Restruct animations assets structure
* Rename assets
* Cleanup headers
* Update Recording animation
* Add BadBattery animation
* Provide loader's pubsub via record
* Fix load/unload animations
* Scripts: add flipper format support, initial dolphin packager rework. Assets: internal and external dolphin.
* Sync internal meta.txt and manifest.txt
* Reorder, rename dolphin assets
* Split essential generated assets
* Add ReadMe for dolphin assets
* Separate essential blocking animations
* Scripts: full dolphin validation before packaging
* Assets, Scripts: dolphin external resources packer
* Github: update codeowners
* Scripts: proper slots handling in dolphin animation meta
* Scripts: correct frames enumeration and fix compiled assets.
* [FL-2209] Add Dolphin Deeds points and many more
* Remove excess frame_rate
* Change dolphin assets directory
* Scripts: add internal resource support to dolphin compiler
* Scripts: add internal assets generation, renaming
* Scripts: correct assert, renaming
* Code cleanup, documentation, fixes
* Update Levelup animations
* Rename essential -> blocking
* Fix Unlocked hint
* Scripts: rewrite Templite compiller, replace regexps with token parser, split block types into code and variable blocks. Update dolphin templates.
* Documentation: add key combos description and use information
* Scripts: cleanup templit, more debug info and add dev comment

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
Albert Kharisov
2022-01-29 13:20:41 +04:00
committed by GitHub
parent 53e7415d12
commit 84410c83b5
366 changed files with 3646 additions and 1566 deletions

View File

@@ -1,23 +1,30 @@
#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 <gui/view_stack.h>
#include <stdint.h>
#include <furi.h>
#include <m-string.h>
#include <portmacro.h>
#include <dolphin/dolphin.h>
#include <power/power_service/power.h>
#include <storage/storage.h>
#include <dolphin/dolphin_i.h>
#include <storage/filesystem_api_defines.h>
#include <assets_icons.h>
#include "views/bubble_animation_view.h"
#include "views/one_shot_animation_view.h"
#include "animation_storage.h"
#include "animation_manager.h"
#define TAG "AnimationManager"
#define HARDCODED_ANIMATION_NAME "L1_Tv_128x47"
#define NO_SD_ANIMATION_NAME "L1_NoSd_128x49"
#define BAD_BATTERY_ANIMATION_NAME "L1_BadBattery_128x47"
#define NO_DB_ANIMATION_NAME "L0_NoDb_128x51"
#define BAD_SD_ANIMATION_NAME "L0_SdBad_128x51"
#define SD_OK_ANIMATION_NAME "L0_SdOk_128x51"
#define URL_ANIMATION_NAME "L0_Url_128x51"
#define NEW_MAIL_ANIMATION_NAME "L0_NewMail_128x51"
typedef enum {
AnimationManagerStateIdle,
AnimationManagerStateBlocked,
@@ -29,10 +36,13 @@ struct AnimationManager {
bool sd_show_url;
bool sd_shown_no_db;
bool sd_shown_sd_ok;
bool levelup_pending;
bool levelup_active;
AnimationManagerState state;
FuriPubSubSubscription* pubsub_subscription_storage;
FuriPubSubSubscription* pubsub_subscription_dolphin;
BubbleAnimationView* animation_view;
OneShotView* one_shot_view;
osTimerId_t idle_animation_timer;
StorageAnimation* current_animation;
AnimationManagerInteractCallback interact_callback;
@@ -41,6 +51,7 @@ struct AnimationManager {
void* context;
string_t freezed_animation_name;
int32_t freezed_animation_time_left;
ViewStack* view_stack;
};
static StorageAnimation*
@@ -50,6 +61,11 @@ static void animation_manager_replace_current_animation(
StorageAnimation* storage_animation);
static void animation_manager_start_new_idle(AnimationManager* animation_manager);
static bool animation_manager_check_blocking(AnimationManager* animation_manager);
static bool animation_manager_is_valid_idle_animation(
const StorageAnimationManifestInfo* info,
const DolphinStats* stats);
static void animation_manager_switch_to_one_shot_view(AnimationManager* animation_manager);
static void animation_manager_switch_to_animation_view(AnimationManager* animation_manager);
void animation_manager_set_context(AnimationManager* animation_manager, void* context) {
furi_assert(animation_manager);
@@ -101,12 +117,26 @@ static void animation_manager_interact_callback(void* context) {
}
}
/* reaction to animation_manager->interact_callback() */
/* reaction to animation_manager->check_blocking_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);
bool blocked = animation_manager_check_blocking(animation_manager);
if(!blocked) {
Dolphin* dolphin = furi_record_open("dolphin");
DolphinStats stats = dolphin_stats(dolphin);
furi_record_close("dolphin");
const StorageAnimationManifestInfo* manifest_info =
animation_storage_get_meta(animation_manager->current_animation);
bool valid = animation_manager_is_valid_idle_animation(manifest_info, &stats);
if(!valid) {
animation_manager_start_new_idle(animation_manager);
}
}
}
}
@@ -119,13 +149,24 @@ void animation_manager_new_idle_process(AnimationManager* animation_manager) {
}
}
/* reaction to animation_manager->check_blocking_callback() */
/* reaction to animation_manager->interact_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 */
if(animation_manager->levelup_pending) {
animation_manager->levelup_pending = false;
animation_manager->levelup_active = true;
animation_manager_switch_to_one_shot_view(animation_manager);
Dolphin* dolphin = furi_record_open("dolphin");
dolphin_upgrade_level(dolphin);
furi_record_close("dolphin");
} else if(animation_manager->levelup_active) {
animation_manager->levelup_active = false;
animation_manager_start_new_idle(animation_manager);
animation_manager_switch_to_animation_view(animation_manager);
} else if(animation_manager->state == AnimationManagerStateBlocked) {
bool blocked = animation_manager_check_blocking(animation_manager);
if(!blocked) {
animation_manager_start_new_idle(animation_manager);
}
@@ -152,22 +193,26 @@ static bool animation_manager_check_blocking(AnimationManager* animation_manager
if(sd_status == FSE_INTERNAL) {
blocking_animation = animation_storage_find_animation(BAD_SD_ANIMATION_NAME);
furi_assert(blocking_animation);
} 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);
furi_assert(blocking_animation);
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);
furi_assert(blocking_animation);
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);
furi_assert(blocking_animation);
animation_manager->sd_show_url = false;
}
}
@@ -176,13 +221,17 @@ static bool animation_manager_check_blocking(AnimationManager* animation_manager
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);
blocking_animation = animation_storage_find_animation(NEW_MAIL_ANIMATION_NAME);
furi_assert(blocking_animation);
if(blocking_animation) {
animation_manager->levelup_pending = true;
}
}
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 */
/* no timer starting because this is blocking animation */
animation_manager->state = AnimationManagerStateBlocked;
}
@@ -199,7 +248,7 @@ static void animation_manager_replace_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);
const char* new_name = animation_storage_get_meta(storage_animation)->name;
FURI_LOG_I(TAG, "Select \'%s\' animation", new_name);
animation_manager->current_animation = storage_animation;
@@ -209,9 +258,11 @@ static void animation_manager_replace_current_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();
animation_manager->view_stack = view_stack_alloc();
View* animation_view = bubble_animation_get_view(animation_manager->animation_view);
view_stack_add_view(animation_manager->view_stack, animation_view);
string_init(animation_manager->freezed_animation_name);
animation_manager->idle_animation_timer =
@@ -251,6 +302,8 @@ void animation_manager_free(AnimationManager* animation_manager) {
furi_record_close("storage");
string_clear(animation_manager->freezed_animation_name);
View* animation_view = bubble_animation_get_view(animation_manager->animation_view);
view_stack_remove_view(animation_manager->view_stack, animation_view);
bubble_animation_view_free(animation_manager->animation_view);
osTimerDelete(animation_manager->idle_animation_timer);
}
@@ -258,7 +311,39 @@ void animation_manager_free(AnimationManager* animation_manager) {
View* animation_manager_get_animation_view(AnimationManager* animation_manager) {
furi_assert(animation_manager);
return bubble_animation_get_view(animation_manager->animation_view);
return view_stack_get_view(animation_manager->view_stack);
}
static bool animation_manager_is_valid_idle_animation(
const StorageAnimationManifestInfo* info,
const DolphinStats* stats) {
furi_assert(info);
furi_assert(info->name);
bool result = true;
if(!strcmp(info->name, BAD_BATTERY_ANIMATION_NAME)) {
Power* power = furi_record_open("power");
bool battery_is_well = power_is_battery_healthy(power);
furi_record_close("power");
result = !battery_is_well;
}
if(!strcmp(info->name, NO_SD_ANIMATION_NAME)) {
Storage* storage = furi_record_open("storage");
FS_Error sd_status = storage_sd_status(storage);
furi_record_close("storage");
result = (sd_status == FSE_NOT_READY);
}
if((stats->butthurt < info->min_butthurt) || (stats->butthurt > info->max_butthurt)) {
result = false;
}
if((stats->level < info->min_level) || (stats->level > info->max_level)) {
result = false;
}
return result;
}
static StorageAnimation*
@@ -267,40 +352,25 @@ static StorageAnimation*
StorageAnimationList_init(animation_list);
animation_storage_fill_animation_list(&animation_list);
Power* power = furi_record_open("power");
bool battery_is_well = power_is_battery_healthy(power);
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);
furi_record_close("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;
}
const StorageAnimationManifestInfo* manifest_info =
animation_storage_get_meta(storage_animation);
bool valid = animation_manager_is_valid_idle_animation(manifest_info, &stats);
if(skip_animation) {
if(valid) {
whole_weight += manifest_info->weight;
StorageAnimationList_next(it);
} else {
animation_storage_free_storage_animation(&storage_animation);
/* remove and increase iterator */
StorageAnimationList_remove(animation_list, it);
} else {
whole_weight += meta->weight;
StorageAnimationList_next(it);
}
}
@@ -325,11 +395,10 @@ static StorageAnimation*
}
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);
const char* name = 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);
@@ -362,9 +431,15 @@ void animation_manager_unload_and_stall_animation(AnimationManager* animation_ma
furi_assert(0);
}
StorageAnimationMeta* meta = animation_storage_get_meta(animation_manager->current_animation);
FURI_LOG_I(
TAG,
"Unload animation \'%s\'",
animation_storage_get_meta(animation_manager->current_animation)->name);
StorageAnimationManifestInfo* 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);
string_set_str(animation_manager->freezed_animation_name, meta->name);
bubble_animation_freeze(animation_manager->animation_view);
animation_storage_free_storage_animation(&animation_manager->current_animation);
@@ -394,18 +469,27 @@ void animation_manager_load_and_continue_animation(AnimationManager* animation_m
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;
Dolphin* dolphin = furi_record_open("dolphin");
DolphinStats stats = dolphin_stats(dolphin);
furi_record_close("dolphin");
const StorageAnimationManifestInfo* manifest_info =
animation_storage_get_meta(restore_animation);
bool valid = animation_manager_is_valid_idle_animation(manifest_info, &stats);
if(valid) {
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);
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(
@@ -423,12 +507,47 @@ void animation_manager_load_and_continue_animation(AnimationManager* animation_m
if(!animation_manager->current_animation) {
animation_manager_start_new_idle(animation_manager);
}
FURI_LOG_D(
FURI_LOG_I(
TAG,
"Load & Continue with \'%s\'",
string_get_cstr(animation_storage_get_meta(animation_manager->current_animation)->name));
"Load animation \'%s\'",
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);
}
static void animation_manager_switch_to_one_shot_view(AnimationManager* animation_manager) {
furi_assert(animation_manager);
furi_assert(!animation_manager->one_shot_view);
Dolphin* dolphin = furi_record_open("dolphin");
DolphinStats stats = dolphin_stats(dolphin);
furi_record_close("dolphin");
animation_manager->one_shot_view = one_shot_view_alloc();
one_shot_view_set_interact_callback(
animation_manager->one_shot_view, animation_manager_interact_callback, animation_manager);
View* prev_view = bubble_animation_get_view(animation_manager->animation_view);
View* next_view = one_shot_view_get_view(animation_manager->one_shot_view);
view_stack_remove_view(animation_manager->view_stack, prev_view);
view_stack_add_view(animation_manager->view_stack, next_view);
if(stats.level == 1) {
one_shot_view_start_animation(animation_manager->one_shot_view, &A_Levelup1_128x64);
} else if(stats.level == 2) {
one_shot_view_start_animation(animation_manager->one_shot_view, &A_Levelup2_128x64);
} else {
furi_assert(0);
}
}
static void animation_manager_switch_to_animation_view(AnimationManager* animation_manager) {
furi_assert(animation_manager);
furi_assert(animation_manager->one_shot_view);
View* prev_view = one_shot_view_get_view(animation_manager->one_shot_view);
View* next_view = bubble_animation_get_view(animation_manager->animation_view);
view_stack_remove_view(animation_manager->view_stack, prev_view);
view_stack_add_view(animation_manager->view_stack, next_view);
one_shot_view_free(animation_manager->one_shot_view);
animation_manager->one_shot_view = NULL;
}

View File

@@ -1,34 +1,35 @@
#pragma once
#include "dolphin/dolphin.h"
#include <gui/view.h>
#include <gui/icon_i.h>
#include <stdint.h>
#include <dolphin/dolphin.h>
typedef struct AnimationManager AnimationManager;
typedef struct {
uint8_t x;
uint8_t y;
const char* str;
Align horizontal;
Align vertical;
const char* text;
Align align_h;
Align align_v;
} Bubble;
typedef struct FrameBubble {
Bubble bubble;
uint8_t starts_at_frame;
uint8_t ends_at_frame;
struct FrameBubble* next_bubble;
uint8_t start_frame;
uint8_t end_frame;
const struct FrameBubble* next_bubble;
} FrameBubble;
typedef struct {
FrameBubble** frame_bubbles;
uint8_t frame_bubbles_count;
const Icon** icons;
const FrameBubble* const* frame_bubble_sequences;
uint8_t frame_bubble_sequences_count;
const Icon icon_animation;
uint8_t frame_order[20];
uint8_t passive_frames;
uint8_t active_frames;
uint8_t active_cycles;
uint8_t frame_rate;
uint16_t duration;
uint16_t active_cooldown;
} BubbleAnimation;

View File

@@ -1,34 +1,86 @@
#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>
// Read documentation before using it
#include <stdint.h>
#include <flipper_file.h>
#include <furi.h>
#include <furi/dangerous_defines.h>
#include <storage/storage.h>
#include <gui/icon_i.h>
#include <m-string.h>
#include "animation_manager.h"
#include "animation_storage.h"
#include "animation_storage_i.h"
#include <assets_dolphin_internal.h>
#include <assets_dolphin_blocking.h>
#define ANIMATION_META_FILE "meta.txt"
#define ANIMATION_DIR "/ext/dolphin/animations"
#define ANIMATION_DIR "/ext/dolphin"
#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);
static bool animation_storage_load_single_manifest_info(
StorageAnimationManifestInfo* manifest_info,
const char* name) {
furi_assert(manifest_info);
bool result = false;
Storage* storage = furi_record_open("storage");
FlipperFile* file = flipper_file_alloc(storage);
flipper_file_set_strict_mode(file, true);
string_t read_string;
string_init(read_string);
do {
uint32_t u32value;
if(FSE_OK != storage_sd_status(storage)) break;
if(!flipper_file_open_existing(file, ANIMATION_MANIFEST_FILE)) break;
if(!flipper_file_read_header(file, read_string, &u32value)) break;
if(string_cmp_str(read_string, "Flipper Animation Manifest")) break;
manifest_info->name = NULL;
/* skip other animation names */
flipper_file_set_strict_mode(file, false);
while(flipper_file_read_string(file, "Name", read_string) &&
string_cmp_str(read_string, name))
;
if(string_cmp_str(read_string, name)) break;
flipper_file_set_strict_mode(file, true);
manifest_info->name = furi_alloc(string_size(read_string) + 1);
strcpy((char*)manifest_info->name, string_get_cstr(read_string));
if(!flipper_file_read_uint32(file, "Min butthurt", &u32value, 1)) break;
manifest_info->min_butthurt = u32value;
if(!flipper_file_read_uint32(file, "Max butthurt", &u32value, 1)) break;
manifest_info->max_butthurt = u32value;
if(!flipper_file_read_uint32(file, "Min level", &u32value, 1)) break;
manifest_info->min_level = u32value;
if(!flipper_file_read_uint32(file, "Max level", &u32value, 1)) break;
manifest_info->max_level = u32value;
if(!flipper_file_read_uint32(file, "Weight", &u32value, 1)) break;
manifest_info->weight = u32value;
result = true;
} while(0);
if(!result && manifest_info->name) {
free((void*)manifest_info->name);
}
string_clear(read_string);
flipper_file_close(file);
flipper_file_free(file);
furi_record_close("storage");
return result;
}
void animation_storage_fill_animation_list(StorageAnimationList_t* animation_list) {
furi_assert(sizeof(StorageAnimationList_t) == sizeof(void*));
furi_assert(!StorageAnimationList_size(*animation_list));
@@ -37,8 +89,8 @@ void animation_storage_fill_animation_list(StorageAnimationList_t* animation_lis
FlipperFile* file = flipper_file_alloc(storage);
/* Forbid skipping fields */
flipper_file_set_strict_mode(file, true);
string_t header;
string_init(header);
string_t read_string;
string_init(read_string);
do {
uint32_t u32value;
@@ -46,24 +98,28 @@ void animation_storage_fill_animation_list(StorageAnimationList_t* animation_lis
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;
if(!flipper_file_read_header(file, read_string, &u32value)) break;
if(string_cmp_str(read_string, "Flipper Animation Manifest")) break;
do {
storage_animation = furi_alloc(sizeof(StorageAnimation));
storage_animation->external = true;
storage_animation->animation = NULL;
storage_animation->manifest_info.name = NULL;
if(!flipper_file_read_string(file, "Name", read_string)) break;
storage_animation->manifest_info.name = furi_alloc(string_size(read_string) + 1);
strcpy((char*)storage_animation->manifest_info.name, string_get_cstr(read_string));
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;
storage_animation->manifest_info.min_butthurt = u32value;
if(!flipper_file_read_uint32(file, "Max butthurt", &u32value, 1)) break;
storage_animation->meta.max_butthurt = u32value;
storage_animation->manifest_info.max_butthurt = u32value;
if(!flipper_file_read_uint32(file, "Min level", &u32value, 1)) break;
storage_animation->meta.min_level = u32value;
storage_animation->manifest_info.min_level = u32value;
if(!flipper_file_read_uint32(file, "Max level", &u32value, 1)) break;
storage_animation->meta.max_level = u32value;
storage_animation->manifest_info.max_level = u32value;
if(!flipper_file_read_uint32(file, "Weight", &u32value, 1)) break;
storage_animation->meta.weight = u32value;
storage_animation->manifest_info.weight = u32value;
StorageAnimationList_push_back(*animation_list, storage_animation);
} while(1);
@@ -71,13 +127,13 @@ void animation_storage_fill_animation_list(StorageAnimationList_t* animation_lis
animation_storage_free_storage_animation(&storage_animation);
} while(0);
string_clear(header);
string_clear(read_string);
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]);
for(int i = 0; i < dolphin_internal_size; ++i) {
StorageAnimationList_push_back(*animation_list, (StorageAnimation*)&dolphin_internal[i]);
}
furi_record_close("storage");
@@ -88,41 +144,45 @@ StorageAnimation* animation_storage_find_animation(const char* 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];
for(int i = 0; i < dolphin_blocking_size; ++i) {
if(!strcmp(dolphin_blocking[i].manifest_info.name, name)) {
storage_animation = (StorageAnimation*)&dolphin_blocking[i];
break;
}
}
if(!storage_animation) {
for(int i = 0; i < dolphin_internal_size; ++i) {
if(!strcmp(dolphin_internal[i].manifest_info.name, name)) {
storage_animation = (StorageAnimation*)&dolphin_internal[i];
break;
}
}
}
/* look through external animations */
if(!storage_animation) {
BubbleAnimation* animation = animation_storage_load_animation(name);
storage_animation = furi_alloc(sizeof(StorageAnimation));
storage_animation->external = true;
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);
bool result = false;
result =
animation_storage_load_single_manifest_info(&storage_animation->manifest_info, name);
if(result) {
storage_animation->animation = animation_storage_load_animation(name);
result = !!storage_animation->animation;
}
if(!result) {
animation_storage_free_storage_animation(&storage_animation);
}
}
return storage_animation;
}
StorageAnimationMeta* animation_storage_get_meta(StorageAnimation* storage_animation) {
StorageAnimationManifestInfo* animation_storage_get_meta(StorageAnimation* storage_animation) {
furi_assert(storage_animation);
return &storage_animation->meta;
return &storage_animation->manifest_info;
}
const BubbleAnimation*
@@ -138,7 +198,7 @@ void animation_storage_cache_animation(StorageAnimation* storage_animation) {
if(storage_animation->external) {
if(!storage_animation->animation) {
storage_animation->animation =
animation_storage_load_animation(string_get_cstr(storage_animation->meta.name));
animation_storage_load_animation(storage_animation->manifest_info.name);
}
}
}
@@ -161,7 +221,9 @@ void animation_storage_free_storage_animation(StorageAnimation** storage_animati
if((*storage_animation)->external) {
animation_storage_free_animation((BubbleAnimation**)&(*storage_animation)->animation);
string_clear((*storage_animation)->meta.name);
if((*storage_animation)->manifest_info.name) {
free((void*)(*storage_animation)->manifest_info.name);
}
free(*storage_animation);
}
@@ -188,41 +250,15 @@ static bool animation_storage_cast_align(string_t align_str, Align* align) {
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;
}
const Icon* icon = &animation->icon_animation;
for(int i = 0; i < icon->frame_count; ++i) {
if(icon->frames[i]) {
free((void*)icon->frames[i]);
}
}
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(
@@ -230,22 +266,25 @@ static bool animation_storage_load_frames(
const char* name,
BubbleAnimation* animation,
uint32_t* frame_order,
uint32_t width,
uint32_t height) {
furi_assert(!animation->icons);
uint8_t width,
uint8_t height) {
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;
Icon* icon = (Icon*)&animation->icon_animation;
FURI_CONST_ASSIGN(icon->frame_count, frame_order_size);
FURI_CONST_ASSIGN(icon->frame_rate, 0);
FURI_CONST_ASSIGN(icon->height, height);
FURI_CONST_ASSIGN(icon->width, width);
icon->frames = furi_alloc(sizeof(const uint8_t*) * frame_order_size);
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]);
@@ -265,24 +304,12 @@ static bool animation_storage_load_frames(
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) {
icon->frames[i] = furi_alloc(file_info.size);
if(storage_file_read(file, (void*)icon->frames[i], 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;
}
@@ -295,11 +322,10 @@ static bool animation_storage_load_frames(
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]);
furi_check(animation->icon_animation.frames);
for(int i = 0; i < animation->icon_animation.frame_count; ++i) {
furi_check(animation->icon_animation.frames[i]);
}
}
@@ -314,72 +340,73 @@ static bool animation_storage_load_bubbles(BubbleAnimation* animation, FlipperFi
string_t str;
string_init(str);
bool success = false;
furi_assert(!animation->frame_bubbles);
furi_assert(!animation->frame_bubble_sequences);
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;
animation->frame_bubble_sequences_count = u32value;
if(animation->frame_bubble_sequences_count == 0) {
animation->frame_bubble_sequences = NULL;
success = true;
break;
}
animation->frame_bubbles =
furi_alloc(sizeof(FrameBubble*) * animation->frame_bubbles_count);
animation->frame_bubble_sequences =
furi_alloc(sizeof(FrameBubble*) * animation->frame_bubble_sequences_count);
uint32_t current_slot = 0;
for(int i = 0; i < animation->frame_bubbles_count; ++i) {
animation->frame_bubbles[i] = furi_alloc(sizeof(FrameBubble));
for(int i = 0; i < animation->frame_bubble_sequences_count; ++i) {
FURI_CONST_ASSIGN_PTR(
animation->frame_bubble_sequences[i], furi_alloc(sizeof(FrameBubble)));
}
FrameBubble* bubble = animation->frame_bubbles[0];
const FrameBubble* bubble = animation->frame_bubble_sequences[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));
FURI_CONST_ASSIGN_PTR(bubble->next_bubble, furi_alloc(sizeof(FrameBubble)));
bubble = bubble->next_bubble;
} else if(current_slot == index + 1) {
++index;
bubble = animation->frame_bubbles[index];
bubble = animation->frame_bubble_sequences[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(index >= animation->frame_bubble_sequences_count) break;
if(!flipper_file_read_uint32(ff, "X", &u32value, 1)) break;
bubble->bubble.x = u32value;
FURI_CONST_ASSIGN(bubble->bubble.x, u32value);
if(!flipper_file_read_uint32(ff, "Y", &u32value, 1)) break;
bubble->bubble.y = u32value;
FURI_CONST_ASSIGN(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));
FURI_CONST_ASSIGN_PTR(bubble->bubble.text, furi_alloc(string_size(str) + 1));
strcpy((char*)bubble->bubble.text, string_get_cstr(str));
if(!flipper_file_read_string(ff, "AlignH", str)) break;
if(!animation_storage_cast_align(str, &bubble->bubble.horizontal)) break;
if(!animation_storage_cast_align(str, (Align*)&bubble->bubble.align_h)) break;
if(!flipper_file_read_string(ff, "AlignV", str)) break;
if(!animation_storage_cast_align(str, &bubble->bubble.vertical)) break;
if(!animation_storage_cast_align(str, (Align*)&bubble->bubble.align_v)) break;
if(!flipper_file_read_uint32(ff, "StartFrame", &u32value, 1)) break;
bubble->starts_at_frame = u32value;
FURI_CONST_ASSIGN(bubble->start_frame, u32value);
if(!flipper_file_read_uint32(ff, "EndFrame", &u32value, 1)) break;
bubble->ends_at_frame = u32value;
FURI_CONST_ASSIGN(bubble->end_frame, u32value);
}
success = (index + 1) == animation->frame_bubbles_count;
success = (index + 1) == animation->frame_bubble_sequences_count;
} while(0);
if(!success) {
if(animation->frame_bubbles) {
if(animation->frame_bubble_sequences) {
FURI_LOG_E(TAG, "Failed to load animation bubbles");
animation_storage_free_bubbles(animation);
}
@@ -402,7 +429,7 @@ static BubbleAnimation* animation_storage_load_animation(const char* name) {
flipper_file_set_strict_mode(ff, true);
string_t str;
string_init(str);
animation->frame_bubbles = NULL;
animation->frame_bubble_sequences = NULL;
bool success = false;
do {
@@ -424,8 +451,17 @@ static BubbleAnimation* animation_storage_load_animation(const char* name) {
animation->active_frames = u32value;
uint8_t frames = animation->passive_frames + animation->active_frames;
uint32_t count = 0;
if(!flipper_file_get_value_count(ff, "Frames order", &count)) break;
if((count != frames) || (frames > COUNT_OF(animation->frame_order))) {
FURI_LOG_E(TAG, "Error loading animation: frames order");
break;
}
u32array = furi_alloc(sizeof(uint32_t) * frames);
if(!flipper_file_read_uint32(ff, "Frames order", u32array, frames)) break;
for(int i = 0; i < frames; ++i) {
animation->frame_order[i] = u32array[i];
}
/* passive and active frames must be loaded up to this point */
if(!animation_storage_load_frames(storage, name, animation, u32array, width, height))
@@ -434,7 +470,7 @@ static BubbleAnimation* animation_storage_load_animation(const char* name) {
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;
FURI_CONST_ASSIGN(animation->icon_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;
@@ -460,10 +496,10 @@ static BubbleAnimation* animation_storage_load_animation(const char* name) {
}
static void animation_storage_free_bubbles(BubbleAnimation* animation) {
if(!animation->frame_bubbles) return;
if(!animation->frame_bubble_sequences) return;
for(int i = 0; i < animation->frame_bubbles_count;) {
FrameBubble** bubble = &animation->frame_bubbles[i];
for(int i = 0; i < animation->frame_bubble_sequences_count;) {
const FrameBubble* const* bubble = &animation->frame_bubble_sequences[i];
if((*bubble) == NULL) break;
@@ -471,15 +507,15 @@ static void animation_storage_free_bubbles(BubbleAnimation* animation) {
bubble = &(*bubble)->next_bubble;
}
if((*bubble)->bubble.str) {
free((void*)(*bubble)->bubble.str);
if((*bubble)->bubble.text) {
free((void*)(*bubble)->bubble.text);
}
if((*bubble) == animation->frame_bubbles[i]) {
if((*bubble) == animation->frame_bubble_sequences[i]) {
++i;
}
free(*bubble);
*bubble = NULL;
free((void*)*bubble);
FURI_CONST_ASSIGN_PTR(*bubble, NULL);
}
free(animation->frame_bubbles);
animation->frame_bubbles = NULL;
free((void*)animation->frame_bubble_sequences);
animation->frame_bubble_sequences = NULL;
}

View File

@@ -4,15 +4,6 @@
#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
@@ -20,13 +11,13 @@
typedef struct StorageAnimation StorageAnimation;
typedef struct {
string_t name;
const char* name;
uint8_t min_butthurt;
uint8_t max_butthurt;
uint8_t min_level;
uint8_t max_level;
uint8_t weight;
} StorageAnimationMeta;
} StorageAnimationManifestInfo;
/** Container to return available animations list */
LIST_DEF(StorageAnimationList, StorageAnimation*, M_PTR_OPLIST)
@@ -81,7 +72,7 @@ StorageAnimation* animation_storage_find_animation(const char* name);
* @storage_animation item of whom we have to extract meta.
* @return meta itself
*/
StorageAnimationMeta* animation_storage_get_meta(StorageAnimation* storage_animation);
StorageAnimationManifestInfo* animation_storage_get_meta(StorageAnimation* storage_animation);
/**
* Free storage_animation, which previously acquired

View File

@@ -1,195 +1,9 @@
#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;
StorageAnimationManifestInfo manifest_info;
};
// 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

@@ -1,23 +1,19 @@
#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 "bubble_animation_view.h"
#include <furi_hal.h>
#include <furi.h>
#include "portmacro.h"
#include <gui/icon.h>
#include <gui/canvas.h>
#include <gui/elements.h>
#include <gui/view.h>
#include <gui/icon_i.h>
#include <input/input.h>
#include <stdint.h>
#include <FreeRTOS.h>
#include <timers.h>
#include "bubble_animation_view.h"
#include <gui/icon_i.h>
#include <furi/dangerous_defines.h>
#define ACTIVE_SHIFT 2
@@ -43,7 +39,7 @@ struct BubbleAnimationView {
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) {
static uint8_t bubble_animation_get_frame_index(BubbleAnimationViewModel* model) {
furi_assert(model);
uint8_t icon_index = 0;
const BubbleAnimation* animation = model->current;
@@ -57,7 +53,7 @@ static uint8_t bubble_animation_get_icon_index(BubbleAnimationViewModel* model)
}
furi_assert(icon_index < (animation->passive_frames + animation->active_frames));
return icon_index;
return animation->frame_order[icon_index];
}
static void bubble_animation_draw_callback(Canvas* canvas, void* model_) {
@@ -79,23 +75,26 @@ static void bubble_animation_draw_callback(Canvas* canvas, void* model_) {
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);
uint8_t index = bubble_animation_get_frame_index(model);
uint8_t width = icon_get_width(&animation->icon_animation);
uint8_t height = icon_get_height(&animation->icon_animation);
uint8_t y_offset = canvas_height(canvas) - height;
canvas_draw_bitmap(
canvas, 0, y_offset, width, height, animation->icon_animation.frames[index]);
const FrameBubble* bubble = model->current_bubble;
if(bubble) {
if((model->current_frame >= bubble->starts_at_frame) &&
(model->current_frame <= bubble->ends_at_frame)) {
if((model->current_frame >= bubble->start_frame) &&
(model->current_frame <= bubble->end_frame)) {
const Bubble* b = &bubble->bubble;
elements_bubble_str(canvas, b->x, b->y, b->str, b->horizontal, b->vertical);
elements_bubble_str(canvas, b->x, b->y, b->text, b->align_h, b->align_v);
}
}
}
static FrameBubble* bubble_animation_pick_bubble(BubbleAnimationViewModel* model, bool active) {
FrameBubble* bubble = NULL;
static const FrameBubble*
bubble_animation_pick_bubble(BubbleAnimationViewModel* model, bool active) {
const FrameBubble* bubble = NULL;
if((model->active_bubbles == 0) && (model->passive_bubbles == 0)) {
return NULL;
@@ -104,10 +103,11 @@ static FrameBubble* bubble_animation_pick_bubble(BubbleAnimationViewModel* model
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) {
for(int i = 0; i < animation->frame_bubble_sequences_count; ++i) {
if((animation->frame_bubble_sequences[i]->start_frame < animation->passive_frames) ^
active) {
if(!index) {
bubble = animation->frame_bubbles[i];
bubble = animation->frame_bubble_sequences[i];
}
--index;
}
@@ -135,10 +135,6 @@ static bool bubble_animation_input_callback(InputEvent* event, void* context) {
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;
@@ -190,7 +186,7 @@ static void bubble_animation_activate_right_now(BubbleAnimationView* 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;
frame_rate = model->current->icon_animation.frame_rate;
}
view_commit_model(view->view, true);
@@ -222,7 +218,7 @@ static void bubble_animation_next_frame(BubbleAnimationViewModel* model) {
}
if(model->current_bubble) {
if(model->current_frame > model->current_bubble->ends_at_frame) {
if(model->current_frame > model->current_bubble->end_frame) {
model->current_bubble = model->current_bubble->next_bubble;
}
}
@@ -251,7 +247,11 @@ static void bubble_animation_timer_callback(void* context) {
}
}
static Icon* bubble_animation_clone_frame(const Icon* icon_orig) {
/* always freeze first passive frame, because
* animation is always activated at unfreezing and played
* passive frame first, and 2 frames after - active
*/
static Icon* bubble_animation_clone_first_frame(const Icon* icon_orig) {
furi_assert(icon_orig);
furi_assert(icon_orig->frames);
furi_assert(icon_orig->frames[0]);
@@ -268,6 +268,7 @@ static Icon* bubble_animation_clone_frame(const Icon* icon_orig) {
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);
FURI_CONST_ASSIGN(icon_clone->frame_count, 1);
return icon_clone;
}
@@ -288,7 +289,7 @@ static void bubble_animation_enter(void* context) {
bubble_animation_activate(view, false);
BubbleAnimationViewModel* model = view_get_model(view->view);
uint8_t frame_rate = model->current->frame_rate;
uint8_t frame_rate = model->current->icon_animation.frame_rate;
view_commit_model(view->view, false);
if(frame_rate) {
@@ -353,8 +354,8 @@ void bubble_animation_view_set_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) {
for(int i = 0; i < new_animation->frame_bubble_sequences_count; ++i) {
if(new_animation->frame_bubble_sequences[i]->start_frame < new_animation->passive_frames) {
++model->passive_bubbles;
} else {
++model->active_bubbles;
@@ -367,7 +368,7 @@ void bubble_animation_view_set_animation(
model->active_cycle = 0;
view_commit_model(view->view, true);
osTimerStart(view->timer, 1000 / new_animation->frame_rate);
osTimerStart(view->timer, 1000 / new_animation->icon_animation.frame_rate);
}
void bubble_animation_freeze(BubbleAnimationView* view) {
@@ -376,12 +377,7 @@ void bubble_animation_freeze(BubbleAnimationView* 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->freeze_frame = bubble_animation_clone_first_frame(&model->current->icon_animation);
model->current = NULL;
view_commit_model(view->view, false);
osTimerStop(view->timer);
@@ -395,8 +391,7 @@ void bubble_animation_unfreeze(BubbleAnimationView* 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;
frame_rate = model->current->icon_animation.frame_rate;
view_commit_model(view->view, true);
osTimerStart(view->timer, 1000 / frame_rate);

View File

@@ -0,0 +1,130 @@
#include "one_shot_animation_view.h"
#include <furi.h>
#include <portmacro.h>
#include <gui/canvas.h>
#include <gui/view.h>
#include <gui/icon_i.h>
#include <stdint.h>
typedef void (*OneShotInteractCallback)(void*);
struct OneShotView {
View* view;
TimerHandle_t update_timer;
OneShotInteractCallback interact_callback;
void* interact_callback_context;
};
typedef struct {
const Icon* icon;
uint32_t index;
bool block_input;
} OneShotViewModel;
static void one_shot_view_update_timer_callback(TimerHandle_t xTimer) {
OneShotView* view = (void*)pvTimerGetTimerID(xTimer);
OneShotViewModel* model = view_get_model(view->view);
if((model->index + 1) < model->icon->frame_count) {
++model->index;
} else {
model->block_input = false;
model->index = model->icon->frame_count - 2;
}
view_commit_model(view->view, true);
}
static void one_shot_view_draw(Canvas* canvas, void* model_) {
furi_assert(canvas);
furi_assert(model_);
OneShotViewModel* model = model_;
furi_check(model->index < model->icon->frame_count);
uint8_t y_offset = canvas_height(canvas) - model->icon->height;
canvas_draw_bitmap(
canvas,
0,
y_offset,
model->icon->width,
model->icon->height,
model->icon->frames[model->index]);
}
static bool one_shot_view_input(InputEvent* event, void* context) {
furi_assert(context);
furi_assert(event);
OneShotView* view = context;
bool consumed = false;
OneShotViewModel* model = view_get_model(view->view);
consumed = model->block_input;
view_commit_model(view->view, false);
if(!consumed) {
if(event->key == InputKeyRight) {
/* Right button reserved for animation activation, so consume */
consumed = true;
if(event->type == InputTypeShort) {
if(view->interact_callback) {
view->interact_callback(view->interact_callback_context);
}
}
}
}
return consumed;
}
OneShotView* one_shot_view_alloc(void) {
OneShotView* view = furi_alloc(sizeof(OneShotView));
view->view = view_alloc();
view->update_timer =
xTimerCreate("Update timer", 1000, pdTRUE, view, one_shot_view_update_timer_callback);
view_allocate_model(view->view, ViewModelTypeLocking, sizeof(OneShotViewModel));
view_set_context(view->view, view);
view_set_draw_callback(view->view, one_shot_view_draw);
view_set_input_callback(view->view, one_shot_view_input);
return view;
}
void one_shot_view_free(OneShotView* view) {
furi_assert(view);
xTimerDelete(view->update_timer, portMAX_DELAY);
view_free(view->view);
view->view = NULL;
free(view);
}
void one_shot_view_set_interact_callback(
OneShotView* view,
OneShotInteractCallback callback,
void* context) {
furi_assert(view);
view->interact_callback_context = context;
view->interact_callback = callback;
}
void one_shot_view_start_animation(OneShotView* view, const Icon* icon) {
furi_assert(view);
furi_assert(icon);
furi_check(icon->frame_count >= 2);
OneShotViewModel* model = view_get_model(view->view);
model->index = 0;
model->icon = icon;
model->block_input = true;
view_commit_model(view->view, true);
xTimerChangePeriod(view->update_timer, 1000 / model->icon->frame_rate, portMAX_DELAY);
}
View* one_shot_view_get_view(OneShotView* view) {
furi_assert(view);
return view->view;
}

View File

@@ -0,0 +1,17 @@
#pragma once
#include <furi.h>
#include <gui/view.h>
#include <stdint.h>
typedef void (*OneShotInteractCallback)(void*);
typedef struct OneShotView OneShotView;
OneShotView* one_shot_view_alloc(void);
void one_shot_view_free(OneShotView* view);
void one_shot_view_set_interact_callback(
OneShotView* view,
OneShotInteractCallback callback,
void* context);
void one_shot_view_start_animation(OneShotView* view, const Icon* icon);
View* one_shot_view_get_view(OneShotView* view);