added more input events
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 7 Nov 2021 22:24:58 +0000 (16:24 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 7 Nov 2021 22:24:58 +0000 (16:24 -0600)
misc/onyx/heartbreak_input.onyx
misc/onyx/qhb.onyx
src/heartbreak_input.c
src/heartbreak_system.c
tests/simp.onyx

index 248dea3e3b5db44ab97bbaaad672d81fc13e63c7..e31afa072071f11595e58ebaaffb7c301850b9e6 100644 (file)
@@ -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" ---
index acd26a965ce798234f066b5cacf18347f8256a1a..d592b2fe998f7bf33e97841daccc73d37d3b4e75 100644 (file)
@@ -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 });
index 7c8593c7c4742ab9662a7954b6009ea20c6a9828..c52a6f908c5ec745fa23c6e49c262ddf875aecd8 100644 (file)
@@ -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
+};
index 2c467f11d716996e716b268ba7ffc266c2f8249b..0967775b3d388d473e8c2440a62830c5833268b6 100644 (file)
@@ -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);
 
index 6aca651039c75d71e46cb9cb8c9c46aa053627a7..c32bf464d43bd04b61682e3cbf617496e79195b3 100644 (file)
@@ -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;