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

View File

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

View File

@ -26,8 +26,7 @@ namespace scripting {
class Environment;
}
using itemsharefunc = std::function<void(uint, ItemStack&)>;
using slotcallback = std::function<void(ItemStack&, ItemStack&)>;
using slotcallback = std::function<void(uint, ItemStack&)>;
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

View File

@ -172,7 +172,7 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
auto textbox = std::make_shared<TextBox>(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(),

View File

@ -223,7 +223,7 @@ std::shared_ptr<InventoryView> 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()