xml: image element

This commit is contained in:
MihailRis
2024-02-12 22:54:09 +03:00
parent 2de5f5c646
commit de6a6dd771
11 changed files with 101 additions and 71 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ bool AssetsLoader::loadNext() {
return false; return false;
} }
aloader_func loader = found->second; aloader_func loader = found->second;
bool status = loader(assets, paths, entry.filename, entry.alias, entry.config); bool status = loader(*this, assets, paths, entry.filename, entry.alias, entry.config);
entries.pop(); entries.pop();
return status; return status;
} }
+3 -1
View File
@@ -15,9 +15,10 @@ const short ASSET_LAYOUT = 5;
class ResPaths; class ResPaths;
class Assets; class Assets;
class AssetsLoader;
class Content; class Content;
using aloader_func = std::function<bool(Assets*, const ResPaths*, const std::string&, const std::string&, std::shared_ptr<void>)>; using aloader_func = std::function<bool(AssetsLoader&, Assets*, const ResPaths*, const std::string&, const std::string&, std::shared_ptr<void>)>;
struct aloader_entry { struct aloader_entry {
int tag; int tag;
@@ -40,6 +41,7 @@ public:
const std::string alias, const std::string alias,
std::shared_ptr<void> settings=nullptr std::shared_ptr<void> settings=nullptr
); );
bool hasNext() const; bool hasNext() const;
bool loadNext(); bool loadNext();
+31 -22
View File
@@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include <filesystem> #include <filesystem>
#include "Assets.h" #include "Assets.h"
#include "AssetsLoader.h"
#include "../files/files.h" #include "../files/files.h"
#include "../files/engine_paths.h" #include "../files/engine_paths.h"
#include "../coders/png.h" #include "../coders/png.h"
@@ -18,11 +19,13 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
bool assetload::texture(Assets* assets, bool assetload::texture(
const ResPaths* paths, AssetsLoader&,
const std::string filename, Assets* assets,
const std::string name, const ResPaths* paths,
std::shared_ptr<void> const std::string filename,
const std::string name,
std::shared_ptr<void>
) { ) {
std::unique_ptr<Texture> texture( std::unique_ptr<Texture> texture(
png::load_texture(paths->find(filename).u8string()) png::load_texture(paths->find(filename).u8string())
@@ -35,11 +38,13 @@ bool assetload::texture(Assets* assets,
return true; return true;
} }
bool assetload::shader(Assets* assets, bool assetload::shader(
const ResPaths* paths, AssetsLoader&,
const std::string filename, Assets* assets,
const std::string name, const ResPaths* paths,
std::shared_ptr<void> const std::string filename,
const std::string name,
std::shared_ptr<void>
) { ) {
fs::path vertexFile = paths->find(filename+".glslv"); fs::path vertexFile = paths->find(filename+".glslv");
fs::path fragmentFile = paths->find(filename+".glslf"); fs::path fragmentFile = paths->find(filename+".glslf");
@@ -80,11 +85,13 @@ static bool appendAtlas(AtlasBuilder& atlas, const fs::path& file) {
return true; return true;
} }
bool assetload::atlas(Assets* assets, bool assetload::atlas(
const ResPaths* paths, AssetsLoader&,
const std::string directory, Assets* assets,
const std::string name, const ResPaths* paths,
std::shared_ptr<void> const std::string directory,
const std::string name,
std::shared_ptr<void>
) { ) {
AtlasBuilder builder; AtlasBuilder builder;
for (const auto& file : paths->listdir(directory)) { for (const auto& file : paths->listdir(directory)) {
@@ -98,11 +105,13 @@ bool assetload::atlas(Assets* assets,
return true; return true;
} }
bool assetload::font(Assets* assets, bool assetload::font(
const ResPaths* paths, AssetsLoader&,
const std::string filename, Assets* assets,
const std::string name, const ResPaths* paths,
std::shared_ptr<void> const std::string filename,
const std::string name,
std::shared_ptr<void>
) { ) {
std::vector<std::unique_ptr<Texture>> pages; std::vector<std::unique_ptr<Texture>> pages;
for (size_t i = 0; i <= 4; i++) { for (size_t i = 0; i <= 4; i++) {
@@ -123,6 +132,7 @@ bool assetload::font(Assets* assets,
} }
bool assetload::layout( bool assetload::layout(
AssetsLoader& loader,
Assets* assets, Assets* assets,
const ResPaths* paths, const ResPaths* paths,
const std::string file, const std::string file,
@@ -131,7 +141,7 @@ bool assetload::layout(
) { ) {
try { try {
LayoutCfg* cfg = reinterpret_cast<LayoutCfg*>(config.get()); LayoutCfg* cfg = reinterpret_cast<LayoutCfg*>(config.get());
auto document = UiDocument::read(cfg->env, name, file); auto document = UiDocument::read(loader, cfg->env, name, file);
assets->store(document.release(), name); assets->store(document.release(), name);
return true; return true;
} catch (const parsing_error& err) { } catch (const parsing_error& err) {
@@ -141,7 +151,6 @@ bool assetload::layout(
} }
} }
bool assetload::animation(Assets* assets, bool assetload::animation(Assets* assets,
const ResPaths* paths, const ResPaths* paths,
const std::string directory, const std::string directory,
+12 -6
View File
@@ -6,39 +6,45 @@
class ResPaths; class ResPaths;
class Assets; class Assets;
class AssetsLoader;
class Atlas; class Atlas;
namespace assetload { namespace assetload {
bool texture( bool texture(
Assets* assets, AssetsLoader&,
Assets*,
const ResPaths* paths, const ResPaths* paths,
const std::string filename, const std::string filename,
const std::string name, const std::string name,
std::shared_ptr<void> settings std::shared_ptr<void> settings
); );
bool shader( bool shader(
Assets* assets, AssetsLoader&,
Assets*,
const ResPaths* paths, const ResPaths* paths,
const std::string filename, const std::string filename,
const std::string name, const std::string name,
std::shared_ptr<void> settings std::shared_ptr<void> settings
); );
bool atlas( bool atlas(
Assets* assets, AssetsLoader&,
Assets*,
const ResPaths* paths, const ResPaths* paths,
const std::string directory, const std::string directory,
const std::string name, const std::string name,
std::shared_ptr<void> settings std::shared_ptr<void> settings
); );
bool font( bool font(
Assets* assets, AssetsLoader&,
Assets*,
const ResPaths* paths, const ResPaths* paths,
const std::string filename, const std::string filename,
const std::string name, const std::string name,
std::shared_ptr<void> settings std::shared_ptr<void> settings
); );
bool layout( bool layout(
Assets* assets, AssetsLoader&,
Assets*,
const ResPaths* paths, const ResPaths* paths,
const std::string file, const std::string file,
const std::string name, const std::string name,
@@ -46,7 +52,7 @@ namespace assetload {
); );
bool animation( bool animation(
Assets* assets, Assets*,
const ResPaths* paths, const ResPaths* paths,
const std::string directory, const std::string directory,
const std::string name, const std::string name,
+2 -19
View File
@@ -424,28 +424,11 @@ static void readSlotsGrid(InventoryView* view, gui::UiXmlReader& reader, xml::xm
} }
} }
std::shared_ptr<InventoryView> InventoryView::readXML(
const std::string& src,
const std::string& file,
const scripting::Environment& env
) {
auto view = std::make_shared<InventoryView>();
gui::UiXmlReader reader(env);
createReaders(reader);
auto document = xml::parse(file, src);
auto root = document->getRoot();
if (root->getTag() != "inventory") {
throw std::runtime_error("'inventory' element expected");
}
return std::dynamic_pointer_cast<InventoryView>(reader.readXML(file, root));
}
void InventoryView::createReaders(gui::UiXmlReader& reader) { void InventoryView::createReaders(gui::UiXmlReader& reader) {
reader.add("inventory", [=](gui::UiXmlReader& reader, xml::xmlelement element) { reader.add("inventory", [=](gui::UiXmlReader& reader, xml::xmlelement element) {
auto view = std::make_shared<InventoryView>(); auto view = std::make_shared<InventoryView>();
reader.readUINode(reader, element, *view, true); reader.addIgnore("slots-grid");
reader.readUINode(reader, element, *view);
for (auto& sub : element->getElements()) { for (auto& sub : element->getElements()) {
if (sub->getTag() == "slot") { if (sub->getTag() == "slot") {
-6
View File
@@ -117,12 +117,6 @@ public:
std::shared_ptr<Inventory> getInventory() const; std::shared_ptr<Inventory> getInventory() const;
static std::shared_ptr<InventoryView> readXML(
const std::string& src,
const std::string& file,
const scripting::Environment& env
);
static void createReaders(gui::UiXmlReader& reader); static void createReaders(gui::UiXmlReader& reader);
static const int SLOT_INTERVAL = 4; static const int SLOT_INTERVAL = 4;
+2 -2
View File
@@ -51,12 +51,12 @@ 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(AssetsLoader& loader, int penv, std::string namesp, fs::path file) {
const std::string text = files::read_string(file); const std::string text = files::read_string(file);
auto xmldoc = xml::parse(file.u8string(), text); auto xmldoc = xml::parse(file.u8string(), text);
auto env = scripting::create_environment(penv); auto env = scripting::create_environment(penv);
gui::UiXmlReader reader(*env); gui::UiXmlReader reader(*env, loader);
InventoryView::createReaders(reader); InventoryView::createReaders(reader);
auto view = reader.readXML( auto view = reader.readXML(
file.u8string(), xmldoc->getRoot() file.u8string(), xmldoc->getRoot()
+3 -1
View File
@@ -24,6 +24,8 @@ struct uidocscript {
using uinodes_map = std::unordered_map<std::string, std::shared_ptr<gui::UINode>>; using uinodes_map = std::unordered_map<std::string, std::shared_ptr<gui::UINode>>;
class AssetsLoader;
class UiDocument { class UiDocument {
std::string id; std::string id;
uidocscript script; uidocscript script;
@@ -46,7 +48,7 @@ public:
/* Collect map of all uinodes having identifiers */ /* Collect map of all uinodes having identifiers */
static void collect(uinodes_map& map, std::shared_ptr<gui::UINode> node); 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(AssetsLoader& loader, int env, std::string namesp, fs::path file);
}; };
#endif // FRONTEND_UI_DOCUMENT_H_ #endif // FRONTEND_UI_DOCUMENT_H_
+1 -1
View File
@@ -49,7 +49,7 @@ namespace gui {
protected: protected:
std::string texture; std::string texture;
public: public:
Image(std::string texture, glm::vec2 size); Image(std::string texture, glm::vec2 size=glm::vec2(32,32));
virtual void draw(const GfxContext* pctx, Assets* assets) override; virtual void draw(const GfxContext* pctx, Assets* assets) override;
}; };
+35 -9
View File
@@ -6,6 +6,7 @@
#include "panels.h" #include "panels.h"
#include "controls.h" #include "controls.h"
#include "../../assets/AssetsLoader.h"
#include "../locale/langs.h" #include "../locale/langs.h"
#include "../../logic/scripting/scripting.h" #include "../../logic/scripting/scripting.h"
#include "../../util/stringutil.h" #include "../../util/stringutil.h"
@@ -41,7 +42,7 @@ static void _readUINode(xml::xmlelement element, UINode& node) {
} }
static void _readContainer(UiXmlReader& reader, xml::xmlelement element, Container& container, bool ignoreUnknown) { static void _readContainer(UiXmlReader& reader, xml::xmlelement element, Container& container) {
_readUINode(element, container); _readUINode(element, container);
if (element->has("scrollable")) { if (element->has("scrollable")) {
@@ -50,15 +51,15 @@ static void _readContainer(UiXmlReader& reader, xml::xmlelement element, Contain
for (auto& sub : element->getElements()) { for (auto& sub : element->getElements()) {
if (sub->isText()) if (sub->isText())
continue; continue;
if (ignoreUnknown && !reader.hasReader(sub->getTag())) { auto subnode = reader.readUINode(sub);
continue; if (subnode) {
container.add(subnode);
} }
container.add(reader.readUINode(sub));
} }
} }
void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, Container& container, bool ignoreUnknown) { void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, Container& container) {
_readContainer(reader, element, container, ignoreUnknown); _readContainer(reader, element, container);
} }
void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& node) { void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& node) {
@@ -86,7 +87,10 @@ static void _readPanel(UiXmlReader& reader, xml::xmlelement element, Panel& pane
for (auto& sub : element->getElements()) { for (auto& sub : element->getElements()) {
if (sub->isText()) if (sub->isText())
continue; continue;
panel.add(reader.readUINode(sub)); auto subnode = reader.readUINode(sub);
if (subnode) {
panel.add(subnode);
}
} }
} }
@@ -113,7 +117,7 @@ static std::shared_ptr<UINode> readLabel(UiXmlReader& reader, xml::xmlelement el
static std::shared_ptr<UINode> readContainer(UiXmlReader& reader, xml::xmlelement element) { static std::shared_ptr<UINode> readContainer(UiXmlReader& reader, xml::xmlelement element) {
auto container = std::make_shared<Container>(glm::vec2(), glm::vec2()); auto container = std::make_shared<Container>(glm::vec2(), glm::vec2());
_readContainer(reader, element, *container, false); _readContainer(reader, element, *container);
return container; return container;
} }
@@ -156,7 +160,18 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
return textbox; return textbox;
} }
UiXmlReader::UiXmlReader(const scripting::Environment& env) : env(env) { static std::shared_ptr<UINode> readImage(UiXmlReader& reader, xml::xmlelement element) {
std::string src = element->attr("src", "").getText();
auto image = std::make_shared<Image>(src);
_readUINode(element, *image);
reader.getAssetsLoader().add(ASSET_TEXTURE, "textures/"+src+".png", src, nullptr);
return image;
}
UiXmlReader::UiXmlReader(const scripting::Environment& env, AssetsLoader& assetsLoader)
: env(env), assetsLoader(assetsLoader)
{
add("image", readImage);
add("label", readLabel); add("label", readLabel);
add("button", readButton); add("button", readButton);
add("textbox", readTextBox); add("textbox", readTextBox);
@@ -171,11 +186,18 @@ bool UiXmlReader::hasReader(const std::string& tag) const {
return readers.find(tag) != readers.end(); return readers.find(tag) != readers.end();
} }
void UiXmlReader::addIgnore(const std::string& tag) {
ignored.insert(tag);
}
std::shared_ptr<UINode> UiXmlReader::readUINode(xml::xmlelement element) { std::shared_ptr<UINode> UiXmlReader::readUINode(xml::xmlelement element) {
const std::string& tag = element->getTag(); const std::string& tag = element->getTag();
auto found = readers.find(tag); auto found = readers.find(tag);
if (found == readers.end()) { if (found == readers.end()) {
if (ignored.find(tag) != ignored.end()) {
return nullptr;
}
throw std::runtime_error("unsupported element '"+tag+"'"); throw std::runtime_error("unsupported element '"+tag+"'");
} }
return found->second(*this, element); return found->second(*this, element);
@@ -206,3 +228,7 @@ const std::string& UiXmlReader::getFilename() const {
const scripting::Environment& UiXmlReader::getEnvironment() const { const scripting::Environment& UiXmlReader::getEnvironment() const {
return env; return env;
} }
AssetsLoader& UiXmlReader::getAssetsLoader() {
return assetsLoader;
}
+11 -3
View File
@@ -2,6 +2,7 @@
#define FRONTEND_GUI_GUI_XML_H_ #define FRONTEND_GUI_GUI_XML_H_
#include <memory> #include <memory>
#include <unordered_set>
#include <unordered_map> #include <unordered_map>
#include "GUI.h" #include "GUI.h"
@@ -11,6 +12,8 @@ namespace scripting {
class Environment; class Environment;
} }
class AssetsLoader;
namespace gui { namespace gui {
class UiXmlReader; class UiXmlReader;
@@ -18,13 +21,17 @@ namespace gui {
class UiXmlReader { class UiXmlReader {
std::unordered_map<std::string, uinode_reader> readers; std::unordered_map<std::string, uinode_reader> readers;
std::unordered_set<std::string> ignored;
std::string filename; std::string filename;
const scripting::Environment& env; const scripting::Environment& env;
AssetsLoader& assetsLoader;
public: public:
UiXmlReader(const scripting::Environment& env); UiXmlReader(const scripting::Environment& env, AssetsLoader& assetsLoader);
void add(const std::string& tag, uinode_reader reader); void add(const std::string& tag, uinode_reader reader);
bool hasReader(const std::string& tag) const; bool hasReader(const std::string& tag) const;
void addIgnore(const std::string& tag);
std::shared_ptr<UINode> readUINode(xml::xmlelement element); std::shared_ptr<UINode> readUINode(xml::xmlelement element);
@@ -37,8 +44,7 @@ namespace gui {
void readUINode( void readUINode(
UiXmlReader& reader, UiXmlReader& reader,
xml::xmlelement element, xml::xmlelement element,
Container& container, Container& container
bool ignoreUnknown=false
); );
std::shared_ptr<UINode> readXML( std::shared_ptr<UINode> readXML(
@@ -53,6 +59,8 @@ namespace gui {
const scripting::Environment& getEnvironment() const; const scripting::Environment& getEnvironment() const;
const std::string& getFilename() const; const std::string& getFilename() const;
AssetsLoader& getAssetsLoader();
}; };
} }