couch/core/Input.cpp

45 lines
1.2 KiB
C++
Raw Permalink Normal View History

2021-01-13 18:51:58 -06:00
#include "Input.h"
Input::Input() {}
Input *Input::instance = nullptr;
Input *Input::GetInstance() {
if (!instance) {
instance = new Input();
2021-01-13 22:50:01 -06:00
instance->lastx = 0.0;
instance->lasty = 0.0;
2021-01-13 18:51:58 -06:00
}
return instance;
}
2021-03-08 13:50:50 -06:00
void Input::Use(Window window){
glfwSetInputMode(window.glfwWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetKeyCallback(window.glfwWindow, (GLFWkeyfun)HandleKeys);
glfwSetCursorPosCallback(window.glfwWindow, (GLFWcursorposfun)HandleMousePosition);
2021-01-13 18:51:58 -06:00
}
2021-03-08 13:50:50 -06:00
void Input::HandleKeys(GLFWwindow *_, int keys, int code, int action, int mods) {
for (KeyHandler keyHandler : instance->keyHandlers) {
2021-03-08 13:50:50 -06:00
keyHandler(keys, code, action, mods);
}
2021-01-13 18:51:58 -06:00
}
2021-01-13 22:50:01 -06:00
2021-03-08 13:50:50 -06:00
void Input::HandleMousePosition(GLFWwindow *_, double xpos, double ypos) {
2021-01-15 16:15:47 -06:00
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) {
2021-03-08 13:50:50 -06:00
mousePositionHandler(xpos, ypos, relx, rely);
}
2021-01-13 22:50:01 -06:00
instance->lastx = xpos;
instance->lasty = ypos;
}