bubble_animation_view.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 == NULL) {
  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(model->current != NULL) {
  127. if(!force) {
  128. if((model->active_ended_at + model->current->active_cooldown * 1000) >
  129. xTaskGetTickCount()) {
  130. activate = false;
  131. } else if(model->active_shift) {
  132. activate = false;
  133. } else if(model->current_frame >= model->current->passive_frames) {
  134. activate = false;
  135. }
  136. }
  137. }
  138. view_commit_model(view->view, false);
  139. if(!activate && !force) {
  140. return;
  141. }
  142. if(ACTIVE_SHIFT > 0) {
  143. BubbleAnimationViewModel* model = view_get_model(view->view);
  144. model->active_shift = ACTIVE_SHIFT;
  145. view_commit_model(view->view, false);
  146. } else {
  147. bubble_animation_activate_right_now(view);
  148. }
  149. }
  150. static void bubble_animation_activate_right_now(BubbleAnimationView* view) {
  151. furi_assert(view);
  152. uint8_t frame_rate = 0;
  153. BubbleAnimationViewModel* model = view_get_model(view->view);
  154. if(model->current && (model->current->active_frames > 0) && (!model->freeze_frame)) {
  155. model->current_frame = model->current->passive_frames;
  156. model->current_bubble = bubble_animation_pick_bubble(model, true);
  157. frame_rate = model->current->icon_animation.frame_rate;
  158. }
  159. view_commit_model(view->view, true);
  160. if(frame_rate) {
  161. furi_timer_start(view->timer, 1000 / frame_rate);
  162. }
  163. }
  164. static void bubble_animation_next_frame(BubbleAnimationViewModel* model) {
  165. furi_assert(model);
  166. if(!model->current) {
  167. return;
  168. }
  169. if(model->current_frame < model->current->passive_frames) {
  170. model->current_frame = (model->current_frame + 1) % model->current->passive_frames;
  171. } else {
  172. ++model->current_frame;
  173. model->active_cycle +=
  174. !((model->current_frame - model->current->passive_frames) %
  175. model->current->active_frames);
  176. if(model->active_cycle >= model->current->active_cycles) {
  177. // switch to passive
  178. model->active_cycle = 0;
  179. model->current_frame = 0;
  180. model->current_bubble = bubble_animation_pick_bubble(model, false);
  181. model->active_ended_at = xTaskGetTickCount();
  182. }
  183. if(model->current_bubble) {
  184. if(model->current_frame > model->current_bubble->end_frame) {
  185. model->current_bubble = model->current_bubble->next_bubble;
  186. }
  187. }
  188. }
  189. }
  190. static void bubble_animation_timer_callback(void* context) {
  191. furi_assert(context);
  192. BubbleAnimationView* view = context;
  193. bool activate = false;
  194. BubbleAnimationViewModel* model = view_get_model(view->view);
  195. if(model->active_shift > 0) {
  196. activate = (--model->active_shift == 0);
  197. }
  198. if(!model->freeze_frame && !activate) {
  199. bubble_animation_next_frame(model);
  200. }
  201. view_commit_model(view->view, !activate);
  202. if(activate) {
  203. bubble_animation_activate_right_now(view);
  204. }
  205. }
  206. /* always freeze first passive frame, because
  207. * animation is always activated at unfreezing and played
  208. * passive frame first, and 2 frames after - active
  209. */
  210. static Icon* bubble_animation_clone_first_frame(const Icon* icon_orig) {
  211. furi_assert(icon_orig);
  212. furi_assert(icon_orig->frames);
  213. furi_assert(icon_orig->frames[0]);
  214. Icon* icon_clone = malloc(sizeof(Icon));
  215. memcpy(icon_clone, icon_orig, sizeof(Icon));
  216. icon_clone->frames = malloc(sizeof(uint8_t*));
  217. /* icon bitmap can be either compressed or not. It is compressed if
  218. * compressed size is less than original, so max size for bitmap is
  219. * uncompressed (width * height) + 1 byte (in uncompressed case)
  220. * for compressed header
  221. */
  222. size_t max_bitmap_size = ROUND_UP_TO(icon_orig->width, 8) * icon_orig->height + 1;
  223. FURI_CONST_ASSIGN_PTR(icon_clone->frames[0], malloc(max_bitmap_size));
  224. memcpy((void*)icon_clone->frames[0], icon_orig->frames[0], max_bitmap_size);
  225. FURI_CONST_ASSIGN(icon_clone->frame_count, 1);
  226. return icon_clone;
  227. }
  228. static void bubble_animation_release_frame(Icon** icon) {
  229. furi_assert(icon);
  230. furi_assert(*icon);
  231. free((void*)(*icon)->frames[0]);
  232. free((void*)(*icon)->frames);
  233. free(*icon);
  234. *icon = NULL;
  235. }
  236. static void bubble_animation_enter(void* context) {
  237. furi_assert(context);
  238. BubbleAnimationView* view = context;
  239. bubble_animation_activate(view, false);
  240. BubbleAnimationViewModel* model = view_get_model(view->view);
  241. uint8_t frame_rate = 0;
  242. if(model->current != NULL) {
  243. frame_rate = model->current->icon_animation.frame_rate;
  244. }
  245. view_commit_model(view->view, false);
  246. if(frame_rate) {
  247. furi_timer_start(view->timer, 1000 / frame_rate);
  248. }
  249. }
  250. static void bubble_animation_exit(void* context) {
  251. furi_assert(context);
  252. BubbleAnimationView* view = context;
  253. furi_timer_stop(view->timer);
  254. }
  255. BubbleAnimationView* bubble_animation_view_alloc(void) {
  256. BubbleAnimationView* view = malloc(sizeof(BubbleAnimationView));
  257. view->view = view_alloc();
  258. view->interact_callback = NULL;
  259. view->timer = furi_timer_alloc(bubble_animation_timer_callback, FuriTimerTypePeriodic, view);
  260. view_allocate_model(view->view, ViewModelTypeLocking, sizeof(BubbleAnimationViewModel));
  261. view_set_context(view->view, view);
  262. view_set_draw_callback(view->view, bubble_animation_draw_callback);
  263. view_set_input_callback(view->view, bubble_animation_input_callback);
  264. view_set_enter_callback(view->view, bubble_animation_enter);
  265. view_set_exit_callback(view->view, bubble_animation_exit);
  266. return view;
  267. }
  268. void bubble_animation_view_free(BubbleAnimationView* view) {
  269. furi_assert(view);
  270. view_set_draw_callback(view->view, NULL);
  271. view_set_input_callback(view->view, NULL);
  272. view_set_context(view->view, NULL);
  273. view_free(view->view);
  274. view->view = NULL;
  275. free(view);
  276. }
  277. void bubble_animation_view_set_interact_callback(
  278. BubbleAnimationView* view,
  279. BubbleAnimationInteractCallback callback,
  280. void* context) {
  281. furi_assert(view);
  282. view->interact_callback_context = context;
  283. view->interact_callback = callback;
  284. }
  285. void bubble_animation_view_set_animation(
  286. BubbleAnimationView* view,
  287. const BubbleAnimation* new_animation) {
  288. furi_assert(view);
  289. furi_assert(new_animation);
  290. BubbleAnimationViewModel* model = view_get_model(view->view);
  291. furi_assert(model);
  292. model->current = new_animation;
  293. model->active_ended_at = xTaskGetTickCount() - (model->current->active_cooldown * 1000);
  294. model->active_bubbles = 0;
  295. model->passive_bubbles = 0;
  296. for(int i = 0; i < new_animation->frame_bubble_sequences_count; ++i) {
  297. if(new_animation->frame_bubble_sequences[i]->start_frame < new_animation->passive_frames) {
  298. ++model->passive_bubbles;
  299. } else {
  300. ++model->active_bubbles;
  301. }
  302. }
  303. /* select bubble sequence */
  304. model->current_bubble = bubble_animation_pick_bubble(model, false);
  305. model->current_frame = 0;
  306. model->active_cycle = 0;
  307. view_commit_model(view->view, true);
  308. furi_timer_start(view->timer, 1000 / new_animation->icon_animation.frame_rate);
  309. }
  310. void bubble_animation_freeze(BubbleAnimationView* view) {
  311. furi_assert(view);
  312. BubbleAnimationViewModel* model = view_get_model(view->view);
  313. furi_assert(model->current);
  314. furi_assert(!model->freeze_frame);
  315. model->freeze_frame = bubble_animation_clone_first_frame(&model->current->icon_animation);
  316. model->current = NULL;
  317. view_commit_model(view->view, false);
  318. furi_timer_stop(view->timer);
  319. }
  320. void bubble_animation_unfreeze(BubbleAnimationView* view) {
  321. furi_assert(view);
  322. uint8_t frame_rate;
  323. BubbleAnimationViewModel* model = view_get_model(view->view);
  324. furi_assert(model->freeze_frame);
  325. bubble_animation_release_frame(&model->freeze_frame);
  326. furi_assert(model->current);
  327. frame_rate = model->current->icon_animation.frame_rate;
  328. view_commit_model(view->view, true);
  329. furi_timer_start(view->timer, 1000 / frame_rate);
  330. bubble_animation_activate(view, false);
  331. }
  332. View* bubble_animation_get_view(BubbleAnimationView* view) {
  333. furi_assert(view);
  334. return view->view;
  335. }