add input.add_callback("key:...") support

This commit is contained in:
MihailRis
2024-12-25 14:08:22 +03:00
parent 5e76cdfadc
commit 96577ee041
4 changed files with 46 additions and 2 deletions
+11 -1
View File
@@ -23,6 +23,7 @@ bool Events::_cursor_locked = false;
std::vector<uint> Events::codepoints;
std::vector<keycode> Events::pressedKeys;
std::unordered_map<std::string, Binding> Events::bindings;
std::unordered_map<keycode, util::RunnablesList> Events::keyCallbacks;
bool Events::pressed(keycode keycode) {
return pressed(static_cast<int>(keycode));
@@ -156,6 +157,12 @@ bool Events::jactive(const std::string& name) {
void Events::setKey(int key, bool b) {
Events::keys[key] = b;
Events::frames[key] = currentFrame;
if (b) {
const auto& callbacks = keyCallbacks.find(static_cast<keycode>(key));
if (callbacks != keyCallbacks.end()) {
callbacks->second.notify();
}
}
}
void Events::setButton(int button, bool b) {
@@ -173,6 +180,10 @@ void Events::setPosition(float xpos, float ypos) {
Events::cursor.y = ypos;
}
observer_handler Events::addKeyCallback(keycode key, runnable callback) {
return keyCallbacks[key].add(std::move(callback));
}
#include "coders/json.hpp"
#include "coders/toml.hpp"
@@ -228,7 +239,6 @@ void Events::loadBindings(
} else if (bindType == BindType::REBIND) {
Events::rebind(key, type, code);
}
}
}
}
+3
View File
@@ -27,6 +27,7 @@ public:
static std::vector<uint> codepoints;
static std::vector<keycode> pressedKeys;
static std::unordered_map<std::string, Binding> bindings;
static std::unordered_map<keycode, util::RunnablesList> keyCallbacks;
static void pollEvents();
@@ -50,6 +51,8 @@ public:
static bool active(const std::string& name);
static bool jactive(const std::string& name);
static observer_handler addKeyCallback(keycode key, runnable callback);
static void setKey(int key, bool b);
static void setButton(int button, bool b);