From b44339ea14c0d72ae0f622c2d6a9992a36de4335 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Sat, 5 Oct 2024 20:27:51 +0200 Subject: [PATCH] input fully floats --- include/camera.hpp | 2 +- include/input.hpp | 9 ++++++--- src/camera.cpp | 6 +++--- src/input.cpp | 29 +++++++++++++++++------------ 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/include/camera.hpp b/include/camera.hpp index 35d3a90..f022160 100644 --- a/include/camera.hpp +++ b/include/camera.hpp @@ -24,5 +24,5 @@ public: glm::mat4 projection() const; glm::vec3 localToWorld(const glm::vec3& direction) const; protected: - void mouseMoved(float deltaX, float deltaY) override; + void mouseMoved(const glm::vec2 &delta) override; }; \ No newline at end of file diff --git a/include/input.hpp b/include/input.hpp index 55d937a..b2e1d75 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -3,6 +3,7 @@ #include #include #include +#include "glm/vec2.hpp" class Listener; class MouseListener; @@ -24,8 +25,8 @@ private: void mouseButtonPressed(int button); void mouseButtonReleased(int button); - void mouseMoved(double newX, double newY); - double oldX {}, oldY {}; + void mouseMoved(const glm::vec2& newCursorPosition); + glm::vec2 currentCursorPosition; }; class Listener { @@ -40,5 +41,7 @@ class KeyListener : Listener { class MouseListener : public Listener { public: - virtual void mouseMoved(float deltaX, float deltaY) {}; + virtual void mouseMoved(const glm::vec2& delta) {}; + virtual void mouseButtonPressed(int button) {}; + virtual void mouseButtonReleased(int button) {}; }; \ No newline at end of file diff --git a/src/camera.cpp b/src/camera.cpp index bee1662..534862d 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -46,14 +46,14 @@ Camera::Camera(VkExtent2D &extent) : extent(extent) { } -void Camera::mouseMoved(float deltaX, float deltaY) { +void Camera::mouseMoved(const glm::vec2 &delta) { if (!Input::MouseButtonIsDown[GLFW_MOUSE_BUTTON_RIGHT]) return; const float div = 300; - phi += deltaX / div; - theta += -deltaY / div; + phi += delta.x / div; + theta += -delta.y / div; float margin = 0.01; theta = glm::clamp(theta, -glm::half_pi() + margin, glm::half_pi() - margin); diff --git a/src/input.cpp b/src/input.cpp index b99bd9e..6d05215 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -19,9 +19,12 @@ Input::Input(GLFWwindow *window) : window(window) { glfwSetCursorPosCallback(window, [](GLFWwindow* window, double xpos, double ypos){ auto inputManager = static_cast(glfwGetWindowUserPointer(window)); - inputManager->mouseMoved(xpos, ypos); + glm::vec2 newPosition = {static_cast(xpos), static_cast(ypos)}; + inputManager->mouseMoved(newPosition); }); - glfwGetCursorPos(window, &oldX, &oldY); + double x = 0, y = 0; + glfwGetCursorPos(window, &x, &y); + currentCursorPosition = glm::vec2(static_cast(x), static_cast(y)); glfwSetMouseButtonCallback(window, [](GLFWwindow* window, int button, int action, int mods){ auto inputManager = static_cast(glfwGetWindowUserPointer(window)); @@ -44,24 +47,26 @@ void Input::mouseButtonPressed(int button) { Input::MouseButtonIsDown[button] = true; if (button == GLFW_MOUSE_BUTTON_RIGHT) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + for (auto listener : listeners) + if (auto mouseListener = dynamic_cast(listener)) + mouseListener->mouseButtonPressed(button); } void Input::mouseButtonReleased(int button) { Input::MouseButtonIsDown[button] = false; if (button == GLFW_MOUSE_BUTTON_RIGHT) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + for (auto listener : listeners) + if (auto mouseListener = dynamic_cast(listener)) + mouseListener->mouseButtonReleased(button); } -void Input::mouseMoved(double newX, double newY) { - float deltaX = static_cast(newX - oldX); - float deltaY = static_cast(newY - oldY); - for (auto listener : listeners){ - if (auto mouseListener = dynamic_cast(listener)){ - mouseListener->mouseMoved(deltaX, deltaY); - } - } - oldX = newX; - oldY = newY; +void Input::mouseMoved(const glm::vec2& newCursorPosition) { + glm::vec2 delta = newCursorPosition - currentCursorPosition; + for (auto listener : listeners) + if (auto mouseListener = dynamic_cast(listener)) + mouseListener->mouseMoved(delta); + currentCursorPosition = newCursorPosition; } Listener::Listener() {