aboutsummaryrefslogtreecommitdiff
path: root/teensy/teensy_ui.c
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-05-23 10:55:01 -0400
committeriamcheeseman <[email protected]>2026-05-23 10:55:01 -0400
commitc2ea095659dea04663b83fe5313173f209c9bb0b (patch)
tree9e9d79902a442bdf1bb2ef6b38507217574b673e /teensy/teensy_ui.c
parent5d36dcd67478cdb235f39d2aecb3270aba954001 (diff)
tyui text input boxes
Diffstat (limited to 'teensy/teensy_ui.c')
-rw-r--r--teensy/teensy_ui.c50
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)
{