From 9a817a86ff578646f8fa25735ce25a0372558fab Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 22 Mar 2024 10:40:28 +0300 Subject: [PATCH] test worlds panel with XML + lua --- res/layouts/pages/main.xml.lua | 15 +++++++++ src/frontend/UiDocument.cpp | 8 ++--- src/frontend/UiDocument.h | 2 +- src/frontend/menu/menu.cpp | 19 +++++++++-- src/graphics/ui/GUI.cpp | 44 +++++++++++++------------ src/graphics/ui/GUI.h | 1 + src/graphics/ui/elements/UINode.h | 5 +++ src/graphics/ui/elements/containers.cpp | 31 +++++++++-------- src/graphics/ui/elements/containers.h | 9 +++-- src/logic/scripting/lua/libgui.cpp | 8 +++-- 10 files changed, 93 insertions(+), 49 deletions(-) create mode 100644 res/layouts/pages/main.xml.lua diff --git a/res/layouts/pages/main.xml.lua b/res/layouts/pages/main.xml.lua new file mode 100644 index 00000000..f6ec94f6 --- /dev/null +++ b/res/layouts/pages/main.xml.lua @@ -0,0 +1,15 @@ +function on_open() + local worlds = core.get_worlds_list() + for _, name in ipairs(worlds) do + document.worlds:add( + "".. + "".. + "" + ) + end +end diff --git a/src/frontend/UiDocument.cpp b/src/frontend/UiDocument.cpp index 0d78e64a..d68ff06c 100644 --- a/src/frontend/UiDocument.cpp +++ b/src/frontend/UiDocument.cpp @@ -58,13 +58,13 @@ void UiDocument::collect(uinodes_map& map, std::shared_ptr node) { } } -std::unique_ptr UiDocument::read(int penv, std::string namesp, fs::path file) { +std::unique_ptr UiDocument::read(int penv, std::string name, fs::path file) { const std::string text = files::read_string(file); auto xmldoc = xml::parse(file.u8string(), text); auto env = penv == -1 ? std::make_unique(0) - : scripting::create_doc_environment(penv, namesp); + : scripting::create_doc_environment(penv, name); gui::UiXmlReader reader(*env); InventoryView::createReaders(reader); @@ -75,9 +75,9 @@ std::unique_ptr UiDocument::read(int penv, std::string namesp, fs::p uidocscript script {}; auto scriptFile = fs::path(file.u8string()+".lua"); if (fs::is_regular_file(scriptFile)) { - scripting::load_layout_script(env->getId(), namesp, scriptFile, script); + scripting::load_layout_script(env->getId(), name, scriptFile, script); } - return std::make_unique(namesp, script, view, std::move(env)); + return std::make_unique(name, script, view, std::move(env)); } std::shared_ptr UiDocument::readElement(fs::path file) { diff --git a/src/frontend/UiDocument.h b/src/frontend/UiDocument.h index 943008cf..8583f34d 100644 --- a/src/frontend/UiDocument.h +++ b/src/frontend/UiDocument.h @@ -47,7 +47,7 @@ public: /* Collect map of all uinodes having identifiers */ static void collect(uinodes_map& map, std::shared_ptr node); - static std::unique_ptr read(int env, std::string namesp, fs::path file); + static std::unique_ptr read(int env, std::string name, fs::path file); static std::shared_ptr readElement(fs::path file); }; diff --git a/src/frontend/menu/menu.cpp b/src/frontend/menu/menu.cpp index a4e86034..9ad58c13 100644 --- a/src/frontend/menu/menu.cpp +++ b/src/frontend/menu/menu.cpp @@ -14,6 +14,7 @@ #include "../../graphics/ui/elements/controls.h" #include "../screens.h" #include "../UiDocument.h" +#include "../../logic/scripting/scripting.h" #include "../../coders/png.h" #include "../../util/stringutil.h" @@ -289,19 +290,33 @@ void create_main_menu_panel(Engine* engine) { )); } +static void add_page_loader(Engine* engine, const std::string& name) { + auto menu = engine->getGUI()->getMenu(); + auto file = engine->getResPaths()->find("layouts/pages/"+name+".xml"); + auto fullname = "core:pages/"+name; + menu->addSupplier(name, [=]() { + auto document = UiDocument::read(0, fullname, file).release(); + engine->getAssets()->store(document, fullname); + scripting::on_ui_open(document, nullptr, glm::ivec3()); + return document->getRoot(); + }); +} + void menus::create_menus(Engine* engine) { menus::generatorID = WorldGenerators::getDefaultGeneratorID(); create_new_world_panel(engine); create_settings_panel(engine); create_languages_panel(engine); - create_main_menu_panel(engine); + //create_main_menu_panel(engine); create_world_generators_panel(engine); + add_page_loader(engine, "main"); load_page(engine, "404"); } void menus::refresh_menus(Engine* engine) { - create_main_menu_panel(engine); + //create_main_menu_panel(engine); create_new_world_panel(engine); create_world_generators_panel(engine); + add_page_loader(engine, "main"); load_page(engine, "404"); } diff --git a/src/graphics/ui/GUI.cpp b/src/graphics/ui/GUI.cpp index 7bb4c75c..1e09c817 100644 --- a/src/graphics/ui/GUI.cpp +++ b/src/graphics/ui/GUI.cpp @@ -81,6 +81,28 @@ void GUI::actMouse(float delta) { } } +void GUI::actFocused() { + if (Events::jpressed(keycode::ESCAPE)) { + focus->defocus(); + focus = nullptr; + return; + } + for (auto codepoint : Events::codepoints) { + focus->typed(codepoint); + } + for (auto key : Events::pressedKeys) { + focus->keyPressed(key); + } + + if (!Events::_cursor_locked) { + if (Events::clicked(mousecode::BUTTON_1) && + (Events::jclicked(mousecode::BUTTON_1) || Events::delta.x || Events::delta.y)) + { + focus->mouseMove(this, Events::cursor.x, Events::cursor.y); + } + } +} + /// @brief Processing user input and UI logic /// @param delta delta time void GUI::act(float delta) { @@ -99,27 +121,7 @@ void GUI::act(float delta) { } if (focus) { - if (Events::jpressed(keycode::ESCAPE)) { - focus->defocus(); - focus = nullptr; - } else { - for (auto codepoint : Events::codepoints) { - focus->typed(codepoint); - } - for (auto key : Events::pressedKeys) { - focus->keyPressed(key); - } - - if (!Events::_cursor_locked) { - if (Events::clicked(mousecode::BUTTON_1)) { - if (Events::jclicked(mousecode::BUTTON_1) || - Events::delta.x || Events::delta.y) - { - focus->mouseMove(this, Events::cursor.x, Events::cursor.y); - } - } - } - } + actFocused(); } if (focus && !focus->isFocused()) { focus = nullptr; diff --git a/src/graphics/ui/GUI.h b/src/graphics/ui/GUI.h index 9aeda7c6..2bfd9e6a 100644 --- a/src/graphics/ui/GUI.h +++ b/src/graphics/ui/GUI.h @@ -63,6 +63,7 @@ namespace gui { std::shared_ptr menu; std::queue postRunnables; void actMouse(float delta); + void actFocused(); public: GUI(); ~GUI(); diff --git a/src/graphics/ui/elements/UINode.h b/src/graphics/ui/elements/UINode.h index e0bce7c4..5f53d0c4 100644 --- a/src/graphics/ui/elements/UINode.h +++ b/src/graphics/ui/elements/UINode.h @@ -176,6 +176,11 @@ namespace gui { virtual void setMinSize(glm::vec2 size); /* Called in containers when new element added */ virtual void refresh() {}; + virtual void fullRefresh() { + if (parent) { + parent->fullRefresh(); + } + }; virtual void lock(); virtual vec2supplier getPositionFunc() const; diff --git a/src/graphics/ui/elements/containers.cpp b/src/graphics/ui/elements/containers.cpp index f0a6ff52..9fd753fa 100644 --- a/src/graphics/ui/elements/containers.cpp +++ b/src/graphics/ui/elements/containers.cpp @@ -192,10 +192,15 @@ void Panel::cropToContent() { } } -void Panel::add(std::shared_ptr node) { - Container::add(node); +void Panel::fullRefresh() { refresh(); cropToContent(); + Container::fullRefresh(); +} + +void Panel::add(std::shared_ptr node) { + Container::add(node); + fullRefresh(); } void Panel::refresh() { @@ -255,7 +260,7 @@ bool Menu::has(const std::string& name) { } void Menu::addPage(std::string name, std::shared_ptr panel) { - pages[name] = Page{panel}; + pages[name] = Page{name, panel}; } void Menu::addSupplier(std::string name, supplier> pageSupplier) { @@ -276,13 +281,16 @@ void Menu::setPage(std::string name, bool history) { } else { page = found->second; } + setPage(page, history); +} + +void Menu::setPage(Page page, bool history) { if (current.panel) { Container::remove(current.panel); } if (history) { - pageStack.push(curname); + pageStack.push(current); } - curname = name; current = page; Container::add(current.panel); setSize(current.panel->getSize()); @@ -291,29 +299,24 @@ void Menu::setPage(std::string name, bool history) { void Menu::back() { if (pageStack.empty()) return; - std::string name = pageStack.top(); + Page page = pageStack.top(); pageStack.pop(); - setPage(name, false); + setPage(page, false); } Page& Menu::getCurrent() { return current; } -const std::string& Menu::getCurrentName() const { - return curname; -} - void Menu::clearHistory() { - pageStack = std::stack(); + pageStack = std::stack(); } void Menu::reset() { clearHistory(); if (current.panel) { - curname = ""; Container::remove(current.panel); - current = Page{nullptr}; + current = Page{"", nullptr}; } } diff --git a/src/graphics/ui/elements/containers.h b/src/graphics/ui/elements/containers.h index 1569e991..cb332cf6 100644 --- a/src/graphics/ui/elements/containers.h +++ b/src/graphics/ui/elements/containers.h @@ -76,6 +76,7 @@ namespace gui { virtual void add(std::shared_ptr node) override; virtual void refresh() override; + virtual void fullRefresh() override; virtual void setMaxLength(int value); int getMaxLength() const; @@ -85,6 +86,7 @@ namespace gui { }; struct Page { + std::string name; std::shared_ptr panel = nullptr; ~Page() { @@ -95,9 +97,8 @@ namespace gui { class Menu : public Container { protected: std::unordered_map pages; - std::stack pageStack; + std::stack pageStack; Page current; - std::string curname = ""; std::unordered_map>> pageSuppliers; public: Menu(); @@ -110,6 +111,7 @@ namespace gui { /// @param name page or page supplier name /// @param history previous page will not be saved in history if false void setPage(std::string name, bool history=true); + void setPage(Page page, bool history=true); void addPage(std::string name, std::shared_ptr panel); /// @brief Add page supplier used if page is not found @@ -128,9 +130,6 @@ namespace gui { /// @brief Get current page Page& getCurrent(); - - /// @brief Get current page name - const std::string& getCurrentName() const; }; } #endif // GRAPHICS_UI_ELEMENTS_CONTAINERS_H_ diff --git a/src/logic/scripting/lua/libgui.cpp b/src/logic/scripting/lua/libgui.cpp index 202ababd..1a8b9afe 100644 --- a/src/logic/scripting/lua/libgui.cpp +++ b/src/logic/scripting/lua/libgui.cpp @@ -139,7 +139,7 @@ static bool getattr(lua_State* L, gui::Menu* menu, const std::string& attr) { if (menu == nullptr) return false; if (attr == "page") { - lua_pushstring(L, menu->getCurrentName().c_str()); + lua_pushstring(L, menu->getCurrent().name.c_str()); return true; } else if (attr == "back") { lua_pushcfunction(L, menu_back); @@ -211,7 +211,11 @@ static bool setattr(lua_State* L, gui::Menu* menu, const std::string& attr) { static int container_add(lua_State* L) { auto node = dynamic_cast(getDocumentNode(L)); auto xmlsrc = lua_tostring(L, 2); - node->add(guiutil::create(xmlsrc)); + try { + node->add(guiutil::create(xmlsrc)); + } catch (const std::exception& err) { + luaL_error(L, err.what()); + } return 0; }