xremote_common_view.c 9.6 KB

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