bubble_animation_view.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #include "cmsis_os2.h"
  2. #include "../animation_manager.h"
  3. #include "../animation_storage.h"
  4. #include "furi_hal_delay.h"
  5. #include "furi_hal_resources.h"
  6. #include "furi/check.h"
  7. #include "furi/memmgr.h"
  8. #include "gui/canvas.h"
  9. #include "gui/elements.h"
  10. #include "gui/view.h"
  11. #include "input/input.h"
  12. #include <furi.h>
  13. #include "portmacro.h"
  14. #include <gui/icon.h>
  15. #include <stdint.h>
  16. #include <FreeRTOS.h>
  17. #include <timers.h>
  18. #include "bubble_animation_view.h"
  19. #include <gui/icon_i.h>
  20. #define ACTIVE_SHIFT 2
  21. typedef struct {
  22. const BubbleAnimation* current;
  23. const FrameBubble* current_bubble;
  24. uint8_t current_frame;
  25. uint8_t active_cycle;
  26. uint8_t active_bubbles;
  27. uint8_t passive_bubbles;
  28. uint8_t active_shift;
  29. TickType_t active_ended_at;
  30. Icon* freeze_frame;
  31. } BubbleAnimationViewModel;
  32. struct BubbleAnimationView {
  33. View* view;
  34. osTimerId_t timer;
  35. BubbleAnimationInteractCallback interact_callback;
  36. void* interact_callback_context;
  37. };
  38. static void bubble_animation_activate(BubbleAnimationView* view, bool force);
  39. static void bubble_animation_activate_right_now(BubbleAnimationView* view);
  40. static uint8_t bubble_animation_get_icon_index(BubbleAnimationViewModel* model) {
  41. furi_assert(model);
  42. uint8_t icon_index = 0;
  43. const BubbleAnimation* animation = model->current;
  44. if(model->current_frame < animation->passive_frames) {
  45. icon_index = model->current_frame;
  46. } else {
  47. icon_index =
  48. (model->current_frame - animation->passive_frames) % animation->active_frames +
  49. animation->passive_frames;
  50. }
  51. furi_assert(icon_index < (animation->passive_frames + animation->active_frames));
  52. return icon_index;
  53. }
  54. static void bubble_animation_draw_callback(Canvas* canvas, void* model_) {
  55. furi_assert(model_);
  56. furi_assert(canvas);
  57. BubbleAnimationViewModel* model = model_;
  58. const BubbleAnimation* animation = model->current;
  59. if(model->freeze_frame) {
  60. uint8_t y_offset = canvas_height(canvas) - icon_get_height(model->freeze_frame);
  61. canvas_draw_icon(canvas, 0, y_offset, model->freeze_frame);
  62. return;
  63. }
  64. if(!animation) {
  65. return;
  66. }
  67. furi_assert(model->current_frame < 255);
  68. const Icon* icon = animation->icons[bubble_animation_get_icon_index(model)];
  69. furi_assert(icon);
  70. uint8_t y_offset = canvas_height(canvas) - icon_get_height(icon);
  71. canvas_draw_icon(canvas, 0, y_offset, icon);
  72. const FrameBubble* bubble = model->current_bubble;
  73. if(bubble) {
  74. if((model->current_frame >= bubble->starts_at_frame) &&
  75. (model->current_frame <= bubble->ends_at_frame)) {
  76. const Bubble* b = &bubble->bubble;
  77. elements_bubble_str(canvas, b->x, b->y, b->str, b->horizontal, b->vertical);
  78. }
  79. }
  80. }
  81. static FrameBubble* bubble_animation_pick_bubble(BubbleAnimationViewModel* model, bool active) {
  82. FrameBubble* bubble = NULL;
  83. if((model->active_bubbles == 0) && (model->passive_bubbles == 0)) {
  84. return NULL;
  85. }
  86. uint8_t index = random() % (active ? model->active_bubbles : model->passive_bubbles);
  87. const BubbleAnimation* animation = model->current;
  88. for(int i = 0; i < animation->frame_bubbles_count; ++i) {
  89. if((animation->frame_bubbles[i]->starts_at_frame < animation->passive_frames) ^ active) {
  90. if(!index) {
  91. bubble = animation->frame_bubbles[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. } else if(event->key == InputKeyBack) {
  115. /* Prevent back button to fall down to common handler - leaving
  116. * application, so consume */
  117. consumed = true;
  118. }
  119. return consumed;
  120. }
  121. static void bubble_animation_activate(BubbleAnimationView* view, bool force) {
  122. furi_assert(view);
  123. bool activate = true;
  124. BubbleAnimationViewModel* model = view_get_model(view->view);
  125. if(!model->current) {
  126. activate = false;
  127. } else if(model->freeze_frame) {
  128. activate = false;
  129. } else if(model->current->active_frames == 0) {
  130. activate = false;
  131. }
  132. if(!force) {
  133. if((model->active_ended_at + model->current->active_cooldown * 1000) >
  134. xTaskGetTickCount()) {
  135. activate = false;
  136. } else if(model->active_shift) {
  137. activate = false;
  138. } else if(model->current_frame >= model->current->passive_frames) {
  139. activate = false;
  140. }
  141. }
  142. view_commit_model(view->view, false);
  143. if(!activate && !force) {
  144. return;
  145. }
  146. if(ACTIVE_SHIFT > 0) {
  147. BubbleAnimationViewModel* model = view_get_model(view->view);
  148. model->active_shift = ACTIVE_SHIFT;
  149. view_commit_model(view->view, false);
  150. } else {
  151. bubble_animation_activate_right_now(view);
  152. }
  153. }
  154. static void bubble_animation_activate_right_now(BubbleAnimationView* view) {
  155. furi_assert(view);
  156. uint8_t frame_rate = 0;
  157. BubbleAnimationViewModel* model = view_get_model(view->view);
  158. if(model->current && (model->current->active_frames > 0) && (!model->freeze_frame)) {
  159. model->current_frame = model->current->passive_frames;
  160. model->current_bubble = bubble_animation_pick_bubble(model, true);
  161. frame_rate = model->current->frame_rate;
  162. }
  163. view_commit_model(view->view, true);
  164. if(frame_rate) {
  165. osTimerStart(view->timer, 1000 / frame_rate);
  166. }
  167. }
  168. static void bubble_animation_next_frame(BubbleAnimationViewModel* model) {
  169. furi_assert(model);
  170. if(!model->current) {
  171. return;
  172. }
  173. if(model->current_frame < model->current->passive_frames) {
  174. model->current_frame = (model->current_frame + 1) % model->current->passive_frames;
  175. } else {
  176. ++model->current_frame;
  177. model->active_cycle +=
  178. !((model->current_frame - model->current->passive_frames) %
  179. model->current->active_frames);
  180. if(model->active_cycle >= model->current->active_cycles) {
  181. // switch to passive
  182. model->active_cycle = 0;
  183. model->current_frame = 0;
  184. model->current_bubble = bubble_animation_pick_bubble(model, false);
  185. model->active_ended_at = xTaskGetTickCount();
  186. }
  187. if(model->current_bubble) {
  188. if(model->current_frame > model->current_bubble->ends_at_frame) {
  189. model->current_bubble = model->current_bubble->next_bubble;
  190. }
  191. }
  192. }
  193. }
  194. static void bubble_animation_timer_callback(void* context) {
  195. furi_assert(context);
  196. BubbleAnimationView* view = context;
  197. bool activate = false;
  198. BubbleAnimationViewModel* model = view_get_model(view->view);
  199. if(model->active_shift > 0) {
  200. activate = (--model->active_shift == 0);
  201. }
  202. if(!model->freeze_frame && !activate) {
  203. bubble_animation_next_frame(model);
  204. }
  205. view_commit_model(view->view, !activate);
  206. if(activate) {
  207. bubble_animation_activate_right_now(view);
  208. }
  209. }
  210. static Icon* bubble_animation_clone_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 = furi_alloc(sizeof(Icon));
  215. memcpy(icon_clone, icon_orig, sizeof(Icon));
  216. icon_clone->frames = furi_alloc(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. icon_clone->frames[0] = furi_alloc(max_bitmap_size);
  224. memcpy((void*)icon_clone->frames[0], icon_orig->frames[0], max_bitmap_size);
  225. return icon_clone;
  226. }
  227. static void bubble_animation_release_frame(Icon** icon) {
  228. furi_assert(icon);
  229. furi_assert(*icon);
  230. free((void*)(*icon)->frames[0]);
  231. free((*icon)->frames);
  232. free(*icon);
  233. *icon = NULL;
  234. }
  235. static void bubble_animation_enter(void* context) {
  236. furi_assert(context);
  237. BubbleAnimationView* view = context;
  238. bubble_animation_activate(view, false);
  239. BubbleAnimationViewModel* model = view_get_model(view->view);
  240. uint8_t frame_rate = model->current->frame_rate;
  241. view_commit_model(view->view, false);
  242. if(frame_rate) {
  243. osTimerStart(view->timer, 1000 / frame_rate);
  244. }
  245. }
  246. static void bubble_animation_exit(void* context) {
  247. furi_assert(context);
  248. BubbleAnimationView* view = context;
  249. osTimerStop(view->timer);
  250. }
  251. BubbleAnimationView* bubble_animation_view_alloc(void) {
  252. BubbleAnimationView* view = furi_alloc(sizeof(BubbleAnimationView));
  253. view->view = view_alloc();
  254. view->interact_callback = NULL;
  255. view->timer = osTimerNew(bubble_animation_timer_callback, osTimerPeriodic, view, NULL);
  256. view_allocate_model(view->view, ViewModelTypeLocking, sizeof(BubbleAnimationViewModel));
  257. view_set_context(view->view, view);
  258. view_set_draw_callback(view->view, bubble_animation_draw_callback);
  259. view_set_input_callback(view->view, bubble_animation_input_callback);
  260. view_set_enter_callback(view->view, bubble_animation_enter);
  261. view_set_exit_callback(view->view, bubble_animation_exit);
  262. return view;
  263. }
  264. void bubble_animation_view_free(BubbleAnimationView* view) {
  265. furi_assert(view);
  266. view_set_draw_callback(view->view, NULL);
  267. view_set_input_callback(view->view, NULL);
  268. view_set_context(view->view, NULL);
  269. view_free(view->view);
  270. view->view = NULL;
  271. free(view);
  272. }
  273. void bubble_animation_view_set_interact_callback(
  274. BubbleAnimationView* view,
  275. BubbleAnimationInteractCallback callback,
  276. void* context) {
  277. furi_assert(view);
  278. view->interact_callback_context = context;
  279. view->interact_callback = callback;
  280. }
  281. void bubble_animation_view_set_animation(
  282. BubbleAnimationView* view,
  283. const BubbleAnimation* new_animation) {
  284. furi_assert(view);
  285. furi_assert(new_animation);
  286. BubbleAnimationViewModel* model = view_get_model(view->view);
  287. furi_assert(model);
  288. model->current = new_animation;
  289. model->active_ended_at = xTaskGetTickCount() - (model->current->active_cooldown * 1000);
  290. model->active_bubbles = 0;
  291. model->passive_bubbles = 0;
  292. for(int i = 0; i < new_animation->frame_bubbles_count; ++i) {
  293. if(new_animation->frame_bubbles[i]->starts_at_frame < new_animation->passive_frames) {
  294. ++model->passive_bubbles;
  295. } else {
  296. ++model->active_bubbles;
  297. }
  298. }
  299. /* select bubble sequence */
  300. model->current_bubble = bubble_animation_pick_bubble(model, false);
  301. model->current_frame = 0;
  302. model->active_cycle = 0;
  303. view_commit_model(view->view, true);
  304. osTimerStart(view->timer, 1000 / new_animation->frame_rate);
  305. }
  306. void bubble_animation_freeze(BubbleAnimationView* view) {
  307. furi_assert(view);
  308. BubbleAnimationViewModel* model = view_get_model(view->view);
  309. furi_assert(model->current);
  310. furi_assert(!model->freeze_frame);
  311. /* always freeze first passive frame, because
  312. * animation is always activated at unfreezing and played
  313. * passive frame first, and 2 frames after - active
  314. */
  315. uint8_t icon_index = 0;
  316. model->freeze_frame = bubble_animation_clone_frame(model->current->icons[icon_index]);
  317. model->current = NULL;
  318. view_commit_model(view->view, false);
  319. osTimerStop(view->timer);
  320. }
  321. void bubble_animation_unfreeze(BubbleAnimationView* view) {
  322. furi_assert(view);
  323. uint8_t frame_rate;
  324. BubbleAnimationViewModel* model = view_get_model(view->view);
  325. furi_assert(model->freeze_frame);
  326. bubble_animation_release_frame(&model->freeze_frame);
  327. furi_assert(model->current);
  328. furi_assert(model->current->icons);
  329. frame_rate = model->current->frame_rate;
  330. view_commit_model(view->view, true);
  331. osTimerStart(view->timer, 1000 / frame_rate);
  332. bubble_animation_activate(view, false);
  333. }
  334. View* bubble_animation_get_view(BubbleAnimationView* view) {
  335. furi_assert(view);
  336. return view->view;
  337. }