xremote_common_view.c 9.6 KB

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