small refactor

This commit is contained in:
neveix 2026-08-14 15:22:06 +05:00
parent 54e90a25fe
commit e76b5b0dcd
3 changed files with 36 additions and 31 deletions

View file

@ -371,35 +371,7 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
action = InteractionAction::PUT;
stack.move(grabbed, indices);
} else {
if (layout.itemSource) {
if (grabbed.isEmpty()) {
action = InteractionAction::TAKE;
grabbed.set(stack);
if (input.pressed(Keycode::LEFT_CONTROL)) {
grabbed.maximizeCount(*content->getIndices());
}
} else {
// TODO: refactor to reduce nesting
if (grabbed.accepts(stack)) {
auto& def = indices.items.require(stack.getItemId());
if (grabbed.getCount() < def.stackSize) {
action = InteractionAction::TAKE;
grabbed.setCount(grabbed.getCount() + 1);
}
} else {
action = InteractionAction::PUT;
grabbed.clear();
}
}
} else if (grabbed.isEmpty()) {
if (layout.taking) {
action = InteractionAction::TAKE;
std::swap(grabbed, stack);
}
} else if (layout.taking && layout.placing) {
action = InteractionAction::PUT;
std::swap(grabbed, stack);
}
actIfCannotPut(stack, grabbed, action);
}
if (action != InteractionAction::UNDEFINED) {
@ -412,6 +384,39 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
}
}
void SlotView::actIfCannotPut(ItemStack& stack, ItemStack& grabbed, InteractionAction& action) {
const auto& input = gui.getInput();
auto indices = *content->getIndices();
if (layout.itemSource) {
if (grabbed.isEmpty()) {
action = InteractionAction::TAKE;
grabbed.set(stack);
if (input.pressed(Keycode::LEFT_CONTROL)) {
grabbed.maximizeCount(*content->getIndices());
}
} else {
if (grabbed.accepts(stack)) {
auto& def = indices.items.require(stack.getItemId());
if (grabbed.getCount() < def.stackSize) {
action = InteractionAction::TAKE;
grabbed.setCount(grabbed.getCount() + 1);
}
} else {
action = InteractionAction::PUT;
grabbed.clear();
}
}
} else if (grabbed.isEmpty()) {
if (layout.taking) {
action = InteractionAction::TAKE;
std::swap(grabbed, stack);
}
} else if (layout.taking && layout.placing) {
action = InteractionAction::PUT;
std::swap(grabbed, stack);
}
}
void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) {
if (layout.rightClick) {
layout.rightClick(inventoryId, stack);

View file

@ -123,6 +123,8 @@ namespace gui {
SHARE = 2,
UNDEFINED,
};
private:
void actIfCannotPut(ItemStack& stack, ItemStack& grabbed, InteractionAction& action);
};
class InventoryView final : public gui::Container {

View file

@ -1,5 +1,3 @@
#include <iostream>
#include "content/Content.hpp"
#include "items/Inventories.hpp"
#include "items/ItemStack.hpp"