xremote_common_view.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*!
  2. * @file flipper-xremote/views/xremote_common_view.c
  3. @license This project is released under the GNU GPLv3 License
  4. * @copyright (c) 2023 Sandro Kalatozishvili (s.kalatoz@gmail.com)
  5. *
  6. * @brief Common view and canvas functionality shared between the pages.
  7. */
  8. #include "xremote_common_view.h"
  9. #include "../infrared/infrared_remote.h"
  10. #include "../xremote_app.h"
  11. struct XRemoteView {
  12. XRemoteViewClearCallback on_clear;
  13. XRemoteAppContext* app_ctx;
  14. View* view;
  15. void *context;
  16. };
  17. const NotificationSequence g_sequence_blink_purple_50 = {
  18. &message_red_255,
  19. &message_blue_255,
  20. &message_delay_50,
  21. NULL,
  22. };
  23. XRemoteView* xremote_view_alloc(void* app_ctx, ViewInputCallback input_cb, ViewDrawCallback draw_cb)
  24. {
  25. XRemoteView* remote_view = malloc(sizeof(XRemoteView));
  26. remote_view->app_ctx = app_ctx;
  27. remote_view->view = view_alloc();
  28. remote_view->context = NULL;
  29. remote_view->on_clear = NULL;
  30. view_set_orientation(remote_view->view, ((XRemoteAppContext*)app_ctx)->app_settings->orientation);
  31. view_allocate_model(remote_view->view, ViewModelTypeLocking, sizeof(XRemoteViewModel));
  32. view_set_input_callback(remote_view->view, input_cb);
  33. view_set_draw_callback(remote_view->view, draw_cb);
  34. view_set_context(remote_view->view, remote_view);
  35. return remote_view;
  36. }
  37. void xremote_view_clear_context(XRemoteView* rview)
  38. {
  39. furi_assert(rview);
  40. if (rview->context != NULL &&
  41. rview->on_clear != NULL)
  42. {
  43. rview->on_clear(rview->context);
  44. rview->context = NULL;
  45. }
  46. }
  47. void xremote_view_set_context(XRemoteView* rview, void *context, XRemoteViewClearCallback on_clear)
  48. {
  49. furi_assert(rview);
  50. xremote_view_clear_context(rview);
  51. rview->context = context;
  52. rview->on_clear = on_clear;
  53. }
  54. void* xremote_view_get_context(XRemoteView* rview)
  55. {
  56. furi_assert(rview);
  57. return rview->context;
  58. }
  59. void* xremote_view_get_app_context(XRemoteView* rview)
  60. {
  61. furi_assert(rview);
  62. return rview->app_ctx;
  63. }
  64. void xremote_view_free(XRemoteView* rview)
  65. {
  66. furi_assert(rview);
  67. xremote_view_clear_context(rview);
  68. view_free(rview->view);
  69. free(rview);
  70. }
  71. View* xremote_view_get_view(XRemoteView* rview)
  72. {
  73. furi_assert(rview);
  74. return rview->view;
  75. }
  76. static InfraredRemoteButton* xremote_view_get_button_by_name(XRemoteView *rview, const char* name)
  77. {
  78. xremote_app_assert(rview->context, NULL);
  79. InfraredRemote* remote = (InfraredRemote*)rview->context;
  80. return infrared_remote_get_button_by_name(remote, name);
  81. }
  82. void xremote_view_send_ir(XRemoteView *rview, const char *name)
  83. {
  84. InfraredRemoteButton* button = xremote_view_get_button_by_name(rview, name);
  85. xremote_app_assert_void(button);
  86. InfraredSignal* signal = infrared_remote_button_get_signal(button);
  87. xremote_app_assert_void(signal);
  88. infrared_signal_transmit(signal);
  89. NotificationApp* notifications = rview->app_ctx->notifications;
  90. notification_message(notifications, &g_sequence_blink_purple_50);
  91. }
  92. void xremote_canvas_draw_icon(Canvas* canvas, uint8_t x, uint8_t y, XRemoteIcon icon)
  93. {
  94. if (icon == XRemoteIconEnter)
  95. {
  96. canvas_draw_circle(canvas, x - 2, y, 4);
  97. canvas_draw_disc(canvas, x - 2, y, 2);
  98. }
  99. else if (icon == XRemoteIconBack)
  100. {
  101. canvas_draw_triangle(canvas, x - 4, y - 2, 5, 3, CanvasDirectionRightToLeft);
  102. canvas_draw_line(canvas, x + 2, y + 1, x + 1, y + 2);
  103. canvas_draw_line(canvas, x + 2, y, x + 1, y - 1);
  104. canvas_draw_line(canvas, x, y - 2, x - 5, y - 2);
  105. canvas_draw_line(canvas, x, y + 3, x - 3, y + 3);
  106. }
  107. else if (icon == XRemoteIconArrowUp)
  108. {
  109. canvas_draw_triangle(canvas, x - 2, y - 2, 5, 3, CanvasDirectionBottomToTop);
  110. canvas_draw_line(canvas, x - 2, y - 3, x - 2, y + 4);
  111. }
  112. else if (icon == XRemoteIconArrowDown)
  113. {
  114. canvas_draw_triangle(canvas, x - 2, y + 2, 5, 3, CanvasDirectionTopToBottom);
  115. canvas_draw_line(canvas, x - 2, y - 4, x - 2, y + 3);
  116. }
  117. else if (icon == XRemoteIconArrowLeft)
  118. {
  119. canvas_draw_triangle(canvas, x - 4, y, 5, 3, CanvasDirectionRightToLeft);
  120. canvas_draw_line(canvas, x + 2, y, x - 5, y);
  121. }
  122. else if (icon == XRemoteIconArrowRight)
  123. {
  124. canvas_draw_triangle(canvas, x, y, 5, 3, CanvasDirectionLeftToRight);
  125. canvas_draw_line(canvas, x - 6, y, x + 1, y);
  126. }
  127. else if (icon == XRemoteIconJumpForward)
  128. {
  129. canvas_draw_triangle(canvas, x - 2, y, 5, 3, CanvasDirectionLeftToRight);
  130. canvas_draw_triangle(canvas, x - 5, y, 5, 3, CanvasDirectionLeftToRight);
  131. canvas_draw_line(canvas, x + 1, y - 2, x + 1, y + 2);
  132. canvas_draw_line(canvas, x - 4, y, x, y);
  133. }
  134. else if (icon == XRemoteIconJumpBackward)
  135. {
  136. canvas_draw_triangle(canvas, x - 2, y, 5, 3, CanvasDirectionRightToLeft);
  137. canvas_draw_triangle(canvas, x + 1, y, 5, 3, CanvasDirectionRightToLeft);
  138. canvas_draw_line(canvas, x - 5, y - 2, x - 5, y + 2);
  139. canvas_draw_line(canvas, x, y, x - 4, y);
  140. }
  141. else if (icon == XRemoteIconFastForward)
  142. {
  143. canvas_draw_triangle(canvas, x - 1, y, 5, 3, CanvasDirectionLeftToRight);
  144. canvas_draw_triangle(canvas, x - 4, y, 5, 3, CanvasDirectionLeftToRight);
  145. canvas_draw_line(canvas, x - 3, y, x, y);
  146. }
  147. else if (icon == XRemoteIconFastBackward)
  148. {
  149. canvas_draw_triangle(canvas, x - 3, y, 5, 3, CanvasDirectionRightToLeft);
  150. canvas_draw_triangle(canvas, x, y, 5, 3, CanvasDirectionRightToLeft);
  151. canvas_draw_line(canvas, x - 1, y, x - 4, y);
  152. }
  153. else if (icon == XRemoteIconPlayPause)
  154. {
  155. canvas_draw_triangle(canvas, x - 5, y, 5, 3, CanvasDirectionLeftToRight);
  156. canvas_draw_dot(canvas, x - 4, y);
  157. canvas_draw_line(canvas, x - 1, y - 2, x - 1, y + 2);
  158. canvas_draw_line(canvas, x + 1, y - 2, x + 1, y + 2);
  159. }
  160. else if (icon == XRemoteIconPlay)
  161. {
  162. canvas_draw_triangle(canvas, x - 3, y, 5, 3, CanvasDirectionLeftToRight);
  163. canvas_draw_dot(canvas, x - 2, y);
  164. }
  165. else if (icon == XRemoteIconPause)
  166. {
  167. canvas_draw_line(canvas, x - 3, y - 2, x - 3, y + 2);
  168. canvas_draw_line(canvas, x - 1, y - 2, x - 1, y + 2);
  169. }
  170. else if (icon == XRemoteIconStop)
  171. {
  172. canvas_draw_box(canvas, x - 4, y - 2, 5, 5);
  173. }
  174. else if (icon == XRemoteIconOk)
  175. {
  176. canvas_draw_str(canvas, x - 7, y + 4, "OK");
  177. }
  178. }
  179. void xremote_canvas_draw_header(Canvas* canvas, ViewOrientation orient, const char* section)
  180. {
  181. Align align = AlignLeft;
  182. uint8_t x = 0;
  183. if (orient == ViewOrientationHorizontal)
  184. {
  185. align = AlignRight;
  186. x = 128;
  187. }
  188. canvas_set_font(canvas, FontPrimary);
  189. elements_multiline_text_aligned(canvas, x, 0, align, AlignTop, "XRemote");
  190. canvas_set_font(canvas, FontSecondary);
  191. elements_multiline_text_aligned(canvas, x, 12, align, AlignTop, section);
  192. }
  193. void xremote_canvas_draw_exit_footer(Canvas* canvas, ViewOrientation orient, const char *text)
  194. {
  195. canvas_set_font(canvas, FontSecondary);
  196. if (orient == ViewOrientationVertical)
  197. {
  198. xremote_canvas_draw_icon(canvas, 6, 124, XRemoteIconBack);
  199. elements_multiline_text_aligned(canvas, 12, 128, AlignLeft, AlignBottom, text);
  200. }
  201. else
  202. {
  203. xremote_canvas_draw_icon(canvas, 71, 60, XRemoteIconBack);
  204. elements_multiline_text_aligned(canvas, 128, 64, AlignRight, AlignBottom, text);
  205. }
  206. }
  207. void xremote_canvas_draw_button(Canvas* canvas, bool pressed, uint8_t x, uint8_t y, XRemoteIcon icon)
  208. {
  209. canvas_draw_icon(canvas, x, y, &I_Button_18x18);
  210. if (pressed)
  211. {
  212. elements_slightly_rounded_box(canvas, x + 3, y + 2, 13, 13);
  213. canvas_set_color(canvas, ColorWhite);
  214. }
  215. xremote_canvas_draw_icon(canvas, x + 11, y + 8, icon);
  216. canvas_set_color(canvas, ColorBlack);
  217. }
  218. void xremote_canvas_draw_button_wide(Canvas* canvas, bool pressed, uint8_t x, uint8_t y, char* text, XRemoteIcon icon)
  219. {
  220. (void)icon;
  221. elements_slightly_rounded_frame(canvas, x + 4, y, 56, 15);
  222. if (pressed)
  223. {
  224. elements_slightly_rounded_box(canvas, x + 6, y + 2, 52, 11);
  225. canvas_set_color(canvas, ColorWhite);
  226. }
  227. xremote_canvas_draw_icon(canvas, x + 15, y + 7, icon);
  228. elements_multiline_text_aligned(canvas, x + 22, y + 10, AlignLeft, AlignBottom, text);
  229. canvas_set_color(canvas, ColorBlack);
  230. }
  231. void xremote_canvas_draw_button_size(Canvas* canvas, bool pressed, uint8_t x, uint8_t y, uint8_t xy, char* text, XRemoteIcon icon)
  232. {
  233. (void)icon;
  234. elements_slightly_rounded_frame(canvas, x + 4, y, xy, 15);
  235. if (pressed)
  236. {
  237. elements_slightly_rounded_box(canvas, x + 6, y + 2, xy - 4, 11);
  238. canvas_set_color(canvas, ColorWhite);
  239. }
  240. xremote_canvas_draw_icon(canvas, x + 15, y + 7, icon);
  241. elements_multiline_text_aligned(canvas, x + 22, y + 10, AlignLeft, AlignBottom, text);
  242. canvas_set_color(canvas, ColorBlack);
  243. }
  244. void xremote_canvas_draw_frame(Canvas* canvas, bool pressed, uint8_t x, uint8_t y, uint8_t xl, const char *text)
  245. {
  246. elements_slightly_rounded_frame(canvas, x, y, xl, 15);
  247. if (pressed)
  248. {
  249. elements_slightly_rounded_box(canvas, x + 2, y + 2, xl - 4, 11);
  250. canvas_set_color(canvas, ColorWhite);
  251. }
  252. canvas_draw_str(canvas, x + 3, y + 11, text);
  253. canvas_set_color(canvas, ColorBlack);
  254. }