InventoryInteraction removed due to redundancy

This commit is contained in:
MihailRis 2024-04-29 00:14:16 +03:00
parent ef28a368cb
commit c561466f64
5 changed files with 54 additions and 59 deletions

View File

@ -10,6 +10,7 @@
#include "../graphics/core/Font.hpp"
#include "../graphics/core/GfxContext.hpp"
#include "../graphics/core/Shader.hpp"
#include "../graphics/ui/GUI.hpp"
#include "../graphics/render/BlocksPreview.hpp"
#include "../items/Inventories.h"
#include "../items/Inventory.h"
@ -200,7 +201,11 @@ void SlotView::clicked(gui::GUI* gui, mousecode button) {
if (bound == nullptr)
return;
ItemStack& grabbed = interaction->getGrabbedItem();
auto exchangeSlot = std::dynamic_pointer_cast<SlotView>(gui->get(EXCHANGE_SLOT_NAME));
if (exchangeSlot == nullptr) {
return;
}
ItemStack& grabbed = exchangeSlot->getStack();
ItemStack& stack = *bound;
if (button == mousecode::BUTTON_1) {
@ -266,19 +271,21 @@ void SlotView::onFocus(gui::GUI* gui) {
void SlotView::bind(
int64_t inventoryid,
ItemStack& stack,
const Content* content,
InventoryInteraction* interaction
const Content* content
) {
this->inventoryid = inventoryid;
bound = &stack;
this->content = content;
this->interaction = interaction;
}
const SlotLayout& SlotView::getLayout() const {
return layout;
}
ItemStack& SlotView::getStack() {
return *bound;
}
InventoryView::InventoryView() : Container(glm::vec2()) {
setColor(glm::vec4(0, 0, 0, 0.0f));
}
@ -319,11 +326,9 @@ size_t InventoryView::getSlotsCount() const {
void InventoryView::bind(
std::shared_ptr<Inventory> inventory,
LevelFrontend* frontend,
InventoryInteraction* interaction
LevelFrontend* frontend
) {
this->frontend = frontend;
this->interaction = interaction;
this->inventory = inventory;
content = frontend->getLevel()->content;
indices = content->getIndices();
@ -331,7 +336,7 @@ void InventoryView::bind(
slot->bind(
inventory->getId(),
inventory->getSlot(slot->getLayout().index),
content, interaction
content
);
}
}

View File

@ -27,16 +27,6 @@ namespace scripting {
using slotcallback = std::function<void(uint, ItemStack&)>;
class InventoryInteraction {
ItemStack grabbedItem;
public:
InventoryInteraction() = default;
ItemStack& getGrabbedItem() {
return grabbedItem;
}
};
struct SlotLayout {
int index;
glm::vec2 position;
@ -59,7 +49,6 @@ struct SlotLayout {
};
class SlotView : public gui::UINode {
InventoryInteraction* interaction = nullptr;
const Content* content;
SlotLayout layout;
bool highlighted = false;
@ -80,11 +69,13 @@ public:
void bind(
int64_t inventoryid,
ItemStack& stack,
const Content* content,
InventoryInteraction* interaction
const Content* content
);
ItemStack& getStack();
const SlotLayout& getLayout() const;
static inline std::string EXCHANGE_SLOT_NAME = "exchange-slot";
};
class InventoryView : public gui::Container {
@ -93,7 +84,6 @@ class InventoryView : public gui::Container {
std::shared_ptr<Inventory> inventory;
LevelFrontend* frontend = nullptr;
InventoryInteraction* interaction = nullptr;
std::vector<SlotView*> slots;
glm::vec2 origin {};
@ -112,8 +102,7 @@ public:
void bind(
std::shared_ptr<Inventory> inventory,
LevelFrontend* frontend,
InventoryInteraction* interaction
LevelFrontend* frontend
);
void unbind();

View File

@ -121,7 +121,7 @@ std::shared_ptr<InventoryView> Hud::createContentAccess() {
InventoryBuilder builder;
builder.addGrid(8, itemsCount-1, glm::vec2(), 8, true, slotLayout);
auto view = builder.build();
view->bind(accessInventory, frontend, interaction.get());
view->bind(accessInventory, frontend);
view->setMargin(glm::vec4());
return view;
}
@ -135,7 +135,7 @@ std::shared_ptr<InventoryView> Hud::createHotbar() {
auto view = builder.build();
view->setOrigin(glm::vec2(view->getSize().x/2, 0));
view->bind(inventory, frontend, interaction.get());
view->bind(inventory, frontend);
view->setInteractive(false);
return view;
}
@ -146,20 +146,6 @@ Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player)
frontend(frontend),
player(player)
{
interaction = std::make_unique<InventoryInteraction>();
grabbedItemView = std::make_shared<SlotView>(
SlotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr)
);
grabbedItemView->bind(
0,
interaction->getGrabbedItem(),
frontend->getLevel()->content,
interaction.get()
);
grabbedItemView->setColor(glm::vec4());
grabbedItemView->setInteractive(false);
grabbedItemView->setZIndex(1);
contentAccess = createContentAccess();
contentAccessPanel = std::make_shared<gui::Panel>(
contentAccess->getSize(), glm::vec4(0.0f), 0.0f
@ -185,7 +171,6 @@ Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player)
gui->add(hotbarView);
gui->add(debugPanel);
gui->add(contentAccessPanel);
gui->add(grabbedItemView);
auto dplotter = std::make_shared<gui::Plotter>(350, 250, 2000, 16);
dplotter->setGravity(gui::Gravity::bottom_right);
@ -194,7 +179,6 @@ Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player)
Hud::~Hud() {
// removing all controlled ui
gui->remove(grabbedItemView);
for (auto& element : elements) {
onRemove(element);
}
@ -304,12 +288,27 @@ void Hud::update(bool visible) {
/// @brief Show inventory on the screen and turn on inventory mode blocking movement
void Hud::openInventory() {
exchangeSlotInv = frontend->getLevel()->inventories->createVirtual(1);
exchangeSlot = std::make_shared<SlotView>(
SlotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr)
);
exchangeSlot->bind(
0,
exchangeSlotInv->getSlot(0),
frontend->getLevel()->content
);
exchangeSlot->setColor(glm::vec4());
exchangeSlot->setInteractive(false);
exchangeSlot->setZIndex(1);
gui->store(SlotView::EXCHANGE_SLOT_NAME, exchangeSlot);
inventoryOpen = true;
auto inventory = player->getInventory();
auto inventoryDocument = assets->getLayout("core:inventory");
inventoryView = std::dynamic_pointer_cast<InventoryView>(inventoryDocument->getRoot());
inventoryView->bind(inventory, frontend, interaction.get());
inventoryView->bind(inventory, frontend);
add(HudElement(hud_element_mode::inventory_bound, inventoryDocument, inventoryView, false));
add(HudElement(hud_element_mode::inventory_bound, nullptr, exchangeSlot, false));
}
void Hud::openInventory(
@ -336,7 +335,7 @@ void Hud::openInventory(
blockinv = level->inventories->createVirtual(blockUI->getSlotsCount());
}
level->chunks->getChunkByVoxel(block.x, block.y, block.z)->setUnsaved(true);
blockUI->bind(blockinv, frontend, interaction.get());
blockUI->bind(blockinv, frontend);
blockPos = block;
currentblockid = level->chunks->get(block.x, block.y, block.z)->id;
add(HudElement(hud_element_mode::inventory_bound, doc, blockUI, false));
@ -362,15 +361,16 @@ void Hud::openPermanent(UiDocument* doc) {
auto invview = std::dynamic_pointer_cast<InventoryView>(root);
if (invview) {
auto inventory = player->getInventory();
invview->bind(inventory, frontend, interaction.get());
invview->bind(inventory, frontend);
}
add(HudElement(hud_element_mode::permanent, doc, doc->getRoot(), false));
}
void Hud::closeInventory() {
gui->remove(SlotView::EXCHANGE_SLOT_NAME);
exchangeSlot = nullptr;
exchangeSlotInv = nullptr;
inventoryOpen = false;
ItemStack& grabbed = interaction->getGrabbedItem();
grabbed.clear();
inventoryView = nullptr;
blockUI = nullptr;
secondUI = nullptr;
@ -495,7 +495,9 @@ void Hud::updateElementsPosition(const Viewport& viewport) {
));
}
}
grabbedItemView->setPos(glm::vec2(Events::cursor));
if (exchangeSlot != nullptr) {
exchangeSlot->setPos(glm::vec2(Events::cursor));
}
hotbarView->setPos(glm::vec2(width/2, height-65));
hotbarView->setSelected(player->getChosenSlot());
}

View File

@ -18,7 +18,6 @@ class Inventory;
class InventoryView;
class LevelFrontend;
class UiDocument;
class InventoryInteraction;
class GfxContext;
class Viewport;
@ -86,10 +85,10 @@ class Hud {
std::shared_ptr<gui::UINode> debugPanel;
/// @brief Overlay used in pause mode
std::shared_ptr<gui::Panel> darkOverlay;
/// @brief Inventories interaction agent (grabbed item and other info)
std::unique_ptr<InventoryInteraction> interaction;
/// @brief Grabbed item visual element
std::shared_ptr<SlotView> grabbedItemView;
/// @brief Inventories interaction agent (grabbed item)
std::shared_ptr<SlotView> exchangeSlot;
/// @brief Exchange slot inventory (1 slot only)
std::shared_ptr<Inventory> exchangeSlotInv = nullptr;
/// @brief List of all controlled hud elements
std::vector<HudElement> elements;
@ -135,7 +134,12 @@ public:
/// @param doc block ui layout
/// @param blockInv block inventory
/// @param playerInventory show player inventory too
void openInventory(glm::ivec3 block, UiDocument* doc, std::shared_ptr<Inventory> blockInv, bool playerInventory);
void openInventory(
glm::ivec3 block,
UiDocument* doc,
std::shared_ptr<Inventory> blockInv,
bool playerInventory
);
/// @brief Show element in inventory-mode
/// @param doc element layout

View File

@ -93,11 +93,6 @@ namespace gui {
/// @param node UI element
void add(std::shared_ptr<UINode> node);
/// @brief Add element to the main container
/// @param node UI element
/// @param coord element position within the main container
void add(std::shared_ptr<UINode> node, glm::vec2 coord);
/// @brief Remove node from the main container
void remove(std::shared_ptr<UINode> node) noexcept;