refactor: add GUI instance reference to UI nodes
This commit is contained in:
@@ -14,7 +14,7 @@ static int l_add_command(lua::State* L) {
|
||||
lua::pushvalue(L, 3);
|
||||
auto func = lua::create_lambda(L);
|
||||
try {
|
||||
engine->getCommandsInterpreter()->getRepository()->add(
|
||||
engine->getCmd().getRepository()->add(
|
||||
scheme,
|
||||
description,
|
||||
[func](auto, auto args, auto kwargs) {
|
||||
@@ -32,7 +32,7 @@ static int l_add_command(lua::State* L) {
|
||||
static int l_execute(lua::State* L) {
|
||||
auto prompt = lua::require_string(L, 1);
|
||||
try {
|
||||
auto result = engine->getCommandsInterpreter()->execute(prompt);
|
||||
auto result = engine->getCmd().execute(prompt);
|
||||
lua::pushvalue(L, result);
|
||||
return 1;
|
||||
} catch (const parsing_error& err) {
|
||||
@@ -45,19 +45,18 @@ static int l_execute(lua::State* L) {
|
||||
|
||||
static int l_get(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
return lua::pushvalue(L, (*engine->getCommandsInterpreter())[name]);
|
||||
return lua::pushvalue(L, engine->getCmd()[name]);
|
||||
}
|
||||
|
||||
static int l_set(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
auto value = lua::tovalue(L, 2);
|
||||
(*engine->getCommandsInterpreter())[name] = value;
|
||||
engine->getCmd()[name] = value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_get_commands_list(lua::State* L) {
|
||||
auto interpreter = engine->getCommandsInterpreter();
|
||||
auto repo = interpreter->getRepository();
|
||||
auto repo = engine->getCmd().getRepository();
|
||||
const auto& commands = repo->getCommands();
|
||||
|
||||
lua::createtable(L, commands.size(), 0);
|
||||
@@ -71,8 +70,7 @@ static int l_get_commands_list(lua::State* L) {
|
||||
|
||||
static int l_get_command_info(lua::State* L) {
|
||||
auto name = lua::require_string(L, 1);
|
||||
auto interpreter = engine->getCommandsInterpreter();
|
||||
auto repo = interpreter->getRepository();
|
||||
auto repo = engine->getCmd().getRepository();
|
||||
auto command = repo->get(name);
|
||||
if (command == nullptr) {
|
||||
return 0;
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
#include "logic/LevelController.hpp"
|
||||
#include "util/listutil.hpp"
|
||||
#include "util/platform.hpp"
|
||||
#include "window/Events.hpp"
|
||||
#include "world/Level.hpp"
|
||||
#include "world/generator/WorldGenerator.hpp"
|
||||
|
||||
|
||||
@@ -80,8 +80,9 @@ static int l_container_add(lua::State* L) {
|
||||
}
|
||||
auto xmlsrc = lua::require_string(L, 2);
|
||||
try {
|
||||
auto subnode =
|
||||
guiutil::create(xmlsrc, docnode.document->getEnvironment());
|
||||
auto subnode = guiutil::create(
|
||||
engine->getGUI(), xmlsrc, docnode.document->getEnvironment()
|
||||
);
|
||||
node->add(subnode);
|
||||
UINode::getIndices(subnode, docnode.document->getMapWriteable());
|
||||
} catch (const std::exception& err) {
|
||||
@@ -93,7 +94,7 @@ static int l_container_add(lua::State* L) {
|
||||
static int l_node_destruct(lua::State* L) {
|
||||
auto docnode = get_document_node(L);
|
||||
auto node = docnode.node;
|
||||
engine->getGUI()->postRunnable([node]() {
|
||||
engine->getGUI().postRunnable([node]() {
|
||||
auto parent = node->getParent();
|
||||
if (auto container = dynamic_cast<Container*>(parent)) {
|
||||
container->remove(node.get());
|
||||
@@ -651,7 +652,7 @@ static void p_set_focused(
|
||||
const std::shared_ptr<UINode>& node, lua::State* L, int idx
|
||||
) {
|
||||
if (lua::toboolean(L, idx) && !node->isFocused()) {
|
||||
engine->getGUI()->setFocus(node);
|
||||
engine->getGUI().setFocus(node);
|
||||
} else if (node->isFocused()) {
|
||||
node->defocus();
|
||||
}
|
||||
@@ -782,7 +783,7 @@ static int l_gui_get_locales_info(lua::State* L) {
|
||||
}
|
||||
|
||||
static int l_gui_getviewport(lua::State* L) {
|
||||
return lua::pushvec2(L, engine->getGUI()->getContainer()->getSize());
|
||||
return lua::pushvec2(L, engine->getGUI().getContainer()->getSize());
|
||||
}
|
||||
|
||||
static int l_gui_clear_markup(lua::State* L) {
|
||||
@@ -845,6 +846,7 @@ static int l_gui_load_document(lua::State* L) {
|
||||
auto args = lua::tovalue(L, 3);
|
||||
|
||||
auto documentPtr = UiDocument::read(
|
||||
engine->getGUI(),
|
||||
scripting::get_root_environment(),
|
||||
alias,
|
||||
filename,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
static int l_get(lua::State* L) {
|
||||
static int l_get(lua::State* L, network::Network& network) {
|
||||
std::string url(lua::require_lstring(L, 1));
|
||||
|
||||
lua::pushvalue(L, 2);
|
||||
auto onResponse = lua::create_lambda_nothrow(L);
|
||||
|
||||
engine->getNetwork().get(url, [onResponse](std::vector<char> bytes) {
|
||||
network.get(url, [onResponse](std::vector<char> bytes) {
|
||||
engine->postRunnable([=]() {
|
||||
onResponse({std::string(bytes.data(), bytes.size())});
|
||||
});
|
||||
@@ -20,13 +20,13 @@ static int l_get(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_get_binary(lua::State* L) {
|
||||
static int l_get_binary(lua::State* L, network::Network& network) {
|
||||
std::string url(lua::require_lstring(L, 1));
|
||||
|
||||
lua::pushvalue(L, 2);
|
||||
auto onResponse = lua::create_lambda_nothrow(L);
|
||||
|
||||
engine->getNetwork().get(url, [onResponse](std::vector<char> bytes) {
|
||||
network.get(url, [onResponse](std::vector<char> bytes) {
|
||||
auto buffer = std::make_shared<util::Buffer<ubyte>>(
|
||||
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
|
||||
);
|
||||
@@ -37,7 +37,7 @@ static int l_get_binary(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_post(lua::State* L) {
|
||||
static int l_post(lua::State* L, network::Network& network) {
|
||||
std::string url(lua::require_lstring(L, 1));
|
||||
auto data = lua::tovalue(L, 2);
|
||||
|
||||
@@ -61,12 +61,12 @@ static int l_post(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_connect(lua::State* L) {
|
||||
static int l_connect(lua::State* L, network::Network& network) {
|
||||
std::string address = lua::require_string(L, 1);
|
||||
int port = lua::tointeger(L, 2);
|
||||
lua::pushvalue(L, 3);
|
||||
auto callback = lua::create_lambda_nothrow(L);
|
||||
u64id_t id = engine->getNetwork().connect(address, port, [callback](u64id_t id) {
|
||||
u64id_t id = network.connect(address, port, [callback](u64id_t id) {
|
||||
engine->postRunnable([=]() {
|
||||
callback({id});
|
||||
});
|
||||
@@ -75,25 +75,25 @@ static int l_connect(lua::State* L) {
|
||||
}
|
||||
|
||||
|
||||
static int l_close(lua::State* L) {
|
||||
static int l_close(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto connection = engine->getNetwork().getConnection(id)) {
|
||||
if (auto connection = network.getConnection(id)) {
|
||||
connection->close(true);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_closeserver(lua::State* L) {
|
||||
static int l_closeserver(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto server = engine->getNetwork().getServer(id)) {
|
||||
if (auto server = network.getServer(id)) {
|
||||
server->close();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_send(lua::State* L) {
|
||||
static int l_send(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
auto connection = engine->getNetwork().getConnection(id);
|
||||
auto connection = network.getConnection(id);
|
||||
if (connection == nullptr ||
|
||||
connection->getState() == network::ConnectionState::CLOSED) {
|
||||
return 0;
|
||||
@@ -120,7 +120,7 @@ static int l_send(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_recv(lua::State* L) {
|
||||
static int l_recv(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
int length = lua::tointeger(L, 2);
|
||||
auto connection = engine->getNetwork().getConnection(id);
|
||||
@@ -149,19 +149,19 @@ static int l_recv(lua::State* L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_available(lua::State* L) {
|
||||
static int l_available(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto connection = engine->getNetwork().getConnection(id)) {
|
||||
if (auto connection = network.getConnection(id)) {
|
||||
return lua::pushinteger(L, connection->available());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_open(lua::State* L) {
|
||||
static int l_open(lua::State* L, network::Network& network) {
|
||||
int port = lua::tointeger(L, 1);
|
||||
lua::pushvalue(L, 2);
|
||||
auto callback = lua::create_lambda_nothrow(L);
|
||||
u64id_t id = engine->getNetwork().openServer(port, [callback](u64id_t id) {
|
||||
u64id_t id = network.openServer(port, [callback](u64id_t id) {
|
||||
engine->postRunnable([=]() {
|
||||
callback({id});
|
||||
});
|
||||
@@ -169,9 +169,9 @@ static int l_open(lua::State* L) {
|
||||
return lua::pushinteger(L, id);
|
||||
}
|
||||
|
||||
static int l_is_alive(lua::State* L) {
|
||||
static int l_is_alive(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto connection = engine->getNetwork().getConnection(id)) {
|
||||
if (auto connection = network.getConnection(id)) {
|
||||
return lua::pushboolean(
|
||||
L,
|
||||
connection->getState() != network::ConnectionState::CLOSED ||
|
||||
@@ -181,9 +181,9 @@ static int l_is_alive(lua::State* L) {
|
||||
return lua::pushboolean(L, false);
|
||||
}
|
||||
|
||||
static int l_is_connected(lua::State* L) {
|
||||
static int l_is_connected(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto connection = engine->getNetwork().getConnection(id)) {
|
||||
if (auto connection = network.getConnection(id)) {
|
||||
return lua::pushboolean(
|
||||
L, connection->getState() == network::ConnectionState::CONNECTED
|
||||
);
|
||||
@@ -191,9 +191,9 @@ static int l_is_connected(lua::State* L) {
|
||||
return lua::pushboolean(L, false);
|
||||
}
|
||||
|
||||
static int l_get_address(lua::State* L) {
|
||||
static int l_get_address(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto connection = engine->getNetwork().getConnection(id)) {
|
||||
if (auto connection = network.getConnection(id)) {
|
||||
lua::pushstring(L, connection->getAddress());
|
||||
lua::pushinteger(L, connection->getPort());
|
||||
return 2;
|
||||
@@ -201,47 +201,65 @@ static int l_get_address(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_is_serveropen(lua::State* L) {
|
||||
static int l_is_serveropen(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto server = engine->getNetwork().getServer(id)) {
|
||||
if (auto server = network.getServer(id)) {
|
||||
return lua::pushboolean(L, server->isOpen());
|
||||
}
|
||||
return lua::pushboolean(L, false);
|
||||
}
|
||||
|
||||
static int l_get_serverport(lua::State* L) {
|
||||
static int l_get_serverport(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto server = engine->getNetwork().getServer(id)) {
|
||||
if (auto server = network.getServer(id)) {
|
||||
return lua::pushinteger(L, server->getPort());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_get_total_upload(lua::State* L) {
|
||||
return lua::pushinteger(L, engine->getNetwork().getTotalUpload());
|
||||
static int l_get_total_upload(lua::State* L, network::Network& network) {
|
||||
return lua::pushinteger(L, network.getTotalUpload());
|
||||
}
|
||||
|
||||
static int l_get_total_download(lua::State* L) {
|
||||
return lua::pushinteger(L, engine->getNetwork().getTotalDownload());
|
||||
static int l_get_total_download(lua::State* L, network::Network& network) {
|
||||
return lua::pushinteger(L, network.getTotalDownload());
|
||||
}
|
||||
|
||||
template <int(*func)(lua::State*, network::Network&)>
|
||||
int wrap(lua_State* L) {
|
||||
int result = 0;
|
||||
try {
|
||||
result = func(L, engine->getNetwork());
|
||||
}
|
||||
// transform exception with description into lua_error
|
||||
catch (std::exception& e) {
|
||||
luaL_error(L, e.what());
|
||||
}
|
||||
// Rethrow any other exception (lua error for example)
|
||||
catch (...) {
|
||||
throw;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
const luaL_Reg networklib[] = {
|
||||
{"get", lua::wrap<l_get>},
|
||||
{"get_binary", lua::wrap<l_get_binary>},
|
||||
{"post", lua::wrap<l_post>},
|
||||
{"get_total_upload", lua::wrap<l_get_total_upload>},
|
||||
{"get_total_download", lua::wrap<l_get_total_download>},
|
||||
{"__open", lua::wrap<l_open>},
|
||||
{"__closeserver", lua::wrap<l_closeserver>},
|
||||
{"__connect", lua::wrap<l_connect>},
|
||||
{"__close", lua::wrap<l_close>},
|
||||
{"__send", lua::wrap<l_send>},
|
||||
{"__recv", lua::wrap<l_recv>},
|
||||
{"__available", lua::wrap<l_available>},
|
||||
{"__is_alive", lua::wrap<l_is_alive>},
|
||||
{"__is_connected", lua::wrap<l_is_connected>},
|
||||
{"__get_address", lua::wrap<l_get_address>},
|
||||
{"__is_serveropen", lua::wrap<l_is_serveropen>},
|
||||
{"__get_serverport", lua::wrap<l_get_serverport>},
|
||||
{"get", wrap<l_get>},
|
||||
{"get_binary", wrap<l_get_binary>},
|
||||
{"post", wrap<l_post>},
|
||||
{"get_total_upload", wrap<l_get_total_upload>},
|
||||
{"get_total_download", wrap<l_get_total_download>},
|
||||
{"__open", wrap<l_open>},
|
||||
{"__closeserver", wrap<l_closeserver>},
|
||||
{"__connect", wrap<l_connect>},
|
||||
{"__close", wrap<l_close>},
|
||||
{"__send", wrap<l_send>},
|
||||
{"__recv", wrap<l_recv>},
|
||||
{"__available", wrap<l_available>},
|
||||
{"__is_alive", wrap<l_is_alive>},
|
||||
{"__is_connected", wrap<l_is_connected>},
|
||||
{"__get_address", wrap<l_get_address>},
|
||||
{"__is_serveropen", wrap<l_is_serveropen>},
|
||||
{"__get_serverport", wrap<l_get_serverport>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@@ -259,7 +259,7 @@ static int l_pack_request_writeable(lua::State* L) {
|
||||
util::replaceAll(str, L"%{0}", util::str2wstr_utf8(packid));
|
||||
guiutil::confirm(*engine, str, [packid, handler]() {
|
||||
handler({engine->getPaths().createWriteablePackDevice(packid)});
|
||||
engine->getGUI()->getMenu()->reset();
|
||||
engine->getGUI().getMenu()->reset();
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user