hud, InventoryView refactor

This commit is contained in:
MihailRis
2024-04-29 01:47:38 +03:00
parent 83e7cedffe
commit d0678c6bd7
4 changed files with 41 additions and 44 deletions
+2 -4
View File
@@ -326,12 +326,10 @@ size_t InventoryView::getSlotsCount() const {
void InventoryView::bind( void InventoryView::bind(
std::shared_ptr<Inventory> inventory, std::shared_ptr<Inventory> inventory,
LevelFrontend* frontend const Content* content
) { ) {
this->frontend = frontend;
this->inventory = inventory; this->inventory = inventory;
content = frontend->getLevel()->content; this->content = content;
indices = content->getIndices();
for (auto slot : slots) { for (auto slot : slots) {
slot->bind( slot->bind(
inventory->getId(), inventory->getId(),
+1 -3
View File
@@ -80,10 +80,8 @@ public:
class InventoryView : public gui::Container { class InventoryView : public gui::Container {
const Content* content; const Content* content;
const ContentIndices* indices;
std::shared_ptr<Inventory> inventory; std::shared_ptr<Inventory> inventory;
LevelFrontend* frontend = nullptr;
std::vector<SlotView*> slots; std::vector<SlotView*> slots;
glm::vec2 origin {}; glm::vec2 origin {};
@@ -102,7 +100,7 @@ public:
void bind( void bind(
std::shared_ptr<Inventory> inventory, std::shared_ptr<Inventory> inventory,
LevelFrontend* frontend const Content* content
); );
void unbind(); void unbind();
+37 -37
View File
@@ -121,13 +121,14 @@ 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); view->bind(accessInventory, content);
view->setMargin(glm::vec4()); view->setMargin(glm::vec4());
return view; return view;
} }
std::shared_ptr<InventoryView> Hud::createHotbar() { std::shared_ptr<InventoryView> Hud::createHotbar() {
auto inventory = player->getInventory(); auto inventory = player->getInventory();
auto content = frontend->getLevel()->content;
SlotLayout slotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr); SlotLayout slotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr);
InventoryBuilder builder; InventoryBuilder builder;
@@ -135,7 +136,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); view->bind(inventory, content);
view->setInteractive(false); view->setInteractive(false);
return view; return view;
} }
@@ -215,27 +216,31 @@ void Hud::processInput(bool visible) {
} }
} }
if (!pause) { if (!pause) {
if (!inventoryOpen && Events::scroll) { updateHotbarControl();
int slot = player->getChosenSlot(); }
slot = (slot - Events::scroll) % 10; }
if (slot < 0) {
slot += 10; void Hud::updateHotbarControl() {
} if (!inventoryOpen && Events::scroll) {
player->setChosenSlot(slot); int slot = player->getChosenSlot();
slot = (slot - Events::scroll) % 10;
if (slot < 0) {
slot += 10;
} }
for ( player->setChosenSlot(slot);
int i = static_cast<int>(keycode::NUM_1); }
i <= static_cast<int>(keycode::NUM_9); for (
i++ int i = static_cast<int>(keycode::NUM_1);
) { i <= static_cast<int>(keycode::NUM_9);
if (Events::jpressed(i)) { i++
player->setChosenSlot(i - static_cast<int>(keycode::NUM_1)); ) {
} if (Events::jpressed(i)) {
} player->setChosenSlot(i - static_cast<int>(keycode::NUM_1));
if (Events::jpressed(keycode::NUM_0)) {
player->setChosenSlot(9);
} }
} }
if (Events::jpressed(keycode::NUM_0)) {
player->setChosenSlot(9);
}
} }
void Hud::update(bool visible) { 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 /// @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); auto level = frontend->getLevel();
auto content = level->content;
exchangeSlotInv = level->inventories->createVirtual(1);
exchangeSlot = std::make_shared<SlotView>( exchangeSlot = std::make_shared<SlotView>(
SlotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr) SlotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr)
); );
exchangeSlot->bind( exchangeSlot->bind(
0, 0,
exchangeSlotInv->getSlot(0), exchangeSlotInv->getSlot(0),
frontend->getLevel()->content content
); );
exchangeSlot->setColor(glm::vec4()); exchangeSlot->setColor(glm::vec4());
exchangeSlot->setInteractive(false); exchangeSlot->setInteractive(false);
@@ -306,7 +313,7 @@ void Hud::openInventory() {
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); inventoryView->bind(inventory, content);
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)); add(HudElement(hud_element_mode::inventory_bound, nullptr, exchangeSlot, false));
} }
@@ -321,6 +328,7 @@ void Hud::openInventory(
closeInventory(); closeInventory();
} }
auto level = frontend->getLevel(); auto level = frontend->getLevel();
auto content = level->content;
blockUI = std::dynamic_pointer_cast<InventoryView>(doc->getRoot()); blockUI = std::dynamic_pointer_cast<InventoryView>(doc->getRoot());
if (blockUI == nullptr) { if (blockUI == nullptr) {
throw std::runtime_error("block UI root element must be 'inventory'"); throw std::runtime_error("block UI root element must be 'inventory'");
@@ -335,7 +343,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); blockUI->bind(blockinv, content);
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));
@@ -360,7 +368,7 @@ 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) {
invview->bind(player->getInventory(), frontend); invview->bind(player->getInventory(), frontend->getLevel()->content);
} }
add(HudElement(hud_element_mode::permanent, doc, doc->getRoot(), false)); 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++) { for (int i = 0; i < 3; i++) {
args.push_back(Value::of(static_cast<integer_t>(blockPos[i]))); args.push_back(Value::of(static_cast<integer_t>(blockPos[i])));
} }
scripting::on_ui_open(
if (invview) { element.getDocument(),
scripting::on_ui_open( std::move(args)
element.getDocument(), );
std::move(args)
);
} else {
scripting::on_ui_open(
element.getDocument(),
std::move(args)
);
}
} }
elements.push_back(element); elements.push_back(element);
} }
+1
View File
@@ -109,6 +109,7 @@ class Hud {
void processInput(bool visible); void processInput(bool visible);
void updateElementsPosition(const Viewport& viewport); void updateElementsPosition(const Viewport& viewport);
void updateHotbarControl();
void cleanup(); void cleanup();
public: public:
Hud(Engine* engine, LevelFrontend* frontend, Player* player); Hud(Engine* engine, LevelFrontend* frontend, Player* player);