add 'slotIndex' ui slot property

This commit is contained in:
MihailRis 2025-12-10 21:58:08 +03:00
parent 1ef174f204
commit dfa9656871
4 changed files with 23 additions and 3 deletions

View file

@ -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);

View file

@ -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
);
}

View file

@ -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";
};

View file

@ -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<SlotView*>(node)) {
return lua::pushinteger(L, slot->getIndex());
}
return 0;
}
static int p_get_reset(UINode* node, lua::State* L) {
if (dynamic_cast<Menu*>(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},