diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp index 7d025bcf..5a81d330 100644 --- a/src/frontend/InventoryView.cpp +++ b/src/frontend/InventoryView.cpp @@ -326,12 +326,10 @@ size_t InventoryView::getSlotsCount() const { void InventoryView::bind( std::shared_ptr inventory, - LevelFrontend* frontend + const Content* content ) { - this->frontend = frontend; this->inventory = inventory; - content = frontend->getLevel()->content; - indices = content->getIndices(); + this->content = content; for (auto slot : slots) { slot->bind( inventory->getId(), diff --git a/src/frontend/InventoryView.h b/src/frontend/InventoryView.h index 2e0bfd3d..9cdd8322 100644 --- a/src/frontend/InventoryView.h +++ b/src/frontend/InventoryView.h @@ -80,10 +80,8 @@ public: class InventoryView : public gui::Container { const Content* content; - const ContentIndices* indices; std::shared_ptr inventory; - LevelFrontend* frontend = nullptr; std::vector slots; glm::vec2 origin {}; @@ -102,7 +100,7 @@ public: void bind( std::shared_ptr inventory, - LevelFrontend* frontend + const Content* content ); void unbind(); diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index bcbf5d03..7fa30c41 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -121,13 +121,14 @@ 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); + view->bind(accessInventory, content); view->setMargin(glm::vec4()); return view; } std::shared_ptr Hud::createHotbar() { auto inventory = player->getInventory(); + auto content = frontend->getLevel()->content; SlotLayout slotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr); InventoryBuilder builder; @@ -135,7 +136,7 @@ std::shared_ptr Hud::createHotbar() { auto view = builder.build(); view->setOrigin(glm::vec2(view->getSize().x/2, 0)); - view->bind(inventory, frontend); + view->bind(inventory, content); view->setInteractive(false); return view; } @@ -215,27 +216,31 @@ void Hud::processInput(bool visible) { } } if (!pause) { - if (!inventoryOpen && Events::scroll) { - int slot = player->getChosenSlot(); - slot = (slot - Events::scroll) % 10; - if (slot < 0) { - slot += 10; - } - player->setChosenSlot(slot); + updateHotbarControl(); + } +} + +void Hud::updateHotbarControl() { + if (!inventoryOpen && Events::scroll) { + int slot = player->getChosenSlot(); + slot = (slot - Events::scroll) % 10; + if (slot < 0) { + slot += 10; } - for ( - int i = static_cast(keycode::NUM_1); - i <= static_cast(keycode::NUM_9); - i++ - ) { - if (Events::jpressed(i)) { - player->setChosenSlot(i - static_cast(keycode::NUM_1)); - } - } - if (Events::jpressed(keycode::NUM_0)) { - player->setChosenSlot(9); + player->setChosenSlot(slot); + } + for ( + int i = static_cast(keycode::NUM_1); + i <= static_cast(keycode::NUM_9); + i++ + ) { + if (Events::jpressed(i)) { + player->setChosenSlot(i - static_cast(keycode::NUM_1)); } } + if (Events::jpressed(keycode::NUM_0)) { + player->setChosenSlot(9); + } } void Hud::update(bool visible) { @@ -288,14 +293,16 @@ 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); + auto level = frontend->getLevel(); + auto content = level->content; + exchangeSlotInv = level->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 + content ); exchangeSlot->setColor(glm::vec4()); exchangeSlot->setInteractive(false); @@ -306,7 +313,7 @@ void Hud::openInventory() { auto inventory = player->getInventory(); auto inventoryDocument = assets->getLayout("core:inventory"); inventoryView = std::dynamic_pointer_cast(inventoryDocument->getRoot()); - inventoryView->bind(inventory, frontend); + inventoryView->bind(inventory, content); add(HudElement(hud_element_mode::inventory_bound, inventoryDocument, inventoryView, false)); add(HudElement(hud_element_mode::inventory_bound, nullptr, exchangeSlot, false)); } @@ -321,6 +328,7 @@ void Hud::openInventory( closeInventory(); } auto level = frontend->getLevel(); + auto content = level->content; blockUI = std::dynamic_pointer_cast(doc->getRoot()); if (blockUI == nullptr) { throw std::runtime_error("block UI root element must be 'inventory'"); @@ -335,7 +343,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); + blockUI->bind(blockinv, content); blockPos = block; currentblockid = level->chunks->get(block.x, block.y, block.z)->id; add(HudElement(hud_element_mode::inventory_bound, doc, blockUI, false)); @@ -360,7 +368,7 @@ void Hud::openPermanent(UiDocument* doc) { auto invview = std::dynamic_pointer_cast(root); if (invview) { - invview->bind(player->getInventory(), frontend); + invview->bind(player->getInventory(), frontend->getLevel()->content); } add(HudElement(hud_element_mode::permanent, doc, doc->getRoot(), false)); } @@ -388,18 +396,10 @@ void Hud::add(HudElement element) { for (int i = 0; i < 3; i++) { args.push_back(Value::of(static_cast(blockPos[i]))); } - - if (invview) { - scripting::on_ui_open( - element.getDocument(), - std::move(args) - ); - } else { - scripting::on_ui_open( - element.getDocument(), - std::move(args) - ); - } + scripting::on_ui_open( + element.getDocument(), + std::move(args) + ); } elements.push_back(element); } diff --git a/src/frontend/hud.h b/src/frontend/hud.h index f756598a..17f58da6 100644 --- a/src/frontend/hud.h +++ b/src/frontend/hud.h @@ -109,6 +109,7 @@ class Hud { void processInput(bool visible); void updateElementsPosition(const Viewport& viewport); + void updateHotbarControl(); void cleanup(); public: Hud(Engine* engine, LevelFrontend* frontend, Player* player);