From 132016d33f95557b5ac8386c090dd2052c0a6acd Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 22 Apr 2024 22:17:06 +0300 Subject: [PATCH] missing content report moved to xml --- res/layouts/reports/missing_content.xml | 7 ++++ res/layouts/reports/missing_content.xml.lua | 5 +++ res/layouts/templates/content_entry.xml | 4 +++ src/frontend/hud.cpp | 16 ++++++--- src/frontend/menu.cpp | 15 +++++++- src/frontend/menu.hpp | 6 ++++ src/logic/EngineController.cpp | 38 ++++++--------------- src/logic/scripting/scripting.cpp | 15 +++++--- src/logic/scripting/scripting.h | 16 ++++++--- 9 files changed, 78 insertions(+), 44 deletions(-) create mode 100644 res/layouts/reports/missing_content.xml create mode 100644 res/layouts/reports/missing_content.xml.lua create mode 100644 res/layouts/templates/content_entry.xml diff --git a/res/layouts/reports/missing_content.xml b/res/layouts/reports/missing_content.xml new file mode 100644 index 00000000..cd8cacb4 --- /dev/null +++ b/res/layouts/reports/missing_content.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/res/layouts/reports/missing_content.xml.lua b/res/layouts/reports/missing_content.xml.lua new file mode 100644 index 00000000..8ea78a1c --- /dev/null +++ b/res/layouts/reports/missing_content.xml.lua @@ -0,0 +1,5 @@ +function on_open(report) + for i, entry in ipairs(report.content) do + document.content_panel:add(gui.template("content_entry", entry)) + end +end diff --git a/res/layouts/templates/content_entry.xml b/res/layouts/templates/content_entry.xml new file mode 100644 index 00000000..cfb9ce68 --- /dev/null +++ b/res/layouts/templates/content_entry.xml @@ -0,0 +1,4 @@ + + + + diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index bfbd02d1..bfa31b86 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -377,22 +377,28 @@ void Hud::closeInventory() { } void Hud::add(HudElement element) { + using dynamic::Value; + gui->add(element.getNode()); auto invview = std::dynamic_pointer_cast(element.getNode()); auto document = element.getDocument(); if (document) { + auto inventory = invview ? invview->getInventory() : nullptr; + std::vector> args; + args.push_back(Value::of(inventory ? inventory.get()->getId() : 0)); + for (int i = 0; i < 3; i++) { + args.push_back(Value::of(blockPos[i])); + } + if (invview) { - auto inventory = invview->getInventory(); scripting::on_ui_open( element.getDocument(), - inventory.get(), - blockPos + std::move(args) ); } else { scripting::on_ui_open( element.getDocument(), - nullptr, - blockPos + std::move(args) ); } } diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index 639a610b..b5dea9ca 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -5,6 +5,7 @@ #include "../delegates.h" #include "../engine.h" +#include "../data/dynamic.h" #include "../interfaces/Task.h" #include "../files/engine_paths.h" #include "../graphics/ui/elements/Label.hpp" @@ -43,11 +44,23 @@ void menus::create_menus(Engine* engine) { auto document = UiDocument::read(scripting::get_root_environment(), fullname, file).release(); engine->getAssets()->store(document, fullname); - scripting::on_ui_open(document, nullptr, glm::ivec3()); + scripting::on_ui_open(document, {}); return document->getRoot(); }); } +void menus::show(Engine* engine, const std::string& name, std::vector> args) { + auto menu = engine->getGUI()->getMenu(); + auto file = engine->getResPaths()->find("layouts/"+name+".xml"); + auto fullname = "core:layouts/"+name; + + auto document = UiDocument::read(scripting::get_root_environment(), fullname, file).release(); + engine->getAssets()->store(document, fullname); + scripting::on_ui_open(document, std::move(args)); + menu->addPage(name, document->getRoot()); + menu->setPage(name); +} + void menus::show_process_panel(Engine* engine, std::shared_ptr task, std::wstring text) { auto menu = engine->getGUI()->getMenu(); auto panel = std::dynamic_pointer_cast(guiutil::create( diff --git a/src/frontend/menu.hpp b/src/frontend/menu.hpp index 5894fd7b..3f22b0da 100644 --- a/src/frontend/menu.hpp +++ b/src/frontend/menu.hpp @@ -2,15 +2,21 @@ #define FRONTEND_MENU_MENU_HPP_ #include +#include #include class Task; class Engine; +namespace dynamic { + class Value; +} + namespace menus { /// @brief Create development version label at the top-right screen corner void create_version_label(Engine* engine); void create_menus(Engine* engine); + void show(Engine* engine, const std::string& name, std::vector> args); void show_process_panel(Engine* engine, std::shared_ptr task, std::wstring text=L""); } diff --git a/src/logic/EngineController.cpp b/src/logic/EngineController.cpp index 277305ea..809f6b1d 100644 --- a/src/logic/EngineController.cpp +++ b/src/logic/EngineController.cpp @@ -74,36 +74,18 @@ static void show_content_missing( const Content* content, std::shared_ptr lut ) { - auto* gui = engine->getGUI(); - auto menu = gui->getMenu(); - auto panel = std::dynamic_pointer_cast(guiutil::create( - "" - "" - "" - )); - auto subpanel = std::dynamic_pointer_cast(guiutil::create( - "" - "" - )); - panel->add(subpanel); - + using namespace dynamic; + auto root = std::make_unique(); + auto& contentEntries = root->putList("content"); for (auto& entry : lut->getMissingContent()) { - std::string contentname = contenttype_name(entry.type); - subpanel->add(guiutil::create( - "" - "" - "" - "" - )); + std::string contentName = contenttype_name(entry.type); + auto& contentEntry = contentEntries.putMap(); + contentEntry.put("type", contentName); + contentEntry.put("name", entry.name); } - - panel->add(std::make_shared( - langs::get(L"Back to Main Menu", L"menu"), glm::vec4(8.0f), [=](auto){ - menu->back(); - } - )); - menu->addPage("missing-content", panel); - menu->setPage("missing-content"); + std::vector> args; + args.emplace_back(std::make_unique(valtype::map, root.release())); + menus::show(engine, "reports/missing_content", std::move(args)); } void EngineController::openWorld(std::string name, bool confirmConvert) { diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index b3b358e0..95951ced 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -226,12 +226,17 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x, }); } -void scripting::on_ui_open(UiDocument* layout, Inventory* inventory, glm::ivec3 blockcoord) { +void scripting::on_ui_open( + UiDocument* layout, + std::vector> args +) { + auto argsptr = std::make_shared>>(std::move(args)); std::string name = layout->getId() + ".open"; - emit_event(name, [inventory, blockcoord] (lua::LuaState* state) { - state->pushinteger(inventory == nullptr ? 0 : inventory->getId()); - state->pushivec3(blockcoord.x, blockcoord.y, blockcoord.z); - return 4; + emit_event(name, [=] (lua::LuaState* state) { + for (const auto& value : *argsptr) { + state->pushvalue(*value); + } + return argsptr->size(); }); } diff --git a/src/logic/scripting/scripting.h b/src/logic/scripting/scripting.h index cc3e6e4d..ec067210 100644 --- a/src/logic/scripting/scripting.h +++ b/src/logic/scripting/scripting.h @@ -1,13 +1,16 @@ -#include -#include -#include - +#include "../../data/dynamic.h" #include "../../typedefs.h" #include "../../delegates.h" #include "lua/LuaState.h" #include "scripting_functional.h" +#include +#include +#include +#include +#include + namespace fs = std::filesystem; class Engine; @@ -71,7 +74,10 @@ namespace scripting { bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z); /// @brief Called on UI view show - void on_ui_open(UiDocument* layout, Inventory* inventory, glm::ivec3 blockcoord); + void on_ui_open( + UiDocument* layout, + std::vector> args + ); /// @brief Called on UI view close void on_ui_close(UiDocument* layout, Inventory* inventory);