refactor: add GUI instance reference to UI nodes

This commit is contained in:
MihailRis
2025-04-02 14:55:53 +03:00
parent 74a94f869c
commit 4c48afbb90
70 changed files with 1133 additions and 832 deletions
+24 -41
View File
@@ -1,15 +1,15 @@
#include <filesystem>
#include "engine/Engine.hpp"
#include "io/io.hpp"
#include "frontend/hud.hpp"
#include "frontend/screens/Screen.hpp"
#include "graphics/ui/GUI.hpp"
#include "graphics/ui/elements/Container.hpp"
#include "io/io.hpp"
#include "libgui.hpp"
#include "util/stringutil.hpp"
#include "window/Events.hpp"
#include "window/input.hpp"
#include "libgui.hpp"
namespace scripting {
extern Hud* hud;
@@ -37,26 +37,26 @@ static int l_add_callback(lua::State* L) {
lua::pushvalue(L, 2);
auto actual_callback = lua::create_simple_handler(L);
observer_handler handler;
auto& gui = engine->getGUI();
auto& input = engine->getInput();
if (pos != std::string::npos) {
std::string prefix = bindname.substr(0, pos);
if (prefix == "key") {
auto key = input_util::keycode_from(bindname.substr(pos + 1));
handler = Events::addKeyCallback(key, actual_callback);
handler = input.addKeyCallback(key, actual_callback);
}
}
auto callback = [=]() -> bool {
if (!scripting::engine->getGUI()->isFocusCaught()) {
auto callback = [&gui, actual_callback]() -> bool {
if (!gui.isFocusCaught()) {
return actual_callback();
}
return false;
};
if (handler == nullptr) {
const auto& bind = Events::bindings.find(bindname);
if (bind == Events::bindings.end()) {
throw std::runtime_error("unknown binding " + util::quote(bindname));
}
handler = bind->second.onactived.add(callback);
auto& bind = input.requireBinding(bindname);
handler = bind.onactived.add(callback);
}
if (hud) {
@@ -64,7 +64,8 @@ static int l_add_callback(lua::State* L) {
return 0;
} else if (lua::gettop(L) >= 3) {
auto node = get_document_node(L, 3);
if (auto container = std::dynamic_pointer_cast<gui::Container>(node.node)) {
if (auto container =
std::dynamic_pointer_cast<gui::Container>(node.node)) {
container->keepAlive(handler);
return 0;
}
@@ -74,11 +75,11 @@ static int l_add_callback(lua::State* L) {
}
static int l_get_mouse_pos(lua::State* L) {
return lua::pushvec2(L, Events::cursor);
return lua::pushvec2(L, engine->getInput().getCursor().pos);
}
static int l_get_bindings(lua::State* L) {
auto& bindings = Events::bindings;
const auto& bindings = engine->getInput().getBindings().getAll();
lua::createtable(L, bindings.size(), 0);
int i = 0;
@@ -92,25 +93,14 @@ static int l_get_bindings(lua::State* L) {
static int l_get_binding_text(lua::State* L) {
auto bindname = lua::require_string(L, 1);
auto index = Events::bindings.find(bindname);
if (index == Events::bindings.end()) {
throw std::runtime_error("unknown binding " + util::quote(bindname));
lua::pushstring(L, "");
} else {
lua::pushstring(L, index->second.text());
}
return 1;
const auto& bind = engine->getInput().requireBinding(bindname);
return lua::pushstring(L, bind.text());
}
static int l_is_active(lua::State* L) {
auto bindname = lua::require_string(L, 1);
const auto& bind = Events::bindings.find(bindname);
if (bind == Events::bindings.end()) {
throw std::runtime_error("unknown binding " + util::quote(bindname));
}
return lua::pushboolean(L, bind->second.active());
auto& bind = engine->getInput().requireBinding(bindname);
return lua::pushboolean(L, bind.active());
}
static int l_is_pressed(lua::State* L) {
@@ -123,12 +113,11 @@ static int l_is_pressed(lua::State* L) {
auto name = code.substr(sep + 1);
if (prefix == "key") {
return lua::pushboolean(
L, Events::pressed(static_cast<int>(input_util::keycode_from(name)))
L, engine->getInput().pressed(input_util::keycode_from(name))
);
} else if (prefix == "mouse") {
return lua::pushboolean(
L,
Events::clicked(static_cast<int>(input_util::mousecode_from(name)))
L, engine->getInput().clicked(input_util::mousecode_from(name))
);
} else {
throw std::runtime_error("unknown input type " + util::quote(code));
@@ -140,9 +129,7 @@ static void reset_pack_bindings(const io::path& packFolder) {
auto bindsFile = configFolder / "bindings.toml";
if (io::is_regular_file(bindsFile)) {
Events::loadBindings(
bindsFile.string(),
io::read_string(bindsFile),
BindType::REBIND
bindsFile.string(), io::read_string(bindsFile), BindType::REBIND
);
}
}
@@ -157,12 +144,8 @@ static int l_reset_bindings(lua::State*) {
static int l_set_enabled(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;
bool enabled = lua::toboolean(L, 2);
engine->getInput().requireBinding(bindname).enabled = enabled;
return 0;
}