bubble_animation_view.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #include "../animation_manager.h"
  2. #include "../animation_storage.h"
  3. #include "bubble_animation_view.h"
  4. #include <furi_hal.h>
  5. #include <furi.h>
  6. #include <gui/canvas.h>
  7. #include <gui/elements.h>
  8. #include <gui/view.h>
  9. #include <gui/icon_i.h>
  10. #include <input/input.h>
  11. #include <stdint.h>
  12. #include <core/dangerous_defines.h>
  13. #define ACTIVE_SHIFT 2
  14. typedef struct {
  15. const BubbleAnimation* current;
  16. const FrameBubble* current_bubble;
  17. uint8_t current_frame;
  18. uint8_t active_cycle;
  19. uint8_t active_bubbles;
  20. uint8_t passive_bubbles;
  21. uint8_t active_shift;
  22. TickType_t active_ended_at;
  23. Icon* freeze_frame;
  24. } BubbleAnimationViewModel;
  25. struct BubbleAnimationView {
  26. View* view;
  27. FuriTimer* timer;
  28. BubbleAnimationInteractCallback interact_callback;
  29. void* interact_callback_context;
  30. };
  31. static void bubble_animation_activate(BubbleAnimationView* view, bool force);
  32. static void bubble_animation_activate_right_now(BubbleAnimationView* view);
  33. static uint8_t bubble_animation_get_frame_index(BubbleAnimationViewModel* model) {
  34. furi_assert(model);
  35. uint8_t icon_index = 0;
  36. const BubbleAnimation* animation = model->current;
  37. if(model->current_frame < animation->passive_frames) {
  38. icon_index = model->current_frame;
  39. } else {
  40. icon_index =
  41. (model->current_frame - animation->passive_frames) % animation->active_frames +
  42. animation->passive_frames;
  43. }
  44. furi_assert(icon_index < (animation->passive_frames + animation->active_frames));
  45. return animation->frame_order[icon_index];
  46. }
  47. static void bubble_animation_draw_callback(Canvas* canvas, void* model_) {
  48. furi_assert(model_);
  49. furi_assert(canvas);
  50. BubbleAnimationViewModel* model = model_;
  51. const BubbleAnimation* animation = model->current;
  52. if(model->freeze_frame) {
  53. uint8_t y_offset = canvas_height(canvas) - icon_get_height(model->freeze_frame);
  54. canvas_draw_icon(canvas, 0, y_offset, model->freeze_frame);
  55. return;
  56. }
  57. if(!animation) {
  58. return;
  59. }
  60. furi_assert(model->current_frame < 255);
  61. uint8_t index = bubble_animation_get_frame_index(model);
  62. uint8_t width = icon_get_width(&animation->icon_animation);
  63. uint8_t height = icon_get_height(&animation->icon_animation);
  64. uint8_t y_offset = canvas_height(canvas) - height;
  65. canvas_draw_bitmap(
  66. canvas, 0, y_offset, width, height, animation->icon_animation.frames[index]);
  67. const FrameBubble* bubble = model->current_bubble;
  68. if(bubble) {
  69. if((model->current_frame >= bubble->start_frame) &&
  70. (model->current_frame <= bubble->end_frame)) {
  71. const Bubble* b = &bubble->bubble;
  72. elements_bubble_str(canvas, b->x, b->y, b->text, b->align_h, b->align_v);
  73. }
  74. }
  75. }
  76. static const FrameBubble*
  77. bubble_animation_pick_bubble(BubbleAnimationViewModel* model, bool active) {
  78. const FrameBubble* bubble = NULL;
  79. if((model->active_bubbles == 0) && (model->passive_bubbles == 0)) {
  80. return NULL;
  81. }
  82. uint8_t index =
  83. furi_hal_random_get() % (active ? model->active_bubbles : model->passive_bubbles);
  84. const BubbleAnimation* animation = model->current;
  85. for(int i = 0; i < animation->frame_bubble_sequences_count; ++i) {
  86. if((animation->frame_bubble_sequences[i]->start_frame < animation->passive_frames) ^
  87. active) {
  88. if(!index) {
  89. bubble = animation->frame_bubble_sequences[i];
  90. }
  91. --index;
  92. }
  93. }
  94. return bubble;
  95. }
  96. static bool bubble_animation_input_callback(InputEvent* event, void* context) {
  97. furi_assert(context);
  98. furi_assert(event);
  99. BubbleAnimationView* animation_view = context;
  100. bool consumed = false;
  101. if(event->type == InputTypePress) {
  102. bubble_animation_activate(animation_view, false);
  103. }
  104. if(event->key == InputKeyRight) {
  105. /* Right button reserved for animation activation, so consume */
  106. consumed = true;
  107. if(event->type == InputTypeShort) {
  108. if(animation_view->interact_callback) {
  109. animation_view->interact_callback(animation_view->interact_callback_context);
  110. }
  111. }
  112. }
  113. return consumed;
  114. }
  115. static void bubble_animation_activate(BubbleAnimationView* view, bool force) {
  116. furi_assert(view);
  117. bool activate = true;
  118. BubbleAnimationViewModel* model = view_get_model(view->view);
  119. if(!model->current) {
  120. activate = false;
  121. } else if(model->freeze_frame) {
  122. activate = false;
  123. } else if(model->current->active_frames == 0) {
  124. activate = false;
  125. }
  126. if(!force) {
  127. if((model->active_ended_at + model->current->active_cooldown * 1000) >
  128. xTaskGetTickCount()) {
  129. activate = false;
  130. } else if(model->active_shift) {
  131. activate = false;
  132. } else if(model->current_frame >= model->current->passive_frames) {
  133. activate = false;
  134. }
  135. }
  136. view_commit_model(view->view, false);
  137. if(!activate && !force) {
  138. return;
  139. }
  140. if(ACTIVE_SHIFT > 0) {
  141. BubbleAnimationViewModel* model = view_get_model(view->view);
  142. model->active_shift = ACTIVE_SHIFT;
  143. view_commit_model(view->view, false);
  144. } else {
  145. bubble_animation_activate_right_now(view);
  146. }
  147. }
  148. static void bubble_animation_activate_right_now(BubbleAnimationView* view) {
  149. furi_assert(view);
  150. uint8_t frame_rate = 0;
  151. BubbleAnimationViewModel* model = view_get_model(view->view);
  152. if(model->current && (model->current->active_frames > 0) && (!model->freeze_frame)) {
  153. model->current_frame = model->current->passive_frames;
  154. model->current_bubble = bubble_animation_pick_bubble(model, true);
  155. frame_rate = model->current->icon_animation.frame_rate;
  156. }
  157. view_commit_model(view->view, true);
  158. if(frame_rate) {
  159. furi_timer_start(view->timer, 1000 / frame_rate);
  160. }
  161. }
  162. static void bubble_animation_next_frame(BubbleAnimationViewModel* model) {
  163. furi_assert(model);
  164. if(!model->current) {
  165. return;
  166. }
  167. if(model->current_frame < model->current->passive_frames) {
  168. model->current_frame = (model->current_frame + 1) % model->current->passive_frames;
  169. } else {
  170. ++model->current_frame;
  171. model->active_cycle +=
  172. !((model->current_frame - model->current->passive_frames) %
  173. model->current->active_frames);
  174. if(model->active_cycle >= model->current->active_cycles) {
  175. // switch to passive
  176. model->active_cycle = 0;
  177. model->current_frame = 0;
  178. model->current_bubble = bubble_animation_pick_bubble(model, false);
  179. model->active_ended_at = xTaskGetTickCount();
  180. }
  181. if(model->current_bubble) {
  182. if(model->current_frame > model->current_bubble->end_frame) {
  183. model->current_bubble = model->current_bubble->next_bubble;
  184. }
  185. }
  186. }
  187. }
  188. static void bubble_animation_timer_callback(void* context) {
  189. furi_assert(context);
  190. BubbleAnimationView* view = context;
  191. bool activate = false;
  192. BubbleAnimationViewModel* model = view_get_model(view->view);
  193. if(model->active_shift > 0) {
  194. activate = (--model->active_shift == 0);
  195. }
  196. if(!model->freeze_frame && !activate) {
  197. bubble_animation_next_frame(model);
  198. }
  199. view_commit_model(view->view, !activate);
  200. if(activate) {
  201. bubble_animation_activate_right_now(view);
  202. }
  203. }
  204. /* always freeze first passive frame, because
  205. * animation is always activated at unfreezing and played
  206. * passive frame first, and 2 frames after - active
  207. */
  208. static Icon* bubble_animation_clone_first_frame(const Icon* icon_orig) {
  209. furi_assert(icon_orig);
  210. furi_assert(icon_orig->frames);
  211. furi_assert(icon_orig->frames[0]);
  212. Icon* icon_clone = malloc(sizeof(Icon));
  213. memcpy(icon_clone, icon_orig, sizeof(Icon));
  214. icon_clone->frames = malloc(sizeof(uint8_t*));
  215. /* icon bitmap can be either compressed or not. It is compressed if
  216. * compressed size is less than original, so max size for bitmap is
  217. * uncompressed (width * height) + 1 byte (in uncompressed case)
  218. * for compressed header
  219. */
  220. size_t max_bitmap_size = ROUND_UP_TO(icon_orig->width, 8) * icon_orig->height + 1;
  221. FURI_CONST_ASSIGN_PTR(icon_clone->frames[0], malloc(max_bitmap_size));
  222. memcpy((void*)icon_clone->frames[0], icon_orig->frames[0], max_bitmap_size);
  223. FURI_CONST_ASSIGN(icon_clone->frame_count, 1);
  224. return icon_clone;
  225. }
  226. static void bubble_animation_release_frame(Icon** icon) {
  227. furi_assert(icon);
  228. furi_assert(*icon);
  229. free((void*)(*icon)->frames[0]);
  230. free((void*)(*icon)->frames);
  231. free(*icon);
  232. *icon = NULL;
  233. }
  234. static void bubble_animation_enter(void* context) {
  235. furi_assert(context);
  236. BubbleAnimationView* view = context;
  237. bubble_animation_activate(view, false);
  238. BubbleAnimationViewModel* model = view_get_model(view->view);
  239. uint8_t frame_rate = model->current->icon_animation.frame_rate;
  240. view_commit_model(view->view, false);
  241. if(frame_rate) {
  242. furi_timer_start(view->timer, 1000 / frame_rate);
  243. }
  244. }
  245. static void bubble_animation_exit(void* context) {
  246. furi_assert(context);
  247. BubbleAnimationView* view = context;
  248. furi_timer_stop(view->timer);
  249. }
  250. BubbleAnimationView* bubble_animation_view_alloc(void) {
  251. BubbleAnimationView* view = malloc(sizeof(BubbleAnimationView));
  252. view->view = view_alloc();
  253. view->interact_callback = NULL;
  254. view->timer = furi_timer_alloc(bubble_animation_timer_callback, FuriTimerTypePeriodic, view);
  255. view_allocate_model(view->view, ViewModelTypeLocking, sizeof(BubbleAnimationViewModel));
  256. view_set_context(view->view, view);
  257. view_set_draw_callback(view->view, bubble_animation_draw_callback);
  258. view_set_input_callback(view->view, bubble_animation_input_callback);
  259. view_set_enter_callback(view->view, bubble_animation_enter);
  260. view_set_exit_callback(view->view, bubble_animation_exit);
  261. return view;
  262. }
  263. void bubble_animation_view_free(BubbleAnimationView* view) {
  264. furi_assert(view);
  265. view_set_draw_callback(view->view, NULL);
  266. view_set_input_callback(view->view, NULL);
  267. view_set_context(view->view, NULL);
  268. view_free(view->view);
  269. view->view = NULL;
  270. free(view);
  271. }
  272. void bubble_animation_view_set_interact_callback(
  273. BubbleAnimationView* view,
  274. BubbleAnimationInteractCallback callback,
  275. void* context) {
  276. furi_assert(view);
  277. view->interact_callback_context = context;
  278. view->interact_callback = callback;
  279. }
  280. void bubble_animation_view_set_animation(
  281. BubbleAnimationView* view,
  282. const BubbleAnimation* new_animation) {
  283. furi_assert(view);
  284. furi_assert(new_animation);
  285. BubbleAnimationViewModel* model = view_get_model(view->view);
  286. furi_assert(model);
  287. model->current = new_animation;
  288. model->active_ended_at = xTaskGetTickCount() - (model->current->active_cooldown * 1000);
  289. model->active_bubbles = 0;
  290. model->passive_bubbles = 0;
  291. for(int i = 0; i < new_animation->frame_bubble_sequences_count; ++i) {
  292. if(new_animation->frame_bubble_sequences[i]->start_frame < new_animation->passive_frames) {
  293. ++model->passive_bubbles;
  294. } else {
  295. ++model->active_bubbles;
  296. }
  297. }
  298. /* select bubble sequence */
  299. model->current_bubble = bubble_animation_pick_bubble(model, false);
  300. model->current_frame = 0;
  301. model->active_cycle = 0;
  302. view_commit_model(view->view, true);
  303. furi_timer_start(view->timer, 1000 / new_animation->icon_animation.frame_rate);
  304. }
  305. void bubble_animation_freeze(BubbleAnimationView* view) {
  306. furi_assert(view);
  307. BubbleAnimationViewModel* model = view_get_model(view->view);
  308. furi_assert(model->current);
  309. furi_assert(!model->freeze_frame);
  310. model->freeze_frame = bubble_animation_clone_first_frame(&model->current->icon_animation);
  311. model->current = NULL;
  312. view_commit_model(view->view, false);
  313. furi_timer_stop(view->timer);
  314. }
  315. void bubble_animation_unfreeze(BubbleAnimationView* view) {
  316. furi_assert(view);
  317. uint8_t frame_rate;
  318. BubbleAnimationViewModel* model = view_get_model(view->view);
  319. furi_assert(model->freeze_frame);
  320. bubble_animation_release_frame(&model->freeze_frame);
  321. furi_assert(model->current);
  322. frame_rate = model->current->icon_animation.frame_rate;
  323. view_commit_model(view->view, true);
  324. furi_timer_start(view->timer, 1000 / frame_rate);
  325. bubble_animation_activate(view, false);
  326. }
  327. View* bubble_animation_get_view(BubbleAnimationView* view) {
  328. furi_assert(view);
  329. return view->view;
  330. }