Improve input
This commit is contained in:
parent
19f791cd35
commit
1d0869794b
@ -43,7 +43,7 @@ static void glDebugOutput(GLenum source, GLenum type, unsigned int id, GLenum se
|
||||
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 double lastX = 0, lastY = 0;
|
||||
if(firstMouse) {
|
||||
@ -63,17 +63,23 @@ void onCursorPosCallback(GLFWwindow *window, double x, double y) {
|
||||
kekData.activeCamera->rotateYaw(xoff);
|
||||
kekData.activeCamera->rotatePitch(yoff);
|
||||
|
||||
for(std::pair<InputListener, MouseCallback> cb : kekData.mouseCallbacks) {
|
||||
for(auto cb : kekData.mouseCallbacks) {
|
||||
cb.second(window, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
void onKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
|
||||
for(std::pair<InputListener, KeyCallback> cb : kekData.keyCallbacks) {
|
||||
static void onKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
|
||||
for(auto cb : kekData.keyCallbacks) {
|
||||
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() {
|
||||
// Init GLFW
|
||||
if (glfwInit() != GL_TRUE) {
|
||||
@ -155,6 +161,8 @@ int init() {
|
||||
|
||||
glfwSetCursorPosCallback(kekData.window, onCursorPosCallback);
|
||||
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);
|
||||
glfwSwapInterval(0);
|
||||
|
@ -38,6 +38,16 @@ void removeMouseListener(InputListener 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) {
|
||||
if(name == KEK_INVALID_KEY_BINDING_NAME) return KEK_INVALID_KEY_BINDING;
|
||||
KeyBinding id = nextID++;
|
||||
@ -48,22 +58,23 @@ KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey) {
|
||||
return id;
|
||||
}
|
||||
|
||||
void reassignKeyBinding(KeyBinding mapping, GLFWKey key) {
|
||||
auto it = kekData.keyBindings.find(mapping);
|
||||
void reassignKeyBinding(KeyBinding binding, GLFWKey key) {
|
||||
if(binding == KEK_INVALID_KEY_BINDING) return;
|
||||
auto it = kekData.keyBindings.find(binding);
|
||||
if(it == kekData.keyBindings.end()) return;
|
||||
KeyBindingData d = it->second;
|
||||
d.key = key;
|
||||
it->second = d;
|
||||
}
|
||||
|
||||
KeyBindingData getKeyBinding(KeyBinding mapping) {
|
||||
auto it = kekData.keyBindings.find(mapping);
|
||||
KeyBindingData getKeyBinding(KeyBinding binding) {
|
||||
auto it = kekData.keyBindings.find(binding);
|
||||
if(it == kekData.keyBindings.end()) return {.name = KEK_INVALID_KEY_BINDING_NAME};
|
||||
return it->second;
|
||||
}
|
||||
|
||||
GLFWKeyState getKeyState(KeyBinding mapping) {
|
||||
auto it = kekData.keyBindings.find(mapping);
|
||||
GLFWKeyState getKeyState(KeyBinding binding) {
|
||||
auto it = kekData.keyBindings.find(binding);
|
||||
if(it == kekData.keyBindings.end()) return -1;
|
||||
return glfwGetKey(kekData.window, it->second.key);
|
||||
}
|
||||
|
@ -25,7 +25,7 @@
|
||||
#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 -1
|
||||
#define KEK_INVALID_KEY_BINDING -1u
|
||||
|
||||
#define KEK_FONT_RESOLUTION 64
|
||||
#define KEK_FONT_BITMAP_WIDTH_BLOCKS 16
|
||||
|
@ -10,12 +10,13 @@ namespace kek {
|
||||
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 *, 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 GLFWKey;
|
||||
typedef unsigned int GLFWKeyState;
|
||||
typedef int GLFWKey;
|
||||
typedef int GLFWKeyState;
|
||||
typedef unsigned int KeyBinding;
|
||||
typedef unsigned int GLFWMouseButton;
|
||||
typedef int GLFWMouseButton;
|
||||
|
||||
struct KeyBindingData {
|
||||
std::string name;
|
||||
@ -27,23 +28,20 @@ struct KeyBindingData {
|
||||
namespace kek::Input {
|
||||
|
||||
InputListener addPeriodicCallback(PeriodicCallback callback);
|
||||
|
||||
void removePeriodicCallback(InputListener listener);
|
||||
|
||||
InputListener addKeyListener(KeyCallback callback);
|
||||
|
||||
void removeKeyListener(InputListener listener);
|
||||
|
||||
InputListener addMouseListener(MouseCallback callback);
|
||||
|
||||
void removeMouseListener(InputListener listener);
|
||||
|
||||
InputListener addMouseButtonListener(MouseButtonCallback callback);
|
||||
void removeMouseButtonListener(InputListener listener);
|
||||
|
||||
KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey);
|
||||
|
||||
void reassignKeyBinding(KeyBinding mapping, GLFWKey key);
|
||||
|
||||
KeyBindingData getKeyBinding(KeyBinding mapping);
|
||||
|
||||
GLFWKeyState getKeyState(KeyBinding mapping);
|
||||
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ struct KekData {
|
||||
std::map<InputListener, PeriodicCallback> periodicCallbacks;
|
||||
std::map<InputListener, KeyCallback> keyCallbacks;
|
||||
std::map<InputListener, MouseCallback> mouseCallbacks;
|
||||
std::map<InputListener, MouseButtonCallback> mouseButtonCallbacks;
|
||||
std::map<KeyBinding, KeyBindingData> keyBindings;
|
||||
|
||||
GLFWwindow *window;
|
||||
|
@ -6,6 +6,24 @@
|
||||
|
||||
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) {
|
||||
Engine::init();
|
||||
|
||||
@ -38,5 +56,10 @@ int main(int argc, char **argv) {
|
||||
|
||||
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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user