From 0733ddb38e91c564a9df8fd0891c12bd528c519a Mon Sep 17 00:00:00 2001 From: ChancellorIkseew Date: Thu, 31 Oct 2024 20:57:56 +1000 Subject: [PATCH] add controls reset --- res/layouts/pages/settings_reset.xml.lua | 3 +-- src/core_defs.cpp | 2 +- src/engine.cpp | 4 ++-- src/logic/scripting/lua/libs/libinput.cpp | 17 +++++++++++++++++ src/window/Events.cpp | 14 ++++++++++++-- src/window/Events.hpp | 8 +++++++- 6 files changed, 40 insertions(+), 8 deletions(-) diff --git a/res/layouts/pages/settings_reset.xml.lua b/res/layouts/pages/settings_reset.xml.lua index d7210001..a8ca2a9f 100644 --- a/res/layouts/pages/settings_reset.xml.lua +++ b/res/layouts/pages/settings_reset.xml.lua @@ -40,6 +40,5 @@ function reset_graphics() end function reset_control() - print("not implemented") - --Wait for binding system update + input.reset_bindings() end diff --git a/src/core_defs.cpp b/src/core_defs.cpp index 7d19acd1..7186aa72 100644 --- a/src/core_defs.cpp +++ b/src/core_defs.cpp @@ -31,7 +31,7 @@ void corecontent::setup(EnginePaths* paths, ContentBuilder* builder) { auto bindsFile = paths->getResourcesFolder()/fs::path("bindings.toml"); if (fs::is_regular_file(bindsFile)) { Events::loadBindings( - bindsFile.u8string(), files::read_string(bindsFile) + bindsFile.u8string(), files::read_string(bindsFile), BindType::BIND ); } diff --git a/src/engine.cpp b/src/engine.cpp index 6ba4fcee..c13e817b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -131,7 +131,7 @@ void Engine::loadControls() { if (fs::is_regular_file(controls_file)) { logger.info() << "loading controls"; std::string text = files::read_string(controls_file); - Events::loadBindings(controls_file.u8string(), text); + Events::loadBindings(controls_file.u8string(), text, BindType::BIND); } } @@ -287,7 +287,7 @@ static void load_configs(const fs::path& root) { auto bindsFile = configFolder/fs::path("bindings.toml"); if (fs::is_regular_file(bindsFile)) { Events::loadBindings( - bindsFile.u8string(), files::read_string(bindsFile) + bindsFile.u8string(), files::read_string(bindsFile), BindType::BIND ); } } diff --git a/src/logic/scripting/lua/libs/libinput.cpp b/src/logic/scripting/lua/libs/libinput.cpp index 84dde256..d347e478 100644 --- a/src/logic/scripting/lua/libs/libinput.cpp +++ b/src/logic/scripting/lua/libs/libinput.cpp @@ -1,4 +1,7 @@ +#include + #include "engine.hpp" +#include "files/files.hpp" #include "frontend/hud.hpp" #include "frontend/screens/Screen.hpp" #include "graphics/ui/GUI.hpp" @@ -109,6 +112,19 @@ static int l_is_pressed(lua::State* L) { } } +static int l_reset_bindings(lua::State*) { + auto resFolder = engine->getPaths()->getResourcesFolder(); + auto configFolder = resFolder/fs::path("config"); + auto bindsFile = configFolder/fs::path("bindings.toml"); + if (fs::is_regular_file(bindsFile)) { + Events::loadBindings( + bindsFile.u8string(), files::read_string(bindsFile), BindType::REBIND + ); + return 0; + } + return 1; +} + const luaL_Reg inputlib[] = { {"keycode", lua::wrap}, {"mousecode", lua::wrap}, @@ -118,4 +134,5 @@ const luaL_Reg inputlib[] = { {"get_binding_text", lua::wrap}, {"is_active", lua::wrap}, {"is_pressed", lua::wrap}, + {"reset_bindings", lua::wrap}, {NULL, NULL}}; diff --git a/src/window/Events.cpp b/src/window/Events.cpp index 092e4a0d..dad96be8 100644 --- a/src/window/Events.cpp +++ b/src/window/Events.cpp @@ -126,6 +126,10 @@ void Events::bind(const std::string& name, inputtype type, int code) { } void Events::rebind(const std::string& name, inputtype type, int code) { + auto& found = bindings.find(name); + if (found == bindings.end()) { + throw std::runtime_error("binding '" + name + "' does not exists"); + } bindings[name] = Binding(type, code); } @@ -193,7 +197,8 @@ std::string Events::writeBindings() { } void Events::loadBindings( - const std::string& filename, const std::string& source + const std::string& filename, const std::string& source, + BindType bindType ) { auto map = toml::parse(filename, source); for (auto& [sectionName, section] : map.asObject()) { @@ -214,7 +219,12 @@ void Events::loadBindings( << util::quote(key) << ")"; continue; } - Events::bind(key, type, code); + if (bindType == BindType::BIND) { + Events::bind(key, type, code); + } else if (bindType == BindType::REBIND) { + Events::rebind(key, type, code); + } + } } } diff --git a/src/window/Events.hpp b/src/window/Events.hpp index 230145a9..f5ed5720 100644 --- a/src/window/Events.hpp +++ b/src/window/Events.hpp @@ -9,6 +9,11 @@ inline constexpr short KEYS_BUFFER_SIZE = 1036; +enum class BindType { + BIND = 0, + REBIND = 1 +}; + class Events { static bool keys[KEYS_BUFFER_SIZE]; static uint frames[KEYS_BUFFER_SIZE]; @@ -52,6 +57,7 @@ public: static std::string writeBindings(); static void loadBindings( - const std::string& filename, const std::string& source + const std::string& filename, const std::string& source, + BindType bindType ); };