Restructure input code
This commit is contained in:
parent
b706100ddc
commit
6725f5eb20
@ -26,6 +26,7 @@
|
||||
|
||||
#include "internal.h"
|
||||
#include "internal/ui.h"
|
||||
#include "internal/input.h"
|
||||
|
||||
kek::KekData kek::kekData;
|
||||
|
||||
@ -45,49 +46,6 @@ static void glDebugOutput(GLenum source, GLenum type, unsigned int id, GLenum se
|
||||
std::cout << "OpenGL Debug (" << id << "): " << message << std::endl;
|
||||
}
|
||||
|
||||
static void onCursorPosCallback(GLFWwindow *window, double x, double y) {
|
||||
for(auto cb : kekData.mouseCallbacks) {
|
||||
cb.second(window, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
static void onKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
|
||||
if(Input::isKeyboardCaptured()) {
|
||||
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
|
||||
Input::uncaptureKeyboardInput(kekData.activeKeyboardCapture.id);
|
||||
return;
|
||||
}
|
||||
|
||||
if(key == GLFW_KEY_BACKSPACE && (action == GLFW_PRESS || action == GLFW_REPEAT)) {
|
||||
kekData.activeKeyboardCapture.charCallback(window, KEK_INPUT_DELETE);
|
||||
}
|
||||
|
||||
kekData.activeKeyboardCapture.keyCallback(window, key, scancode, action, mods);
|
||||
return;
|
||||
}
|
||||
|
||||
for(auto cb : kekData.keyCallbacks) {
|
||||
cb.second(window, key, scancode, action, mods);
|
||||
}
|
||||
}
|
||||
|
||||
static void onKeyCharCallback(GLFWwindow *window, unsigned int codepoint) {
|
||||
if(Input::isKeyboardCaptured()) {
|
||||
kekData.activeKeyboardCapture.charCallback(window, codepoint);
|
||||
return;
|
||||
}
|
||||
|
||||
for(auto cb : kekData.keyCharCallbacks) {
|
||||
cb.second(window, codepoint);
|
||||
}
|
||||
}
|
||||
|
||||
void onMouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
|
||||
for(auto cb : kekData.mouseButtonCallbacks) {
|
||||
cb.second(window, button, action, mods);
|
||||
}
|
||||
}
|
||||
|
||||
int init() {
|
||||
// Init GLFW
|
||||
if (glfwInit() != GL_TRUE) {
|
||||
@ -183,11 +141,11 @@ int init() {
|
||||
kekData.shader = new Shader("shader/mesh/vertex.glsl", "shader/mesh/fragment.glsl");
|
||||
kekData.shader->initLighting();
|
||||
|
||||
glfwSetCursorPosCallback(kekData.window, onCursorPosCallback);
|
||||
glfwSetCursorPosCallback(kekData.window, Input::onCursorPosCallback);
|
||||
Input::setCursorMode(GLFWCursorMode::CAPTURE);
|
||||
glfwSetKeyCallback(kekData.window, onKeyCallback);
|
||||
glfwSetCharCallback(kekData.window, onKeyCharCallback);
|
||||
glfwSetMouseButtonCallback(kekData.window, onMouseButtonCallback);
|
||||
glfwSetKeyCallback(kekData.window, Input::onKeyCallback);
|
||||
glfwSetCharCallback(kekData.window, Input::onKeyCharCallback);
|
||||
glfwSetMouseButtonCallback(kekData.window, Input::onMouseButtonCallback);
|
||||
|
||||
glClearColor(0.1f, 0.3f, 0.1f, 0.0f);
|
||||
glfwSwapInterval(0);
|
||||
|
@ -4,11 +4,63 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "internal.h"
|
||||
#include "internal/input.h"
|
||||
|
||||
namespace kek::Input {
|
||||
|
||||
static InputListener nextID = 0;
|
||||
|
||||
void init() {
|
||||
kekData.input = new InputData();
|
||||
}
|
||||
|
||||
void destroy() {
|
||||
delete kekData.input;
|
||||
}
|
||||
|
||||
void onCursorPosCallback(GLFWwindow *window, double x, double y) {
|
||||
for(auto cb : kekData.mouseCallbacks) {
|
||||
cb.second(window, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
void onKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
|
||||
if(Input::isKeyboardCaptured()) {
|
||||
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
|
||||
Input::uncaptureKeyboardInput(kekData.input->activeKeyboardCapture.id);
|
||||
return;
|
||||
}
|
||||
|
||||
if(key == GLFW_KEY_BACKSPACE && (action == GLFW_PRESS || action == GLFW_REPEAT)) {
|
||||
kekData.input->activeKeyboardCapture.charCallback(window, KEK_INPUT_DELETE);
|
||||
}
|
||||
|
||||
kekData.input->activeKeyboardCapture.keyCallback(window, key, scancode, action, mods);
|
||||
return;
|
||||
}
|
||||
|
||||
for(auto cb : kekData.keyCallbacks) {
|
||||
cb.second(window, key, scancode, action, mods);
|
||||
}
|
||||
}
|
||||
|
||||
void onKeyCharCallback(GLFWwindow *window, unsigned int codepoint) {
|
||||
if(Input::isKeyboardCaptured()) {
|
||||
kekData.input->activeKeyboardCapture.charCallback(window, codepoint);
|
||||
return;
|
||||
}
|
||||
|
||||
for(auto cb : kekData.keyCharCallbacks) {
|
||||
cb.second(window, codepoint);
|
||||
}
|
||||
}
|
||||
|
||||
void onMouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
|
||||
for(auto cb : kekData.mouseButtonCallbacks) {
|
||||
cb.second(window, button, action, mods);
|
||||
}
|
||||
}
|
||||
|
||||
InputListener addPeriodicCallback(PeriodicCallback callback) {
|
||||
InputListener id = nextID++;
|
||||
kekData.periodicCallbacks.emplace(id, callback);
|
||||
@ -92,16 +144,16 @@ GLFWKeyState getKeyState(KeyBinding binding) {
|
||||
|
||||
void setCursorMode(GLFWCursorMode mode) {
|
||||
glfwSetInputMode(kekData.window, GLFW_CURSOR, (int) mode);
|
||||
kekData.cursorMode = mode;
|
||||
kekData.input->cursorMode = mode;
|
||||
}
|
||||
|
||||
GLFWCursorMode getCursorMode() {
|
||||
return kekData.cursorMode;
|
||||
return kekData.input->cursorMode;
|
||||
}
|
||||
|
||||
KeyboardCapture captureKeyboardInput(KeyCharCallback charCallback, KeyCallback keyCallback, Callable uncaptureCallback) {
|
||||
KeyboardCapture id = nextID++;
|
||||
kekData.activeKeyboardCapture = {
|
||||
kekData.input->activeKeyboardCapture = {
|
||||
id,
|
||||
charCallback,
|
||||
keyCallback,
|
||||
@ -111,9 +163,9 @@ KeyboardCapture captureKeyboardInput(KeyCharCallback charCallback, KeyCallback k
|
||||
}
|
||||
|
||||
bool uncaptureKeyboardInput(KeyboardCapture capture) {
|
||||
if(capture == KEK_INVALID_ID || capture != kekData.activeKeyboardCapture.id) return false;
|
||||
kekData.activeKeyboardCapture.uncaptureCallback();
|
||||
kekData.activeKeyboardCapture = {
|
||||
if(capture == KEK_INVALID_ID || capture != kekData.input->activeKeyboardCapture.id) return false;
|
||||
kekData.input->activeKeyboardCapture.uncaptureCallback();
|
||||
kekData.input->activeKeyboardCapture = {
|
||||
KEK_INVALID_ID,
|
||||
KeyCharCallback(),
|
||||
KeyCallback(),
|
||||
@ -123,7 +175,7 @@ bool uncaptureKeyboardInput(KeyboardCapture capture) {
|
||||
}
|
||||
|
||||
bool isKeyboardCaptured() {
|
||||
return kekData.activeKeyboardCapture.id != KEK_INVALID_ID;
|
||||
return kekData.input->activeKeyboardCapture.id != KEK_INVALID_ID;
|
||||
}
|
||||
|
||||
}
|
@ -40,6 +40,9 @@ enum class GLFWMouseButton {
|
||||
|
||||
namespace kek::Input {
|
||||
|
||||
void init();
|
||||
void destroy();
|
||||
|
||||
InputListener addPeriodicCallback(PeriodicCallback callback);
|
||||
void removePeriodicCallback(InputListener listener);
|
||||
|
||||
|
@ -15,13 +15,7 @@ namespace kek {
|
||||
|
||||
struct PhysicsData;
|
||||
struct UIData;
|
||||
|
||||
struct ActiveKeyboardCapture {
|
||||
KeyboardCapture id = KEK_INVALID_ID;
|
||||
KeyCharCallback charCallback;
|
||||
KeyCallback keyCallback;
|
||||
Callable uncaptureCallback;
|
||||
};
|
||||
struct InputData;
|
||||
|
||||
struct KekData {
|
||||
std::map<InputListener, PeriodicCallback> periodicCallbacks;
|
||||
@ -46,11 +40,9 @@ struct KekData {
|
||||
|
||||
FT_Library freetype;
|
||||
|
||||
ActiveKeyboardCapture activeKeyboardCapture;
|
||||
GLFWCursorMode cursorMode;
|
||||
|
||||
UIData *ui;
|
||||
PhysicsData *physics;
|
||||
InputData *input;
|
||||
};
|
||||
|
||||
extern KekData kekData;
|
||||
|
26
src/kekengine/include/internal/input.h
Normal file
26
src/kekengine/include/internal/input.h
Normal file
@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
namespace kek {
|
||||
|
||||
struct ActiveKeyboardCapture {
|
||||
KeyboardCapture id = KEK_INVALID_ID;
|
||||
KeyCharCallback charCallback;
|
||||
KeyCallback keyCallback;
|
||||
Callable uncaptureCallback;
|
||||
};
|
||||
|
||||
struct InputData {
|
||||
ActiveKeyboardCapture activeKeyboardCapture;
|
||||
GLFWCursorMode cursorMode;
|
||||
};
|
||||
|
||||
namespace Input {
|
||||
|
||||
void onCursorPosCallback(GLFWwindow *window, double x, double y);
|
||||
void onKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
|
||||
void onKeyCharCallback(GLFWwindow *window, unsigned int codepoint);
|
||||
void onMouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user