Mouse control

This commit is contained in:
Dane Johnson
2021-01-13 22:50:01 -06:00
parent ca8a44a017
commit 35f43e9520
3 changed files with 32 additions and 2 deletions

View File

@@ -7,12 +7,16 @@ 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) {
@@ -26,3 +30,19 @@ void Input::HandleKeys(Window *window, int keys, int code, int action, int mods)
lua_call(L, 4, 0);
#endif // LUA_SCRIPTING
}
void Input::HandleMousePosition(Window *window, double xpos, double ypos) {
double relx = xpos - instance->lastx;
double rely = ypos - instance->lasty;
#ifdef LUA_SCRIPTING
lua_State *L = (lua_State*) glfwGetWindowUserPointer(window);
lua_getglobal(L, "onmousemotion");
lua_pushnumber(L, xpos);
lua_pushnumber(L, ypos);
lua_pushnumber(L, relx);
lua_pushnumber(L, rely);
lua_call(L, 4, 0);
#endif // LUA_SCRIPTING
instance->lastx = xpos;
instance->lasty = ypos;
}

View File

@@ -16,7 +16,9 @@ public:
static Input *GetInstance();
void Use(Window *window);
private:
double lastx, lasty;
Input();
static void HandleKeys(Window *window, int key, int code, int action, int mods);
static void HandleMousePosition(Window *window, double xpos, double ypos);
static Input *instance;
};