add on_destroy layout event

This commit is contained in:
MihailRis 2026-06-02 23:57:22 +03:00
parent 5930ef8285
commit bb135d33b5
4 changed files with 42 additions and 2 deletions

View file

@ -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);

View file

@ -17,6 +17,7 @@ struct UiDocScript {
bool onopen : 1;
bool onprogress : 1;
bool onclose : 1;
bool ondestroy : 1;
};
using UINodesMap = std::unordered_map<std::string, std::weak_ptr<gui::UINode>>;
@ -35,6 +36,8 @@ public:
scriptenv env
);
~UiDocument();
void rebuildIndices();
void pushIndices(const std::shared_ptr<gui::UINode>& node);

View file

@ -594,6 +594,9 @@ bool scripting::on_item_break_block(
void scripting::on_ui_open(
UiDocument* layout, std::vector<dv::value> args
) {
if (!layout->getScript().onopen) {
return;
}
auto argsptr =
std::make_shared<std::vector<dv::value>>(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() {

View file

@ -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<dv::value> 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();