couch/core/Input.cpp
2021-01-15 16:15:47 -06:00

44 lines
1.1 KiB
C++

#include "Input.h"
Input::Input() {}
Input *Input::instance = nullptr;
Input *Input::GetInstance() {
if (!instance) {
instance = new Input();
instance->lastx = 0.0;
instance->lasty = 0.0;
}
return instance;
}
void Input::Use(Window *window){
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetKeyCallback(window, (GLFWkeyfun)HandleKeys);
glfwSetCursorPosCallback(window, (GLFWcursorposfun)HandleMousePosition);
}
void Input::HandleKeys(Window *window, int keys, int code, int action, int mods) {
for (KeyHandler keyHandler : instance->keyHandlers) {
keyHandler(window, keys, code, action, mods);
}
}
void Input::HandleMousePosition(Window *window, double xpos, double ypos) {
double relx, rely;
if (instance->firstMousePositionUpdate) {
relx = 0.0;
rely = 0.0;
instance->firstMousePositionUpdate = false;
} else {
relx = xpos - instance->lastx;
rely = ypos - instance->lasty;
}
for (MousePositionHandler mousePositionHandler : instance->mousePositionHandlers) {
mousePositionHandler(window, xpos, ypos, relx, rely);
}
instance->lastx = xpos;
instance->lasty = ypos;
}