refactor: add GUI instance reference to UI nodes

This commit is contained in:
MihailRis
2025-04-02 14:55:53 +03:00
parent 74a94f869c
commit 4c48afbb90
70 changed files with 1133 additions and 832 deletions
+10 -5
View File
@@ -19,13 +19,14 @@
#include "items/ItemDef.hpp"
#include "Assets.hpp"
#include "assetload_funcs.hpp"
#include "engine/Engine.hpp"
namespace fs = std::filesystem;
static debug::Logger logger("assets-loader");
AssetsLoader::AssetsLoader(Assets* assets, const ResPaths* paths)
: assets(assets), paths(paths) {
AssetsLoader::AssetsLoader(Engine& engine, Assets& assets, const ResPaths* paths)
: engine(engine), assets(assets), paths(paths) {
addLoader(AssetType::SHADER, assetload::shader);
addLoader(AssetType::TEXTURE, assetload::texture);
addLoader(AssetType::FONT, assetload::font);
@@ -73,7 +74,7 @@ void AssetsLoader::loadNext() {
aloader_func loader = getLoader(entry.tag);
auto postfunc =
loader(this, paths, entry.filename, entry.alias, entry.config);
postfunc(assets);
postfunc(&assets);
entries.pop();
} catch (std::runtime_error& err) {
logger.error() << err.what();
@@ -101,7 +102,7 @@ static void add_layouts(
AssetType::LAYOUT,
file.string(),
name,
std::make_shared<LayoutCfg>(env)
std::make_shared<LayoutCfg>(&loader.getEngine().getGUI(), env)
);
}
}
@@ -296,6 +297,10 @@ bool AssetsLoader::loadExternalTexture(
return false;
}
Engine& AssetsLoader::getEngine() {
return engine;
}
const ResPaths* AssetsLoader::getPaths() const {
return paths;
}
@@ -324,7 +329,7 @@ std::shared_ptr<Task> AssetsLoader::startTask(runnable onDone) {
std::make_shared<util::ThreadPool<aloader_entry, assetload::postfunc>>(
"assets-loader-pool",
[=]() { return std::make_shared<LoaderWorker>(this); },
[=](const assetload::postfunc& func) { func(assets); }
[this](const assetload::postfunc& func) { func(&assets); }
);
pool->setOnComplete(std::move(onDone));
while (!entries.empty()) {
+12 -3
View File
@@ -18,6 +18,11 @@
class ResPaths;
class AssetsLoader;
class Content;
class Engine;
namespace gui {
class GUI;
}
struct AssetCfg {
virtual ~AssetCfg() {
@@ -25,9 +30,10 @@ struct AssetCfg {
};
struct LayoutCfg : AssetCfg {
gui::GUI* gui;
scriptenv env;
LayoutCfg(scriptenv env) : env(std::move(env)) {
LayoutCfg(gui::GUI* gui, scriptenv env) : gui(gui), env(std::move(env)) {
}
};
@@ -61,7 +67,8 @@ struct aloader_entry {
};
class AssetsLoader {
Assets* assets;
Engine& engine;
Assets& assets;
std::map<AssetType, aloader_func> loaders;
std::queue<aloader_entry> entries;
std::set<std::pair<AssetType, std::string>> enqueued;
@@ -76,7 +83,7 @@ class AssetsLoader {
void processPreloadConfig(const io::path& file);
void processPreloadConfigs(const Content* content);
public:
AssetsLoader(Assets* assets, const ResPaths* paths);
AssetsLoader(Engine& engine, Assets& assets, const ResPaths* paths);
void addLoader(AssetType tag, aloader_func func);
/// @brief Enqueue asset load
@@ -111,4 +118,6 @@ public:
const std::string& name,
const std::vector<io::path>& alternatives
);
Engine& getEngine();
};
+1
View File
@@ -189,6 +189,7 @@ assetload::postfunc assetload::layout(
auto prefix = name.substr(0, pos);
assets->store(
UiDocument::read(
*cfg->gui,
cfg->env,
name,
file,