test worlds panel with XML + lua

This commit is contained in:
MihailRis 2024-03-22 10:40:28 +03:00
parent 71f4dff32f
commit 9a817a86ff
10 changed files with 93 additions and 49 deletions

View File

@ -0,0 +1,15 @@
function on_open()
local worlds = core.get_worlds_list()
for _, name in ipairs(worlds) do
document.worlds:add(
"<container "..
"size='380,46' "..
"color='#0F1E2DB2' "..
"hover-color='#162B3399' "..
"onclick='core.open_world(\""..name.."\")'"..
">"..
"<label pos='8,8'>"..name.."</label>"..
"</container>"
)
end
end

View File

@ -58,13 +58,13 @@ void UiDocument::collect(uinodes_map& map, std::shared_ptr<gui::UINode> node) {
}
}
std::unique_ptr<UiDocument> UiDocument::read(int penv, std::string namesp, fs::path file) {
std::unique_ptr<UiDocument> 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<scripting::Environment>(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> 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<UiDocument>(namesp, script, view, std::move(env));
return std::make_unique<UiDocument>(name, script, view, std::move(env));
}
std::shared_ptr<gui::UINode> UiDocument::readElement(fs::path file) {

View File

@ -47,7 +47,7 @@ public:
/* Collect map of all uinodes having identifiers */
static void collect(uinodes_map& map, std::shared_ptr<gui::UINode> node);
static std::unique_ptr<UiDocument> read(int env, std::string namesp, fs::path file);
static std::unique_ptr<UiDocument> read(int env, std::string name, fs::path file);
static std::shared_ptr<gui::UINode> readElement(fs::path file);
};

View File

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

View File

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

View File

@ -63,6 +63,7 @@ namespace gui {
std::shared_ptr<Menu> menu;
std::queue<runnable> postRunnables;
void actMouse(float delta);
void actFocused();
public:
GUI();
~GUI();

View File

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

View File

@ -192,10 +192,15 @@ void Panel::cropToContent() {
}
}
void Panel::add(std::shared_ptr<UINode> node) {
Container::add(node);
void Panel::fullRefresh() {
refresh();
cropToContent();
Container::fullRefresh();
}
void Panel::add(std::shared_ptr<UINode> 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<UINode> panel) {
pages[name] = Page{panel};
pages[name] = Page{name, panel};
}
void Menu::addSupplier(std::string name, supplier<std::shared_ptr<UINode>> 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<std::string>();
pageStack = std::stack<Page>();
}
void Menu::reset() {
clearHistory();
if (current.panel) {
curname = "";
Container::remove(current.panel);
current = Page{nullptr};
current = Page{"", nullptr};
}
}

View File

@ -76,6 +76,7 @@ namespace gui {
virtual void add(std::shared_ptr<UINode> 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<UINode> panel = nullptr;
~Page() {
@ -95,9 +97,8 @@ namespace gui {
class Menu : public Container {
protected:
std::unordered_map<std::string, Page> pages;
std::stack<std::string> pageStack;
std::stack<Page> pageStack;
Page current;
std::string curname = "";
std::unordered_map<std::string, supplier<std::shared_ptr<UINode>>> 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<UINode> 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_

View File

@ -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<gui::Container*>(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;
}