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::vec3 localToWorld(const glm::vec3& direction) const;
protected:
void mouseMoved(float deltaX, float deltaY) override;
void mouseMoved(const glm::vec2 &delta) override;
};

@ -3,6 +3,7 @@
#include <GLFW/glfw3.h>
#include <map>
#include <set>
#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) {};
};

@ -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<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){
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){
auto inputManager = static_cast<Input*>(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<MouseListener*>(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<MouseListener*>(listener))
mouseListener->mouseButtonReleased(button);
}
void Input::mouseMoved(double newX, double newY) {
float deltaX = static_cast<float>(newX - oldX);
float deltaY = static_cast<float>(newY - oldY);
for (auto listener : listeners){
if (auto mouseListener = dynamic_cast<MouseListener*>(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<MouseListener*>(listener))
mouseListener->mouseMoved(delta);
currentCursorPosition = newCursorPosition;
}
Listener::Listener() {

Loading…
Cancel
Save