couch/core/Input.cpp

61 lines
1.5 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
2021-08-02 14:34:49 -05:00
void Input::Use(Window *window){
this->window = window;
glfwSetKeyCallback(window->glfwWindow, (GLFWkeyfun)HandleKeys);
glfwSetCursorPosCallback(window->glfwWindow, (GLFWcursorposfun)HandleMousePosition);
}
void Input::SetMouseMode(MouseMode mouseMode) {
this->mouseMode = mouseMode;
switch(mouseMode) {
case MouseMode::VISIBLE:
glfwSetInputMode(window->glfwWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
break;
case MouseMode::CAPTURED:
glfwSetInputMode(window->glfwWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
break;
case MouseMode::HIDDEN:
glfwSetInputMode(window->glfwWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
break;
}
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;
}