|
| 1 | +#include "int_input.h" |
| 2 | + |
| 3 | +#include <gui/elements.h> |
| 4 | +#include <furi.h> |
| 5 | +#include <assets_icons.h> |
| 6 | + |
| 7 | +/** IntInput type */ |
| 8 | +struct IntInput { |
| 9 | + View* view; |
| 10 | +}; |
| 11 | + |
| 12 | +typedef struct { |
| 13 | + const char text; |
| 14 | + const uint8_t x; |
| 15 | + const uint8_t y; |
| 16 | +} IntInputKey; |
| 17 | + |
| 18 | +typedef struct { |
| 19 | + const char* header; |
| 20 | + char* text_buffer; |
| 21 | + size_t text_buffer_size; |
| 22 | + bool clear_default_text; |
| 23 | + |
| 24 | + IntInputCallback callback; |
| 25 | + void* callback_context; |
| 26 | + |
| 27 | + int8_t selected_row; |
| 28 | + uint8_t selected_column; |
| 29 | +} IntInputModel; |
| 30 | + |
| 31 | +static const uint8_t keyboard_origin_x = 7; |
| 32 | +static const uint8_t keyboard_origin_y = 31; |
| 33 | +static const uint8_t keyboard_row_count = 2; |
| 34 | +static const uint8_t enter_symbol = '\r'; |
| 35 | +static const uint8_t backspace_symbol = '\b'; |
| 36 | + |
| 37 | +static const IntInputKey keyboard_keys_row_1[] = { |
| 38 | + {'0', 0, 12}, |
| 39 | + {'1', 11, 12}, |
| 40 | + {'2', 22, 12}, |
| 41 | + {'3', 33, 12}, |
| 42 | + {'4', 44, 12}, |
| 43 | + {backspace_symbol, 103, 4}, |
| 44 | +}; |
| 45 | + |
| 46 | +static const IntInputKey keyboard_keys_row_2[] = { |
| 47 | + {'5', 0, 26}, |
| 48 | + {'6', 11, 26}, |
| 49 | + {'7', 22, 26}, |
| 50 | + {'8', 33, 26}, |
| 51 | + {'9', 44, 26}, |
| 52 | + {enter_symbol, 95, 17}, |
| 53 | +}; |
| 54 | + |
| 55 | +/** Get row size |
| 56 | + * |
| 57 | + * @param row_index Index of row |
| 58 | + * |
| 59 | + * @return uint8_t Row size |
| 60 | + */ |
| 61 | +static uint8_t int_input_get_row_size(uint8_t row_index) { |
| 62 | + uint8_t row_size = 0; |
| 63 | + |
| 64 | + switch(row_index + 1) { |
| 65 | + case 1: |
| 66 | + row_size = COUNT_OF(keyboard_keys_row_1); |
| 67 | + break; |
| 68 | + case 2: |
| 69 | + row_size = COUNT_OF(keyboard_keys_row_2); |
| 70 | + break; |
| 71 | + default: |
| 72 | + furi_crash(); |
| 73 | + } |
| 74 | + |
| 75 | + return row_size; |
| 76 | +} |
| 77 | + |
| 78 | +/** Get row pointer |
| 79 | + * |
| 80 | + * @param row_index Index of row |
| 81 | + * |
| 82 | + * @return const IntInputKey* Row pointer |
| 83 | + */ |
| 84 | +static const IntInputKey* int_input_get_row(uint8_t row_index) { |
| 85 | + const IntInputKey* row = NULL; |
| 86 | + |
| 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 | + default: |
| 95 | + furi_crash(); |
| 96 | + } |
| 97 | + |
| 98 | + return row; |
| 99 | +} |
| 100 | + |
| 101 | +/** Draw input box (common view) |
| 102 | + * |
| 103 | + * @param canvas The canvas |
| 104 | + * @param model The model |
| 105 | + */ |
| 106 | +static void int_input_draw_input(Canvas* canvas, IntInputModel* model) { |
| 107 | + const uint8_t text_x = 8; |
| 108 | + const uint8_t text_y = 25; |
| 109 | + |
| 110 | + elements_slightly_rounded_frame(canvas, 6, 14, 116, 15); |
| 111 | + |
| 112 | + const char* text = model->text_buffer; |
| 113 | + canvas_draw_str(canvas, text_x, text_y, text); |
| 114 | +} |
| 115 | + |
| 116 | +static void int_input_backspace_cb(IntInputModel* model) { |
| 117 | + uint8_t text_length = model->clear_default_text ? 1 : strlen(model->text_buffer); |
| 118 | + if(text_length > 0) { |
| 119 | + model->text_buffer[text_length - 1] = 0; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/** Handle up button |
| 124 | + * |
| 125 | + * @param model The model |
| 126 | + */ |
| 127 | +static void int_input_handle_up(IntInputModel* model) { |
| 128 | + if(model->selected_row > 0) { |
| 129 | + model->selected_row--; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/** Handle down button |
| 134 | + * |
| 135 | + * @param model The model |
| 136 | + */ |
| 137 | +static void int_input_handle_down(IntInputModel* model) { |
| 138 | + if(model->selected_row < keyboard_row_count - 1) { |
| 139 | + model->selected_row += 1; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/** Handle left button |
| 144 | + * |
| 145 | + * @param model The model |
| 146 | + */ |
| 147 | +static void int_input_handle_left(IntInputModel* model) { |
| 148 | + if(model->selected_column > 0) { |
| 149 | + model->selected_column--; |
| 150 | + } else { |
| 151 | + model->selected_column = int_input_get_row_size(model->selected_row) - 1; |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +/** Handle right button |
| 156 | + * |
| 157 | + * @param model The model |
| 158 | + */ |
| 159 | +static void int_input_handle_right(IntInputModel* model) { |
| 160 | + if(model->selected_column < int_input_get_row_size(model->selected_row) - 1) { |
| 161 | + model->selected_column++; |
| 162 | + } else { |
| 163 | + model->selected_column = 0; |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +/** Handle OK button |
| 168 | + * |
| 169 | + * @param model The model |
| 170 | + */ |
| 171 | +static void int_input_handle_ok(IntInputModel* model) { |
| 172 | + char selected = int_input_get_row(model->selected_row)[model->selected_column].text; |
| 173 | + size_t text_length = strlen(model->text_buffer); |
| 174 | + if(selected == enter_symbol) { |
| 175 | + model->callback(model->callback_context); |
| 176 | + } else if(selected == backspace_symbol) { |
| 177 | + int_input_backspace_cb(model); |
| 178 | + } else { |
| 179 | + if(model->clear_default_text) { |
| 180 | + text_length = 0; |
| 181 | + } |
| 182 | + if(text_length < (model->text_buffer_size - 1)) { |
| 183 | + model->text_buffer[text_length] = selected; |
| 184 | + model->text_buffer[text_length + 1] = 0; |
| 185 | + } |
| 186 | + } |
| 187 | + model->clear_default_text = false; |
| 188 | +} |
| 189 | + |
| 190 | +/** Draw callback |
| 191 | + * |
| 192 | + * @param canvas The canvas |
| 193 | + * @param _model The model |
| 194 | + */ |
| 195 | +static void int_input_view_draw_callback(Canvas* canvas, void* _model) { |
| 196 | + IntInputModel* model = _model; |
| 197 | + uint8_t text_length = model->text_buffer ? strlen(model->text_buffer) : 0; |
| 198 | + UNUSED(text_length); |
| 199 | + |
| 200 | + canvas_clear(canvas); |
| 201 | + canvas_set_color(canvas, ColorBlack); |
| 202 | + |
| 203 | + int_input_draw_input(canvas, model); |
| 204 | + |
| 205 | + canvas_set_font(canvas, FontSecondary); |
| 206 | + canvas_draw_str(canvas, 2, 9, model->header); |
| 207 | + canvas_set_font(canvas, FontKeyboard); |
| 208 | + // Draw keyboard |
| 209 | + for(uint8_t row = 0; row < keyboard_row_count; row++) { |
| 210 | + const uint8_t column_count = int_input_get_row_size(row); |
| 211 | + const IntInputKey* keys = int_input_get_row(row); |
| 212 | + |
| 213 | + for(size_t column = 0; column < column_count; column++) { |
| 214 | + if(keys[column].text == enter_symbol) { |
| 215 | + canvas_set_color(canvas, ColorBlack); |
| 216 | + if(model->selected_row == row && model->selected_column == column) { |
| 217 | + canvas_draw_icon( |
| 218 | + canvas, |
| 219 | + keyboard_origin_x + keys[column].x, |
| 220 | + keyboard_origin_y + keys[column].y, |
| 221 | + &I_KeySaveSelected_24x11); |
| 222 | + } else { |
| 223 | + canvas_draw_icon( |
| 224 | + canvas, |
| 225 | + keyboard_origin_x + keys[column].x, |
| 226 | + keyboard_origin_y + keys[column].y, |
| 227 | + &I_KeySave_24x11); |
| 228 | + } |
| 229 | + } else if(keys[column].text == backspace_symbol) { |
| 230 | + canvas_set_color(canvas, ColorBlack); |
| 231 | + if(model->selected_row == row && model->selected_column == column) { |
| 232 | + canvas_draw_icon( |
| 233 | + canvas, |
| 234 | + keyboard_origin_x + keys[column].x, |
| 235 | + keyboard_origin_y + keys[column].y, |
| 236 | + &I_KeyBackspaceSelected_16x9); |
| 237 | + } else { |
| 238 | + canvas_draw_icon( |
| 239 | + canvas, |
| 240 | + keyboard_origin_x + keys[column].x, |
| 241 | + keyboard_origin_y + keys[column].y, |
| 242 | + &I_KeyBackspace_16x9); |
| 243 | + } |
| 244 | + } else { |
| 245 | + if(model->selected_row == row && model->selected_column == column) { |
| 246 | + canvas_set_color(canvas, ColorBlack); |
| 247 | + canvas_draw_box( |
| 248 | + canvas, |
| 249 | + keyboard_origin_x + keys[column].x - 3, |
| 250 | + keyboard_origin_y + keys[column].y - 10, |
| 251 | + 11, |
| 252 | + 13); |
| 253 | + canvas_set_color(canvas, ColorWhite); |
| 254 | + } else if(model->selected_row == -1 && row == 0 && model->selected_column == column) { |
| 255 | + canvas_set_color(canvas, ColorBlack); |
| 256 | + canvas_draw_frame( |
| 257 | + canvas, |
| 258 | + keyboard_origin_x + keys[column].x - 3, |
| 259 | + keyboard_origin_y + keys[column].y - 10, |
| 260 | + 11, |
| 261 | + 13); |
| 262 | + } else { |
| 263 | + canvas_set_color(canvas, ColorBlack); |
| 264 | + } |
| 265 | + |
| 266 | + canvas_draw_glyph( |
| 267 | + canvas, |
| 268 | + keyboard_origin_x + keys[column].x, |
| 269 | + keyboard_origin_y + keys[column].y, |
| 270 | + keys[column].text); |
| 271 | + } |
| 272 | + } |
| 273 | + } |
| 274 | +} |
| 275 | + |
| 276 | +/** Input callback |
| 277 | + * |
| 278 | + * @param event The event |
| 279 | + * @param context The context |
| 280 | + * |
| 281 | + * @return true |
| 282 | + * @return false |
| 283 | + */ |
| 284 | +static bool int_input_view_input_callback(InputEvent* event, void* context) { |
| 285 | + IntInput* int_input = context; |
| 286 | + furi_assert(int_input); |
| 287 | + |
| 288 | + bool consumed = false; |
| 289 | + |
| 290 | + // Fetch the model |
| 291 | + IntInputModel* model = view_get_model(int_input->view); |
| 292 | + |
| 293 | + if(event->type == InputTypeShort || event->type == InputTypeLong || |
| 294 | + event->type == InputTypeRepeat) { |
| 295 | + consumed = true; |
| 296 | + switch(event->key) { |
| 297 | + case InputKeyLeft: |
| 298 | + int_input_handle_left(model); |
| 299 | + break; |
| 300 | + case InputKeyRight: |
| 301 | + int_input_handle_right(model); |
| 302 | + break; |
| 303 | + case InputKeyUp: |
| 304 | + int_input_handle_up(model); |
| 305 | + break; |
| 306 | + case InputKeyDown: |
| 307 | + int_input_handle_down(model); |
| 308 | + break; |
| 309 | + case InputKeyOk: |
| 310 | + int_input_handle_ok(model); |
| 311 | + break; |
| 312 | + default: |
| 313 | + consumed = false; |
| 314 | + break; |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + // commit view |
| 319 | + view_commit_model(int_input->view, consumed); |
| 320 | + |
| 321 | + return consumed; |
| 322 | +} |
| 323 | + |
| 324 | +void int_input_reset(IntInput* int_input) { |
| 325 | + FURI_LOG_D("INT_INPUT", "Resetting Model"); |
| 326 | + furi_assert(int_input); |
| 327 | + with_view_model( |
| 328 | + int_input->view, |
| 329 | + IntInputModel * model, |
| 330 | + { |
| 331 | + model->header = ""; |
| 332 | + model->selected_row = 0; |
| 333 | + model->selected_column = 0; |
| 334 | + model->clear_default_text = false; |
| 335 | + model->text_buffer = ""; |
| 336 | + model->text_buffer_size = 0; |
| 337 | + model->callback = NULL; |
| 338 | + model->callback_context = NULL; |
| 339 | + }, |
| 340 | + true); |
| 341 | +} |
| 342 | + |
| 343 | +IntInput* int_input_alloc() { |
| 344 | + IntInput* int_input = malloc(sizeof(IntInput)); |
| 345 | + int_input->view = view_alloc(); |
| 346 | + view_set_context(int_input->view, int_input); |
| 347 | + view_allocate_model(int_input->view, ViewModelTypeLocking, sizeof(IntInputModel)); |
| 348 | + view_set_draw_callback(int_input->view, int_input_view_draw_callback); |
| 349 | + view_set_input_callback(int_input->view, int_input_view_input_callback); |
| 350 | + |
| 351 | + int_input_reset(int_input); |
| 352 | + |
| 353 | + return int_input; |
| 354 | +} |
| 355 | + |
| 356 | +void int_input_free(IntInput* int_input) { |
| 357 | + furi_assert(int_input); |
| 358 | + view_free(int_input->view); |
| 359 | + free(int_input); |
| 360 | +} |
| 361 | + |
| 362 | +View* int_input_get_view(IntInput* int_input) { |
| 363 | + furi_assert(int_input); |
| 364 | + return int_input->view; |
| 365 | +} |
| 366 | + |
| 367 | +void int_input_set_result_callback( |
| 368 | + IntInput* int_input, |
| 369 | + IntInputCallback callback, |
| 370 | + void* callback_context, |
| 371 | + char* text_buffer, |
| 372 | + size_t text_buffer_size, |
| 373 | + bool clear_default_text) { |
| 374 | + with_view_model( |
| 375 | + int_input->view, |
| 376 | + IntInputModel * model, |
| 377 | + { |
| 378 | + model->callback = callback; |
| 379 | + model->callback_context = callback_context; |
| 380 | + model->text_buffer = text_buffer; |
| 381 | + model->text_buffer_size = text_buffer_size; |
| 382 | + model->clear_default_text = clear_default_text; |
| 383 | + }, |
| 384 | + true); |
| 385 | +} |
| 386 | + |
| 387 | +void int_input_set_header_text(IntInput* int_input, const char* text) { |
| 388 | + with_view_model( |
| 389 | + int_input->view, IntInputModel * model, { model->header = text; }, true); |
| 390 | +} |
0 commit comments