text_input.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include "text_input.h"
  2. #include <furi.h>
  3. struct TextInput {
  4. View* view;
  5. };
  6. typedef struct {
  7. const char text;
  8. const uint8_t x;
  9. const uint8_t y;
  10. } TextInputKey;
  11. typedef struct {
  12. const char* header;
  13. char* text;
  14. uint8_t max_text_length;
  15. TextInputCallback callback;
  16. void* callback_context;
  17. uint8_t selected_row;
  18. uint8_t selected_column;
  19. } TextInputModel;
  20. static const uint8_t keyboard_origin_x = 1;
  21. static const uint8_t keyboard_origin_y = 29;
  22. static const uint8_t keyboard_row_count = 3;
  23. #define ENTER_KEY '\r'
  24. #define BACKSPACE_KEY '\b'
  25. static const TextInputKey keyboard_keys_row_1[] = {
  26. {'q', 1, 8},
  27. {'w', 10, 8},
  28. {'e', 19, 8},
  29. {'r', 28, 8},
  30. {'t', 37, 8},
  31. {'y', 46, 8},
  32. {'u', 55, 8},
  33. {'i', 64, 8},
  34. {'o', 73, 8},
  35. {'p', 82, 8},
  36. {'7', 91, 8},
  37. {'8', 100, 8},
  38. {'9', 109, 8},
  39. {'_', 118, 8},
  40. };
  41. static const TextInputKey keyboard_keys_row_2[] = {
  42. {'a', 1, 20},
  43. {'s', 10, 20},
  44. {'d', 19, 20},
  45. {'f', 28, 20},
  46. {'g', 37, 20},
  47. {'h', 46, 20},
  48. {'j', 55, 20},
  49. {'k', 64, 20},
  50. {'l', 73, 20},
  51. {'4', 82, 20},
  52. {'5', 91, 20},
  53. {'6', 100, 20},
  54. {BACKSPACE_KEY, 110, 12},
  55. };
  56. static const TextInputKey keyboard_keys_row_3[] = {
  57. {'z', 1, 32},
  58. {'x', 10, 32},
  59. {'c', 19, 32},
  60. {'v', 28, 32},
  61. {'b', 37, 32},
  62. {'n', 46, 32},
  63. {'m', 55, 32},
  64. {'0', 64, 32},
  65. {'1', 73, 32},
  66. {'2', 82, 32},
  67. {'3', 91, 32},
  68. {ENTER_KEY, 102, 23},
  69. };
  70. static uint8_t get_row_size(uint8_t row_index) {
  71. uint8_t row_size = 0;
  72. switch(row_index + 1) {
  73. case 1:
  74. row_size = sizeof(keyboard_keys_row_1) / sizeof(TextInputKey);
  75. break;
  76. case 2:
  77. row_size = sizeof(keyboard_keys_row_2) / sizeof(TextInputKey);
  78. break;
  79. case 3:
  80. row_size = sizeof(keyboard_keys_row_3) / sizeof(TextInputKey);
  81. break;
  82. }
  83. return row_size;
  84. }
  85. static const TextInputKey* get_row(uint8_t row_index) {
  86. const TextInputKey* row = NULL;
  87. switch(row_index + 1) {
  88. case 1:
  89. row = keyboard_keys_row_1;
  90. break;
  91. case 2:
  92. row = keyboard_keys_row_2;
  93. break;
  94. case 3:
  95. row = keyboard_keys_row_3;
  96. break;
  97. }
  98. return row;
  99. }
  100. static const char get_selected_char(TextInputModel* model) {
  101. return get_row(model->selected_row)[model->selected_column].text;
  102. }
  103. static const bool char_is_lowercase(char letter) {
  104. return (letter >= 0x61 && letter <= 0x7A);
  105. }
  106. static const char char_to_uppercase(const char letter) {
  107. return (letter - 0x20);
  108. }
  109. static void text_input_view_draw_callback(Canvas* canvas, void* _model) {
  110. TextInputModel* model = _model;
  111. uint8_t text_length = strlen(model->text);
  112. uint8_t needed_string_width = canvas_width(canvas) - 4 - 7 - 4;
  113. char* text = model->text;
  114. canvas_clear(canvas);
  115. canvas_set_color(canvas, ColorBlack);
  116. canvas_draw_str(canvas, 2, 8, model->header);
  117. canvas_draw_line(canvas, 2, 12, canvas_width(canvas) - 7, 12);
  118. canvas_draw_line(canvas, 1, 13, 1, 25);
  119. canvas_draw_line(canvas, canvas_width(canvas) - 6, 13, canvas_width(canvas) - 6, 25);
  120. canvas_draw_line(canvas, 2, 26, canvas_width(canvas) - 7, 26);
  121. while(text != 0 && canvas_string_width(canvas, text) > needed_string_width) {
  122. text++;
  123. }
  124. canvas_draw_str(canvas, 4, 22, text);
  125. canvas_draw_str(canvas, 4 + canvas_string_width(canvas, text) + 1, 22, "|");
  126. canvas_set_font(canvas, FontKeyboard);
  127. for(uint8_t row = 0; row <= keyboard_row_count; row++) {
  128. uint8_t volatile column_count = get_row_size(row);
  129. const TextInputKey* keys = get_row(row);
  130. for(size_t column = 0; column < column_count; column++) {
  131. if(keys[column].text == ENTER_KEY) {
  132. canvas_set_color(canvas, ColorBlack);
  133. if(model->selected_row == row && model->selected_column == column) {
  134. canvas_draw_icon_name(
  135. canvas,
  136. keyboard_origin_x + keys[column].x,
  137. keyboard_origin_y + keys[column].y,
  138. I_KeySaveSelected_24x11);
  139. } else {
  140. canvas_draw_icon_name(
  141. canvas,
  142. keyboard_origin_x + keys[column].x,
  143. keyboard_origin_y + keys[column].y,
  144. I_KeySave_24x11);
  145. }
  146. } else if(keys[column].text == BACKSPACE_KEY) {
  147. canvas_set_color(canvas, ColorBlack);
  148. if(model->selected_row == row && model->selected_column == column) {
  149. canvas_draw_icon_name(
  150. canvas,
  151. keyboard_origin_x + keys[column].x,
  152. keyboard_origin_y + keys[column].y,
  153. I_KeyBackspaceSelected_16x9);
  154. } else {
  155. canvas_draw_icon_name(
  156. canvas,
  157. keyboard_origin_x + keys[column].x,
  158. keyboard_origin_y + keys[column].y,
  159. I_KeyBackspace_16x9);
  160. }
  161. } else {
  162. if(model->selected_row == row && model->selected_column == column) {
  163. canvas_set_color(canvas, ColorBlack);
  164. canvas_draw_box(
  165. canvas,
  166. keyboard_origin_x + keys[column].x - 1,
  167. keyboard_origin_y + keys[column].y - 8,
  168. 7,
  169. 10);
  170. canvas_set_color(canvas, ColorWhite);
  171. } else {
  172. canvas_set_color(canvas, ColorBlack);
  173. }
  174. if(text_length == 0 && char_is_lowercase(keys[column].text)) {
  175. canvas_draw_glyph(
  176. canvas,
  177. keyboard_origin_x + keys[column].x,
  178. keyboard_origin_y + keys[column].y,
  179. char_to_uppercase(keys[column].text));
  180. } else {
  181. canvas_draw_glyph(
  182. canvas,
  183. keyboard_origin_x + keys[column].x,
  184. keyboard_origin_y + keys[column].y,
  185. keys[column].text);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. static void text_input_handle_up(TextInput* text_input) {
  192. with_view_model(
  193. text_input->view, (TextInputModel * model) {
  194. if(model->selected_row > 0) {
  195. model->selected_row--;
  196. }
  197. });
  198. }
  199. static void text_input_handle_down(TextInput* text_input) {
  200. with_view_model(
  201. text_input->view, (TextInputModel * model) {
  202. if(model->selected_row < keyboard_row_count - 1) {
  203. model->selected_row++;
  204. if(model->selected_column > get_row_size(model->selected_row) - 1) {
  205. model->selected_column = get_row_size(model->selected_row) - 1;
  206. }
  207. }
  208. });
  209. }
  210. static void text_input_handle_left(TextInput* text_input) {
  211. with_view_model(
  212. text_input->view, (TextInputModel * model) {
  213. if(model->selected_column > 0) {
  214. model->selected_column--;
  215. } else {
  216. model->selected_column = get_row_size(model->selected_row) - 1;
  217. }
  218. });
  219. }
  220. static void text_input_handle_right(TextInput* text_input) {
  221. with_view_model(
  222. text_input->view, (TextInputModel * model) {
  223. if(model->selected_column < get_row_size(model->selected_row) - 1) {
  224. model->selected_column++;
  225. } else {
  226. model->selected_column = 0;
  227. }
  228. });
  229. }
  230. static void text_input_handle_ok(TextInput* text_input) {
  231. with_view_model(
  232. text_input->view, (TextInputModel * model) {
  233. char selected = get_selected_char(model);
  234. uint8_t text_length = strlen(model->text);
  235. if(selected == ENTER_KEY) {
  236. if(model->callback != 0) {
  237. model->callback(model->callback_context, model->text);
  238. }
  239. } else if(selected == BACKSPACE_KEY) {
  240. if(text_length > 0) {
  241. model->text[text_length - 1] = 0;
  242. }
  243. } else if(text_length < model->max_text_length) {
  244. if(text_length == 0 && char_is_lowercase(selected)) {
  245. selected = char_to_uppercase(selected);
  246. }
  247. model->text[text_length] = selected;
  248. model->text[text_length + 1] = 0;
  249. }
  250. });
  251. }
  252. static bool text_input_view_input_callback(InputEvent* event, void* context) {
  253. TextInput* text_input = context;
  254. furi_assert(text_input);
  255. bool consumed = false;
  256. if(event->state) {
  257. switch(event->input) {
  258. case InputUp:
  259. text_input_handle_up(text_input);
  260. consumed = true;
  261. break;
  262. case InputDown:
  263. text_input_handle_down(text_input);
  264. consumed = true;
  265. break;
  266. case InputLeft:
  267. text_input_handle_left(text_input);
  268. consumed = true;
  269. break;
  270. case InputRight:
  271. text_input_handle_right(text_input);
  272. consumed = true;
  273. break;
  274. case InputOk:
  275. text_input_handle_ok(text_input);
  276. consumed = true;
  277. break;
  278. default:
  279. break;
  280. }
  281. }
  282. return consumed;
  283. }
  284. TextInput* text_input_alloc() {
  285. TextInput* text_input = furi_alloc(sizeof(TextInput));
  286. text_input->view = view_alloc();
  287. view_set_context(text_input->view, text_input);
  288. view_allocate_model(text_input->view, ViewModelTypeLocking, sizeof(TextInputModel));
  289. view_set_draw_callback(text_input->view, text_input_view_draw_callback);
  290. view_set_input_callback(text_input->view, text_input_view_input_callback);
  291. with_view_model(
  292. text_input->view, (TextInputModel * model) {
  293. model->max_text_length = 0;
  294. model->header = "";
  295. model->selected_row = 0;
  296. model->selected_column = 0;
  297. });
  298. return text_input;
  299. }
  300. void text_input_free(TextInput* text_input) {
  301. furi_assert(text_input);
  302. view_free(text_input->view);
  303. free(text_input);
  304. }
  305. View* text_input_get_view(TextInput* text_input) {
  306. furi_assert(text_input);
  307. return text_input->view;
  308. }
  309. void text_input_set_result_callback(
  310. TextInput* text_input,
  311. TextInputCallback callback,
  312. void* callback_context,
  313. char* text,
  314. uint8_t max_text_length) {
  315. with_view_model(
  316. text_input->view, (TextInputModel * model) {
  317. model->callback = callback;
  318. model->callback_context = callback_context;
  319. model->text = text;
  320. model->max_text_length = max_text_length;
  321. });
  322. }
  323. void text_input_set_header_text(TextInput* text_input, const char* text) {
  324. with_view_model(
  325. text_input->view, (TextInputModel * model) { model->header = text; });
  326. }