From 4d99508ca116f163701807cd4b07a9206c19c062 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 10 Dec 2025 21:45:22 +0300 Subject: [PATCH 1/4] add 'inventory' property to slot ui element --- src/graphics/ui/elements/InventoryView.cpp | 8 ++++++-- src/graphics/ui/elements/InventoryView.hpp | 3 ++- src/logic/scripting/lua/libs/libgui.cpp | 2 ++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index d3a747225..1df173951 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -382,7 +382,7 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { if (layout.rightClick) { - layout.rightClick(inventoryid, stack); + layout.rightClick(inventoryId, stack); if (layout.updateFunc) { layout.updateFunc(layout.index, stack); } @@ -454,7 +454,7 @@ const std::wstring& SlotView::getTooltip() const { void SlotView::bind( int64_t inventoryid, ItemStack& stack, const Content* content ) { - this->inventoryid = inventoryid; + this->inventoryId = inventoryid; bound = &stack; this->content = content; } @@ -467,6 +467,10 @@ ItemStack& SlotView::getStack() { return *bound; } +int64_t SlotView::getInventoryId() const { + return inventoryId; +} + InventoryView::InventoryView(GUI& gui) : Container(gui, glm::vec2()) { setColor(glm::vec4(0, 0, 0, 0.0f)); } diff --git a/src/graphics/ui/elements/InventoryView.hpp b/src/graphics/ui/elements/InventoryView.hpp index b80f65e68..4dbb9afcf 100644 --- a/src/graphics/ui/elements/InventoryView.hpp +++ b/src/graphics/ui/elements/InventoryView.hpp @@ -61,7 +61,7 @@ namespace gui { SlotLayout layout; bool highlighted = false; - int64_t inventoryid = 0; + int64_t inventoryId = 0; ItemStack* bound = nullptr; void performLeftClick(ItemStack& stack, ItemStack& grabbed); @@ -105,6 +105,7 @@ namespace gui { ItemStack& getStack(); const SlotLayout& getLayout() const; + int64_t getInventoryId() const; static inline std::string EXCHANGE_SLOT_NAME = "exchange-slot"; }; diff --git a/src/logic/scripting/lua/libs/libgui.cpp b/src/logic/scripting/lua/libs/libgui.cpp index b08d54455..31147e767 100644 --- a/src/logic/scripting/lua/libs/libgui.cpp +++ b/src/logic/scripting/lua/libs/libgui.cpp @@ -209,6 +209,8 @@ static int p_get_inventory(UINode* node, lua::State* L) { if (auto inventory = dynamic_cast(node)) { auto inv = inventory->getInventory(); return lua::pushinteger(L, inv ? inv->getId() : 0); + } else if (auto slot = dynamic_cast(node)) { + return lua::pushinteger(L, slot->getInventoryId()); } return 0; } From 1ef174f204abfdce98f59a731ab70625f5feca66 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 10 Dec 2025 21:47:20 +0300 Subject: [PATCH 2/4] update doc/*/scripting/ui.md --- doc/en/scripting/ui.md | 8 ++++++++ doc/ru/scripting/ui.md | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/doc/en/scripting/ui.md b/doc/en/scripting/ui.md index 7ce304b8c..02cebe709 100644 --- a/doc/en/scripting/ui.md +++ b/doc/en/scripting/ui.md @@ -236,3 +236,11 @@ Properties: | Name | Type | Read | Write | Description | | --------- | ---- | ---- | ----- | ------------------------------------------------- | | inventory | int | yes | yes | id of the inventory to which the element is bound | + +## Slot + +Properties: + +| Name | Type | Read | Write | Description | +| --------- | ---- | ---- | ----- | ------------------------------------------------- | +| inventory | int | yes | no | id of the inventory to which the element is bound | diff --git a/doc/ru/scripting/ui.md b/doc/ru/scripting/ui.md index 963e8b004..4d1fee695 100644 --- a/doc/ru/scripting/ui.md +++ b/doc/ru/scripting/ui.md @@ -237,3 +237,11 @@ Canvas.decode(data: Bytearray, format: str) -> Canvas |-----------|-----|--------|--------|-------------------------------------------| | inventory | int | да | да | id инвентаря, к которому привязан элемент | +## Слот (slot) + +Свойства: + +| Название | Тип | Чтение | Запись | Описание | +|-----------|-----|--------|--------|-------------------------------------------| +| inventory | int | да | нет | id инвентаря, к которому привязан элемент | + From dfa96568714f3eefb7f099fa87d964931e2cbe24 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 10 Dec 2025 21:58:08 +0300 Subject: [PATCH 3/4] add 'slotIndex' ui slot property --- src/frontend/hud.cpp | 4 +++- src/graphics/ui/elements/InventoryView.cpp | 11 +++++++++-- src/graphics/ui/elements/InventoryView.hpp | 3 +++ src/logic/scripting/lua/libs/libgui.cpp | 8 ++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 6ddc6744c..d40ab3b8e 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -470,7 +470,9 @@ void Hud::showExchangeSlot() { SlotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr) ); exchangeSlot->setId("hud.exchange-slot"); - exchangeSlot->bind(exchangeSlotInv->getId(), exchangeSlotInv->getSlot(0), &content); + exchangeSlot->bind( + exchangeSlotInv->getId(), exchangeSlotInv->getSlot(0), 0, &content + ); exchangeSlot->setColor(glm::vec4()); exchangeSlot->setInteractive(false); exchangeSlot->setZIndex(1); diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index 1df173951..2d610c7ca 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -452,10 +452,11 @@ const std::wstring& SlotView::getTooltip() const { } void SlotView::bind( - int64_t inventoryid, ItemStack& stack, const Content* content + int64_t inventoryid, ItemStack& stack, size_t index, const Content* content ) { this->inventoryId = inventoryid; bound = &stack; + this->index = index; this->content = content; } @@ -471,6 +472,10 @@ int64_t SlotView::getInventoryId() const { return inventoryId; } +size_t SlotView::getIndex() const { + return index; +} + InventoryView::InventoryView(GUI& gui) : Container(gui, glm::vec2()) { setColor(glm::vec4(0, 0, 0, 0.0f)); } @@ -514,9 +519,11 @@ void InventoryView::bind( this->inventory = inventory; this->content = content; for (auto slot : slots) { + const auto& layout = slot->getLayout(); slot->bind( inventory->getId(), - inventory->getSlot(slot->getLayout().index), + inventory->getSlot(layout.index), + layout.index, content ); } diff --git a/src/graphics/ui/elements/InventoryView.hpp b/src/graphics/ui/elements/InventoryView.hpp index 4dbb9afcf..9275a2daa 100644 --- a/src/graphics/ui/elements/InventoryView.hpp +++ b/src/graphics/ui/elements/InventoryView.hpp @@ -63,6 +63,7 @@ namespace gui { int64_t inventoryId = 0; ItemStack* bound = nullptr; + size_t index = 0; void performLeftClick(ItemStack& stack, ItemStack& grabbed); void performRightClick(ItemStack& stack, ItemStack& grabbed); @@ -100,12 +101,14 @@ namespace gui { void bind( int64_t inventoryid, ItemStack& stack, + size_t index, const Content* content ); ItemStack& getStack(); const SlotLayout& getLayout() const; int64_t getInventoryId() const; + size_t getIndex() const; static inline std::string EXCHANGE_SLOT_NAME = "exchange-slot"; }; diff --git a/src/logic/scripting/lua/libs/libgui.cpp b/src/logic/scripting/lua/libs/libgui.cpp index 31147e767..e8cc70186 100644 --- a/src/logic/scripting/lua/libs/libgui.cpp +++ b/src/logic/scripting/lua/libs/libgui.cpp @@ -215,6 +215,13 @@ static int p_get_inventory(UINode* node, lua::State* L) { return 0; } +static int p_get_slot_index(UINode* node, lua::State* L) { + if (auto slot = dynamic_cast(node)) { + return lua::pushinteger(L, slot->getIndex()); + } + return 0; +} + static int p_get_reset(UINode* node, lua::State* L) { if (dynamic_cast(node)) { return lua::pushcfunction(L, l_menu_reset); @@ -663,6 +670,7 @@ static int l_gui_getattr(lua::State* L) { {"reset", p_get_reset}, {"paste", p_get_paste}, {"inventory", p_get_inventory}, + {"slotIndex", p_get_slot_index}, {"focused", p_get_focused}, {"cursor", p_get_cursor}, {"data", p_get_data}, From ecdaf186ebdf797755622c188fbb5901268e03b0 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 10 Dec 2025 22:00:09 +0300 Subject: [PATCH 4/4] update doc/*/scripting/ui.md --- doc/en/scripting/ui.md | 7 ++++--- doc/ru/scripting/ui.md | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/doc/en/scripting/ui.md b/doc/en/scripting/ui.md index 02cebe709..22a878944 100644 --- a/doc/en/scripting/ui.md +++ b/doc/en/scripting/ui.md @@ -241,6 +241,7 @@ Properties: Properties: -| Name | Type | Read | Write | Description | -| --------- | ---- | ---- | ----- | ------------------------------------------------- | -| inventory | int | yes | no | id of the inventory to which the element is bound | +| Name | Type | Read | Write | Description | +| --------- | ---- | ---- | ----- | -------------------------------------------------- | +| inventory | int | yes | no | id of the inventory to which the element is bound | +| slotIndex | int | yes | no | index of the slot to which the element is attached | diff --git a/doc/ru/scripting/ui.md b/doc/ru/scripting/ui.md index 4d1fee695..05b448a43 100644 --- a/doc/ru/scripting/ui.md +++ b/doc/ru/scripting/ui.md @@ -244,4 +244,5 @@ Canvas.decode(data: Bytearray, format: str) -> Canvas | Название | Тип | Чтение | Запись | Описание | |-----------|-----|--------|--------|-------------------------------------------| | inventory | int | да | нет | id инвентаря, к которому привязан элемент | +| slotIndex | int | да | нет | индекс слота, к которому примязан элемент |