InventoryView::readXML

This commit is contained in:
MihailRis 2024-02-06 23:26:11 +03:00
parent 8dbbea7b6d
commit 23705e065e
4 changed files with 91 additions and 10 deletions

View File

@ -89,13 +89,13 @@ void InventoryBuilder::addGrid(
auto builtSlot = slotLayout;
builtSlot.index = row * cols + col;
builtSlot.position = position;
view->addSlot(builtSlot);
add(builtSlot);
}
}
}
void InventoryBuilder::add(SlotLayout layout) {
view->addSlot(layout);
view->add(view->addSlot(layout), layout.position);
}
std::shared_ptr<InventoryView> InventoryBuilder::build() {
@ -285,7 +285,7 @@ InventoryView::InventoryView(
InventoryView::~InventoryView() {}
void InventoryView::addSlot(SlotLayout layout) {
std::shared_ptr<SlotView> InventoryView::addSlot(SlotLayout layout) {
uint width = InventoryView::SLOT_SIZE;
uint height = InventoryView::SLOT_SIZE;
@ -297,6 +297,7 @@ void InventoryView::addSlot(SlotLayout layout) {
if (coord.y + height > vsize.y) {
vsize.y = coord.y + height;
}
setSize(vsize);
auto slot = std::make_shared<SlotView>(
frontend, interaction, layout
@ -304,8 +305,8 @@ void InventoryView::addSlot(SlotLayout layout) {
if (!layout.background) {
slot->setColor(glm::vec4());
}
add(slot, layout.position);
slots.push_back(slot.get());
return slot;
}
void InventoryView::bind(std::shared_ptr<Inventory> inventory) {
@ -337,3 +338,41 @@ glm::vec2 InventoryView::getOrigin() const {
void InventoryView::setInventory(std::shared_ptr<Inventory> inventory) {
this->inventory = inventory;
}
#include "../coders/xml.h"
#include "gui/gui_xml.h"
std::shared_ptr<InventoryView> InventoryView::readXML(
LevelFrontend* frontend,
InventoryInteraction& interaction,
const std::string& src,
const std::string& file
) {
auto view = std::make_shared<InventoryView>(frontend, interaction);
gui::UiXmlReader reader;
reader.add("inventory", [=](gui::UiXmlReader& reader, xml::xmlelement element) {
reader.readUINode(reader, element, *view);
return view;
});
reader.add("slot", [=](gui::UiXmlReader& reader, xml::xmlelement element) {
int index = element->attr("index", "0").asInt();
bool itemSource = element->attr("item-source", "false").asBool();
SlotLayout layout(index, glm::vec2(), true, itemSource, nullptr, nullptr);
if (element->has("coord")) {
layout.position = element->attr("coord").asVec2();
}
auto slot = view->addSlot(layout);
reader.readUINode(reader, element, *slot);
return slot;
});
auto document = xml::parse(file, src);
auto root = document->getRoot();
if (root->getTag() != "inventory") {
throw std::runtime_error("'inventory' element expected");
}
reader.readXML(file, root);
return view;
}

View File

@ -98,7 +98,14 @@ public:
void bind(std::shared_ptr<Inventory> inventory);
void addSlot(SlotLayout layout);
std::shared_ptr<SlotView> addSlot(SlotLayout layout);
static std::shared_ptr<InventoryView> readXML(
LevelFrontend* frontend,
InventoryInteraction& interaction,
const std::string& src,
const std::string& file
);
static const int SLOT_INTERVAL = 4;
static const int SLOT_SIZE = ITEM_ICON_SIZE;
@ -116,7 +123,8 @@ public:
glm::vec2 coord,
int padding,
bool addpanel,
SlotLayout slotLayout);
SlotLayout slotLayout
);
void add(SlotLayout slotLayout);
std::shared_ptr<InventoryView> build();

View File

@ -20,7 +20,7 @@ static Align align_from_string(const std::string& str, Align def) {
}
/* Read basic UINode properties */
static void readUINode(xml::xmlelement element, UINode& node) {
static void _readUINode(xml::xmlelement element, UINode& node) {
if (element->has("coord")) {
node.setCoord(element->attr("coord").asVec2());
}
@ -37,8 +37,9 @@ static void readUINode(xml::xmlelement element, UINode& node) {
node.setAlign(align_from_string(alignName, node.getAlign()));
}
static void _readContainer(UiXmlReader& reader, xml::xmlelement element, Container& container) {
readUINode(element, container);
_readUINode(element, container);
if (element->has("scrollable")) {
container.setScrollable(element->attr("scrollable").asBool());
@ -50,8 +51,16 @@ static void _readContainer(UiXmlReader& reader, xml::xmlelement element, Contain
}
}
void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, Container& container) {
_readContainer(reader, element, container);
}
void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& node) {
_readUINode(element, node);
}
static void _readPanel(UiXmlReader& reader, xml::xmlelement element, Panel& panel) {
readUINode(element, panel);
_readUINode(element, panel);
if (element->has("padding")) {
panel.setPadding(element->attr("padding").asVec4());
@ -86,7 +95,7 @@ static std::wstring readAndProcessInnerText(xml::xmlelement element) {
static std::shared_ptr<UINode> readLabel(UiXmlReader& reader, xml::xmlelement element) {
std::wstring text = readAndProcessInnerText(element);
auto label = std::make_shared<Label>(text);
readUINode(element, *label);
_readUINode(element, *label);
return label;
}
@ -161,6 +170,14 @@ std::shared_ptr<UINode> UiXmlReader::readXML(
return readUINode(root);
}
std::shared_ptr<UINode> UiXmlReader::readXML(
const std::string& filename,
xml::xmlelement root
) {
this->filename = filename;
return readUINode(root);
}
const std::string& UiXmlReader::getFilename() const {
return filename;
}

View File

@ -21,12 +21,29 @@ namespace gui {
void add(const std::string& tag, uinode_reader reader);
std::shared_ptr<UINode> readUINode(xml::xmlelement element);
void readUINode(
UiXmlReader& reader,
xml::xmlelement element,
UINode& node
);
void readUINode(
UiXmlReader& reader,
xml::xmlelement element,
Container& container
);
std::shared_ptr<UINode> readXML(
const std::string& filename,
const std::string& source
);
std::shared_ptr<UINode> readXML(
const std::string& filename,
xml::xmlelement root
);
const std::string& getFilename() const;
};
}