bubble_animation_view.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 <FreeRTOS.h>
  13. #include <timers.h>
  14. #include <furi/dangerous_defines.h>
  15. #define ACTIVE_SHIFT 2
  16. typedef struct {
  17. const BubbleAnimation* current;
  18. const FrameBubble* current_bubble;
  19. uint8_t current_frame;
  20. uint8_t active_cycle;
  21. uint8_t active_bubbles;
  22. uint8_t passive_bubbles;
  23. uint8_t active_shift;
  24. TickType_t active_ended_at;
  25. Icon* freeze_frame;
  26. } BubbleAnimationViewModel;
  27. struct BubbleAnimationView {
  28. View* view;
  29. osTimerId_t timer;
  30. BubbleAnimationInteractCallback interact_callback;
  31. void* interact_callback_context;
  32. };
  33. static void bubble_animation_activate(BubbleAnimationView* view, bool force);
  34. static void bubble_animation_activate_right_now(BubbleAnimationView* view);
  35. static uint8_t bubble_animation_get_frame_index(BubbleAnimationViewModel* model) {
  36. furi_assert(model);
  37. uint8_t icon_index = 0;
  38. const BubbleAnimation* animation = model->current;
  39. if(model->current_frame < animation->passive_frames) {
  40. icon_index = model->current_frame;
  41. } else {
  42. icon_index =
  43. (model->current_frame - animation->passive_frames) % animation->active_frames +
  44. animation->passive_frames;
  45. }
  46. furi_assert(icon_index < (animation->passive_frames + animation->active_frames));
  47. return animation->frame_order[icon_index];
  48. }
  49. static void bubble_animation_draw_callback(Canvas* canvas, void* model_) {
  50. furi_assert(model_);
  51. furi_assert(canvas);
  52. BubbleAnimationViewModel* model = model_;
  53. const BubbleAnimation* animation = model->current;
  54. if(model->freeze_frame) {
  55. uint8_t y_offset = canvas_height(canvas) - icon_get_height(model->freeze_frame);
  56. canvas_draw_icon(canvas, 0, y_offset, model->freeze_frame);
  57. return;
  58. }
  59. if(!animation) {
  60. return;
  61. }
  62. furi_assert(model->current_frame < 255);
  63. uint8_t index = bubble_animation_get_frame_index(model);
  64. uint8_t width = icon_get_width(&animation->icon_animation);
  65. uint8_t height = icon_get_height(&animation->icon_animation);
  66. uint8_t y_offset = canvas_height(canvas) - height;
  67. canvas_draw_bitmap(
  68. canvas, 0, y_offset, width, height, animation->icon_animation.frames[index]);
  69. const FrameBubble* bubble = model->current_bubble;
  70. if(bubble) {
  71. if((model->current_frame >= bubble->start_frame) &&
  72. (model->current_frame <= bubble->end_frame)) {
  73. const Bubble* b = &bubble->bubble;
  74. elements_bubble_str(canvas, b->x, b->y, b->text, b->align_h, b->align_v);
  75. }
  76. }
  77. }
  78. static const FrameBubble*
  79. bubble_animation_pick_bubble(BubbleAnimationViewModel* model, bool active) {
  80. const FrameBubble* bubble = NULL;
  81. if((model->active_bubbles == 0) && (model->passive_bubbles == 0)) {
  82. return NULL;
  83. }
  84. uint8_t index =
  85. furi_hal_random_get() % (active ? model->active_bubbles : model->passive_bubbles);
  86. const BubbleAnimation* animation = model->current;
  87. for(int i = 0; i < animation->frame_bubble_sequences_count; ++i) {
  88. if((animation->frame_bubble_sequences[i]->start_frame < animation->passive_frames) ^
  89. active) {
  90. if(!index) {
  91. bubble = animation->frame_bubble_sequences[i];
  92. }
  93. --index;
  94. }
  95. }
  96. return bubble;
  97. }
  98. static bool bubble_animation_input_callback(InputEvent* event, void* context) {
  99. furi_assert(context);
  100. furi_assert(event);
  101. BubbleAnimationView* animation_view = context;
  102. bool consumed = false;
  103. if(event->type == InputTypePress) {
  104. bubble_animation_activate(animation_view, false);
  105. }
  106. if(event->key == InputKeyRight) {
  107. /* Right button reserved for animation activation, so consume */
  108. consumed = true;
  109. if(event->type == InputTypeShort) {
  110. if(animation_view->interact_callback) {
  111. animation_view->interact_callback(animation_view->interact_callback_context);
  112. }
  113. }
  114. }
  115. return consumed;
  116. }
  117. static void bubble_animation_activate(BubbleAnimationView* view, bool force) {
  118. furi_assert(view);
  119. bool activate = true;
  120. BubbleAnimationViewModel* model = view_get_model(view->view);
  121. if(!model->current) {
  122. activate = false;
  123. } else if(model->freeze_frame) {
  124. activate = false;
  125. } else if(model->current->active_frames == 0) {
  126. activate = false;
  127. }
  128. if(!force) {
  129. if((model->active_ended_at + model->current->active_cooldown * 1000) >
  130. xTaskGetTickCount()) {
  131. activate = false;
  132. } else if(model->active_shift) {
  133. activate = false;
  134. } else if(model->current_frame >= model->current->passive_frames) {
  135. activate = false;
  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. osTimerStart(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 = model->current->icon_animation.frame_rate;
  242. view_commit_model(view->view, false);
  243. if(frame_rate) {
  244. osTimerStart(view->timer, 1000 / frame_rate);
  245. }
  246. }
  247. static void bubble_animation_exit(void* context) {
  248. furi_assert(context);
  249. BubbleAnimationView* view = context;
  250. osTimerStop(view->timer);
  251. }
  252. BubbleAnimationView* bubble_animation_view_alloc(void) {
  253. BubbleAnimationView* view = malloc(sizeof(BubbleAnimationView));
  254. view->view = view_alloc();
  255. view->interact_callback = NULL;
  256. view->timer = osTimerNew(bubble_animation_timer_callback, osTimerPeriodic, view, NULL);
  257. view_allocate_model(view->view, ViewModelTypeLocking, sizeof(BubbleAnimationViewModel));
  258. view_set_context(view->view, view);
  259. view_set_draw_callback(view->view, bubble_animation_draw_callback);
  260. view_set_input_callback(view->view, bubble_animation_input_callback);
  261. view_set_enter_callback(view->view, bubble_animation_enter);
  262. view_set_exit_callback(view->view, bubble_animation_exit);
  263. return view;
  264. }
  265. void bubble_animation_view_free(BubbleAnimationView* view) {
  266. furi_assert(view);
  267. view_set_draw_callback(view->view, NULL);
  268. view_set_input_callback(view->view, NULL);
  269. view_set_context(view->view, NULL);
  270. view_free(view->view);
  271. view->view = NULL;
  272. free(view);
  273. }
  274. void bubble_animation_view_set_interact_callback(
  275. BubbleAnimationView* view,
  276. BubbleAnimationInteractCallback callback,
  277. void* context) {
  278. furi_assert(view);
  279. view->interact_callback_context = context;
  280. view->interact_callback = callback;
  281. }
  282. void bubble_animation_view_set_animation(
  283. BubbleAnimationView* view,
  284. const BubbleAnimation* new_animation) {
  285. furi_assert(view);
  286. furi_assert(new_animation);
  287. BubbleAnimationViewModel* model = view_get_model(view->view);
  288. furi_assert(model);
  289. model->current = new_animation;
  290. model->active_ended_at = xTaskGetTickCount() - (model->current->active_cooldown * 1000);
  291. model->active_bubbles = 0;
  292. model->passive_bubbles = 0;
  293. for(int i = 0; i < new_animation->frame_bubble_sequences_count; ++i) {
  294. if(new_animation->frame_bubble_sequences[i]->start_frame < new_animation->passive_frames) {
  295. ++model->passive_bubbles;
  296. } else {
  297. ++model->active_bubbles;
  298. }
  299. }
  300. /* select bubble sequence */
  301. model->current_bubble = bubble_animation_pick_bubble(model, false);
  302. model->current_frame = 0;
  303. model->active_cycle = 0;
  304. view_commit_model(view->view, true);
  305. osTimerStart(view->timer, 1000 / new_animation->icon_animation.frame_rate);
  306. }
  307. void bubble_animation_freeze(BubbleAnimationView* view) {
  308. furi_assert(view);
  309. BubbleAnimationViewModel* model = view_get_model(view->view);
  310. furi_assert(model->current);
  311. furi_assert(!model->freeze_frame);
  312. model->freeze_frame = bubble_animation_clone_first_frame(&model->current->icon_animation);
  313. model->current = NULL;
  314. view_commit_model(view->view, false);
  315. osTimerStop(view->timer);
  316. }
  317. void bubble_animation_unfreeze(BubbleAnimationView* view) {
  318. furi_assert(view);
  319. uint8_t frame_rate;
  320. BubbleAnimationViewModel* model = view_get_model(view->view);
  321. furi_assert(model->freeze_frame);
  322. bubble_animation_release_frame(&model->freeze_frame);
  323. furi_assert(model->current);
  324. frame_rate = model->current->icon_animation.frame_rate;
  325. view_commit_model(view->view, true);
  326. osTimerStart(view->timer, 1000 / frame_rate);
  327. bubble_animation_activate(view, false);
  328. }
  329. View* bubble_animation_get_view(BubbleAnimationView* view) {
  330. furi_assert(view);
  331. return view->view;
  332. }