add 'taking' and 'placing' xml properties to slot and slots-grid

This commit is contained in:
MihailRis
2024-11-25 07:12:40 +03:00
parent 4e25362f9b
commit 69df0eba1b
3 changed files with 22 additions and 4 deletions
+11 -4
View File
@@ -209,7 +209,7 @@ bool SlotView::isHighlighted() const {
} }
void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
if (Events::pressed(keycode::LEFT_SHIFT)) { if (layout.taking && Events::pressed(keycode::LEFT_SHIFT)) {
if (layout.shareFunc) { if (layout.shareFunc) {
layout.shareFunc(layout.index, stack); layout.shareFunc(layout.index, stack);
} }
@@ -218,7 +218,7 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
} }
return; return;
} }
if (!layout.itemSource && stack.accepts(grabbed)) { if (!layout.itemSource && stack.accepts(grabbed) && layout.placing) {
stack.move(grabbed, content->getIndices()); stack.move(grabbed, content->getIndices());
} else { } else {
if (layout.itemSource) { if (layout.itemSource) {
@@ -227,7 +227,11 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
} else { } else {
grabbed.clear(); grabbed.clear();
} }
} else { } else if (grabbed.isEmpty()) {
if (layout.taking) {
std::swap(grabbed, stack);
}
} else if (layout.taking && layout.placing) {
std::swap(grabbed, stack); std::swap(grabbed, stack);
} }
} }
@@ -244,7 +248,7 @@ void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) {
if (layout.itemSource) if (layout.itemSource)
return; return;
if (grabbed.isEmpty()) { if (grabbed.isEmpty()) {
if (!stack.isEmpty()) { if (!stack.isEmpty() && layout.taking) {
grabbed.set(stack); grabbed.set(stack);
int halfremain = stack.getCount() / 2; int halfremain = stack.getCount() / 2;
grabbed.setCount(stack.getCount() - halfremain); grabbed.setCount(stack.getCount() - halfremain);
@@ -253,6 +257,9 @@ void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) {
return; return;
} }
auto& stackDef = content->getIndices()->items.require(stack.getItemId()); auto& stackDef = content->getIndices()->items.require(stack.getItemId());
if (!layout.placing) {
return;
}
if (stack.isEmpty()) { if (stack.isEmpty()) {
stack.set(grabbed); stack.set(grabbed);
stack.setCount(1); stack.setCount(1);
@@ -34,6 +34,9 @@ namespace gui {
slotcallback rightClick; slotcallback rightClick;
int padding = 0; int padding = 0;
bool taking = true;
bool placing = true;
SlotLayout( SlotLayout(
int index, int index,
glm::vec2 position, glm::vec2 position,
+8
View File
@@ -483,6 +483,8 @@ static slotcallback readSlotFunc(InventoryView* view, UiXmlReader& reader, xml::
static void readSlot(InventoryView* view, UiXmlReader& reader, xml::xmlelement element) { static void readSlot(InventoryView* view, UiXmlReader& reader, xml::xmlelement element) {
int index = element->attr("index", "0").asInt(); int index = element->attr("index", "0").asInt();
bool itemSource = element->attr("item-source", "false").asBool(); bool itemSource = element->attr("item-source", "false").asBool();
bool taking = element->attr("taking", "true").asBool();
bool placing = element->attr("placing", "true").asBool();
SlotLayout layout(index, glm::vec2(), true, itemSource, nullptr, nullptr, nullptr); SlotLayout layout(index, glm::vec2(), true, itemSource, nullptr, nullptr, nullptr);
if (element->has("pos")) { if (element->has("pos")) {
layout.position = element->attr("pos").asVec2(); layout.position = element->attr("pos").asVec2();
@@ -496,6 +498,8 @@ static void readSlot(InventoryView* view, UiXmlReader& reader, xml::xmlelement e
if (element->has("onrightclick")) { if (element->has("onrightclick")) {
layout.rightClick = readSlotFunc(view, reader, element, "onrightclick"); layout.rightClick = readSlotFunc(view, reader, element, "onrightclick");
} }
layout.taking = taking;
layout.placing = placing;
auto slot = view->addSlot(layout); auto slot = view->addSlot(layout);
reader.readUINode(reader, element, *slot); reader.readUINode(reader, element, *slot);
view->add(slot); view->add(slot);
@@ -507,6 +511,8 @@ static void readSlotsGrid(InventoryView* view, UiXmlReader& reader, xml::xmlelem
int cols = element->attr("cols", "0").asInt(); int cols = element->attr("cols", "0").asInt();
int count = element->attr("count", "0").asInt(); int count = element->attr("count", "0").asInt();
const int slotSize = InventoryView::SLOT_SIZE; const int slotSize = InventoryView::SLOT_SIZE;
bool taking = element->attr("taking", "true").asBool();
bool placing = element->attr("placing", "true").asBool();
int interval = element->attr("interval", "-1").asInt(); int interval = element->attr("interval", "-1").asInt();
if (interval < 0) { if (interval < 0) {
interval = InventoryView::SLOT_INTERVAL; interval = InventoryView::SLOT_INTERVAL;
@@ -537,6 +543,8 @@ static void readSlotsGrid(InventoryView* view, UiXmlReader& reader, xml::xmlelem
layout.rightClick = readSlotFunc(view, reader, element, "onrightclick"); layout.rightClick = readSlotFunc(view, reader, element, "onrightclick");
} }
layout.padding = padding; layout.padding = padding;
layout.taking = taking;
layout.placing = placing;
int idx = 0; int idx = 0;
for (int row = 0; row < rows; row++) { for (int row = 0; row < rows; row++) {