From 05cb8f7905719d046bdc5dc856e1cf3221dbc8c4 Mon Sep 17 00:00:00 2001
From: Sebastian Gaida <gaida@ca-digit.com>
Date: Thu, 20 May 2021 15:15:39 +0200
Subject: [PATCH] [#14] add basic events to window

adds basic events like mousemovement, resize or keyEvents
This is only the basic code to fetch these events not to handle the actual event
---
 include/vkcv/Window.hpp | 39 ++++++++++++++++++++++++++++++++++++++
 src/vkcv/Window.cpp     | 42 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 79 insertions(+), 2 deletions(-)

diff --git a/include/vkcv/Window.hpp b/include/vkcv/Window.hpp
index 87c98a92..bb8081c1 100644
--- a/include/vkcv/Window.hpp
+++ b/include/vkcv/Window.hpp
@@ -7,6 +7,7 @@
 
 #define NOMINMAX
 #include <algorithm>
+#include "Event.hpp"
 
 struct GLFWwindow;
 
@@ -23,6 +24,32 @@ namespace vkcv {
          */
         explicit Window(GLFWwindow *window);
 
+        /**
+         * mouse callback for moving the mouse on the screen
+         * @param[in] window The window that received the event.
+         * @param[in] xpos The new cursor x-coordinate, relative to the left edge of the content area.
+         * @param[in] ypos The new cursor y-coordinate, relative to the top edge of the content area.
+         */
+        static void onMouseMoveEvent(GLFWwindow *window, double x, double y);
+
+        /**
+         * resize callback for the resize option of the window
+         * @param[in] window The window that was resized.
+         * @param[in] width The new width, in screen coordinates, of the window.
+         * @param[in] height The new height, in screen coordinates, of the window.
+         */
+        static void onResize(GLFWwindow *callbackWindow, int width, int height);
+
+        /**
+         * key callback for the pressed key
+         * @param[in] window The window that received the event.
+         * @param[in] key The [keyboard key](@ref keys) that was pressed or released.
+         * @param[in] scancode The system-specific scancode of the key.
+         * @param[in] action `GLFW_PRESS`, `GLFW_RELEASE` or `GLFW_REPEAT`.
+         * @param[in] mods Bit field describing which [modifier keys](@ref mods) were held down.
+         */
+        static void onKeyEvent(GLFWwindow *callbackWindow, int key, int scancode, int action, int mods);
+
     public:
         /**
          * creates a GLFWwindow with the parameters in the function
@@ -41,11 +68,23 @@ namespace vkcv {
         [[nodiscard]]
         bool isWindowOpen() const;
 
+        /**
+         * binds windowEvents to lambda events
+         */
+        void initEvents();
+
         /**
          * polls all events on the GLFWwindow
          */
         static void pollEvents();
 
+        /**
+         * basic events of the window
+         */
+        event< double, double > e_mouseMove;
+        event< int, int > e_resize;
+        event< int, int, int, int > e_key;
+
         /**
          * returns the current window
          * @return window handle
diff --git a/src/vkcv/Window.cpp b/src/vkcv/Window.cpp
index 376f363a..87f302c1 100644
--- a/src/vkcv/Window.cpp
+++ b/src/vkcv/Window.cpp
@@ -42,14 +42,52 @@ namespace vkcv {
         return Window(window);
     }
 
-    bool Window::isWindowOpen() const {
-        return !glfwWindowShouldClose(m_window);
+    void Window::initEvents() {
+        glfwSetWindowUserPointer(m_window, this);
+
+        // combine Callbacks with Events
+        glfwSetCursorPosCallback(m_window, Window::onMouseMoveEvent);
+
+        glfwSetWindowSizeCallback(m_window, Window::onResize);
+
+        glfwSetKeyCallback(m_window, Window::onKeyEvent);
     }
 
     void Window::pollEvents() {
         glfwPollEvents();
     }
 
+    void Window::onMouseMoveEvent(GLFWwindow *callbackWindow, double x, double y) {
+
+        auto window = static_cast<Window *>(glfwGetWindowUserPointer(callbackWindow));
+
+        if (window != nullptr) {
+            window->e_mouseMove(x, y);
+        }
+    }
+
+    void Window::onResize(GLFWwindow *callbackWindow, int width, int height) {
+
+        auto window = static_cast<Window *>(glfwGetWindowUserPointer(callbackWindow));
+
+        if (window != nullptr) {
+            window->e_resize(width, height);
+        }
+    }
+
+    void Window::onKeyEvent(GLFWwindow *callbackWindow, int key, int scancode, int action, int mods) {
+
+        auto window = static_cast<Window *>(glfwGetWindowUserPointer(callbackWindow));
+
+        if (window != nullptr) {
+            window->e_key(key, scancode, action, mods);
+        }
+    }
+
+    bool Window::isWindowOpen() const {
+        return !glfwWindowShouldClose(m_window);
+    }
+
     int Window::getWidth() const {
         int width;
         glfwGetWindowSize(m_window, &width, nullptr);
-- 
GitLab