72 lines
1.7 KiB
C++
72 lines
1.7 KiB
C++
#include "input.h"
|
|
|
|
#include <map>
|
|
|
|
#include "internal.h"
|
|
|
|
namespace kek::Input {
|
|
|
|
static InputListener nextID = 0;
|
|
|
|
InputListener addPeriodicCallback(PeriodicCallback callback) {
|
|
InputListener id = nextID++;
|
|
kekData.periodicCallbacks.emplace(id, callback);
|
|
return id;
|
|
}
|
|
|
|
void removePeriodicCallback(InputListener listener) {
|
|
kekData.periodicCallbacks.erase(listener);
|
|
}
|
|
|
|
InputListener addKeyListener(KeyCallback callback) {
|
|
InputListener id = nextID++;
|
|
kekData.keyCallbacks.emplace(id, callback);
|
|
return id;
|
|
}
|
|
|
|
void removeKeyListener(InputListener listener) {
|
|
kekData.keyCallbacks.erase(listener);
|
|
}
|
|
|
|
InputListener addMouseListener(MouseCallback callback) {
|
|
InputListener id = nextID++;
|
|
kekData.mouseCallbacks.emplace(id, callback);
|
|
return id;
|
|
}
|
|
|
|
void removeMouseListener(InputListener listener) {
|
|
kekData.mouseCallbacks.erase(listener);
|
|
}
|
|
|
|
KeyBinding createKeyBinding(std::string name, GLFWKey defaultKey) {
|
|
if(name == KEK_INVALID_KEY_BINDING_NAME) return KEK_INVALID_KEY_BINDING;
|
|
KeyBinding id = nextID++;
|
|
KeyBindingData d;
|
|
d.name = name;
|
|
d.key = defaultKey;
|
|
kekData.keyBindings.emplace(id, d);
|
|
return id;
|
|
}
|
|
|
|
void reassignKeyBinding(KeyBinding mapping, GLFWKey key) {
|
|
auto it = kekData.keyBindings.find(mapping);
|
|
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);
|
|
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);
|
|
if(it == kekData.keyBindings.end()) return -1;
|
|
return glfwGetKey(kekData.window, it->second.key);
|
|
}
|
|
|
|
}
|