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
@@ -49,7 +49,7 @@ namespace gui {
protected:
std::string texture;
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;
};
+35 -9
View File
@@ -6,6 +6,7 @@
#include "panels.h"
#include "controls.h"
#include "../../assets/AssetsLoader.h"
#include "../locale/langs.h"
#include "../../logic/scripting/scripting.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);
if (element->has("scrollable")) {
@@ -50,15 +51,15 @@ static void _readContainer(UiXmlReader& reader, xml::xmlelement element, Contain
for (auto& sub : element->getElements()) {
if (sub->isText())
continue;
if (ignoreUnknown && !reader.hasReader(sub->getTag())) {
continue;
auto subnode = reader.readUINode(sub);
if (subnode) {
container.add(subnode);
}
container.add(reader.readUINode(sub));
}
}
void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, Container& container, bool ignoreUnknown) {
_readContainer(reader, element, container, ignoreUnknown);
void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, Container& container) {
_readContainer(reader, element, container);
}
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()) {
if (sub->isText())
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) {
auto container = std::make_shared<Container>(glm::vec2(), glm::vec2());
_readContainer(reader, element, *container, false);
_readContainer(reader, element, *container);
return container;
}
@@ -156,7 +160,18 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
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("button", readButton);
add("textbox", readTextBox);
@@ -171,11 +186,18 @@ bool UiXmlReader::hasReader(const std::string& tag) const {
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) {
const std::string& tag = element->getTag();
auto found = readers.find(tag);
if (found == readers.end()) {
if (ignored.find(tag) != ignored.end()) {
return nullptr;
}
throw std::runtime_error("unsupported element '"+tag+"'");
}
return found->second(*this, element);
@@ -206,3 +228,7 @@ const std::string& UiXmlReader::getFilename() const {
const scripting::Environment& UiXmlReader::getEnvironment() const {
return env;
}
AssetsLoader& UiXmlReader::getAssetsLoader() {
return assetsLoader;
}
+11 -3
View File
@@ -2,6 +2,7 @@
#define FRONTEND_GUI_GUI_XML_H_
#include <memory>
#include <unordered_set>
#include <unordered_map>
#include "GUI.h"
@@ -11,6 +12,8 @@ namespace scripting {
class Environment;
}
class AssetsLoader;
namespace gui {
class UiXmlReader;
@@ -18,13 +21,17 @@ namespace gui {
class UiXmlReader {
std::unordered_map<std::string, uinode_reader> readers;
std::unordered_set<std::string> ignored;
std::string filename;
const scripting::Environment& env;
AssetsLoader& assetsLoader;
public:
UiXmlReader(const scripting::Environment& env);
UiXmlReader(const scripting::Environment& env, AssetsLoader& assetsLoader);
void add(const std::string& tag, uinode_reader reader);
bool hasReader(const std::string& tag) const;
void addIgnore(const std::string& tag);
std::shared_ptr<UINode> readUINode(xml::xmlelement element);
@@ -37,8 +44,7 @@ namespace gui {
void readUINode(
UiXmlReader& reader,
xml::xmlelement element,
Container& container,
bool ignoreUnknown=false
Container& container
);
std::shared_ptr<UINode> readXML(
@@ -53,6 +59,8 @@ namespace gui {
const scripting::Environment& getEnvironment() const;
const std::string& getFilename() const;
AssetsLoader& getAssetsLoader();
};
}