input fully floats

feature/softbody-runtime-control
Benjamin Kraft 3 months ago
parent 69f18910bc
commit b44339ea14
  1. 2
      include/camera.hpp
  2. 9
      include/input.hpp
  3. 6
      src/camera.cpp
  4. 29
      src/input.cpp

@ -24,5 +24,5 @@ public:
glm::mat4 projection() const; glm::mat4 projection() const;
glm::vec3 localToWorld(const glm::vec3& direction) const; glm::vec3 localToWorld(const glm::vec3& direction) const;
protected: protected:
void mouseMoved(float deltaX, float deltaY) override; void mouseMoved(const glm::vec2 &delta) override;
}; };

@ -3,6 +3,7 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <map> #include <map>
#include <set> #include <set>
#include "glm/vec2.hpp"
class Listener; class Listener;
class MouseListener; class MouseListener;
@ -24,8 +25,8 @@ private:
void mouseButtonPressed(int button); void mouseButtonPressed(int button);
void mouseButtonReleased(int button); void mouseButtonReleased(int button);
void mouseMoved(double newX, double newY); void mouseMoved(const glm::vec2& newCursorPosition);
double oldX {}, oldY {}; glm::vec2 currentCursorPosition;
}; };
class Listener { class Listener {
@ -40,5 +41,7 @@ class KeyListener : Listener {
class MouseListener : public Listener { class MouseListener : public Listener {
public: 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) {};
}; };

@ -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]) if (!Input::MouseButtonIsDown[GLFW_MOUSE_BUTTON_RIGHT])
return; return;
const float div = 300; const float div = 300;
phi += deltaX / div; phi += delta.x / div;
theta += -deltaY / div; theta += -delta.y / div;
float margin = 0.01; float margin = 0.01;
theta = glm::clamp(theta, -glm::half_pi<float>() + margin, glm::half_pi<float>() - margin); theta = glm::clamp(theta, -glm::half_pi<float>() + margin, glm::half_pi<float>() - margin);

@ -19,9 +19,12 @@ Input::Input(GLFWwindow *window) : window(window) {
glfwSetCursorPosCallback(window, [](GLFWwindow* window, double xpos, double ypos){ glfwSetCursorPosCallback(window, [](GLFWwindow* window, double xpos, double ypos){
auto inputManager = static_cast<Input*>(glfwGetWindowUserPointer(window)); auto inputManager = static_cast<Input*>(glfwGetWindowUserPointer(window));
inputManager->mouseMoved(xpos, ypos); glm::vec2 newPosition = {static_cast<float>(xpos), static_cast<float>(ypos)};
inputManager->mouseMoved(newPosition);
}); });
glfwGetCursorPos(window, &oldX, &oldY); double x = 0, y = 0;
glfwGetCursorPos(window, &x, &y);
currentCursorPosition = glm::vec2(static_cast<float>(x), static_cast<float>(y));
glfwSetMouseButtonCallback(window, [](GLFWwindow* window, int button, int action, int mods){ glfwSetMouseButtonCallback(window, [](GLFWwindow* window, int button, int action, int mods){
auto inputManager = static_cast<Input*>(glfwGetWindowUserPointer(window)); auto inputManager = static_cast<Input*>(glfwGetWindowUserPointer(window));
@ -44,24 +47,26 @@ void Input::mouseButtonPressed(int button) {
Input::MouseButtonIsDown[button] = true; Input::MouseButtonIsDown[button] = true;
if (button == GLFW_MOUSE_BUTTON_RIGHT) if (button == GLFW_MOUSE_BUTTON_RIGHT)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
for (auto listener : listeners)
if (auto mouseListener = dynamic_cast<MouseListener*>(listener))
mouseListener->mouseButtonPressed(button);
} }
void Input::mouseButtonReleased(int button) { void Input::mouseButtonReleased(int button) {
Input::MouseButtonIsDown[button] = false; Input::MouseButtonIsDown[button] = false;
if (button == GLFW_MOUSE_BUTTON_RIGHT) if (button == GLFW_MOUSE_BUTTON_RIGHT)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
for (auto listener : listeners)
if (auto mouseListener = dynamic_cast<MouseListener*>(listener))
mouseListener->mouseButtonReleased(button);
} }
void Input::mouseMoved(double newX, double newY) { void Input::mouseMoved(const glm::vec2& newCursorPosition) {
float deltaX = static_cast<float>(newX - oldX); glm::vec2 delta = newCursorPosition - currentCursorPosition;
float deltaY = static_cast<float>(newY - oldY); for (auto listener : listeners)
for (auto listener : listeners){ if (auto mouseListener = dynamic_cast<MouseListener*>(listener))
if (auto mouseListener = dynamic_cast<MouseListener*>(listener)){ mouseListener->mouseMoved(delta);
mouseListener->mouseMoved(deltaX, deltaY); currentCursorPosition = newCursorPosition;
}
}
oldX = newX;
oldY = newY;
} }
Listener::Listener() { Listener::Listener() {

Loading…
Cancel
Save