Merge pull request #339 from ChancellorIkseew/add-bindings-block/unblock

add lua function block/unblock bindings
This commit is contained in:
MihailRis 2024-11-02 13:08:46 +03:00 committed by GitHub
commit 47c6c98b4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 27 additions and 0 deletions

View File

@ -60,6 +60,8 @@ static int l_close_world(lua::State* L) {
if (save_world) {
controller->saveWorld();
}
// unblock all bindings
Events::enableBindings();
// destroy LevelScreen and run quit callbacks
engine->setScreen(nullptr);
// create and go to menu screen

View File

@ -133,6 +133,17 @@ static int l_reset_bindings(lua::State*) {
return 0;
}
static int l_enable_binding(lua::State* L) {
std::string bindname = lua::require_string(L, 1);
bool enable = lua::toboolean(L, 2);
const auto& bind = Events::bindings.find(bindname);
if (bind == Events::bindings.end()) {
throw std::runtime_error("unknown binding " + util::quote(bindname));
}
Events::bindings[bindname].enable = enable;
return 0;
}
const luaL_Reg inputlib[] = {
{"keycode", lua::wrap<l_keycode>},
{"mousecode", lua::wrap<l_mousecode>},
@ -143,4 +154,5 @@ const luaL_Reg inputlib[] = {
{"is_active", lua::wrap<l_is_active>},
{"is_pressed", lua::wrap<l_is_pressed>},
{"reset_bindings", lua::wrap<l_reset_bindings>},
{"enable_binding", lua::wrap<l_enable_binding>},
{NULL, NULL}};

View File

@ -78,6 +78,10 @@ void Events::pollEvents() {
for (auto& entry : bindings) {
auto& binding = entry.second;
if (!binding.enable) {
binding.state = false;
continue;
}
binding.justChange = false;
bool newstate = false;
@ -228,3 +232,10 @@ void Events::loadBindings(
}
}
}
void Events::enableBindings() {
for (auto& entry : bindings) {
auto& binding = entry.second;
binding.enable = true;
}
}

View File

@ -60,4 +60,5 @@ public:
const std::string& filename, const std::string& source,
BindType bindType
);
static void enableBindings();
};

View File

@ -137,6 +137,7 @@ struct Binding {
int code;
bool state = false;
bool justChange = false;
bool enable = true;
Binding() = default;
Binding(inputtype type, int code) : type(type), code(code) {