text_input.c 11 KB

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