Move mouse code to defaults

This commit is contained in:
MrLetsplay 2022-11-02 13:37:35 +01:00
parent cc006e7469
commit aba3d387e6
5 changed files with 66 additions and 26 deletions

View File

@ -12,7 +12,8 @@ static KeyBinding
keyRight, keyRight,
keyUp, keyUp,
keyDown, keyDown,
keyOptions; keyOptions,
keyToggleCursorMode;
static ButtonElement *options; static ButtonElement *options;
@ -48,6 +49,35 @@ static void defaultKey(GLFWwindow *window, int key, int scancode, int action, in
if(key == Input::getKeyBinding(keyOptions).key && action == GLFW_PRESS) { if(key == Input::getKeyBinding(keyOptions).key && action == GLFW_PRESS) {
options->visible = !options->visible; options->visible = !options->visible;
} }
if(key == Input::getKeyBinding(keyToggleCursorMode).key && action == GLFW_PRESS) {
if(Input::getCursorMode() == GLFWCursorMode::CAPTURE) {
Input::setCursorMode(GLFWCursorMode::FREE);
}else {
Input::setCursorMode(GLFWCursorMode::CAPTURE);
}
}
}
static void defaultMouseCallback(GLFWwindow *window, double x, double y, void *data) {
static bool firstMouse = true;
static double lastX = 0, lastY = 0;
if(firstMouse) {
lastX = x;
lastY = y;
firstMouse = false;
}
float xoff = lastX - x;
float yoff = lastY - y;
lastX = x;
lastY = y;
xoff *= 0.1f;
yoff *= 0.1f;
kekData.activeCamera->rotateYaw(xoff);
kekData.activeCamera->rotatePitch(yoff);
} }
void init() { void init() {
@ -58,9 +88,11 @@ void init() {
keyUp = Input::createKeyBinding("Up", GLFW_KEY_SPACE); keyUp = Input::createKeyBinding("Up", GLFW_KEY_SPACE);
keyDown = Input::createKeyBinding("Down", GLFW_KEY_LEFT_CONTROL); keyDown = Input::createKeyBinding("Down", GLFW_KEY_LEFT_CONTROL);
keyOptions = Input::createKeyBinding("Options", GLFW_KEY_Q); keyOptions = Input::createKeyBinding("Options", GLFW_KEY_Q);
keyToggleCursorMode = Input::createKeyBinding("Toggle Cursor Mode", GLFW_KEY_TAB);
Input::addPeriodicCallback(PeriodicCallback(defaultInput, nullptr)); Input::addPeriodicCallback(PeriodicCallback(defaultInput, nullptr));
Input::addKeyListener(KeyCallback(defaultKey, nullptr)); Input::addKeyListener(KeyCallback(defaultKey, nullptr));
Input::addMouseListener(MouseCallback(defaultMouseCallback, nullptr));
options = new ButtonElement(px(0), px(100), px(100), px(50)); options = new ButtonElement(px(0), px(100), px(100), px(50));
UI::addElement(options); UI::addElement(options);

View File

@ -44,25 +44,6 @@ static void glDebugOutput(GLenum source, GLenum type, unsigned int id, GLenum se
} }
static void onCursorPosCallback(GLFWwindow *window, double x, double y) { static void onCursorPosCallback(GLFWwindow *window, double x, double y) {
static bool firstMouse = true;
static double lastX = 0, lastY = 0;
if(firstMouse) {
lastX = x;
lastY = y;
firstMouse = false;
}
float xoff = lastX - x;
float yoff = lastY - y;
lastX = x;
lastY = y;
xoff *= 0.1f;
yoff *= 0.1f;
kekData.activeCamera->rotateYaw(xoff);
kekData.activeCamera->rotatePitch(yoff);
for(auto cb : kekData.mouseCallbacks) { for(auto cb : kekData.mouseCallbacks) {
cb.second(window, x, y); cb.second(window, x, y);
} }
@ -159,8 +140,13 @@ int init() {
return KEK_ERROR; return KEK_ERROR;
} }
kekData.activeCamera = new Camera();
std::cout << "Poop cam is " << kekData.activeCamera << std::endl;
kekData.shader = new Shader("shader/mesh/vertex.glsl", "shader/mesh/fragment.glsl");
kekData.shader->initLighting();
glfwSetCursorPosCallback(kekData.window, onCursorPosCallback); glfwSetCursorPosCallback(kekData.window, onCursorPosCallback);
glfwSetInputMode(kekData.window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); Input::setCursorMode(GLFWCursorMode::CAPTURE);
glfwSetKeyCallback(kekData.window, onKeyCallback); glfwSetKeyCallback(kekData.window, onKeyCallback);
glfwSetMouseButtonCallback(kekData.window, onMouseButtonCallback); glfwSetMouseButtonCallback(kekData.window, onMouseButtonCallback);
@ -169,10 +155,6 @@ int init() {
stbi_set_flip_vertically_on_load(true); stbi_set_flip_vertically_on_load(true);
kekData.activeCamera = new Camera();
kekData.shader = new Shader("shader/mesh/vertex.glsl", "shader/mesh/fragment.glsl");
kekData.shader->initLighting();
FT_Error error = FT_Init_FreeType(&kekData.freetype); FT_Error error = FT_Init_FreeType(&kekData.freetype);
if(error) { if(error) {
ErrorDialog::showError("Failed to initialize FreeType: " + std::string(FT_Error_String(error))); ErrorDialog::showError("Failed to initialize FreeType: " + std::string(FT_Error_String(error)));

View File

@ -1,6 +1,7 @@
#include "input.h" #include "input.h"
#include <map> #include <map>
#include <iostream>
#include "internal.h" #include "internal.h"
@ -79,4 +80,14 @@ GLFWKeyState getKeyState(KeyBinding binding) {
return glfwGetKey(kekData.window, it->second.key); return glfwGetKey(kekData.window, it->second.key);
} }
void setCursorMode(GLFWCursorMode mode) {
//std::cout << "POOP is " << mode << std::endl;
glfwSetInputMode(kekData.window, GLFW_CURSOR, (int) mode);
kekData.uiCursorMode = mode;
}
GLFWCursorMode getCursorMode() {
return kekData.uiCursorMode;
}
} }

View File

@ -16,13 +16,24 @@ typedef unsigned int InputListener;
typedef int GLFWKey; typedef int GLFWKey;
typedef int GLFWKeyState; typedef int GLFWKeyState;
typedef unsigned int KeyBinding; typedef unsigned int KeyBinding;
typedef int GLFWMouseButton;
struct KeyBindingData { struct KeyBindingData {
std::string name; std::string name;
GLFWKey key; GLFWKey key;
}; };
enum class GLFWCursorMode {
CAPTURE = GLFW_CURSOR_DISABLED,
FREE = GLFW_CURSOR_NORMAL,
HIDDEN = GLFW_CURSOR_HIDDEN
};
enum class GLFWMouseButton {
LEFT = GLFW_MOUSE_BUTTON_LEFT,
RIGHT = GLFW_MOUSE_BUTTON_RIGHT,
MIDDLE = GLFW_MOUSE_BUTTON_MIDDLE
};
} }
namespace kek::Input { namespace kek::Input {
@ -44,4 +55,7 @@ void reassignKeyBinding(KeyBinding mapping, GLFWKey key);
KeyBindingData getKeyBinding(KeyBinding mapping); KeyBindingData getKeyBinding(KeyBinding mapping);
GLFWKeyState getKeyState(KeyBinding mapping); GLFWKeyState getKeyState(KeyBinding mapping);
void setCursorMode(GLFWCursorMode mode);
GLFWCursorMode getCursorMode();
} }

View File

@ -38,6 +38,7 @@ struct KekData {
std::vector<UIElement *> uiElements; std::vector<UIElement *> uiElements;
Font *uiDefaultFont; Font *uiDefaultFont;
Shader *uiRectangleShader; Shader *uiRectangleShader;
GLFWCursorMode uiCursorMode;
}; };
extern KekData kekData; extern KekData kekData;