Merge pull request #732 from MihailRis/ui-slot-item-access

add 'inventory' property to slot ui element
This commit is contained in:
MihailRis 2025-12-10 22:16:57 +03:00 • committed by GitHub
commit b993a62018
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 51 additions and 6 deletions

View file

@ -236,3 +236,12 @@ Properties:
| Name | Type | Read | Write | Description | | Name | Type | Read | Write | Description |
| --------- | ---- | ---- | ----- | ------------------------------------------------- | | --------- | ---- | ---- | ----- | ------------------------------------------------- |
| inventory | int | yes | yes | id of the inventory to which the element is bound | | 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 |
| slotIndex | int | yes | no | index of the slot to which the element is attached |

View file

@ -237,3 +237,12 @@ Canvas.decode(data: Bytearray, format: str) -> Canvas
|-----------|-----|--------|--------|-------------------------------------------| |-----------|-----|--------|--------|-------------------------------------------|
| inventory | int | да | да | id инвентаря, к которому привязан элемент | | inventory | int | да | да | id инвентаря, к которому привязан элемент |
## Слот (slot)
Свойства:
| Название | Тип | Чтение | Запись | Описание |
|-----------|-----|--------|--------|-------------------------------------------|
| inventory | int | да | нет | id инвентаря, к которому привязан элемент |
| slotIndex | int | да | нет | индекс слота, к которому примязан элемент |

View file

@ -470,7 +470,9 @@ void Hud::showExchangeSlot() {
SlotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr) SlotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr)
); );
exchangeSlot->setId("hud.exchange-slot"); 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->setColor(glm::vec4());
exchangeSlot->setInteractive(false); exchangeSlot->setInteractive(false);
exchangeSlot->setZIndex(1); exchangeSlot->setZIndex(1);

View file

@ -382,7 +382,7 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) {
if (layout.rightClick) { if (layout.rightClick) {
layout.rightClick(inventoryid, stack); layout.rightClick(inventoryId, stack);
if (layout.updateFunc) { if (layout.updateFunc) {
layout.updateFunc(layout.index, stack); layout.updateFunc(layout.index, stack);
} }
@ -452,10 +452,11 @@ const std::wstring& SlotView::getTooltip() const {
} }
void SlotView::bind( 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; this->inventoryId = inventoryid;
bound = &stack; bound = &stack;
this->index = index;
this->content = content; this->content = content;
} }
@ -467,6 +468,14 @@ ItemStack& SlotView::getStack() {
return *bound; return *bound;
} }
int64_t SlotView::getInventoryId() const {
return inventoryId;
}
size_t SlotView::getIndex() const {
return index;
}
InventoryView::InventoryView(GUI& gui) : Container(gui, glm::vec2()) { InventoryView::InventoryView(GUI& gui) : Container(gui, glm::vec2()) {
setColor(glm::vec4(0, 0, 0, 0.0f)); setColor(glm::vec4(0, 0, 0, 0.0f));
} }
@ -510,9 +519,11 @@ void InventoryView::bind(
this->inventory = inventory; this->inventory = inventory;
this->content = content; this->content = content;
for (auto slot : slots) { for (auto slot : slots) {
const auto& layout = slot->getLayout();
slot->bind( slot->bind(
inventory->getId(), inventory->getId(),
inventory->getSlot(slot->getLayout().index), inventory->getSlot(layout.index),
layout.index,
content content
); );
} }

View file

@ -61,8 +61,9 @@ namespace gui {
SlotLayout layout; SlotLayout layout;
bool highlighted = false; bool highlighted = false;
int64_t inventoryid = 0; int64_t inventoryId = 0;
ItemStack* bound = nullptr; ItemStack* bound = nullptr;
size_t index = 0;
void performLeftClick(ItemStack& stack, ItemStack& grabbed); void performLeftClick(ItemStack& stack, ItemStack& grabbed);
void performRightClick(ItemStack& stack, ItemStack& grabbed); void performRightClick(ItemStack& stack, ItemStack& grabbed);
@ -100,11 +101,14 @@ namespace gui {
void bind( void bind(
int64_t inventoryid, int64_t inventoryid,
ItemStack& stack, ItemStack& stack,
size_t index,
const Content* content const Content* content
); );
ItemStack& getStack(); ItemStack& getStack();
const SlotLayout& getLayout() const; const SlotLayout& getLayout() const;
int64_t getInventoryId() const;
size_t getIndex() const;
static inline std::string EXCHANGE_SLOT_NAME = "exchange-slot"; static inline std::string EXCHANGE_SLOT_NAME = "exchange-slot";
}; };

View file

@ -209,6 +209,15 @@ static int p_get_inventory(UINode* node, lua::State* L) {
if (auto inventory = dynamic_cast<InventoryView*>(node)) { if (auto inventory = dynamic_cast<InventoryView*>(node)) {
auto inv = inventory->getInventory(); auto inv = inventory->getInventory();
return lua::pushinteger(L, inv ? inv->getId() : 0); return lua::pushinteger(L, inv ? inv->getId() : 0);
} else if (auto slot = dynamic_cast<SlotView*>(node)) {
return lua::pushinteger(L, slot->getInventoryId());
}
return 0;
}
static int p_get_slot_index(UINode* node, lua::State* L) {
if (auto slot = dynamic_cast<SlotView*>(node)) {
return lua::pushinteger(L, slot->getIndex());
} }
return 0; return 0;
} }
@ -661,6 +670,7 @@ static int l_gui_getattr(lua::State* L) {
{"reset", p_get_reset}, {"reset", p_get_reset},
{"paste", p_get_paste}, {"paste", p_get_paste},
{"inventory", p_get_inventory}, {"inventory", p_get_inventory},
{"slotIndex", p_get_slot_index},
{"focused", p_get_focused}, {"focused", p_get_focused},
{"cursor", p_get_cursor}, {"cursor", p_get_cursor},
{"data", p_get_data}, {"data", p_get_data},