scripting: PagesControl

This commit is contained in:
MihailRis 2024-03-20 10:01:07 +03:00
parent 93acc5ce57
commit b3ca4b8efc
8 changed files with 95 additions and 43 deletions

View File

@ -180,6 +180,10 @@ function Document.new(docname)
})
end
_GUI_ROOT = Document.new("core:root")
_MENU = _GUI_ROOT.menu
menu = _MENU
-- Deprecated functions
block_index = block.index
block_name = block.name

View File

@ -1,5 +1,40 @@
#include "engine.h"
#define GLEW_STATIC
#include "assets/Assets.h"
#include "assets/AssetsLoader.h"
#include "audio/audio.h"
#include "coders/GLSLExtension.h"
#include "coders/json.h"
#include "coders/png.h"
#include "content/Content.h"
#include "content/ContentLoader.h"
#include "content/ContentPack.h"
#include "core_defs.h"
#include "files/engine_paths.h"
#include "files/files.h"
#include "frontend/locale/langs.h"
#include "frontend/menu/menu.h"
#include "frontend/screens.h"
#include "frontend/UiDocument.h"
#include "graphics/core/Batch2D.h"
#include "graphics/core/GfxContext.h"
#include "graphics/core/ImageData.h"
#include "graphics/core/Shader.h"
#include "graphics/ui/GUI.h"
#include "graphics/ui/elements/UINode.h"
#include "graphics/ui/elements/containers.h"
#include "logic/scripting/scripting.h"
#include "util/platform.h"
#include "voxels/DefaultWorldGenerator.h"
#include "voxels/FlatWorldGenerator.h"
#include "window/Camera.h"
#include "window/Events.h"
#include "window/input.h"
#include "window/Window.h"
#include "world/WorldGenerators.h"
#include <memory>
#include <iostream>
#include <assert.h>
@ -8,40 +43,6 @@
#include <filesystem>
#include <unordered_set>
#include <functional>
#define GLEW_STATIC
#include "audio/audio.h"
#include "assets/Assets.h"
#include "assets/AssetsLoader.h"
#include "world/WorldGenerators.h"
#include "voxels/DefaultWorldGenerator.h"
#include "voxels/FlatWorldGenerator.h"
#include "window/Window.h"
#include "window/Events.h"
#include "window/Camera.h"
#include "window/input.h"
#include "graphics/core/Batch2D.h"
#include "graphics/core/GfxContext.h"
#include "graphics/core/Shader.h"
#include "graphics/core/ImageData.h"
#include "graphics/ui/GUI.h"
#include "frontend/screens.h"
#include "frontend/menu/menu.h"
#include "util/platform.h"
#include "coders/json.h"
#include "coders/png.h"
#include "coders/GLSLExtension.h"
#include "files/files.h"
#include "files/engine_paths.h"
#include "content/Content.h"
#include "content/ContentPack.h"
#include "content/ContentLoader.h"
#include "frontend/locale/langs.h"
#include "logic/scripting/scripting.h"
#include "core_defs.h"
namespace fs = std::filesystem;
@ -63,8 +64,6 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
audio::create_channel("ui");
auto resdir = paths->getResources();
scripting::initialize(this);
std::cout << "-- loading assets" << std::endl;
std::vector<fs::path> roots {resdir};
@ -92,6 +91,18 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
}
setLanguage(settings.ui.language);
addWorldGenerators();
onAssetsLoaded();
scripting::initialize(this);
}
void Engine::onAssetsLoaded() {
assets->store(new UiDocument(
"core:root",
uidocscript {},
std::dynamic_pointer_cast<gui::UINode>(gui->getContainer()),
nullptr
), "core:root");
}
void Engine::updateTimers() {
@ -236,6 +247,7 @@ void Engine::loadContent() {
}
}
assets->extend(*new_assets.get());
onAssetsLoaded();
}
void Engine::loadWorldContent(const fs::path& folder) {

View File

@ -56,6 +56,8 @@ public:
/// Automatically sets MenuScreen
void mainloop();
/// @brief Called after assets loading when all engine systems are initialized
void onAssetsLoaded();
/// @brief Set screen (scene).
/// nullptr may be used to delete previous screen before creating new one,

View File

@ -22,6 +22,7 @@ GUI::GUI() {
uicamera->flipped = true;
menu = std::make_shared<PagesControl>();
menu->setId("menu");
container->add(menu);
container->setScrollable(false);
}

View File

@ -285,6 +285,10 @@ Page& PagesControl::getCurrent() {
return current;
}
const std::string& PagesControl::getCurrentName() const {
return curname;
}
void PagesControl::clearHistory() {
pageStack = std::stack<std::string>();
}

View File

@ -107,6 +107,7 @@ namespace gui {
void reset();
Page& getCurrent();
const std::string& getCurrentName() const;
};
}
#endif // GRAPHICS_UI_ELEMENTS_CONTAINERS_H_

View File

@ -7,6 +7,7 @@
#include "../../logic/scripting/scripting.h"
#include "../../frontend/locale/langs.h"
#include "../../util/stringutil.h"
#include "../../delegates.h"
using namespace gui;
@ -25,13 +26,11 @@ std::shared_ptr<Button> guiutil::gotoButton(
std::shared_ptr<PagesControl> menu
) {
text = langs::get(text, L"menu");
return std::make_shared<Button>(text, glm::vec4(10.f), [=](GUI* gui) {
if (menu->has(page)) {
menu->setPage(page);
} else {
menu->setPage("404");
}
});
return std::dynamic_pointer_cast<Button>(create(
"<button onclick='menu.page=\""+page+"\"' padding='10,10,10,10'>"+
util::wstr2str_utf8(text)+
"</button>"
));
}
std::shared_ptr<gui::UINode> guiutil::create(const std::string& source) {

View File

@ -104,6 +104,16 @@ static bool getattr(lua_State* L, gui::FullCheckBox* box, const std::string& att
return false;
}
static bool getattr(lua_State* L, gui::PagesControl* menu, const std::string& attr) {
if (menu == nullptr)
return false;
if (attr == "page") {
lua_pushstring(L, menu->getCurrentName().c_str());
return true;
}
return false;
}
static bool getattr(lua_State* L, gui::TextBox* box, const std::string& attr) {
if (box == nullptr)
return false;
@ -162,6 +172,21 @@ static bool setattr(lua_State* L, gui::Label* label, const std::string& attr) {
return false;
}
static bool setattr(lua_State* L, gui::PagesControl* menu, const std::string& attr) {
if (menu == nullptr)
return false;
if (attr == "page") {
auto page = lua_tostring(L, 4);
if (menu->has(page)) {
menu->setPage(page);
} else {
menu->setPage("404");
}
return true;
}
return false;
}
static int l_gui_getattr(lua_State* L) {
auto docname = lua_tostring(L, 1);
auto element = lua_tostring(L, 2);
@ -194,6 +219,8 @@ static int l_gui_getattr(lua_State* L) {
return 1;
if (getattr(L, dynamic_cast<gui::FullCheckBox*>(node), attr))
return 1;
if (getattr(L, dynamic_cast<gui::PagesControl*>(node), attr))
return 1;
return 0;
}
@ -232,6 +259,8 @@ static int l_gui_setattr(lua_State* L) {
return 0;
if (setattr(L, dynamic_cast<gui::FullCheckBox*>(node), attr))
return 0;
if (setattr(L, dynamic_cast<gui::PagesControl*>(node), attr))
return 0;
}
return 0;
}