refactor: replace references to Events with Input

This commit is contained in:
MihailRis
2025-04-02 14:55:53 +03:00
parent 4c48afbb90
commit b202d1455b
15 changed files with 160 additions and 116 deletions
-65
View File
@@ -184,71 +184,6 @@ observer_handler Events::addKeyCallback(keycode key, KeyCallback callback) {
return ::key_callbacks[key].add(std::move(callback));
}
#include "coders/json.hpp"
#include "coders/toml.hpp"
std::string Events::writeBindings() {
auto obj = dv::object();
for (auto& entry : bindings.getAll()) {
const auto& binding = entry.second;
std::string value;
switch (binding.type) {
case inputtype::keyboard:
value =
"key:" +
input_util::get_name(static_cast<keycode>(binding.code));
break;
case inputtype::mouse:
value =
"mouse:" +
input_util::get_name(static_cast<mousecode>(binding.code));
break;
default:
throw std::runtime_error("unsupported control type");
}
obj[entry.first] = std::move(value);
}
return toml::stringify(obj);
}
void Events::loadBindings(
const std::string& filename, const std::string& source,
BindType bindType
) {
auto map = toml::parse(filename, source);
for (auto& [sectionName, section] : map.asObject()) {
for (auto& [name, value] : section.asObject()) {
auto key = sectionName + "." + name;
auto [prefix, codename] = util::split_at(value.asString(), ':');
inputtype type;
int code;
if (prefix == "key") {
type = inputtype::keyboard;
code = static_cast<int>(input_util::keycode_from(codename));
} else if (prefix == "mouse") {
type = inputtype::mouse;
code = static_cast<int>(input_util::mousecode_from(codename));
} else {
logger.error()
<< "unknown input type: " << prefix << " (binding "
<< util::quote(key) << ")";
continue;
}
if (bindType == BindType::BIND) {
Events::bind(key, type, code);
} else if (bindType == BindType::REBIND) {
Events::rebind(key, type, code);
}
}
}
}
void Events::enableBindings() {
for (auto& entry : bindings.getAll()) {
entry.second.enabled = true;
}
}
bool Events::isCursorLocked() {
return cursor_locked;
}
-13
View File
@@ -10,11 +10,6 @@
inline constexpr short KEYS_BUFFER_SIZE = 1036;
enum class BindType {
BIND = 0,
REBIND = 1
};
namespace Events {
extern int scroll;
extern glm::vec2 delta;
@@ -55,13 +50,5 @@ namespace Events {
void setPosition(float xpos, float ypos);
std::string writeBindings();
void loadBindings(
const std::string& filename,
const std::string& source,
BindType bindType
);
void enableBindings();
bool isCursorLocked();
};
+12 -4
View File
@@ -189,10 +189,6 @@ public:
return Events::getScroll();
}
Binding& requireBinding(const std::string& name) override {
return Events::requireBinding(name);
}
bool pressed(keycode keycode) const override {
return Events::pressed(keycode);
}
@@ -215,6 +211,10 @@ public:
};
}
void toggleCursor() override {
Events::toggleCursor();
}
Bindings& getBindings() override {
return Events::bindings;
}
@@ -226,6 +226,14 @@ public:
observer_handler addKeyCallback(keycode key, KeyCallback callback) override {
return Events::addKeyCallback(key, std::move(callback));
}
const std::vector<keycode>& getPressedKeys() const override {
return Events::pressedKeys;
}
const std::vector<uint>& getCodepoints() const override {
return Events::codepoints;
}
};
static_assert(!std::is_abstract<GLFWInput>());
+67
View File
@@ -1,5 +1,9 @@
#include "input.hpp"
#include "debug/Logger.hpp"
#include "util/stringutil.hpp"
#include "data/dv.hpp"
#include <GLFW/glfw3.h>
#include <unordered_map>
@@ -8,6 +12,8 @@
#include <windows.h>
#endif // _WIN32
static debug::Logger logger("input");
static std::unordered_map<std::string, int> keycodes {
{"enter", GLFW_KEY_ENTER},
{"space", GLFW_KEY_SPACE},
@@ -225,3 +231,64 @@ std::string input_util::to_string(mousecode code) {
return "unknown button";
}
}
const Binding& Bindings::require(const std::string& name) const {
if (const auto found = get(name)) {
return *found;
}
throw std::runtime_error("binding '" + name + "' does not exist");
}
void Bindings::read(const dv::value& map, BindType bindType) {
for (auto& [sectionName, section] : map.asObject()) {
for (auto& [name, value] : section.asObject()) {
auto key = sectionName + "." + name;
auto [prefix, codename] = util::split_at(value.asString(), ':');
inputtype type;
int code;
if (prefix == "key") {
type = inputtype::keyboard;
code = static_cast<int>(input_util::keycode_from(codename));
} else if (prefix == "mouse") {
type = inputtype::mouse;
code = static_cast<int>(input_util::mousecode_from(codename));
} else {
logger.error()
<< "unknown input type: " << prefix << " (binding "
<< util::quote(key) << ")";
continue;
}
if (bindType == BindType::BIND) {
bind(key, type, code);
} else if (bindType == BindType::REBIND) {
rebind(key, type, code);
}
}
}
}
#include "coders/toml.hpp"
std::string Bindings::write() const {
auto obj = dv::object();
for (auto& entry : bindings) {
const auto& binding = entry.second;
std::string value;
switch (binding.type) {
case inputtype::keyboard:
value =
"key:" +
input_util::get_name(static_cast<keycode>(binding.code));
break;
case inputtype::mouse:
value =
"mouse:" +
input_util::get_name(static_cast<mousecode>(binding.code));
break;
default:
throw std::runtime_error("unsupported control type");
}
obj[entry.first] = std::move(value);
}
return toml::stringify(obj);
}
+38 -4
View File
@@ -5,6 +5,15 @@
#include "util/HandlersList.hpp"
namespace dv {
class value;
}
enum class BindType {
BIND = 0,
REBIND = 1
};
/// @brief Represents glfw3 keycode values.
enum class keycode : int {
SPACE = 32,
@@ -195,6 +204,10 @@ public:
}
Binding* get(const std::string& name) {
return const_cast<Bindings*>(this)->get(name);
}
const Binding* get(const std::string& name) const {
const auto found = bindings.find(name);
if (found == bindings.end()) {
return nullptr;
@@ -202,13 +215,32 @@ public:
return &found->second;
}
Binding& require(const std::string& name) {
return const_cast<Bindings*>(this)->require(name);
}
const Binding& require(const std::string& name) const;
void bind(const std::string& name, inputtype type, int code) {
bindings.try_emplace(name, Binding(type, code));
}
void rebind(const std::string& name, inputtype type, int code) {
require(name) = Binding(type, code);
}
auto& getAll() {
return bindings;
}
void enableAll() {
for (auto& entry : bindings) {
entry.second.enabled = true;
}
}
void read(const dv::value& map, BindType bindType);
std::string write() const;
};
struct CursorState {
@@ -227,8 +259,6 @@ public:
virtual int getScroll() = 0;
virtual Binding& requireBinding(const std::string& name) = 0;
virtual bool pressed(keycode keycode) const = 0;
virtual bool jpressed(keycode keycode) const = 0;
@@ -237,14 +267,18 @@ public:
virtual CursorState getCursor() const = 0;
virtual void toggleCursor() = 0;
virtual Bindings& getBindings() = 0;
virtual const Bindings& getBindings() const = 0;
virtual observer_handler addKeyCallback(keycode key, KeyCallback callback) = 0;
virtual const std::vector<keycode>& getPressedKeys() const = 0;
virtual const std::vector<uint>& getCodepoints() const = 0;
observer_handler addCallback(const std::string& name, KeyCallback callback) {
return requireBinding(name).onactived.add(callback);
return getBindings().require(name).onactived.add(callback);
}
};