From c561466f6439449e74f28fcc36dcaa38f5018816 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 29 Apr 2024 00:14:16 +0300 Subject: [PATCH] InventoryInteraction removed due to redundancy --- src/frontend/InventoryView.cpp | 21 ++++++++------ src/frontend/InventoryView.h | 21 ++++---------- src/frontend/hud.cpp | 50 ++++++++++++++++++---------------- src/frontend/hud.h | 16 +++++++---- src/graphics/ui/GUI.hpp | 5 ---- 5 files changed, 54 insertions(+), 59 deletions(-) diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp index ec4a2d0c..7d025bcf 100644 --- a/src/frontend/InventoryView.cpp +++ b/src/frontend/InventoryView.cpp @@ -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(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, - 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 ); } } diff --git a/src/frontend/InventoryView.h b/src/frontend/InventoryView.h index 95f66ab9..2e0bfd3d 100644 --- a/src/frontend/InventoryView.h +++ b/src/frontend/InventoryView.h @@ -27,16 +27,6 @@ namespace scripting { using slotcallback = std::function; -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; LevelFrontend* frontend = nullptr; - InventoryInteraction* interaction = nullptr; std::vector slots; glm::vec2 origin {}; @@ -112,8 +102,7 @@ public: void bind( std::shared_ptr inventory, - LevelFrontend* frontend, - InventoryInteraction* interaction + LevelFrontend* frontend ); void unbind(); diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index f1c0b3c1..5ac59be6 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -121,7 +121,7 @@ std::shared_ptr 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 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(); - grabbedItemView = std::make_shared( - 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( 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(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( + 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(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(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()); } diff --git a/src/frontend/hud.h b/src/frontend/hud.h index 981afd4d..f6f06bcf 100644 --- a/src/frontend/hud.h +++ b/src/frontend/hud.h @@ -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 debugPanel; /// @brief Overlay used in pause mode std::shared_ptr darkOverlay; - /// @brief Inventories interaction agent (grabbed item and other info) - std::unique_ptr interaction; - /// @brief Grabbed item visual element - std::shared_ptr grabbedItemView; + /// @brief Inventories interaction agent (grabbed item) + std::shared_ptr exchangeSlot; + /// @brief Exchange slot inventory (1 slot only) + std::shared_ptr exchangeSlotInv = nullptr; /// @brief List of all controlled hud elements std::vector 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 blockInv, bool playerInventory); + void openInventory( + glm::ivec3 block, + UiDocument* doc, + std::shared_ptr blockInv, + bool playerInventory + ); /// @brief Show element in inventory-mode /// @param doc element layout diff --git a/src/graphics/ui/GUI.hpp b/src/graphics/ui/GUI.hpp index d18972e2..9eb08da0 100644 --- a/src/graphics/ui/GUI.hpp +++ b/src/graphics/ui/GUI.hpp @@ -93,11 +93,6 @@ namespace gui { /// @param node UI element void add(std::shared_ptr 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 node, glm::vec2 coord); - /// @brief Remove node from the main container void remove(std::shared_ptr node) noexcept;