From c902ed506b955be956cc643adb52bc71302cf00b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 7 Feb 2024 09:47:14 +0300 Subject: [PATCH] slots-grid xml element --- src/frontend/InventoryView.cpp | 60 +++++++++++++++++++++++++++++++--- src/frontend/InventoryView.h | 3 +- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp index ae7d4036..dfc3608a 100644 --- a/src/frontend/InventoryView.cpp +++ b/src/frontend/InventoryView.cpp @@ -42,9 +42,7 @@ SlotLayout::SlotLayout( InventoryBuilder::InventoryBuilder( LevelFrontend* frontend, InventoryInteraction& interaction -) : frontend(frontend), - interaction(interaction) -{ +) { view = std::make_shared(frontend, interaction); } @@ -286,8 +284,8 @@ InventoryView::~InventoryView() {} std::shared_ptr InventoryView::addSlot(SlotLayout layout) { - uint width = InventoryView::SLOT_SIZE; - uint height = InventoryView::SLOT_SIZE; + uint width = InventoryView::SLOT_SIZE + layout.padding; + uint height = InventoryView::SLOT_SIZE + layout.padding; auto coord = layout.position; auto vsize = getSize(); @@ -368,6 +366,58 @@ std::shared_ptr InventoryView::readXML( return slot; }); + reader.add("slots-grid", [=](gui::UiXmlReader& reader, xml::xmlelement element) { + int startIndex = element->attr("start-index", "0").asInt(); + int rows = element->attr("rows", "0").asInt(); + int cols = element->attr("cols", "0").asInt(); + int count = element->attr("count", "0").asInt(); + const int slotSize = InventoryView::SLOT_SIZE; + int interval = element->attr("interval", "-1").asInt(); + if (interval < 0) { + interval = InventoryView::SLOT_INTERVAL; + } + int padding = element->attr("padding", "-1").asInt(); + if (padding < 0) { + padding = interval; + } + if (rows == 0) { + rows = ceildiv(count, cols); + } else if (cols == 0) { + cols = ceildiv(count, rows); + } else if (count == 0) { + count = rows * cols; + } + bool itemSource = element->attr("item-source", "false").asBool(); + SlotLayout layout(-1, glm::vec2(), true, itemSource, nullptr, nullptr); + if (element->has("coord")) { + layout.position = element->attr("coord").asVec2(); + } + layout.padding = padding; + + glm::vec2 size ( + cols * slotSize + (cols - 1) * interval + padding * 2, + rows * slotSize + (rows - 1) * interval + padding * 2 + ); + auto container = std::make_shared(layout.position, size); + int idx = 0; + for (int row = 0; row < rows; row++) { + for (int col = 0; col < cols; col++, idx++) { + if (idx >= count) { + return container; + } + SlotLayout slotLayout = layout; + slotLayout.index = startIndex + idx; + slotLayout.position = glm::vec2( + padding + col * (slotSize + interval), + padding + row * (slotSize + interval) + ); + auto slot = view->addSlot(slotLayout); + container->add(slot, slotLayout.position); + } + } + return container; + }); + auto document = xml::parse(file, src); auto root = document->getRoot(); if (root->getTag() != "inventory") { diff --git a/src/frontend/InventoryView.h b/src/frontend/InventoryView.h index ded4557a..7fcd1912 100644 --- a/src/frontend/InventoryView.h +++ b/src/frontend/InventoryView.h @@ -38,6 +38,7 @@ struct SlotLayout { bool itemSource; itemsharefunc shareFunc; slotcallback rightClick; + int padding = 0; SlotLayout(int index, glm::vec2 position, @@ -113,8 +114,6 @@ public: class InventoryBuilder { std::shared_ptr view; - LevelFrontend* frontend; - InventoryInteraction& interaction; public: InventoryBuilder(LevelFrontend* frontend, InventoryInteraction& interaction);