diff options
| author | iamcheeseman <[email protected]> | 2026-05-23 10:55:01 -0400 |
|---|---|---|
| committer | iamcheeseman <[email protected]> | 2026-05-23 10:55:01 -0400 |
| commit | c2ea095659dea04663b83fe5313173f209c9bb0b (patch) | |
| tree | 9e9d79902a442bdf1bb2ef6b38507217574b673e | |
| parent | 5d36dcd67478cdb235f39d2aecb3270aba954001 (diff) | |
tyui text input boxes
| -rw-r--r-- | Makefile.dep | 3 | ||||
| -rw-r--r-- | dc/dc.c | 39 | ||||
| -rw-r--r-- | teensy/platform/gl/gl.c | 19 | ||||
| -rw-r--r-- | teensy/teensy.h | 1 | ||||
| -rw-r--r-- | teensy/teensy_platform.h | 1 | ||||
| -rw-r--r-- | teensy/teensy_ui.c | 50 | ||||
| -rw-r--r-- | teensy/teensy_ui.h | 6 |
7 files changed, 105 insertions, 14 deletions
diff --git a/Makefile.dep b/Makefile.dep index b4778de..942d214 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -21,7 +21,8 @@ teensy_renderer.o: teensy/teensy_renderer.c teensy/teensy_renderer.h \ teensy/teensy.h teensy/teensy_list.h teensy/teensy_context.h teensy_ui.o: teensy/teensy_ui.c teensy/teensy_ui.h teensy/teensy_common.h \ teensy/teensy_log.h teensy/teensy_mem.h teensy/teensy.h \ - teensy/teensy_list.h + teensy/teensy_list.h teensy/teensy_platform.h teensy/teensy_context.h \ + teensy/teensy_renderer.h glad.o: teensy/platform/gl/glad/glad.c teensy/platform/gl/glad/glad.h gl.o: teensy/platform/gl/gl.c teensy/platform/gl/glad/glad.h \ teensy/platform/gl/GLFW/glfw3.h teensy/teensy.h teensy/teensy_common.h \ @@ -69,7 +69,7 @@ void tick(void) if (tyui_begin_window("Test Window", ty_recti(5, 5, 100, 120), &winid1)) { tyui_button("before"); - tyui_push_layout((float[]){-50, -1, 0}); + tyui_push_layout((float[]){-49, -1, 0}); tyui_push_layout((float[]){-1, 0}); tyui_text_ex(TYUI_ALIGN_CENTER, "Buttons"); tyui_button("test1"); @@ -92,20 +92,33 @@ void tick(void) } if (tyui_begin_window("2nd Window", ty_recti(110, 5, 100, 120), &winid2)) { - static float value = 100; - tyui_slider(0, 100, &value); - tyui_slider_ex(0, 100, &value, true, TYUI_ALIGN_LEFT); - tyui_slider_ex(0, 100, &value, true, TYUI_ALIGN_RIGHT); - tyui_blank_slider(0, 100, &value); + static bool opened = false; - tyui_push_layout((float[]){50, -1, 0}); - tyui_slider(0, 100, &value); - if (tyui_button("inc")) - value++; + if (tyui_button("sliders")) + opened = !opened; + + if (opened) { + static float value = 100; tyui_slider(0, 100, &value); - if (tyui_button("dec")) - value--; - tyui_pop_layout(); + tyui_slider_ex(0, 100, &value, true, TYUI_ALIGN_LEFT); + tyui_slider_ex(0, 100, &value, true, TYUI_ALIGN_RIGHT); + tyui_blank_slider(0, 100, &value); + + tyui_push_layout((float[]){50, -1, 0}); + tyui_slider(0, 100, &value); + if (tyui_button("inc")) + value++; + tyui_slider(0, 100, &value); + if (tyui_button("dec")) + value--; + tyui_pop_layout(); + } + + static char string[16]; + static tyui_Text_Input tinput; + tinput.chars = string; + tinput.max_len = 16; + tyui_text_input(&tinput); tyui_end_window(); } diff --git a/teensy/platform/gl/gl.c b/teensy/platform/gl/gl.c index c6b8194..9f9c8f5 100644 --- a/teensy/platform/gl/gl.c +++ b/teensy/platform/gl/gl.c @@ -43,11 +43,18 @@ typedef struct { GLuint program; GLint uniform_tex_loc; GLuint screen_handle; + uint32_t typed; } Gl_Platform; Gl_Platform p; static +void character_callback(GLFWwindow *window, unsigned int cp) +{ + p.typed = cp; +} + +static GLuint compile_shader(GLenum type, const char *src) { GLuint shader = glCreateShader(type); @@ -88,6 +95,8 @@ void ty_platform_init(ty_Ctx *ctx) glfwMakeContextCurrent(p.win); + glfwSetCharCallback(p.win, character_callback); + ty_log_info("initializing GLAD..."); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) ty_log_fatal(TY_PLATFORM_ERR, "could not init GLAD"); @@ -248,6 +257,7 @@ bool ty_platform_is_button_down(ty_Button btn) case TY_BTN_ACTION_4: res = glfwGetKey(p.win, GLFW_KEY_O); break; case TY_BTN_DB_CTRL: res = glfwGetKey(p.win, GLFW_KEY_LEFT_CONTROL); break; case TY_BTN_DB_SHIFT: res = glfwGetKey(p.win, GLFW_KEY_LEFT_SHIFT); break; + case TY_BTN_DB_BKSPACE: res = glfwGetKey(p.win, GLFW_KEY_BACKSPACE); break; case TY_BTN_DB_LMB: res = glfwGetMouseButton(p.win, GLFW_MOUSE_BUTTON_LEFT); break; @@ -261,3 +271,12 @@ bool ty_platform_is_button_down(ty_Button btn) return res == GLFW_PRESS; } + +bool ty_platform_get_typed_char(uint32_t *c) +{ + assert(c); + *c = p.typed; + if (p.typed == 0) + return false; + p.typed = 0; +} diff --git a/teensy/teensy.h b/teensy/teensy.h index 80f9a71..c909c52 100644 --- a/teensy/teensy.h +++ b/teensy/teensy.h @@ -58,6 +58,7 @@ enum { TY_BTN_DB_CTRL, TY_BTN_DB_SHIFT, + TY_BTN_DB_BKSPACE, TY_BTN_DB_LMB, TY_BTN_DB_RMB, TY_BTN_DB_MMB, diff --git a/teensy/teensy_platform.h b/teensy/teensy_platform.h index 5301385..8a1daa6 100644 --- a/teensy/teensy_platform.h +++ b/teensy/teensy_platform.h @@ -15,5 +15,6 @@ bool ty_platform_is_button_down(ty_Button btn); // Optional, no editor if not implemented ty_Vec2i ty_platform_get_mouse(void); +bool ty_platform_get_typed_char(uint32_t *c); #endif // TEENSY_PLATFORM_H_ 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) { diff --git a/teensy/teensy_ui.h b/teensy/teensy_ui.h index 1e97376..5ceda30 100644 --- a/teensy/teensy_ui.h +++ b/teensy/teensy_ui.h @@ -68,6 +68,11 @@ typedef struct { uint32_t flags; } tyui_Window_Conf; +typedef struct { + char *chars; + int max_len; +} tyui_Text_Input; + void tyui_init(const ty_Font *font); void tyui_deinit(void); @@ -96,6 +101,7 @@ void tyui_slider_ex( bool show_value, tyui_Align value_align ); +void tyui_text_input(tyui_Text_Input *input); // Draws all accumulated draw commands and initializes the next frame. void tyui_draw(void); |
