Can update mouse mode from lua

This commit is contained in:
Dane Johnson 2021-08-02 14:34:49 -05:00
parent 44e8f784f5
commit 2fbbf39e9a
5 changed files with 33 additions and 6 deletions

View File

@ -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) {

View File

@ -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<KeyHandler> keyHandlers;
std::vector<MousePositionHandler> 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);

View File

@ -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;

View File

@ -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))

View File

@ -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"