From bb135d33b5ab8dde0d71342a323d64424befc357 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 2 Jun 2026 23:57:22 +0300 Subject: [PATCH] add on_destroy layout event --- src/frontend/UiDocument.cpp | 16 ++++++++++++++++ src/frontend/UiDocument.hpp | 3 +++ src/logic/scripting/scripting.cpp | 18 ++++++++++++++++++ src/logic/scripting/scripting.hpp | 7 +++++-- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/frontend/UiDocument.cpp b/src/frontend/UiDocument.cpp index 7e0c9bfc6..d0a158baf 100644 --- a/src/frontend/UiDocument.cpp +++ b/src/frontend/UiDocument.cpp @@ -7,6 +7,9 @@ #include "graphics/ui/elements/InventoryView.hpp" #include "graphics/ui/gui_xml.hpp" #include "logic/scripting/scripting.hpp" +#include "debug/Logger.hpp" + +static debug::Logger logger("ui-document"); UiDocument::UiDocument( std::string id, @@ -17,6 +20,19 @@ UiDocument::UiDocument( rebuildIndices(); } +UiDocument::~UiDocument() { + try { + scripting::on_ui_destroy(this); + } catch (const std::exception& err) { + logger.error() << "an error occurred on calling on_destroy event for document '" + << id << "': " << err.what(); + } catch (...) { + logger.error() << "unknown exception caught on calling on_destroy " + "event for document '" + << id << "'"; + } +} + void UiDocument::rebuildIndices() { map.clear(); gui::UINode::getIndices(root, map); diff --git a/src/frontend/UiDocument.hpp b/src/frontend/UiDocument.hpp index 5b40ca8ca..b2f0c9761 100644 --- a/src/frontend/UiDocument.hpp +++ b/src/frontend/UiDocument.hpp @@ -17,6 +17,7 @@ struct UiDocScript { bool onopen : 1; bool onprogress : 1; bool onclose : 1; + bool ondestroy : 1; }; using UINodesMap = std::unordered_map>; @@ -35,6 +36,8 @@ public: scriptenv env ); + ~UiDocument(); + void rebuildIndices(); void pushIndices(const std::shared_ptr& node); diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 37597b058..67f8989c8 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -594,6 +594,9 @@ bool scripting::on_item_break_block( void scripting::on_ui_open( UiDocument* layout, std::vector args ) { + if (!layout->getScript().onopen) { + return; + } auto argsptr = std::make_shared>(std::move(args)); std::string name = layout->getId() + ".open"; @@ -608,6 +611,9 @@ void scripting::on_ui_open( void scripting::on_ui_progress( UiDocument* layout, int workDone, int workTotal ) { + if (!layout->getScript().onprogress){ + return; + } std::string name = layout->getId() + ".progress"; lua::emit_event(lua::get_main_state(), name, [=](auto L) { lua::pushinteger(L, workDone); @@ -617,12 +623,23 @@ void scripting::on_ui_progress( } void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) { + if (!layout->getScript().onclose) { + return; + } 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) { + if (!layout->getScript().ondestroy) { + return; + } + std::string name = layout->getId() + ".destroy"; + lua::emit_event(lua::get_main_state(), name, [](auto L) { return 0; }); +} + void scripting::on_scripts_loading() { auto L = lua::get_main_state(); @@ -810,6 +827,7 @@ void scripting::load_layout_script( script.onprogress = register_event(env, "on_progress", prefix + ".progress"); script.onclose = register_event(env, "on_close", prefix + ".close"); + script.ondestroy = register_event(env, "on_destroy", prefix + ".destroy"); } void scripting::close() { diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index 9d29528b8..68ac57486 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -152,14 +152,17 @@ namespace scripting { void on_attacked(const Entity& entity, Player* player, entityid_t attacker); void on_entity_used(const Entity& entity, Player* player); - /// @brief Called on UI view show + /// @brief Called on UI document show void on_ui_open(UiDocument* layout, std::vector args); void on_ui_progress(UiDocument* layout, int workDone, int totalWork); - /// @brief Called on UI view close + /// @brief Called on UI document close void on_ui_close(UiDocument* layout, Inventory* inventory); + /// @brief Called on UI document destroy + void on_ui_destroy(UiDocument* layout); + /// @brief Called on Content loading void on_scripts_loading();