InventoryInteraction removed due to redundancy
This commit is contained in:
parent
ef28a368cb
commit
c561466f64
@ -10,6 +10,7 @@
|
|||||||
#include "../graphics/core/Font.hpp"
|
#include "../graphics/core/Font.hpp"
|
||||||
#include "../graphics/core/GfxContext.hpp"
|
#include "../graphics/core/GfxContext.hpp"
|
||||||
#include "../graphics/core/Shader.hpp"
|
#include "../graphics/core/Shader.hpp"
|
||||||
|
#include "../graphics/ui/GUI.hpp"
|
||||||
#include "../graphics/render/BlocksPreview.hpp"
|
#include "../graphics/render/BlocksPreview.hpp"
|
||||||
#include "../items/Inventories.h"
|
#include "../items/Inventories.h"
|
||||||
#include "../items/Inventory.h"
|
#include "../items/Inventory.h"
|
||||||
@ -200,7 +201,11 @@ void SlotView::clicked(gui::GUI* gui, mousecode button) {
|
|||||||
if (bound == nullptr)
|
if (bound == nullptr)
|
||||||
return;
|
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;
|
ItemStack& stack = *bound;
|
||||||
|
|
||||||
if (button == mousecode::BUTTON_1) {
|
if (button == mousecode::BUTTON_1) {
|
||||||
@ -266,19 +271,21 @@ void SlotView::onFocus(gui::GUI* gui) {
|
|||||||
void SlotView::bind(
|
void SlotView::bind(
|
||||||
int64_t inventoryid,
|
int64_t inventoryid,
|
||||||
ItemStack& stack,
|
ItemStack& stack,
|
||||||
const Content* content,
|
const Content* content
|
||||||
InventoryInteraction* interaction
|
|
||||||
) {
|
) {
|
||||||
this->inventoryid = inventoryid;
|
this->inventoryid = inventoryid;
|
||||||
bound = &stack;
|
bound = &stack;
|
||||||
this->content = content;
|
this->content = content;
|
||||||
this->interaction = interaction;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const SlotLayout& SlotView::getLayout() const {
|
const SlotLayout& SlotView::getLayout() const {
|
||||||
return layout;
|
return layout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ItemStack& SlotView::getStack() {
|
||||||
|
return *bound;
|
||||||
|
}
|
||||||
|
|
||||||
InventoryView::InventoryView() : Container(glm::vec2()) {
|
InventoryView::InventoryView() : Container(glm::vec2()) {
|
||||||
setColor(glm::vec4(0, 0, 0, 0.0f));
|
setColor(glm::vec4(0, 0, 0, 0.0f));
|
||||||
}
|
}
|
||||||
@ -319,11 +326,9 @@ size_t InventoryView::getSlotsCount() const {
|
|||||||
|
|
||||||
void InventoryView::bind(
|
void InventoryView::bind(
|
||||||
std::shared_ptr<Inventory> inventory,
|
std::shared_ptr<Inventory> inventory,
|
||||||
LevelFrontend* frontend,
|
LevelFrontend* frontend
|
||||||
InventoryInteraction* interaction
|
|
||||||
) {
|
) {
|
||||||
this->frontend = frontend;
|
this->frontend = frontend;
|
||||||
this->interaction = interaction;
|
|
||||||
this->inventory = inventory;
|
this->inventory = inventory;
|
||||||
content = frontend->getLevel()->content;
|
content = frontend->getLevel()->content;
|
||||||
indices = content->getIndices();
|
indices = content->getIndices();
|
||||||
@ -331,7 +336,7 @@ void InventoryView::bind(
|
|||||||
slot->bind(
|
slot->bind(
|
||||||
inventory->getId(),
|
inventory->getId(),
|
||||||
inventory->getSlot(slot->getLayout().index),
|
inventory->getSlot(slot->getLayout().index),
|
||||||
content, interaction
|
content
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,16 +27,6 @@ namespace scripting {
|
|||||||
|
|
||||||
using slotcallback = std::function<void(uint, ItemStack&)>;
|
using slotcallback = std::function<void(uint, ItemStack&)>;
|
||||||
|
|
||||||
class InventoryInteraction {
|
|
||||||
ItemStack grabbedItem;
|
|
||||||
public:
|
|
||||||
InventoryInteraction() = default;
|
|
||||||
|
|
||||||
ItemStack& getGrabbedItem() {
|
|
||||||
return grabbedItem;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct SlotLayout {
|
struct SlotLayout {
|
||||||
int index;
|
int index;
|
||||||
glm::vec2 position;
|
glm::vec2 position;
|
||||||
@ -59,7 +49,6 @@ struct SlotLayout {
|
|||||||
};
|
};
|
||||||
|
|
||||||
class SlotView : public gui::UINode {
|
class SlotView : public gui::UINode {
|
||||||
InventoryInteraction* interaction = nullptr;
|
|
||||||
const Content* content;
|
const Content* content;
|
||||||
SlotLayout layout;
|
SlotLayout layout;
|
||||||
bool highlighted = false;
|
bool highlighted = false;
|
||||||
@ -80,11 +69,13 @@ public:
|
|||||||
void bind(
|
void bind(
|
||||||
int64_t inventoryid,
|
int64_t inventoryid,
|
||||||
ItemStack& stack,
|
ItemStack& stack,
|
||||||
const Content* content,
|
const Content* content
|
||||||
InventoryInteraction* interaction
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
ItemStack& getStack();
|
||||||
const SlotLayout& getLayout() const;
|
const SlotLayout& getLayout() const;
|
||||||
|
|
||||||
|
static inline std::string EXCHANGE_SLOT_NAME = "exchange-slot";
|
||||||
};
|
};
|
||||||
|
|
||||||
class InventoryView : public gui::Container {
|
class InventoryView : public gui::Container {
|
||||||
@ -93,7 +84,6 @@ class InventoryView : public gui::Container {
|
|||||||
|
|
||||||
std::shared_ptr<Inventory> inventory;
|
std::shared_ptr<Inventory> inventory;
|
||||||
LevelFrontend* frontend = nullptr;
|
LevelFrontend* frontend = nullptr;
|
||||||
InventoryInteraction* interaction = nullptr;
|
|
||||||
|
|
||||||
std::vector<SlotView*> slots;
|
std::vector<SlotView*> slots;
|
||||||
glm::vec2 origin {};
|
glm::vec2 origin {};
|
||||||
@ -112,8 +102,7 @@ public:
|
|||||||
|
|
||||||
void bind(
|
void bind(
|
||||||
std::shared_ptr<Inventory> inventory,
|
std::shared_ptr<Inventory> inventory,
|
||||||
LevelFrontend* frontend,
|
LevelFrontend* frontend
|
||||||
InventoryInteraction* interaction
|
|
||||||
);
|
);
|
||||||
|
|
||||||
void unbind();
|
void unbind();
|
||||||
|
|||||||
@ -121,7 +121,7 @@ std::shared_ptr<InventoryView> Hud::createContentAccess() {
|
|||||||
InventoryBuilder builder;
|
InventoryBuilder builder;
|
||||||
builder.addGrid(8, itemsCount-1, glm::vec2(), 8, true, slotLayout);
|
builder.addGrid(8, itemsCount-1, glm::vec2(), 8, true, slotLayout);
|
||||||
auto view = builder.build();
|
auto view = builder.build();
|
||||||
view->bind(accessInventory, frontend, interaction.get());
|
view->bind(accessInventory, frontend);
|
||||||
view->setMargin(glm::vec4());
|
view->setMargin(glm::vec4());
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
@ -135,7 +135,7 @@ std::shared_ptr<InventoryView> Hud::createHotbar() {
|
|||||||
auto view = builder.build();
|
auto view = builder.build();
|
||||||
|
|
||||||
view->setOrigin(glm::vec2(view->getSize().x/2, 0));
|
view->setOrigin(glm::vec2(view->getSize().x/2, 0));
|
||||||
view->bind(inventory, frontend, interaction.get());
|
view->bind(inventory, frontend);
|
||||||
view->setInteractive(false);
|
view->setInteractive(false);
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
@ -146,20 +146,6 @@ Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player)
|
|||||||
frontend(frontend),
|
frontend(frontend),
|
||||||
player(player)
|
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();
|
contentAccess = createContentAccess();
|
||||||
contentAccessPanel = std::make_shared<gui::Panel>(
|
contentAccessPanel = std::make_shared<gui::Panel>(
|
||||||
contentAccess->getSize(), glm::vec4(0.0f), 0.0f
|
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(hotbarView);
|
||||||
gui->add(debugPanel);
|
gui->add(debugPanel);
|
||||||
gui->add(contentAccessPanel);
|
gui->add(contentAccessPanel);
|
||||||
gui->add(grabbedItemView);
|
|
||||||
|
|
||||||
auto dplotter = std::make_shared<gui::Plotter>(350, 250, 2000, 16);
|
auto dplotter = std::make_shared<gui::Plotter>(350, 250, 2000, 16);
|
||||||
dplotter->setGravity(gui::Gravity::bottom_right);
|
dplotter->setGravity(gui::Gravity::bottom_right);
|
||||||
@ -194,7 +179,6 @@ Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player)
|
|||||||
|
|
||||||
Hud::~Hud() {
|
Hud::~Hud() {
|
||||||
// removing all controlled ui
|
// removing all controlled ui
|
||||||
gui->remove(grabbedItemView);
|
|
||||||
for (auto& element : elements) {
|
for (auto& element : elements) {
|
||||||
onRemove(element);
|
onRemove(element);
|
||||||
}
|
}
|
||||||
@ -304,12 +288,27 @@ void Hud::update(bool visible) {
|
|||||||
|
|
||||||
/// @brief Show inventory on the screen and turn on inventory mode blocking movement
|
/// @brief Show inventory on the screen and turn on inventory mode blocking movement
|
||||||
void Hud::openInventory() {
|
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;
|
inventoryOpen = true;
|
||||||
auto inventory = player->getInventory();
|
auto inventory = player->getInventory();
|
||||||
auto inventoryDocument = assets->getLayout("core:inventory");
|
auto inventoryDocument = assets->getLayout("core:inventory");
|
||||||
inventoryView = std::dynamic_pointer_cast<InventoryView>(inventoryDocument->getRoot());
|
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, inventoryDocument, inventoryView, false));
|
||||||
|
add(HudElement(hud_element_mode::inventory_bound, nullptr, exchangeSlot, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Hud::openInventory(
|
void Hud::openInventory(
|
||||||
@ -336,7 +335,7 @@ void Hud::openInventory(
|
|||||||
blockinv = level->inventories->createVirtual(blockUI->getSlotsCount());
|
blockinv = level->inventories->createVirtual(blockUI->getSlotsCount());
|
||||||
}
|
}
|
||||||
level->chunks->getChunkByVoxel(block.x, block.y, block.z)->setUnsaved(true);
|
level->chunks->getChunkByVoxel(block.x, block.y, block.z)->setUnsaved(true);
|
||||||
blockUI->bind(blockinv, frontend, interaction.get());
|
blockUI->bind(blockinv, frontend);
|
||||||
blockPos = block;
|
blockPos = block;
|
||||||
currentblockid = level->chunks->get(block.x, block.y, block.z)->id;
|
currentblockid = level->chunks->get(block.x, block.y, block.z)->id;
|
||||||
add(HudElement(hud_element_mode::inventory_bound, doc, blockUI, false));
|
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);
|
auto invview = std::dynamic_pointer_cast<InventoryView>(root);
|
||||||
if (invview) {
|
if (invview) {
|
||||||
auto inventory = player->getInventory();
|
auto inventory = player->getInventory();
|
||||||
invview->bind(inventory, frontend, interaction.get());
|
invview->bind(inventory, frontend);
|
||||||
}
|
}
|
||||||
add(HudElement(hud_element_mode::permanent, doc, doc->getRoot(), false));
|
add(HudElement(hud_element_mode::permanent, doc, doc->getRoot(), false));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Hud::closeInventory() {
|
void Hud::closeInventory() {
|
||||||
|
gui->remove(SlotView::EXCHANGE_SLOT_NAME);
|
||||||
|
exchangeSlot = nullptr;
|
||||||
|
exchangeSlotInv = nullptr;
|
||||||
inventoryOpen = false;
|
inventoryOpen = false;
|
||||||
ItemStack& grabbed = interaction->getGrabbedItem();
|
|
||||||
grabbed.clear();
|
|
||||||
inventoryView = nullptr;
|
inventoryView = nullptr;
|
||||||
blockUI = nullptr;
|
blockUI = nullptr;
|
||||||
secondUI = 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->setPos(glm::vec2(width/2, height-65));
|
||||||
hotbarView->setSelected(player->getChosenSlot());
|
hotbarView->setSelected(player->getChosenSlot());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,6 @@ class Inventory;
|
|||||||
class InventoryView;
|
class InventoryView;
|
||||||
class LevelFrontend;
|
class LevelFrontend;
|
||||||
class UiDocument;
|
class UiDocument;
|
||||||
class InventoryInteraction;
|
|
||||||
class GfxContext;
|
class GfxContext;
|
||||||
class Viewport;
|
class Viewport;
|
||||||
|
|
||||||
@ -86,10 +85,10 @@ class Hud {
|
|||||||
std::shared_ptr<gui::UINode> debugPanel;
|
std::shared_ptr<gui::UINode> debugPanel;
|
||||||
/// @brief Overlay used in pause mode
|
/// @brief Overlay used in pause mode
|
||||||
std::shared_ptr<gui::Panel> darkOverlay;
|
std::shared_ptr<gui::Panel> darkOverlay;
|
||||||
/// @brief Inventories interaction agent (grabbed item and other info)
|
/// @brief Inventories interaction agent (grabbed item)
|
||||||
std::unique_ptr<InventoryInteraction> interaction;
|
std::shared_ptr<SlotView> exchangeSlot;
|
||||||
/// @brief Grabbed item visual element
|
/// @brief Exchange slot inventory (1 slot only)
|
||||||
std::shared_ptr<SlotView> grabbedItemView;
|
std::shared_ptr<Inventory> exchangeSlotInv = nullptr;
|
||||||
/// @brief List of all controlled hud elements
|
/// @brief List of all controlled hud elements
|
||||||
std::vector<HudElement> elements;
|
std::vector<HudElement> elements;
|
||||||
|
|
||||||
@ -135,7 +134,12 @@ public:
|
|||||||
/// @param doc block ui layout
|
/// @param doc block ui layout
|
||||||
/// @param blockInv block inventory
|
/// @param blockInv block inventory
|
||||||
/// @param playerInventory show player inventory too
|
/// @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
|
/// @brief Show element in inventory-mode
|
||||||
/// @param doc element layout
|
/// @param doc element layout
|
||||||
|
|||||||
@ -93,11 +93,6 @@ namespace gui {
|
|||||||
/// @param node UI element
|
/// @param node UI element
|
||||||
void add(std::shared_ptr<UINode> node);
|
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
|
/// @brief Remove node from the main container
|
||||||
void remove(std::shared_ptr<UINode> node) noexcept;
|
void remove(std::shared_ptr<UINode> node) noexcept;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user