fix document events & breaking: document events removed from 'events' library temporary

This commit is contained in:
MihailRis 2026-06-03 21:50:36 +03:00
parent 8bccdc0250
commit 089bcbbb64
7 changed files with 50 additions and 55 deletions

View file

@ -22,7 +22,7 @@ UiDocument::UiDocument(
UiDocument::~UiDocument() { UiDocument::~UiDocument() {
try { try {
scripting::on_ui_destroy(this); scripting::on_ui_destroy(*this);
} catch (const std::exception& err) { } catch (const std::exception& err) {
logger.error() << "an error occurred on calling on_destroy event for document '" logger.error() << "an error occurred on calling on_destroy event for document '"
<< id << "': " << err.what(); << id << "': " << err.what();

View file

@ -563,7 +563,7 @@ void Hud::add(const HudElement& element, const dv::value& argsArray) {
args.emplace_back(static_cast<integer_t>(blockPos[i])); args.emplace_back(static_cast<integer_t>(blockPos[i]));
} }
scripting::on_ui_open( scripting::on_ui_open(
element.getDocument(), *element.getDocument(),
std::move(args) std::move(args)
); );
} }
@ -578,7 +578,7 @@ void Hud::onRemove(const HudElement& element) {
if (invview) { if (invview) {
inventory = invview->getInventory().get(); inventory = invview->getInventory().get();
} }
scripting::on_ui_close(document, inventory); scripting::on_ui_close(*document, inventory);
if (invview) { if (invview) {
invview->unbind(); invview->unbind();
} }

View file

@ -86,7 +86,7 @@ UiDocument* menus::show(
); );
auto document = documentPtr.get(); auto document = documentPtr.get();
engine.getAssets()->store(std::move(documentPtr), fullname); engine.getAssets()->store(std::move(documentPtr), fullname);
scripting::on_ui_open(document, std::move(args)); scripting::on_ui_open(*document, std::move(args));
menu->addPage(name, document->getRoot()); menu->addPage(name, document->getRoot());
menu->setPage(name); menu->setPage(name);
return document; return document;
@ -99,13 +99,13 @@ void menus::show_process_panel(
auto menu = engine.getGUI().getMenu(); auto menu = engine.getGUI().getMenu();
menu->reset(); menu->reset();
auto doc = auto document =
menus::show(engine, "process", {util::wstr2str_utf8(langs::get(text))}); menus::show(engine, "process", {util::wstr2str_utf8(langs::get(text))});
std::dynamic_pointer_cast<Container>(doc->getRoot()) std::dynamic_pointer_cast<Container>(document->getRoot())
->listenInterval(0.01f, [=]() { ->listenInterval(0.01f, [=]() {
task->update(); task->update();
uint tasksDone = task->getWorkDone(); uint tasksDone = task->getWorkDone();
scripting::on_ui_progress(doc, tasksDone, initialWork); scripting::on_ui_progress(*document, tasksDone, initialWork);
}); });
} }

View file

@ -13,7 +13,7 @@ InlineFrame::~InlineFrame() = default;
void InlineFrame::setSrc(const std::string& src) { void InlineFrame::setSrc(const std::string& src) {
this->src = src; this->src = src;
if (document) { if (document) {
scripting::on_ui_close(document.get(), nullptr); scripting::on_ui_close(*document, nullptr);
document = nullptr; document = nullptr;
root = nullptr; root = nullptr;
} }
@ -31,7 +31,7 @@ void InlineFrame::setDocument(const std::shared_ptr<UiDocument>& document) {
root->setSize(size); root->setSize(size);
gui.postRunnable([this]() { gui.postRunnable([this]() {
scripting::on_ui_open(this->document.get(), {}); scripting::on_ui_open(*this->document, {});
}); });
} }

View file

@ -1144,7 +1144,7 @@ static int l_gui_load_document(lua::State* L) {
auto document = documentPtr.get(); auto document = documentPtr.get();
engine->requireAssets().store(std::move(documentPtr), alias); engine->requireAssets().store(std::move(documentPtr), alias);
scripting::on_ui_open(document, {args}); scripting::on_ui_open(*document, {args});
return 0; return 0;
} }

View file

@ -591,53 +591,46 @@ bool scripting::on_item_break_block(
); );
} }
void scripting::on_ui_open( static void call_layout_event(const UiDocument& layout, const std::vector<dv::value>& args, const std::string& eventName) {
UiDocument* layout, std::vector<dv::value> args auto L = lua::get_main_state();
) { lua::pushenv(L, *layout.getEnvironment());
if (!layout->getScript().onopen) { if (lua::getfield(L, eventName)) {
return; for (const auto& arg : args) {
} lua::pushvalue(L, arg);
auto argsptr =
std::make_shared<std::vector<dv::value>>(std::move(args));
std::string name = layout->getId() + ".open";
lua::emit_event(lua::get_main_state(), name, [=](auto L) {
for (const auto& value : *argsptr) {
lua::pushvalue(L, value);
} }
return argsptr->size(); lua::call_nothrow(L, args.size(), 0);
}); }
lua::pop(L);
}
void scripting::on_ui_open(
const UiDocument& layout, std::vector<dv::value> args
) {
if (layout.getScript().onopen) {
call_layout_event(layout, args, "on_open");
}
} }
void scripting::on_ui_progress( void scripting::on_ui_progress(
UiDocument* layout, int workDone, int workTotal const UiDocument& layout, int workDone, int workTotal
) { ) {
if (!layout->getScript().onprogress){ if (layout.getScript().onprogress){
return; call_layout_event(layout, {workDone, workTotal}, "on_progress");
} }
std::string name = layout->getId() + ".progress";
lua::emit_event(lua::get_main_state(), name, [=](auto L) {
lua::pushinteger(L, workDone);
lua::pushinteger(L, workTotal);
return 2;
});
} }
void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) { void scripting::on_ui_close(const UiDocument& layout, Inventory* inventory) {
if (!layout->getScript().onclose) { if (layout.getScript().onclose) {
return; call_layout_event(
layout, {inventory ? inventory->getId() : 0}, "on_close"
);
} }
std::string name = layout->getId() + ".close";
lua::emit_event(lua::get_main_state(), name, [inventory](auto L) {
return lua::pushinteger(L, inventory ? inventory->getId() : 0);
});
} }
void scripting::on_ui_destroy(UiDocument* layout) { void scripting::on_ui_destroy(const UiDocument& layout) {
if (!layout->getScript().ondestroy) { if (layout.getScript().ondestroy) {
return; call_layout_event(layout, {}, "on_destroy");
} }
std::string name = layout->getId() + ".destroy";
lua::emit_event(lua::get_main_state(), name, [](auto L) { return 0; });
} }
void scripting::on_scripts_loading() { void scripting::on_scripts_loading() {
@ -822,12 +815,14 @@ void scripting::load_layout_script(
) { ) {
int env = *senv; int env = *senv;
lua::pop(lua::get_main_state(), load_script(env, "layout", file, fileName)); auto L = lua::get_main_state();
script.onopen = register_event(env, "on_open", prefix + ".open"); lua::pop(L, load_script(env, "layout", file, fileName));
script.onprogress = lua::pushenv(L, env);
register_event(env, "on_progress", prefix + ".progress"); script.onopen = lua::hasfield(L, "on_open");
script.onclose = register_event(env, "on_close", prefix + ".close"); script.onprogress = lua::hasfield(L, "on_progress");
script.ondestroy = register_event(env, "on_destroy", prefix + ".destroy"); script.onclose = lua::hasfield(L, "on_close");
script.ondestroy = lua::hasfield(L, "on_destroy");
lua::pop(L);
} }
void scripting::close() { void scripting::close() {

View file

@ -153,15 +153,15 @@ namespace scripting {
void on_entity_used(const Entity& entity, Player* player); void on_entity_used(const Entity& entity, Player* player);
/// @brief Called on UI document show /// @brief Called on UI document show
void on_ui_open(UiDocument* layout, std::vector<dv::value> args); void on_ui_open(const UiDocument& layout, std::vector<dv::value> args);
void on_ui_progress(UiDocument* layout, int workDone, int totalWork); void on_ui_progress(const UiDocument& layout, int workDone, int totalWork);
/// @brief Called on UI document close /// @brief Called on UI document close
void on_ui_close(UiDocument* layout, Inventory* inventory); void on_ui_close(const UiDocument& layout, Inventory* inventory);
/// @brief Called on UI document destroy /// @brief Called on UI document destroy
void on_ui_destroy(UiDocument* layout); void on_ui_destroy(const UiDocument& layout);
/// @brief Called on Content loading /// @brief Called on Content loading
void on_scripts_loading(); void on_scripts_loading();