diff --git a/doc/ru/scripting/events.md b/doc/ru/scripting/events.md index 598a10d44..f2ccd4787 100644 --- a/doc/ru/scripting/events.md +++ b/doc/ru/scripting/events.md @@ -189,13 +189,6 @@ function on_inventory_closed(invid: int, playerid: int) Вызывается при закрытии инвентаря. -```lua -function on_inventory_interact(invid: int, slot: int, action: int) -``` - -Вызывается при взаимодействии со слотом игрока. -**action** отвечает за тип взаимодействия (0 - левый клик, 1 - правый клик, 2 - левый клик + шифт) - ## События макета @@ -237,6 +230,13 @@ function on_hud_close(playerid: int) Вызывается при выходе из мира, перед его сохранением. +```lua +function on_hud_inventory_interact"(invid: int, slot: int, action: int) +``` + +Вызывается при взаимодействии со слотом игрока. +**action** отвечает за тип взаимодействия (0 - левый клик, 1 - правый клик, 2 - левый клик + шифт) + ## События ECS События **ECS** описаны [тут](ecs.md#события-компонента) diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index f366a64a8..1ed3518f6 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -350,6 +350,8 @@ bool SlotView::isHighlighted() const { void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { const auto& input = gui.getInput(); + const int action = 0; + if (layout.taking && input.pressed(Keycode::LEFT_SHIFT)) { if (layout.shareFunc) { layout.shareFunc(layout.index, stack); @@ -357,6 +359,7 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { if (layout.updateFunc) { layout.updateFunc(layout.index, stack); } + scripting::on_inventory_interact(inventoryId, layout.index, action | 2); return; } if (!layout.itemSource && stack.accepts(grabbed) && layout.placing) { @@ -376,17 +379,26 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { std::swap(grabbed, stack); } } + + scripting::on_inventory_interact(inventoryId, layout.index, action); } void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { + const auto& input = gui.getInput(); + const int action = 1; + if (layout.rightClick) { layout.rightClick(inventoryId, stack); if (layout.updateFunc) { layout.updateFunc(layout.index, stack); } + scripting::on_inventory_interact(inventoryId, layout.index, action); + return; + } + if (layout.itemSource) { + scripting::on_inventory_interact(inventoryId, layout.index, action); return; } - if (layout.itemSource) return; if (grabbed.isEmpty()) { if (!stack.isEmpty() && layout.taking) { grabbed.set(std::move(stack)); @@ -399,10 +411,14 @@ void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { stack = ItemStack(0, 0); } } + scripting::on_inventory_interact(inventoryId, layout.index, action); return; } + auto& stackDef = content->getIndices()->items.require(stack.getItemId()); + if (!layout.placing) { + scripting::on_inventory_interact(inventoryId, layout.index, action); return; } if (stack.isEmpty()) { @@ -419,6 +435,8 @@ void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { stack.setCount(stack.getCount() + 1); grabbed.setCount(grabbed.getCount() - 1); } + + scripting::on_inventory_interact(inventoryId, layout.index, action); } void SlotView::clicked(Mousecode button) { @@ -431,23 +449,15 @@ void SlotView::clicked(Mousecode button) { ItemStack& grabbed = exchangeSlot->getStack(); ItemStack& stack = *bound; - int action = -1; - if (button == Mousecode::BUTTON_1) { performLeftClick(stack, grabbed); - action = 0; } else if (button == Mousecode::BUTTON_2) { performRightClick(stack, grabbed); - action = 1; } if (layout.updateFunc) { layout.updateFunc(layout.index, stack); } - - const auto& input = gui.getInput(); - action = action | (input.pressed(Keycode::LEFT_SHIFT) ? 2 : 0); - scripting::on_inventory_interact(inventoryId, layout.index, action); } void SlotView::onFocus() {