button_panel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include "button_panel.h"
  2. #include "furi_hal_resources.h"
  3. #include "gui/canvas.h"
  4. #include <m-array.h>
  5. #include <m-i-list.h>
  6. #include <m-list.h>
  7. #include <furi.h>
  8. #include <gui/elements.h>
  9. #include <stdint.h>
  10. typedef struct {
  11. // uint16_t to support multi-screen, wide button panel
  12. uint16_t x;
  13. uint16_t y;
  14. Font font;
  15. const char* str;
  16. } LabelElement;
  17. LIST_DEF(LabelList, LabelElement, M_POD_OPLIST)
  18. #define M_OPL_LabelList_t() LIST_OPLIST(LabelList)
  19. typedef struct {
  20. uint16_t x;
  21. uint16_t y;
  22. const Icon* name;
  23. const Icon* name_selected;
  24. } IconElement;
  25. typedef struct ButtonItem {
  26. uint32_t index;
  27. ButtonItemCallback callback;
  28. IconElement icon;
  29. void* callback_context;
  30. } ButtonItem;
  31. ARRAY_DEF(ButtonArray, ButtonItem*, M_PTR_OPLIST);
  32. #define M_OPL_ButtonArray_t() ARRAY_OPLIST(ButtonArray, M_PTR_OPLIST)
  33. ARRAY_DEF(ButtonMatrix, ButtonArray_t);
  34. #define M_OPL_ButtonMatrix_t() ARRAY_OPLIST(ButtonMatrix, M_OPL_ButtonArray_t())
  35. struct ButtonPanel {
  36. View* view;
  37. };
  38. typedef struct {
  39. ButtonMatrix_t button_matrix;
  40. LabelList_t labels;
  41. uint16_t reserve_x;
  42. uint16_t reserve_y;
  43. uint16_t selected_item_x;
  44. uint16_t selected_item_y;
  45. } ButtonPanelModel;
  46. static ButtonItem** button_panel_get_item(ButtonPanelModel* model, size_t x, size_t y);
  47. static void button_panel_process_up(ButtonPanel* button_panel);
  48. static void button_panel_process_down(ButtonPanel* button_panel);
  49. static void button_panel_process_left(ButtonPanel* button_panel);
  50. static void button_panel_process_right(ButtonPanel* button_panel);
  51. static void button_panel_process_ok(ButtonPanel* button_panel);
  52. static void button_panel_view_draw_callback(Canvas* canvas, void* _model);
  53. static bool button_panel_view_input_callback(InputEvent* event, void* context);
  54. ButtonPanel* button_panel_alloc() {
  55. ButtonPanel* button_panel = malloc(sizeof(ButtonPanel));
  56. button_panel->view = view_alloc();
  57. view_set_orientation(button_panel->view, ViewOrientationVertical);
  58. view_set_context(button_panel->view, button_panel);
  59. view_allocate_model(button_panel->view, ViewModelTypeLocking, sizeof(ButtonPanelModel));
  60. view_set_draw_callback(button_panel->view, button_panel_view_draw_callback);
  61. view_set_input_callback(button_panel->view, button_panel_view_input_callback);
  62. with_view_model(
  63. button_panel->view,
  64. ButtonPanelModel * model,
  65. {
  66. model->reserve_x = 0;
  67. model->reserve_y = 0;
  68. model->selected_item_x = 0;
  69. model->selected_item_y = 0;
  70. ButtonMatrix_init(model->button_matrix);
  71. LabelList_init(model->labels);
  72. },
  73. true);
  74. return button_panel;
  75. }
  76. void button_panel_reserve(ButtonPanel* button_panel, size_t reserve_x, size_t reserve_y) {
  77. furi_check(reserve_x > 0);
  78. furi_check(reserve_y > 0);
  79. with_view_model(
  80. button_panel->view,
  81. ButtonPanelModel * model,
  82. {
  83. model->reserve_x = reserve_x;
  84. model->reserve_y = reserve_y;
  85. ButtonMatrix_reserve(model->button_matrix, model->reserve_y);
  86. for(size_t i = 0; i > model->reserve_y; ++i) {
  87. ButtonArray_t* array = ButtonMatrix_get(model->button_matrix, i);
  88. ButtonArray_init(*array);
  89. ButtonArray_reserve(*array, reserve_x);
  90. // TODO: do we need to clear allocated memory of ptr-s to ButtonItem ??
  91. }
  92. LabelList_init(model->labels);
  93. },
  94. true);
  95. }
  96. void button_panel_free(ButtonPanel* button_panel) {
  97. furi_assert(button_panel);
  98. button_panel_reset(button_panel);
  99. with_view_model(
  100. button_panel->view,
  101. ButtonPanelModel * model,
  102. {
  103. LabelList_clear(model->labels);
  104. ButtonMatrix_clear(model->button_matrix);
  105. },
  106. true);
  107. view_free(button_panel->view);
  108. free(button_panel);
  109. }
  110. void button_panel_reset(ButtonPanel* button_panel) {
  111. furi_assert(button_panel);
  112. with_view_model(
  113. button_panel->view,
  114. ButtonPanelModel * model,
  115. {
  116. for(size_t x = 0; x < model->reserve_x; ++x) {
  117. for(size_t y = 0; y < model->reserve_y; ++y) {
  118. ButtonItem** button_item = button_panel_get_item(model, x, y);
  119. free(*button_item);
  120. *button_item = NULL;
  121. }
  122. }
  123. model->reserve_x = 0;
  124. model->reserve_y = 0;
  125. model->selected_item_x = 0;
  126. model->selected_item_y = 0;
  127. LabelList_reset(model->labels);
  128. ButtonMatrix_reset(model->button_matrix);
  129. },
  130. true);
  131. }
  132. static ButtonItem** button_panel_get_item(ButtonPanelModel* model, size_t x, size_t y) {
  133. furi_assert(model);
  134. furi_check(x < model->reserve_x);
  135. furi_check(y < model->reserve_y);
  136. ButtonArray_t* button_array = ButtonMatrix_safe_get(model->button_matrix, x);
  137. ButtonItem** button_item = ButtonArray_safe_get(*button_array, y);
  138. return button_item;
  139. }
  140. void button_panel_add_item(
  141. ButtonPanel* button_panel,
  142. uint32_t index,
  143. uint16_t matrix_place_x,
  144. uint16_t matrix_place_y,
  145. uint16_t x,
  146. uint16_t y,
  147. const Icon* icon_name,
  148. const Icon* icon_name_selected,
  149. ButtonItemCallback callback,
  150. void* callback_context) {
  151. furi_assert(button_panel);
  152. with_view_model(
  153. button_panel->view,
  154. ButtonPanelModel * model,
  155. {
  156. ButtonItem** button_item_ptr =
  157. button_panel_get_item(model, matrix_place_x, matrix_place_y);
  158. furi_check(*button_item_ptr == NULL);
  159. *button_item_ptr = malloc(sizeof(ButtonItem));
  160. ButtonItem* button_item = *button_item_ptr;
  161. button_item->callback = callback;
  162. button_item->callback_context = callback_context;
  163. button_item->icon.x = x;
  164. button_item->icon.y = y;
  165. button_item->icon.name = icon_name;
  166. button_item->icon.name_selected = icon_name_selected;
  167. button_item->index = index;
  168. },
  169. true);
  170. }
  171. View* button_panel_get_view(ButtonPanel* button_panel) {
  172. furi_assert(button_panel);
  173. return button_panel->view;
  174. }
  175. static void button_panel_view_draw_callback(Canvas* canvas, void* _model) {
  176. furi_assert(canvas);
  177. furi_assert(_model);
  178. ButtonPanelModel* model = _model;
  179. canvas_clear(canvas);
  180. canvas_set_color(canvas, ColorBlack);
  181. for(size_t x = 0; x < model->reserve_x; ++x) {
  182. for(size_t y = 0; y < model->reserve_y; ++y) {
  183. ButtonItem* button_item = *button_panel_get_item(model, x, y);
  184. const Icon* icon_name = button_item->icon.name;
  185. if((model->selected_item_x == x) && (model->selected_item_y == y)) {
  186. icon_name = button_item->icon.name_selected;
  187. }
  188. canvas_draw_icon(canvas, button_item->icon.x, button_item->icon.y, icon_name);
  189. }
  190. }
  191. for
  192. M_EACH(label, model->labels, LabelList_t) {
  193. canvas_set_font(canvas, label->font);
  194. canvas_draw_str(canvas, label->x, label->y, label->str);
  195. }
  196. }
  197. static void button_panel_process_down(ButtonPanel* button_panel) {
  198. with_view_model(
  199. button_panel->view,
  200. ButtonPanelModel * model,
  201. {
  202. uint16_t new_selected_item_x = model->selected_item_x;
  203. uint16_t new_selected_item_y = model->selected_item_y;
  204. size_t i;
  205. if(new_selected_item_y < (model->reserve_y - 1)) {
  206. ++new_selected_item_y;
  207. for(i = 0; i < model->reserve_x; ++i) {
  208. new_selected_item_x = (model->selected_item_x + i) % model->reserve_x;
  209. if(*button_panel_get_item(model, new_selected_item_x, new_selected_item_y)) {
  210. break;
  211. }
  212. }
  213. if(i != model->reserve_x) {
  214. model->selected_item_x = new_selected_item_x;
  215. model->selected_item_y = new_selected_item_y;
  216. }
  217. }
  218. },
  219. true);
  220. }
  221. static void button_panel_process_up(ButtonPanel* button_panel) {
  222. with_view_model(
  223. button_panel->view,
  224. ButtonPanelModel * model,
  225. {
  226. size_t new_selected_item_x = model->selected_item_x;
  227. size_t new_selected_item_y = model->selected_item_y;
  228. size_t i;
  229. if(new_selected_item_y > 0) {
  230. --new_selected_item_y;
  231. for(i = 0; i < model->reserve_x; ++i) {
  232. new_selected_item_x = (model->selected_item_x + i) % model->reserve_x;
  233. if(*button_panel_get_item(model, new_selected_item_x, new_selected_item_y)) {
  234. break;
  235. }
  236. }
  237. if(i != model->reserve_x) {
  238. model->selected_item_x = new_selected_item_x;
  239. model->selected_item_y = new_selected_item_y;
  240. }
  241. }
  242. },
  243. true);
  244. }
  245. static void button_panel_process_left(ButtonPanel* button_panel) {
  246. with_view_model(
  247. button_panel->view,
  248. ButtonPanelModel * model,
  249. {
  250. size_t new_selected_item_x = model->selected_item_x;
  251. size_t new_selected_item_y = model->selected_item_y;
  252. size_t i;
  253. if(new_selected_item_x > 0) {
  254. --new_selected_item_x;
  255. for(i = 0; i < model->reserve_y; ++i) {
  256. new_selected_item_y = (model->selected_item_y + i) % model->reserve_y;
  257. if(*button_panel_get_item(model, new_selected_item_x, new_selected_item_y)) {
  258. break;
  259. }
  260. }
  261. if(i != model->reserve_y) {
  262. model->selected_item_x = new_selected_item_x;
  263. model->selected_item_y = new_selected_item_y;
  264. }
  265. }
  266. },
  267. true);
  268. }
  269. static void button_panel_process_right(ButtonPanel* button_panel) {
  270. with_view_model(
  271. button_panel->view,
  272. ButtonPanelModel * model,
  273. {
  274. uint16_t new_selected_item_x = model->selected_item_x;
  275. uint16_t new_selected_item_y = model->selected_item_y;
  276. size_t i;
  277. if(new_selected_item_x < (model->reserve_x - 1)) {
  278. ++new_selected_item_x;
  279. for(i = 0; i < model->reserve_y; ++i) {
  280. new_selected_item_y = (model->selected_item_y + i) % model->reserve_y;
  281. if(*button_panel_get_item(model, new_selected_item_x, new_selected_item_y)) {
  282. break;
  283. }
  284. }
  285. if(i != model->reserve_y) {
  286. model->selected_item_x = new_selected_item_x;
  287. model->selected_item_y = new_selected_item_y;
  288. }
  289. }
  290. },
  291. true);
  292. }
  293. void button_panel_process_ok(ButtonPanel* button_panel) {
  294. ButtonItem* button_item = NULL;
  295. with_view_model(
  296. button_panel->view,
  297. ButtonPanelModel * model,
  298. {
  299. button_item =
  300. *button_panel_get_item(model, model->selected_item_x, model->selected_item_y);
  301. },
  302. true);
  303. if(button_item && button_item->callback) {
  304. button_item->callback(button_item->callback_context, button_item->index);
  305. }
  306. }
  307. static bool button_panel_view_input_callback(InputEvent* event, void* context) {
  308. ButtonPanel* button_panel = context;
  309. furi_assert(button_panel);
  310. bool consumed = false;
  311. if(event->type == InputTypeShort) {
  312. switch(event->key) {
  313. case InputKeyUp:
  314. consumed = true;
  315. button_panel_process_up(button_panel);
  316. break;
  317. case InputKeyDown:
  318. consumed = true;
  319. button_panel_process_down(button_panel);
  320. break;
  321. case InputKeyLeft:
  322. consumed = true;
  323. button_panel_process_left(button_panel);
  324. break;
  325. case InputKeyRight:
  326. consumed = true;
  327. button_panel_process_right(button_panel);
  328. break;
  329. case InputKeyOk:
  330. consumed = true;
  331. button_panel_process_ok(button_panel);
  332. break;
  333. default:
  334. break;
  335. }
  336. }
  337. return consumed;
  338. }
  339. void button_panel_add_label(
  340. ButtonPanel* button_panel,
  341. uint16_t x,
  342. uint16_t y,
  343. Font font,
  344. const char* label_str) {
  345. furi_assert(button_panel);
  346. with_view_model(
  347. button_panel->view,
  348. ButtonPanelModel * model,
  349. {
  350. LabelElement* label = LabelList_push_raw(model->labels);
  351. label->x = x;
  352. label->y = y;
  353. label->font = font;
  354. label->str = label_str;
  355. },
  356. true);
  357. }