diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp index cc61cb91..d3a16549 100644 --- a/src/frontend/InventoryView.cpp +++ b/src/frontend/InventoryView.cpp @@ -30,7 +30,7 @@ SlotLayout::SlotLayout( glm::vec2 position, bool background, bool itemSource, - itemsharefunc shareFunc, + slotcallback shareFunc, slotcallback rightClick ) : index(index), @@ -234,7 +234,7 @@ void SlotView::clicked(gui::GUI* gui, int button) { } } else if (button == mousecode::BUTTON_2) { if (layout.rightClick) { - layout.rightClick(stack, grabbed); + layout.rightClick(inventoryid, stack); return; } if (layout.itemSource) @@ -264,10 +264,12 @@ void SlotView::focus(gui::GUI* gui) { } void SlotView::bind( + int64_t inventoryid, ItemStack& stack, LevelFrontend* frontend, InventoryInteraction* interaction ) { + this->inventoryid = inventoryid; bound = &stack; content = frontend->getLevel()->content; this->frontend = frontend; @@ -328,6 +330,7 @@ void InventoryView::bind( indices = content->getIndices(); for (auto slot : slots) { slot->bind( + inventory->getId(), inventory->getSlot(slot->getLayout().index), frontend, interaction ); @@ -360,10 +363,10 @@ void InventoryView::setInventory(std::shared_ptr inventory) { #include "../coders/xml.h" #include "gui/gui_xml.h" -static itemsharefunc readShareFunc(InventoryView* view, gui::UiXmlReader& reader, xml::xmlelement& element) { +static slotcallback readSlotFunc(InventoryView* view, gui::UiXmlReader& reader, xml::xmlelement& element, const std::string& attr) { auto consumer = scripting::create_int_array_consumer( reader.getEnvironment().getId(), - element->attr("sharefunc").getText() + element->attr(attr).getText() ); return [=](uint slot, ItemStack& stack) { int args[] {int(view->getInventory()->getId()), int(slot)}; @@ -379,7 +382,10 @@ static void readSlot(InventoryView* view, gui::UiXmlReader& reader, xml::xmlelem layout.position = element->attr("coord").asVec2(); } if (element->has("sharefunc")) { - layout.shareFunc = readShareFunc(view, reader, element); + layout.shareFunc = readSlotFunc(view, reader, element, "sharefunc"); + } + if (element->has("onrightclick")) { + layout.rightClick = readSlotFunc(view, reader, element, "onrightclick"); } auto slot = view->addSlot(layout); reader.readUINode(reader, element, *slot); @@ -413,7 +419,10 @@ static void readSlotsGrid(InventoryView* view, gui::UiXmlReader& reader, xml::xm layout.position = element->attr("pos").asVec2(); } if (element->has("sharefunc")) { - layout.shareFunc = readShareFunc(view, reader, element); + layout.shareFunc = readSlotFunc(view, reader, element, "sharefunc"); + } + if (element->has("onrightclick")) { + layout.rightClick = readSlotFunc(view, reader, element, "onrightclick"); } layout.padding = padding; diff --git a/src/frontend/InventoryView.h b/src/frontend/InventoryView.h index d72a75f5..eb42f1c7 100644 --- a/src/frontend/InventoryView.h +++ b/src/frontend/InventoryView.h @@ -26,8 +26,7 @@ namespace scripting { class Environment; } -using itemsharefunc = std::function; -using slotcallback = std::function; +using slotcallback = std::function; class InventoryInteraction { ItemStack grabbedItem; @@ -44,7 +43,7 @@ struct SlotLayout { glm::vec2 position; bool background; bool itemSource; - itemsharefunc shareFunc; + slotcallback shareFunc; slotcallback rightClick; int padding = 0; @@ -52,7 +51,7 @@ struct SlotLayout { glm::vec2 position, bool background, bool itemSource, - itemsharefunc shareFunc, + slotcallback shareFunc, slotcallback rightClick); }; @@ -63,6 +62,7 @@ class SlotView : public gui::UINode { SlotLayout layout; bool highlighted = false; + int64_t inventoryid = 0; ItemStack* bound = nullptr; public: SlotView(SlotLayout layout); @@ -76,6 +76,7 @@ public: virtual void focus(gui::GUI*) override; void bind( + int64_t inventoryid, ItemStack& stack, LevelFrontend* frontend, InventoryInteraction* interaction diff --git a/src/frontend/gui/gui_xml.cpp b/src/frontend/gui/gui_xml.cpp index 26aaf2e7..bb1923da 100644 --- a/src/frontend/gui/gui_xml.cpp +++ b/src/frontend/gui/gui_xml.cpp @@ -172,7 +172,7 @@ static std::shared_ptr readTextBox(UiXmlReader& reader, xml::xmlelement auto textbox = std::make_shared(placeholder, glm::vec4(0.0f)); _readPanel(reader, element, *textbox); textbox->setText(text); - + if (element->has("consumer")) { auto consumer = scripting::create_wstring_consumer( reader.getEnvironment().getId(), diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 4696dee5..0eaf9db2 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -223,7 +223,7 @@ std::shared_ptr Hud::createContentAccess() { auto copy = ItemStack(item); inventory->move(copy, indices); }, - [=](ItemStack& item, ItemStack& grabbed) { + [=](uint, ItemStack& item) { inventory->getSlot(player->getChosenSlot()).set(item); }); @@ -263,6 +263,7 @@ Hud::Hud(Engine* engine, LevelFrontend* frontend) SlotLayout(-1, glm::vec2(), false, false, nullptr, nullptr) ); grabbedItemView->bind( + 0, interaction->getGrabbedItem(), frontend, interaction.get()