diff options
Diffstat (limited to 'teensy')
| -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 |
5 files changed, 77 insertions, 0 deletions
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); |
