From c24317d982b3d31ff7098cfb4e98749ed70409d5 Mon Sep 17 00:00:00 2001 From: bjorn Date: Fri, 7 Aug 2020 15:17:19 -0600 Subject: [PATCH] Start textinput event; --- src/api/l_event.c | 5 +++++ src/core/os.h | 2 ++ src/core/os_glfw.h | 12 ++++++++++++ src/modules/event/event.c | 9 +++++++++ src/modules/event/event.h | 7 +++++++ 5 files changed, 35 insertions(+) diff --git a/src/api/l_event.c b/src/api/l_event.c index 23bb0521..486a08a2 100644 --- a/src/api/l_event.c +++ b/src/api/l_event.c @@ -14,6 +14,7 @@ StringEntry EventTypes[] = { [EVENT_RESIZE] = ENTRY("resize"), [EVENT_KEYPRESSED] = ENTRY("keypressed"), [EVENT_KEYRELEASED] = ENTRY("keyreleased"), + [EVENT_TEXTINPUT] = ENTRY("textinput"), #ifdef LOVR_ENABLE_THREAD [EVENT_THREAD_ERROR] = ENTRY("threaderror"), #endif @@ -140,6 +141,10 @@ static int nextEvent(lua_State* L) { lua_pushinteger(L, event.data.key.scancode); return 3; + case EVENT_TEXTINPUT: + lua_pushinteger(L, event.data.text.codepoint); + return 2; + #ifdef LOVR_ENABLE_THREAD case EVENT_THREAD_ERROR: luax_pushtype(L, Thread, event.data.thread.thread); diff --git a/src/core/os.h b/src/core/os.h index 21ef2a63..94e87dc1 100644 --- a/src/core/os.h +++ b/src/core/os.h @@ -55,6 +55,7 @@ typedef void (*windowFocusCallback)(bool focused); typedef void (*windowResizeCallback)(int width, int height); typedef void (*mouseButtonCallback)(MouseButton button, ButtonAction action); typedef void (*keyboardCallback)(ButtonAction action, KeyCode key, uint32_t scancode, bool repeat); +typedef void (*textCallback)(uint32_t codepoint); bool lovrPlatformInit(void); void lovrPlatformDestroy(void); @@ -81,6 +82,7 @@ void lovrPlatformOnWindowFocus(windowFocusCallback callback); void lovrPlatformOnWindowResize(windowResizeCallback callback); void lovrPlatformOnMouseButton(mouseButtonCallback callback); void lovrPlatformOnKeyboardEvent(keyboardCallback callback); +void lovrPlatformOnTextEvent(textCallback callback); void lovrPlatformGetMousePosition(double* x, double* y); void lovrPlatformSetMouseMode(MouseMode mode); bool lovrPlatformIsMouseDown(MouseButton button); diff --git a/src/core/os_glfw.h b/src/core/os_glfw.h index ee51360e..7f556dd6 100644 --- a/src/core/os_glfw.h +++ b/src/core/os_glfw.h @@ -17,6 +17,7 @@ static struct { windowResizeCallback onWindowResize; mouseButtonCallback onMouseButton; keyboardCallback onKeyboardEvent; + textCallback onTextEvent; } glfwState; static void onError(int code, const char* description) { @@ -74,6 +75,12 @@ static void onKeyboardEvent(GLFWwindow* window, int k, int scancode, int a, int } } +static void onTextEvent(GLFWwindow* window, unsigned int codepoint) { + if (glfwState.onTextEvent) { + glfwState.onTextEvent(codepoint); + } +} + static int convertMouseButton(MouseButton button) { switch (button) { case MOUSE_LEFT: return GLFW_MOUSE_BUTTON_LEFT; @@ -161,6 +168,7 @@ bool lovrPlatformCreateWindow(WindowFlags* flags) { glfwSetWindowSizeCallback(glfwState.window, onWindowResize); glfwSetMouseButtonCallback(glfwState.window, onMouseButton); glfwSetKeyCallback(glfwState.window, onKeyboardEvent); + glfwSetCharCallback(glfwState.window, onTextEvent); lovrPlatformSetSwapInterval(flags->vsync); return true; } @@ -223,6 +231,10 @@ void lovrPlatformOnKeyboardEvent(keyboardCallback callback) { glfwState.onKeyboardEvent = callback; } +void lovrPlatformOnTextEvent(textCallback callback) { + glfwState.onTextEvent = callback; +} + void lovrPlatformGetMousePosition(double* x, double* y) { if (glfwState.window) { glfwGetCursorPos(glfwState.window, x, y); diff --git a/src/modules/event/event.c b/src/modules/event/event.c index 4969ec08..413256d3 100644 --- a/src/modules/event/event.c +++ b/src/modules/event/event.c @@ -22,6 +22,13 @@ static void onKeyboardEvent(ButtonAction action, KeyCode key, uint32_t scancode, }); } +static void onTextEvent(uint32_t codepoint) { + lovrEventPush((Event) { + .type = EVENT_TEXTINPUT, + .data.text.codepoint = codepoint + }); +} + void lovrVariantDestroy(Variant* variant) { switch (variant->type) { case TYPE_STRING: free(variant->value.string); return; @@ -34,6 +41,7 @@ bool lovrEventInit() { if (state.initialized) return false; arr_init(&state.events); lovrPlatformOnKeyboardEvent(onKeyboardEvent); + lovrPlatformOnTextEvent(onTextEvent); return state.initialized = true; } @@ -53,6 +61,7 @@ void lovrEventDestroy() { } arr_free(&state.events); lovrPlatformOnKeyboardEvent(NULL); + lovrPlatformOnTextEvent(NULL); memset(&state, 0, sizeof(state)); } diff --git a/src/modules/event/event.h b/src/modules/event/event.h index 33a49ac9..2e5b9e2d 100644 --- a/src/modules/event/event.h +++ b/src/modules/event/event.h @@ -14,6 +14,7 @@ typedef enum { EVENT_RESIZE, EVENT_KEYPRESSED, EVENT_KEYRELEASED, + EVENT_TEXTINPUT, EVENT_CUSTOM, #ifdef LOVR_ENABLE_THREAD EVENT_THREAD_ERROR, @@ -63,6 +64,11 @@ typedef struct { bool repeat; } KeyEvent; +typedef struct { + char utf8[4]; + uint32_t codepoint; +} TextEvent; + typedef struct { struct Thread* thread; char* error; @@ -79,6 +85,7 @@ typedef union { BoolEvent boolean; ResizeEvent resize; KeyEvent key; + TextEvent text; ThreadEvent thread; CustomEvent custom; } EventData;