diff --git a/core/Input.cpp b/core/Input.cpp index e84b21b..b4c17f4 100644 --- a/core/Input.cpp +++ b/core/Input.cpp @@ -14,10 +14,26 @@ Input *Input::GetInstance() { } -void Input::Use(Window window){ - glfwSetInputMode(window.glfwWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED); - glfwSetKeyCallback(window.glfwWindow, (GLFWkeyfun)HandleKeys); - glfwSetCursorPosCallback(window.glfwWindow, (GLFWcursorposfun)HandleMousePosition); +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; + } } void Input::HandleKeys(GLFWwindow *_, int keys, int code, int action, int mods) { diff --git a/core/Input.h b/core/Input.h index a291068..a746b93 100644 --- a/core/Input.h +++ b/core/Input.h @@ -13,13 +13,20 @@ typedef void (*MousePositionHandler)(double xpos, double ypos, double xrel, doub class Input { public: + enum MouseMode { + VISIBLE, HIDDEN, CAPTURED + }; + static Input *GetInstance(); - void Use(Window window); + void Use(Window *window); + void SetMouseMode(MouseMode mouseMode); std::vector keyHandlers; std::vector mousePositionHandlers; private: bool firstMousePositionUpdate = true; double lastx, lasty; + MouseMode mouseMode; + Window *window; Input(); static void HandleKeys(GLFWwindow *_, int key, int code, int action, int mods); static void HandleMousePosition(GLFWwindow *_, double xpos, double ypos); diff --git a/core/couch.cpp b/core/couch.cpp index e01f81d..446d70e 100644 --- a/core/couch.cpp +++ b/core/couch.cpp @@ -69,7 +69,7 @@ int main(int argc, char *argv[]) { root = Node::GetRoot(); Input *input = Input::GetInstance(); - input->Use(window); + input->Use(&window); Camera defaultCamera; diff --git a/demo/exampleworld/main.lua b/demo/exampleworld/main.lua index ebd83fc..5e58c49 100644 --- a/demo/exampleworld/main.lua +++ b/demo/exampleworld/main.lua @@ -26,6 +26,8 @@ function init() local material local transform + couch.Input.GetInstance():SetMouseMode(couch.Input.CAPTURED) + freecam.init_camera() freecam.camera:Translate(couch.Vector3(0.0, 0.0, 10.0)) diff --git a/scripting/couch.i b/scripting/couch.i index bf29990..51b8951 100644 --- a/scripting/couch.i +++ b/scripting/couch.i @@ -18,6 +18,7 @@ #include "Skybox.h" #include "Rigidbody.h" #include "CollisionShape.h" +#include "Input.h" struct RaycastResult { bool hit; Vector3 position; @@ -73,3 +74,4 @@ public: %include "Skybox.h" %include "Rigidbody.h" %include "CollisionShape.h" +%include "Input.h"