InventoryBuilder add(InventoryPanel), add(SlotLayout)

This commit is contained in:
MihailRis
2024-01-22 04:05:41 +03:00
parent 0d62067e5a
commit e4ef813705
3 changed files with 58 additions and 9 deletions
+46 -5
View File
@@ -28,6 +28,10 @@ void InventoryLayout::add(SlotLayout slot) {
slots.push_back(slot); slots.push_back(slot);
} }
void InventoryLayout::add(InventoryPanel panel) {
panels.push_back(panel);
}
void InventoryLayout::setSize(glm::vec2 size) { void InventoryLayout::setSize(glm::vec2 size) {
this->size = size; this->size = size;
} }
@@ -48,6 +52,10 @@ std::vector<SlotLayout>& InventoryLayout::getSlots() {
return slots; return slots;
} }
std::vector<InventoryPanel>& InventoryLayout::getPanels() {
return panels;
}
SlotLayout::SlotLayout( SlotLayout::SlotLayout(
glm::vec2 position, glm::vec2 position,
bool background, bool background,
@@ -74,7 +82,8 @@ InventoryBuilder::InventoryBuilder()
void InventoryBuilder::addGrid( void InventoryBuilder::addGrid(
int cols, int count, int cols, int count,
glm::vec2 coord, glm::vec2 coord,
int padding, int padding,
bool addpanel,
SlotLayout slotLayout) SlotLayout slotLayout)
{ {
const int slotSize = InventoryView::SLOT_SIZE; const int slotSize = InventoryView::SLOT_SIZE;
@@ -97,7 +106,7 @@ void InventoryBuilder::addGrid(
for (int row = 0; row < rows; row++) { for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) { for (int col = 0; col < cols; col++) {
if (row * cols + col >= count) if (row * cols + col >= count)
return; break;
glm::vec2 position ( glm::vec2 position (
col * (slotSize + interval) + padding, col * (slotSize + interval) + padding,
row * (slotSize + interval) + padding row * (slotSize + interval) + padding
@@ -107,6 +116,32 @@ void InventoryBuilder::addGrid(
layout->add(builtSlot); layout->add(builtSlot);
} }
} }
if (addpanel) {
add(InventoryPanel(
coord,
glm::vec2(width, height),
glm::vec4(0, 0, 0, 0.5f)));
}
}
void InventoryBuilder::add(SlotLayout slotLayout) {
uint width = InventoryView::SLOT_SIZE;
uint height = InventoryView::SLOT_SIZE;
auto coord = slotLayout.position;
auto lsize = layout->getSize();
if (coord.x + width > lsize.x) {
lsize.x = coord.x + width;
}
if (coord.y + height > lsize.y) {
lsize.y = coord.y + height;
}
layout->add(slotLayout);
}
void InventoryBuilder::add(InventoryPanel panel) {
layout->add(panel);
} }
std::unique_ptr<InventoryLayout> InventoryBuilder::build() { std::unique_ptr<InventoryLayout> InventoryBuilder::build() {
@@ -275,7 +310,7 @@ InventoryView::InventoryView(
frontend(frontend), frontend(frontend),
interaction(interaction) { interaction(interaction) {
size(this->layout->getSize()); size(this->layout->getSize());
color(glm::vec4(0, 0, 0, 0.5f)); color(glm::vec4(0, 0, 0, 0.0f));
} }
InventoryView::~InventoryView() {} InventoryView::~InventoryView() {}
@@ -321,7 +356,13 @@ InventoryLayout* InventoryView::getLayout() const {
void InventoryView::drawBackground(Batch2D* batch, Assets* assets) { void InventoryView::drawBackground(Batch2D* batch, Assets* assets) {
glm::vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
batch->texture(nullptr); batch->texture(nullptr);
batch->color = color_;
batch->rect(coord.x-1, coord.y-1, size_.x+2, size_.y+2); for (auto& panel : layout->getPanels()) {
glm::vec2 size = panel.size;
glm::vec2 pos = coord + panel.position;
batch->color = panel.color;
batch->rect(pos.x-1, pos.y-1, size.x+2, size.y+2);
}
} }
+9 -1
View File
@@ -53,10 +53,12 @@ class InventoryLayout {
glm::vec2 size; glm::vec2 size;
glm::vec2 origin; glm::vec2 origin;
std::vector<SlotLayout> slots; std::vector<SlotLayout> slots;
std::vector<InventoryPanel> panels;
public: public:
InventoryLayout(glm::vec2 size); InventoryLayout(glm::vec2 size);
void add(SlotLayout slot); void add(SlotLayout slot);
void add(InventoryPanel panel);
void setSize(glm::vec2 size); void setSize(glm::vec2 size);
void setOrigin(glm::vec2 origin); void setOrigin(glm::vec2 origin);
@@ -64,6 +66,7 @@ public:
glm::vec2 getOrigin() const; glm::vec2 getOrigin() const;
std::vector<SlotLayout>& getSlots(); std::vector<SlotLayout>& getSlots();
std::vector<InventoryPanel>& getPanels();
}; };
class InventoryBuilder { class InventoryBuilder {
@@ -74,8 +77,13 @@ public:
void addGrid( void addGrid(
int cols, int count, int cols, int count,
glm::vec2 coord, glm::vec2 coord,
int padding, int padding,
bool addpanel,
SlotLayout slotLayout); SlotLayout slotLayout);
void add(SlotLayout slotLayout);
void add(InventoryPanel panel);
std::unique_ptr<InventoryLayout> build(); std::unique_ptr<InventoryLayout> build();
}; };
+3 -3
View File
@@ -187,7 +187,7 @@ std::shared_ptr<InventoryView> HudRenderer::createContentAccess() {
}); });
InventoryBuilder builder; InventoryBuilder builder;
builder.addGrid(8, itemsCount-1, glm::vec2(), 8, slotLayout); builder.addGrid(8, itemsCount-1, glm::vec2(), 8, true, slotLayout);
auto layout = builder.build(); auto layout = builder.build();
auto contentAccess = std::make_shared<InventoryView>( auto contentAccess = std::make_shared<InventoryView>(
@@ -209,7 +209,7 @@ std::shared_ptr<InventoryView> HudRenderer::createHotbar() {
SlotLayout slotLayout(glm::vec2(), false, false, nullptr, nullptr); SlotLayout slotLayout(glm::vec2(), false, false, nullptr, nullptr);
InventoryBuilder builder; InventoryBuilder builder;
builder.addGrid(10, 10, glm::vec2(), 4, slotLayout); builder.addGrid(10, 10, glm::vec2(), 4, true, slotLayout);
auto layout = builder.build(); auto layout = builder.build();
layout->setOrigin(glm::vec2(layout->getSize().x/2, 0)); layout->setOrigin(glm::vec2(layout->getSize().x/2, 0));
@@ -236,7 +236,7 @@ std::shared_ptr<InventoryView> HudRenderer::createInventory() {
}, nullptr); }, nullptr);
InventoryBuilder builder; InventoryBuilder builder;
builder.addGrid(10, inventory->size(), glm::vec2(), 4, slotLayout); builder.addGrid(10, inventory->size(), glm::vec2(), 4, true, slotLayout);
auto layout = builder.build(); auto layout = builder.build();
auto view = std::make_shared<InventoryView>( auto view = std::make_shared<InventoryView>(