Restructure input code
This commit is contained in:
parent
b706100ddc
commit
6725f5eb20
@ -26,6 +26,7 @@
|
|||||||
|
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
#include "internal/ui.h"
|
#include "internal/ui.h"
|
||||||
|
#include "internal/input.h"
|
||||||
|
|
||||||
kek::KekData kek::kekData;
|
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;
|
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() {
|
int init() {
|
||||||
// Init GLFW
|
// Init GLFW
|
||||||
if (glfwInit() != GL_TRUE) {
|
if (glfwInit() != GL_TRUE) {
|
||||||
@ -183,11 +141,11 @@ int init() {
|
|||||||
kekData.shader = new Shader("shader/mesh/vertex.glsl", "shader/mesh/fragment.glsl");
|
kekData.shader = new Shader("shader/mesh/vertex.glsl", "shader/mesh/fragment.glsl");
|
||||||
kekData.shader->initLighting();
|
kekData.shader->initLighting();
|
||||||
|
|
||||||
glfwSetCursorPosCallback(kekData.window, onCursorPosCallback);
|
glfwSetCursorPosCallback(kekData.window, Input::onCursorPosCallback);
|
||||||
Input::setCursorMode(GLFWCursorMode::CAPTURE);
|
Input::setCursorMode(GLFWCursorMode::CAPTURE);
|
||||||
glfwSetKeyCallback(kekData.window, onKeyCallback);
|
glfwSetKeyCallback(kekData.window, Input::onKeyCallback);
|
||||||
glfwSetCharCallback(kekData.window, onKeyCharCallback);
|
glfwSetCharCallback(kekData.window, Input::onKeyCharCallback);
|
||||||
glfwSetMouseButtonCallback(kekData.window, onMouseButtonCallback);
|
glfwSetMouseButtonCallback(kekData.window, Input::onMouseButtonCallback);
|
||||||
|
|
||||||
glClearColor(0.1f, 0.3f, 0.1f, 0.0f);
|
glClearColor(0.1f, 0.3f, 0.1f, 0.0f);
|
||||||
glfwSwapInterval(0);
|
glfwSwapInterval(0);
|
||||||
|
@ -4,11 +4,63 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
|
#include "internal/input.h"
|
||||||
|
|
||||||
namespace kek::Input {
|
namespace kek::Input {
|
||||||
|
|
||||||
static InputListener nextID = 0;
|
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 addPeriodicCallback(PeriodicCallback callback) {
|
||||||
InputListener id = nextID++;
|
InputListener id = nextID++;
|
||||||
kekData.periodicCallbacks.emplace(id, callback);
|
kekData.periodicCallbacks.emplace(id, callback);
|
||||||
@ -92,16 +144,16 @@ GLFWKeyState getKeyState(KeyBinding binding) {
|
|||||||
|
|
||||||
void setCursorMode(GLFWCursorMode mode) {
|
void setCursorMode(GLFWCursorMode mode) {
|
||||||
glfwSetInputMode(kekData.window, GLFW_CURSOR, (int) mode);
|
glfwSetInputMode(kekData.window, GLFW_CURSOR, (int) mode);
|
||||||
kekData.cursorMode = mode;
|
kekData.input->cursorMode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
GLFWCursorMode getCursorMode() {
|
GLFWCursorMode getCursorMode() {
|
||||||
return kekData.cursorMode;
|
return kekData.input->cursorMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
KeyboardCapture captureKeyboardInput(KeyCharCallback charCallback, KeyCallback keyCallback, Callable uncaptureCallback) {
|
KeyboardCapture captureKeyboardInput(KeyCharCallback charCallback, KeyCallback keyCallback, Callable uncaptureCallback) {
|
||||||
KeyboardCapture id = nextID++;
|
KeyboardCapture id = nextID++;
|
||||||
kekData.activeKeyboardCapture = {
|
kekData.input->activeKeyboardCapture = {
|
||||||
id,
|
id,
|
||||||
charCallback,
|
charCallback,
|
||||||
keyCallback,
|
keyCallback,
|
||||||
@ -111,9 +163,9 @@ KeyboardCapture captureKeyboardInput(KeyCharCallback charCallback, KeyCallback k
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool uncaptureKeyboardInput(KeyboardCapture capture) {
|
bool uncaptureKeyboardInput(KeyboardCapture capture) {
|
||||||
if(capture == KEK_INVALID_ID || capture != kekData.activeKeyboardCapture.id) return false;
|
if(capture == KEK_INVALID_ID || capture != kekData.input->activeKeyboardCapture.id) return false;
|
||||||
kekData.activeKeyboardCapture.uncaptureCallback();
|
kekData.input->activeKeyboardCapture.uncaptureCallback();
|
||||||
kekData.activeKeyboardCapture = {
|
kekData.input->activeKeyboardCapture = {
|
||||||
KEK_INVALID_ID,
|
KEK_INVALID_ID,
|
||||||
KeyCharCallback(),
|
KeyCharCallback(),
|
||||||
KeyCallback(),
|
KeyCallback(),
|
||||||
@ -123,7 +175,7 @@ bool uncaptureKeyboardInput(KeyboardCapture capture) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool isKeyboardCaptured() {
|
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 {
|
namespace kek::Input {
|
||||||
|
|
||||||
|
void init();
|
||||||
|
void destroy();
|
||||||
|
|
||||||
InputListener addPeriodicCallback(PeriodicCallback callback);
|
InputListener addPeriodicCallback(PeriodicCallback callback);
|
||||||
void removePeriodicCallback(InputListener listener);
|
void removePeriodicCallback(InputListener listener);
|
||||||
|
|
||||||
|
@ -15,13 +15,7 @@ namespace kek {
|
|||||||
|
|
||||||
struct PhysicsData;
|
struct PhysicsData;
|
||||||
struct UIData;
|
struct UIData;
|
||||||
|
struct InputData;
|
||||||
struct ActiveKeyboardCapture {
|
|
||||||
KeyboardCapture id = KEK_INVALID_ID;
|
|
||||||
KeyCharCallback charCallback;
|
|
||||||
KeyCallback keyCallback;
|
|
||||||
Callable uncaptureCallback;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct KekData {
|
struct KekData {
|
||||||
std::map<InputListener, PeriodicCallback> periodicCallbacks;
|
std::map<InputListener, PeriodicCallback> periodicCallbacks;
|
||||||
@ -46,11 +40,9 @@ struct KekData {
|
|||||||
|
|
||||||
FT_Library freetype;
|
FT_Library freetype;
|
||||||
|
|
||||||
ActiveKeyboardCapture activeKeyboardCapture;
|
|
||||||
GLFWCursorMode cursorMode;
|
|
||||||
|
|
||||||
UIData *ui;
|
UIData *ui;
|
||||||
PhysicsData *physics;
|
PhysicsData *physics;
|
||||||
|
InputData *input;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern KekData kekData;
|
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