|
|
@ -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() { |
|
|
|