add controls reset

This commit is contained in:
ChancellorIkseew 2024-10-31 20:57:56 +10:00
parent d46afee063
commit 0733ddb38e
6 changed files with 40 additions and 8 deletions

View File

@ -40,6 +40,5 @@ function reset_graphics()
end end
function reset_control() function reset_control()
print("not implemented") input.reset_bindings()
--Wait for binding system update
end end

View File

@ -31,7 +31,7 @@ void corecontent::setup(EnginePaths* paths, ContentBuilder* builder) {
auto bindsFile = paths->getResourcesFolder()/fs::path("bindings.toml"); auto bindsFile = paths->getResourcesFolder()/fs::path("bindings.toml");
if (fs::is_regular_file(bindsFile)) { if (fs::is_regular_file(bindsFile)) {
Events::loadBindings( Events::loadBindings(
bindsFile.u8string(), files::read_string(bindsFile) bindsFile.u8string(), files::read_string(bindsFile), BindType::BIND
); );
} }

View File

@ -131,7 +131,7 @@ void Engine::loadControls() {
if (fs::is_regular_file(controls_file)) { if (fs::is_regular_file(controls_file)) {
logger.info() << "loading controls"; logger.info() << "loading controls";
std::string text = files::read_string(controls_file); 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"); auto bindsFile = configFolder/fs::path("bindings.toml");
if (fs::is_regular_file(bindsFile)) { if (fs::is_regular_file(bindsFile)) {
Events::loadBindings( Events::loadBindings(
bindsFile.u8string(), files::read_string(bindsFile) bindsFile.u8string(), files::read_string(bindsFile), BindType::BIND
); );
} }
} }

View File

@ -1,4 +1,7 @@
#include <filesystem>
#include "engine.hpp" #include "engine.hpp"
#include "files/files.hpp"
#include "frontend/hud.hpp" #include "frontend/hud.hpp"
#include "frontend/screens/Screen.hpp" #include "frontend/screens/Screen.hpp"
#include "graphics/ui/GUI.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[] = { const luaL_Reg inputlib[] = {
{"keycode", lua::wrap<l_keycode>}, {"keycode", lua::wrap<l_keycode>},
{"mousecode", lua::wrap<l_mousecode>}, {"mousecode", lua::wrap<l_mousecode>},
@ -118,4 +134,5 @@ const luaL_Reg inputlib[] = {
{"get_binding_text", lua::wrap<l_get_binding_text>}, {"get_binding_text", lua::wrap<l_get_binding_text>},
{"is_active", lua::wrap<l_is_active>}, {"is_active", lua::wrap<l_is_active>},
{"is_pressed", lua::wrap<l_is_pressed>}, {"is_pressed", lua::wrap<l_is_pressed>},
{"reset_bindings", lua::wrap<l_reset_bindings>},
{NULL, NULL}}; {NULL, NULL}};

View File

@ -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) { 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); bindings[name] = Binding(type, code);
} }
@ -193,7 +197,8 @@ std::string Events::writeBindings() {
} }
void Events::loadBindings( 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); auto map = toml::parse(filename, source);
for (auto& [sectionName, section] : map.asObject()) { for (auto& [sectionName, section] : map.asObject()) {
@ -214,7 +219,12 @@ void Events::loadBindings(
<< util::quote(key) << ")"; << util::quote(key) << ")";
continue; continue;
} }
if (bindType == BindType::BIND) {
Events::bind(key, type, code); Events::bind(key, type, code);
} else if (bindType == BindType::REBIND) {
Events::rebind(key, type, code);
}
} }
} }
} }

View File

@ -9,6 +9,11 @@
inline constexpr short KEYS_BUFFER_SIZE = 1036; inline constexpr short KEYS_BUFFER_SIZE = 1036;
enum class BindType {
BIND = 0,
REBIND = 1
};
class Events { class Events {
static bool keys[KEYS_BUFFER_SIZE]; static bool keys[KEYS_BUFFER_SIZE];
static uint frames[KEYS_BUFFER_SIZE]; static uint frames[KEYS_BUFFER_SIZE];
@ -52,6 +57,7 @@ public:
static std::string writeBindings(); static std::string writeBindings();
static void loadBindings( static void loadBindings(
const std::string& filename, const std::string& source const std::string& filename, const std::string& source,
BindType bindType
); );
}; };