From 23705e065e3559078949e2d5f2e33476bf62a110 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 6 Feb 2024 23:26:11 +0300 Subject: [PATCH] InventoryView::readXML --- src/frontend/InventoryView.cpp | 47 +++++++++++++++++++++++++++++++--- src/frontend/InventoryView.h | 12 +++++++-- src/frontend/gui/gui_xml.cpp | 25 +++++++++++++++--- src/frontend/gui/gui_xml.h | 17 ++++++++++++ 4 files changed, 91 insertions(+), 10 deletions(-) diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp index 9ef101c9..ae7d4036 100644 --- a/src/frontend/InventoryView.cpp +++ b/src/frontend/InventoryView.cpp @@ -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 InventoryBuilder::build() { @@ -285,7 +285,7 @@ InventoryView::InventoryView( InventoryView::~InventoryView() {} -void InventoryView::addSlot(SlotLayout layout) { +std::shared_ptr 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( 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) { @@ -337,3 +338,41 @@ glm::vec2 InventoryView::getOrigin() const { void InventoryView::setInventory(std::shared_ptr inventory) { this->inventory = inventory; } + +#include "../coders/xml.h" +#include "gui/gui_xml.h" + +std::shared_ptr InventoryView::readXML( + LevelFrontend* frontend, + InventoryInteraction& interaction, + const std::string& src, + const std::string& file +) { + auto view = std::make_shared(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; +} diff --git a/src/frontend/InventoryView.h b/src/frontend/InventoryView.h index ff6fc7cc..ded4557a 100644 --- a/src/frontend/InventoryView.h +++ b/src/frontend/InventoryView.h @@ -98,7 +98,14 @@ public: void bind(std::shared_ptr inventory); - void addSlot(SlotLayout layout); + std::shared_ptr addSlot(SlotLayout layout); + + static std::shared_ptr 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 build(); diff --git a/src/frontend/gui/gui_xml.cpp b/src/frontend/gui/gui_xml.cpp index ed014934..3499b825 100644 --- a/src/frontend/gui/gui_xml.cpp +++ b/src/frontend/gui/gui_xml.cpp @@ -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 readLabel(UiXmlReader& reader, xml::xmlelement element) { std::wstring text = readAndProcessInnerText(element); auto label = std::make_shared