slot onrightclick event

This commit is contained in:
MihailRis
2024-02-19 00:18:08 +03:00
parent d8cc810441
commit 8409e0d82e
4 changed files with 23 additions and 12 deletions
+15 -6
View File
@@ -30,7 +30,7 @@ SlotLayout::SlotLayout(
glm::vec2 position, glm::vec2 position,
bool background, bool background,
bool itemSource, bool itemSource,
itemsharefunc shareFunc, slotcallback shareFunc,
slotcallback rightClick slotcallback rightClick
) )
: index(index), : index(index),
@@ -234,7 +234,7 @@ void SlotView::clicked(gui::GUI* gui, int button) {
} }
} else if (button == mousecode::BUTTON_2) { } else if (button == mousecode::BUTTON_2) {
if (layout.rightClick) { if (layout.rightClick) {
layout.rightClick(stack, grabbed); layout.rightClick(inventoryid, stack);
return; return;
} }
if (layout.itemSource) if (layout.itemSource)
@@ -264,10 +264,12 @@ void SlotView::focus(gui::GUI* gui) {
} }
void SlotView::bind( void SlotView::bind(
int64_t inventoryid,
ItemStack& stack, ItemStack& stack,
LevelFrontend* frontend, LevelFrontend* frontend,
InventoryInteraction* interaction InventoryInteraction* interaction
) { ) {
this->inventoryid = inventoryid;
bound = &stack; bound = &stack;
content = frontend->getLevel()->content; content = frontend->getLevel()->content;
this->frontend = frontend; this->frontend = frontend;
@@ -328,6 +330,7 @@ void InventoryView::bind(
indices = content->getIndices(); indices = content->getIndices();
for (auto slot : slots) { for (auto slot : slots) {
slot->bind( slot->bind(
inventory->getId(),
inventory->getSlot(slot->getLayout().index), inventory->getSlot(slot->getLayout().index),
frontend, interaction frontend, interaction
); );
@@ -360,10 +363,10 @@ void InventoryView::setInventory(std::shared_ptr<Inventory> inventory) {
#include "../coders/xml.h" #include "../coders/xml.h"
#include "gui/gui_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( auto consumer = scripting::create_int_array_consumer(
reader.getEnvironment().getId(), reader.getEnvironment().getId(),
element->attr("sharefunc").getText() element->attr(attr).getText()
); );
return [=](uint slot, ItemStack& stack) { return [=](uint slot, ItemStack& stack) {
int args[] {int(view->getInventory()->getId()), int(slot)}; 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(); layout.position = element->attr("coord").asVec2();
} }
if (element->has("sharefunc")) { 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); auto slot = view->addSlot(layout);
reader.readUINode(reader, element, *slot); 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(); layout.position = element->attr("pos").asVec2();
} }
if (element->has("sharefunc")) { 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; layout.padding = padding;
+5 -4
View File
@@ -26,8 +26,7 @@ namespace scripting {
class Environment; class Environment;
} }
using itemsharefunc = std::function<void(uint, ItemStack&)>; using slotcallback = std::function<void(uint, ItemStack&)>;
using slotcallback = std::function<void(ItemStack&, ItemStack&)>;
class InventoryInteraction { class InventoryInteraction {
ItemStack grabbedItem; ItemStack grabbedItem;
@@ -44,7 +43,7 @@ struct SlotLayout {
glm::vec2 position; glm::vec2 position;
bool background; bool background;
bool itemSource; bool itemSource;
itemsharefunc shareFunc; slotcallback shareFunc;
slotcallback rightClick; slotcallback rightClick;
int padding = 0; int padding = 0;
@@ -52,7 +51,7 @@ struct SlotLayout {
glm::vec2 position, glm::vec2 position,
bool background, bool background,
bool itemSource, bool itemSource,
itemsharefunc shareFunc, slotcallback shareFunc,
slotcallback rightClick); slotcallback rightClick);
}; };
@@ -63,6 +62,7 @@ class SlotView : public gui::UINode {
SlotLayout layout; SlotLayout layout;
bool highlighted = false; bool highlighted = false;
int64_t inventoryid = 0;
ItemStack* bound = nullptr; ItemStack* bound = nullptr;
public: public:
SlotView(SlotLayout layout); SlotView(SlotLayout layout);
@@ -76,6 +76,7 @@ public:
virtual void focus(gui::GUI*) override; virtual void focus(gui::GUI*) override;
void bind( void bind(
int64_t inventoryid,
ItemStack& stack, ItemStack& stack,
LevelFrontend* frontend, LevelFrontend* frontend,
InventoryInteraction* interaction InventoryInteraction* interaction
+2 -1
View File
@@ -223,7 +223,7 @@ std::shared_ptr<InventoryView> Hud::createContentAccess() {
auto copy = ItemStack(item); auto copy = ItemStack(item);
inventory->move(copy, indices); inventory->move(copy, indices);
}, },
[=](ItemStack& item, ItemStack& grabbed) { [=](uint, ItemStack& item) {
inventory->getSlot(player->getChosenSlot()).set(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) SlotLayout(-1, glm::vec2(), false, false, nullptr, nullptr)
); );
grabbedItemView->bind( grabbedItemView->bind(
0,
interaction->getGrabbedItem(), interaction->getGrabbedItem(),
frontend, frontend,
interaction.get() interaction.get()