Start textinput event;

This commit is contained in:
bjorn 2020-08-07 15:17:19 -06:00
parent 682633d98b
commit c24317d982
5 changed files with 35 additions and 0 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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));
}

View File

@ -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;