slots-grid xml element

This commit is contained in:
MihailRis 2024-02-07 09:47:14 +03:00
parent 23705e065e
commit c902ed506b
2 changed files with 56 additions and 7 deletions

View File

@ -42,9 +42,7 @@ SlotLayout::SlotLayout(
InventoryBuilder::InventoryBuilder(
LevelFrontend* frontend,
InventoryInteraction& interaction
) : frontend(frontend),
interaction(interaction)
{
) {
view = std::make_shared<InventoryView>(frontend, interaction);
}
@ -286,8 +284,8 @@ InventoryView::~InventoryView() {}
std::shared_ptr<SlotView> 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> 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<Container>(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") {

View File

@ -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<InventoryView> view;
LevelFrontend* frontend;
InventoryInteraction& interaction;
public:
InventoryBuilder(LevelFrontend* frontend, InventoryInteraction& interaction);