diff options
Diffstat (limited to 'teensy/teensy_ui.c')
| -rw-r--r-- | teensy/teensy_ui.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/teensy/teensy_ui.c b/teensy/teensy_ui.c index 86b2c94..d055802 100644 --- a/teensy/teensy_ui.c +++ b/teensy/teensy_ui.c @@ -6,6 +6,7 @@ #include <string.h> #include "teensy_list.h" +#include "teensy_platform.h" #define TEXT_HEIGHT (8) #define MAX_LAYOUTS (16) @@ -72,6 +73,8 @@ typedef struct { const float *layout; ty_Vec2i prev_mouse_pos; + + const tyui_Text_Input *focused_text_box; } Ctx; static @@ -512,6 +515,53 @@ void tyui_slider_ex( } } +void tyui_text_input(tyui_Text_Input *input) +{ + // ASSUMES ASCII + + ty_Recti rect = next_rect(TEXT_HEIGHT + control_padding() * 2); + + size_t len = strlen(input->chars); + uint32_t c; + + bool focused = input == uictx.focused_text_box; + bool hovered = is_hovered(rect); + + if (!focused && hovered && window()->lmb_pressed) { + uictx.focused_text_box = input; + focused = true; + } else if (focused && !hovered && window()->lmb_pressed) { + uictx.focused_text_box = NULL; + focused = false; + } + + if (focused) { + if (len < input->max_len - 1 && ty_platform_get_typed_char(&c)) { + input->chars[len] = c < CHAR_MAX ? (char)c : 'x'; + input->chars[len + 1] = '\0'; + } + + if (ty_button_pressed(TY_BTN_DB_BKSPACE)) + input->chars[len - 1] = '\0'; + } + + draw_frame(rect, uictx.style.bg_normal); + + ty_Recti text_rect = ty_recti_shrink(rect, control_padding()); + int text_width = ty_font_width(uictx.font, input->chars); + text_rect.w = fmin(text_rect.w, text_width); + text_cmd( + input->chars, + text_rect, + focused ? TYUI_ALIGN_RIGHT : TYUI_ALIGN_LEFT + ); + if (focused && (int)(ty_get_time() * 2) % 2 == 0) { + text_rect.x += fmin(text_width, text_rect.w - 1); + text_rect.w = 1; + rect_cmd(text_rect, uictx.style.fg_normal); + } +} + static void draw_window(Window *win) { |
