108 lines
2.8 KiB
C++
108 lines
2.8 KiB
C++
//
|
|
// Created by sebastian on 15.02.26.
|
|
//
|
|
|
|
#include "Mouse.h"
|
|
#include "../../Window.h"
|
|
#include "../../Application.h"
|
|
#include <GLFW/glfw3.h>
|
|
|
|
Mouse::Mouse(Window &window) {
|
|
addMoveListener(window);
|
|
addScrollListener(window);
|
|
addClickListener(window);
|
|
}
|
|
|
|
bool Mouse::isButtonDown(MouseButton button) {
|
|
return buttonsDown.contains(MouseButtonToInt(button));
|
|
}
|
|
|
|
bool Mouse::isClickEvent(MouseButton button) {
|
|
return buttonsClickedThisFrame.contains(MouseButtonToInt(button));
|
|
}
|
|
|
|
bool Mouse::isReleaseEvent(MouseButton button) {
|
|
return buttonsReleasedThisFrame.contains(MouseButtonToInt(button));
|
|
}
|
|
|
|
float Mouse::getX() {
|
|
return x;
|
|
}
|
|
|
|
float Mouse::getY() {
|
|
return y;
|
|
}
|
|
|
|
float Mouse::getDX() {
|
|
return dx;
|
|
}
|
|
|
|
float Mouse::getDY() {
|
|
return dy;
|
|
}
|
|
|
|
float Mouse::getScroll() {
|
|
return scroll;
|
|
}
|
|
|
|
void Mouse::update() {
|
|
buttonsClickedThisFrame.clear();
|
|
buttonsReleasedThisFrame.clear();
|
|
updateDetlas();
|
|
scroll = 0.0f;
|
|
}
|
|
|
|
void Mouse::reportButtonClick(int button) {
|
|
buttonsDown.insert(button);
|
|
buttonsClickedThisFrame.insert(button);
|
|
}
|
|
|
|
void Mouse::reportButtonRelease(int button) {
|
|
buttonsDown.erase(button);
|
|
buttonsReleasedThisFrame.insert(button);
|
|
}
|
|
|
|
void Mouse::updateDetlas() {
|
|
dx = x - lastX;
|
|
dy = y - lastY;
|
|
lastX = x;
|
|
lastY = y;
|
|
}
|
|
|
|
void Mouse::addMoveListener(Window &window) {
|
|
glfwSetCursorPosCallback(static_cast<GLFWwindow *>(window.GetNativeWindow()), [](GLFWwindow *window, double xpos, double ypos) {
|
|
Application::getInstance().mouse->x = static_cast<float>(xpos / Application::getInstance().getWindow().GetWidth() );
|
|
Application::getInstance().mouse->y = static_cast<float>(ypos / Application::getInstance().getWindow().GetHeight());
|
|
});
|
|
}
|
|
|
|
void Mouse::addScrollListener(Window &window) {
|
|
glfwSetScrollCallback(static_cast<GLFWwindow *>(window.GetNativeWindow()), [](GLFWwindow *window, double xoff, double yoff) {
|
|
Application::getInstance().mouse->scroll = static_cast<float>(yoff);
|
|
});
|
|
}
|
|
|
|
void Mouse::addClickListener(Window &window) {
|
|
glfwSetMouseButtonCallback(static_cast<GLFWwindow *>(window.GetNativeWindow()), [](GLFWwindow *window, int button, int action, int mods) {
|
|
if (action == GLFW_PRESS) {
|
|
Application::getInstance().mouse->reportButtonClick(button);
|
|
} else if (action == GLFW_RELEASE) {
|
|
Application::getInstance().mouse->reportButtonRelease(button);
|
|
}
|
|
});
|
|
}
|
|
|
|
int Mouse::MouseButtonToInt(MouseButton button) {
|
|
int glfwButton = 0;
|
|
switch (button) {
|
|
case MouseButton::LEFT: glfwButton = GLFW_MOUSE_BUTTON_LEFT; break;
|
|
case MouseButton::RIGHT: glfwButton = GLFW_MOUSE_BUTTON_RIGHT; break;
|
|
case MouseButton::MIDDLE: glfwButton = GLFW_MOUSE_BUTTON_MIDDLE; break;
|
|
}
|
|
return glfwButton;
|
|
}
|
|
|
|
|
|
|
|
|