Improve input

This commit is contained in:
MrLetsplay 2022-10-31 21:52:22 +01:00
parent 19f791cd35
commit 1d0869794b
6 changed files with 61 additions and 20 deletions

View File

@ -43,7 +43,7 @@ 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;
} }
void onCursorPosCallback(GLFWwindow *window, double x, double y) { static void onCursorPosCallback(GLFWwindow *window, double x, double y) {
static bool firstMouse = true; static bool firstMouse = true;
static double lastX = 0, lastY = 0; static double lastX = 0, lastY = 0;
if(firstMouse) { if(firstMouse) {
@ -63,17 +63,23 @@ void onCursorPosCallback(GLFWwindow *window, double x, double y) {
kekData.activeCamera->rotateYaw(xoff); kekData.activeCamera->rotateYaw(xoff);
kekData.activeCamera->rotatePitch(yoff); kekData.activeCamera->rotatePitch(yoff);
for(std::pair<InputListener, MouseCallback> cb : kekData.mouseCallbacks) { for(auto cb : kekData.mouseCallbacks) {
cb.second(window, x, y); cb.second(window, x, y);
} }
} }
void onKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { static void onKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
for(std::pair<InputListener, KeyCallback> cb : kekData.keyCallbacks) { for(auto cb : kekData.keyCallbacks) {
cb.second(window, key, scancode, action, mods); cb.second(window, key, scancode, action, mods);
} }
} }
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) {
@ -155,6 +161,8 @@ int init() {
glfwSetCursorPosCallback(kekData.window, onCursorPosCallback); glfwSetCursorPosCallback(kekData.window, onCursorPosCallback);
glfwSetInputMode(kekData.window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(kekData.window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetKeyCallback(kekData.window, onKeyCallback);
glfwSetMouseButtonCallback(kekData.window, onMouseButtonCallback);
glClearColor(0.1f, 0.3f, 0.1f, 0.0f); glClearColor(0.1f, 0.3f, 0.1f, 0.0f);
glfwSwapInterval(0); glfwSwapInterval(0);

View File

@ -38,6 +38,16 @@ void removeMouseListener(InputListener listener) {
kekData.mouseCallbacks.erase(listener); kekData.mouseCallbacks.erase(listener);
} }
InputListener addMouseButtonListener(MouseButtonCallback callback) {
InputListener id = nextID++;
kekData.mouseButtonCallbacks.emplace(id, callback);
return id;
}
void removeMouseButtonListener(InputListener listener) {
kekData.mouseButtonCallbacks.erase(listener);
}
KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey) { KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey) {
if(name == KEK_INVALID_KEY_BINDING_NAME) return KEK_INVALID_KEY_BINDING; if(name == KEK_INVALID_KEY_BINDING_NAME) return KEK_INVALID_KEY_BINDING;
KeyBinding id = nextID++; KeyBinding id = nextID++;
@ -48,22 +58,23 @@ KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey) {
return id; return id;
} }
void reassignKeyBinding(KeyBinding mapping, GLFWKey key) { void reassignKeyBinding(KeyBinding binding, GLFWKey key) {
auto it = kekData.keyBindings.find(mapping); if(binding == KEK_INVALID_KEY_BINDING) return;
auto it = kekData.keyBindings.find(binding);
if(it == kekData.keyBindings.end()) return; if(it == kekData.keyBindings.end()) return;
KeyBindingData d = it->second; KeyBindingData d = it->second;
d.key = key; d.key = key;
it->second = d; it->second = d;
} }
KeyBindingData getKeyBinding(KeyBinding mapping) { KeyBindingData getKeyBinding(KeyBinding binding) {
auto it = kekData.keyBindings.find(mapping); auto it = kekData.keyBindings.find(binding);
if(it == kekData.keyBindings.end()) return {.name = KEK_INVALID_KEY_BINDING_NAME}; if(it == kekData.keyBindings.end()) return {.name = KEK_INVALID_KEY_BINDING_NAME};
return it->second; return it->second;
} }
GLFWKeyState getKeyState(KeyBinding mapping) { GLFWKeyState getKeyState(KeyBinding binding) {
auto it = kekData.keyBindings.find(mapping); auto it = kekData.keyBindings.find(binding);
if(it == kekData.keyBindings.end()) return -1; if(it == kekData.keyBindings.end()) return -1;
return glfwGetKey(kekData.window, it->second.key); return glfwGetKey(kekData.window, it->second.key);
} }

View File

@ -25,7 +25,7 @@
#define KEK_LIGHT_MAX_DISTANCE_SQUARED (KEK_LIGHT_MAX_DISTANCE * KEK_LIGHT_MAX_DISTANCE) #define KEK_LIGHT_MAX_DISTANCE_SQUARED (KEK_LIGHT_MAX_DISTANCE * KEK_LIGHT_MAX_DISTANCE)
#define KEK_INVALID_KEY_BINDING_NAME "INVALID" #define KEK_INVALID_KEY_BINDING_NAME "INVALID"
#define KEK_INVALID_KEY_BINDING -1 #define KEK_INVALID_KEY_BINDING -1u
#define KEK_FONT_RESOLUTION 64 #define KEK_FONT_RESOLUTION 64
#define KEK_FONT_BITMAP_WIDTH_BLOCKS 16 #define KEK_FONT_BITMAP_WIDTH_BLOCKS 16

View File

@ -10,12 +10,13 @@ namespace kek {
typedef generic_callable_t<GLFWwindow *> PeriodicCallback; // periodicCallback(GLFWwindow *window) typedef generic_callable_t<GLFWwindow *> PeriodicCallback; // periodicCallback(GLFWwindow *window)
typedef generic_callable_t<GLFWwindow *, int, int, int, int> KeyCallback; // keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) typedef generic_callable_t<GLFWwindow *, int, int, int, int> KeyCallback; // keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
typedef generic_callable_t<GLFWwindow *, double, double> MouseCallback; // mouseCallback(GLFWwindow *window, double x, double y) typedef generic_callable_t<GLFWwindow *, double, double> MouseCallback; // mouseCallback(GLFWwindow *window, double x, double y)
typedef generic_callable_t<GLFWwindow *, int, int, int> MouseButtonCallback; // void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
typedef unsigned int InputListener; typedef unsigned int InputListener;
typedef unsigned int GLFWKey; typedef int GLFWKey;
typedef unsigned int GLFWKeyState; typedef int GLFWKeyState;
typedef unsigned int KeyBinding; typedef unsigned int KeyBinding;
typedef unsigned int GLFWMouseButton; typedef int GLFWMouseButton;
struct KeyBindingData { struct KeyBindingData {
std::string name; std::string name;
@ -27,23 +28,20 @@ struct KeyBindingData {
namespace kek::Input { namespace kek::Input {
InputListener addPeriodicCallback(PeriodicCallback callback); InputListener addPeriodicCallback(PeriodicCallback callback);
void removePeriodicCallback(InputListener listener); void removePeriodicCallback(InputListener listener);
InputListener addKeyListener(KeyCallback callback); InputListener addKeyListener(KeyCallback callback);
void removeKeyListener(InputListener listener); void removeKeyListener(InputListener listener);
InputListener addMouseListener(MouseCallback callback); InputListener addMouseListener(MouseCallback callback);
void removeMouseListener(InputListener listener); void removeMouseListener(InputListener listener);
InputListener addMouseButtonListener(MouseButtonCallback callback);
void removeMouseButtonListener(InputListener listener);
KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey); KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey);
void reassignKeyBinding(KeyBinding mapping, GLFWKey key); void reassignKeyBinding(KeyBinding mapping, GLFWKey key);
KeyBindingData getKeyBinding(KeyBinding mapping); KeyBindingData getKeyBinding(KeyBinding mapping);
GLFWKeyState getKeyState(KeyBinding mapping); GLFWKeyState getKeyState(KeyBinding mapping);
} }

View File

@ -17,6 +17,7 @@ struct KekData {
std::map<InputListener, PeriodicCallback> periodicCallbacks; std::map<InputListener, PeriodicCallback> periodicCallbacks;
std::map<InputListener, KeyCallback> keyCallbacks; std::map<InputListener, KeyCallback> keyCallbacks;
std::map<InputListener, MouseCallback> mouseCallbacks; std::map<InputListener, MouseCallback> mouseCallbacks;
std::map<InputListener, MouseButtonCallback> mouseButtonCallbacks;
std::map<KeyBinding, KeyBindingData> keyBindings; std::map<KeyBinding, KeyBindingData> keyBindings;
GLFWwindow *window; GLFWwindow *window;

View File

@ -6,6 +6,24 @@
using namespace kek; using namespace kek;
static KeyBinding keyWow;
void periodicCallback(GLFWwindow *window, void *data){
if(Input::getKeyState(keyWow) == GLFW_PRESS) {
//std::cout << "WOW" << std::endl;
}
}
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods, void *data){
if(key == Input::getKeyBinding(keyWow).key && action == GLFW_PRESS) {
std::cout << "WOW" << std::endl;
}
}
void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods, void *data){
std::cout << "HELLO" << std::endl;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
Engine::init(); Engine::init();
@ -38,5 +56,10 @@ int main(int argc, char **argv) {
Engine::setActiveScene(scene); Engine::setActiveScene(scene);
keyWow = Input::createKeyBinding("wow", GLFW_KEY_O);
Input::addPeriodicCallback(PeriodicCallback(periodicCallback, nullptr));
Input::addKeyListener(KeyCallback(keyCallback, nullptr));
Input::addMouseButtonListener(MouseButtonCallback(mouseButtonCallback, nullptr));
Engine::start(); Engine::start();
} }