From: Brendan Hansen Date: Sun, 7 Nov 2021 22:24:58 +0000 (-0600) Subject: added more input events X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=af4287165a6c3cae02948d6130d3695035fe4216;p=heartbreak.git added more input events --- diff --git a/misc/onyx/heartbreak_input.onyx b/misc/onyx/heartbreak_input.onyx index 248dea3..e31afa0 100644 --- a/misc/onyx/heartbreak_input.onyx +++ b/misc/onyx/heartbreak_input.onyx @@ -144,8 +144,8 @@ ButtonConstant :: enum { keyIsDown :: (key: KeyConstant) -> bool #foreign "heartbreak" "input_key_is_down" --- -mouseGetX :: () -> i32 #foreign "heartbreak" "input_mouse_get_x" --- -mouseGetY :: () -> i32 #foreign "heartbreak" "input_mouse_get_y" --- +mouseGetX :: () -> f32 #foreign "heartbreak" "input_mouse_get_x" --- +mouseGetY :: () -> f32 #foreign "heartbreak" "input_mouse_get_y" --- mouseGetPos :: () => mouseGetX(), mouseGetY(); mouseIsDown :: (button: ButtonConstant) -> bool #foreign "heartbreak" "input_mouse_is_down" --- diff --git a/misc/onyx/qhb.onyx b/misc/onyx/qhb.onyx index acd26a9..d592b2f 100644 --- a/misc/onyx/qhb.onyx +++ b/misc/onyx/qhb.onyx @@ -23,8 +23,14 @@ package main } // This is one place where top-level macros would be nice. -#if #defined(symbol_source.keydown) { #export "__heartbreak_keydown" symbol_source.keydown } -#if #defined(symbol_source.keyup) { #export "__heartbreak_keyup" symbol_source.keyup } -#if #defined(symbol_source.resize) { #export "__heartbreak_resize" symbol_source.resize } +#if #defined(symbol_source.keydown) { #export "__heartbreak_keydown" symbol_source.keydown } +#if #defined(symbol_source.keyup) { #export "__heartbreak_keyup" symbol_source.keyup } +#if #defined(symbol_source.mousedown) { #export "__heartbreak_mousedown" symbol_source.mousedown } +#if #defined(symbol_source.mouseup) { #export "__heartbreak_mouseup" symbol_source.mouseup } +#if #defined(symbol_source.mousemove) { #export "__heartbreak_mousemove" symbol_source.mousemove } +#if #defined(symbol_source.mouseenter) { #export "__heartbreak_mouseenter" symbol_source.mouseenter } +#if #defined(symbol_source.mouseleave) { #export "__heartbreak_mouseleave" symbol_source.mouseleave } +#if #defined(symbol_source.wheelmoved) { #export "__heartbreak_wheelmoved" symbol_source.wheelmoved } +#if #defined(symbol_source.resize) { #export "__heartbreak_resize" symbol_source.resize } main :: (_) => hb.run(.{ symbol_source.load, symbol_source.update, symbol_source.draw }); diff --git a/src/heartbreak_input.c b/src/heartbreak_input.c index 7c8593c..c52a6f9 100644 --- a/src/heartbreak_input.c +++ b/src/heartbreak_input.c @@ -8,17 +8,17 @@ HEARTBREAK_DEF(key_is_down, (WASM_I32), (WASM_I32)) { return NULL; } -HEARTBREAK_DEF(mouse_get_x, (), (WASM_I32)) { +HEARTBREAK_DEF(mouse_get_x, (), (WASM_F32)) { f64 x_pos; glfwGetCursorPos(glfw_window, &x_pos, NULL); - results->data[0] = WASM_I32_VAL((i32) floor(x_pos)); + results->data[0] = WASM_F32_VAL(x_pos); return NULL; } -HEARTBREAK_DEF(mouse_get_y, (), (WASM_I32)) { +HEARTBREAK_DEF(mouse_get_y, (), (WASM_F32)) { f64 y_pos; glfwGetCursorPos(glfw_window, NULL, &y_pos); - results->data[0] = WASM_I32_VAL((i32) floor(y_pos)); + results->data[0] = WASM_F32_VAL(y_pos); return NULL; } @@ -35,4 +35,4 @@ HEARTBREAK_MODULE { HEARTBREAK_FUNC(mouse_is_down) { NULL } -}; \ No newline at end of file +}; diff --git a/src/heartbreak_system.c b/src/heartbreak_system.c index 2c467f1..0967775 100644 --- a/src/heartbreak_system.c +++ b/src/heartbreak_system.c @@ -4,12 +4,15 @@ #define HEARTBREAK_MODULE_NAME system #define HEARTBREAK_EVENTS \ - HB_EVENT(keydown, (WASM_I32, WASM_I32)) \ - HB_EVENT(keyup, (WASM_I32)) \ - HB_EVENT(mousedown, ()) \ - HB_EVENT(mouseup, ()) \ - HB_EVENT(mousemove, ()) \ - HB_EVENT(resize, (WASM_I32, WASM_I32)) \ + HB_EVENT(keydown, (WASM_I32, WASM_I32)) \ + HB_EVENT(keyup, (WASM_I32)) \ + HB_EVENT(mousedown, (WASM_I32, WASM_I32)) \ + HB_EVENT(mouseup, (WASM_I32)) \ + HB_EVENT(mousemove, (WASM_F32, WASM_F32)) \ + HB_EVENT(mouseenter, ()) \ + HB_EVENT(mouseleave, ()) \ + HB_EVENT(wheelmoved, (WASM_F32, WASM_F32)) \ + HB_EVENT(resize, (WASM_I32, WASM_I32)) \ #define HB_EVENT(name, pt) static wasm_func_t *__heartbreak_event_##name; HEARTBREAK_EVENTS @@ -62,13 +65,36 @@ static void __glfw_window_size_callback(GLFWwindow *w, i32 new_width, i32 new_he HEARTBREAK_EVENT_CALL(resize, new_width, new_height); } -static void __glfw_window_key_callback(GLFWwindow *w, i32 key, int scancode, int action, int mods) { +static void __glfw_key_callback(GLFWwindow *w, i32 key, int scancode, int action, int mods) { switch (action) { case GLFW_PRESS: HEARTBREAK_EVENT_CALL(keydown, key, mods); break; case GLFW_RELEASE: HEARTBREAK_EVENT_CALL(keyup, key); break; } } +static void __glfw_mouse_button_callback(GLFWwindow *w, i32 button, i32 action, i32 mods) { + switch (action) { + case GLFW_PRESS: HEARTBREAK_EVENT_CALL(mousedown, button, mods); break; + case GLFW_RELEASE: HEARTBREAK_EVENT_CALL(mouseup, button); break; + } +} + +static void __glfw_mouse_position_callback(GLFWwindow *w, f64 xpos, f64 ypos) { + HEARTBREAK_EVENT_CALL(mousemove, (f32) xpos, (f32) ypos); +} + +static void __glfw_mouse_enter_callback(GLFWwindow *w, i32 entered) { + if (entered == GLFW_TRUE) { + HEARTBREAK_EVENT_CALL(mouseenter, 0); + } else { + HEARTBREAK_EVENT_CALL(mouseleave, 0); + } +} + +static void __glfw_scroll_callback(GLFWwindow *w, f64 xoff, f64 yoff) { + HEARTBREAK_EVENT_CALL(wheelmoved, xoff, yoff); +} + typedef struct HeartbreakEventLink { const char *name; wasm_func_t **func; @@ -92,7 +118,11 @@ HEARTBREAK_DEF(init, (), (WASM_I32)) { glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwMakeContextCurrent(glfw_window); glfwSetWindowSizeCallback(glfw_window, __glfw_window_size_callback); - glfwSetKeyCallback(glfw_window, __glfw_window_key_callback); + glfwSetKeyCallback(glfw_window, __glfw_key_callback); + glfwSetMouseButtonCallback(glfw_window, __glfw_mouse_button_callback); + glfwSetCursorPosCallback(glfw_window, __glfw_mouse_position_callback); + glfwSetCursorEnterCallback(glfw_window, __glfw_mouse_enter_callback); + glfwSetScrollCallback(glfw_window, __glfw_scroll_callback); glfwSwapInterval(1); diff --git a/tests/simp.onyx b/tests/simp.onyx index 6aca651..c32bf46 100644 --- a/tests/simp.onyx +++ b/tests/simp.onyx @@ -41,6 +41,18 @@ resize :: (nw, nh: i32) { printf("The window is now {} by {}.\n", nw, nh); } +mousedown :: (button, _: i32) { + mx, my := hb.input.mouseGetPos(); + printf("The mouse was clicked at {.2}, {.2} with button {}.\n", mx, my, button); +} + +mouseenter :: () { println("Mouse entered!"); } +mouseleave :: () { println("Mouse left!"); } + +wheelmoved :: (dx, dy: f32) { + printf("The scroll wheel was scrolled by {} and {}.\n", dx, dy); +} + t: f32 = 0; update :: (dt: f32) { t += 0.016;