From e8ee3e04b1398a3ada8445591267525304410571 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 24 Nov 2024 17:47:28 +0300 Subject: [PATCH 1/4] fix disabled slots display --- src/graphics/ui/elements/InventoryView.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index 9d07441d..e1e68b29 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -135,7 +135,7 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) { const int slotSize = InventoryView::SLOT_SIZE; const ItemStack& stack = *bound; - glm::vec4 tint(1.0f); + glm::vec4 tint(1, 1, 1, isEnabled() ? 1 : 0.5f); glm::vec2 pos = calcPos(); glm::vec4 color = getColor(); From 4e25362f9bc95d36f853fc8bbb4bed94c607fc4e Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 24 Nov 2024 17:56:14 +0300 Subject: [PATCH 2/4] set slot tooltip delay to 0 & refactor --- src/graphics/ui/elements/InventoryView.cpp | 113 +++++++++++---------- src/graphics/ui/elements/InventoryView.hpp | 3 + 2 files changed, 63 insertions(+), 53 deletions(-) diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index e1e68b29..f3687799 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -112,7 +112,7 @@ SlotView::SlotView( layout(std::move(layout)) { setColor(glm::vec4(0, 0, 0, 0.2f)); - setTooltipDelay(0.05f); + setTooltipDelay(0.0f); } void SlotView::draw(const DrawContext* pctx, Assets* assets) { @@ -208,11 +208,66 @@ bool SlotView::isHighlighted() const { return highlighted; } +void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { + if (Events::pressed(keycode::LEFT_SHIFT)) { + if (layout.shareFunc) { + layout.shareFunc(layout.index, stack); + } + if (layout.updateFunc) { + layout.updateFunc(layout.index, stack); + } + return; + } + if (!layout.itemSource && stack.accepts(grabbed)) { + stack.move(grabbed, content->getIndices()); + } else { + if (layout.itemSource) { + if (grabbed.isEmpty()) { + grabbed.set(stack); + } else { + grabbed.clear(); + } + } else { + std::swap(grabbed, stack); + } + } +} + +void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { + if (layout.rightClick) { + layout.rightClick(inventoryid, stack); + if (layout.updateFunc) { + layout.updateFunc(layout.index, stack); + } + return; + } + if (layout.itemSource) + return; + if (grabbed.isEmpty()) { + if (!stack.isEmpty()) { + grabbed.set(stack); + int halfremain = stack.getCount() / 2; + grabbed.setCount(stack.getCount() - halfremain); + stack.setCount(halfremain); + } + return; + } + auto& stackDef = content->getIndices()->items.require(stack.getItemId()); + if (stack.isEmpty()) { + stack.set(grabbed); + stack.setCount(1); + grabbed.setCount(grabbed.getCount() - 1); + } else if (stack.accepts(grabbed) && stack.getCount() < stackDef.stackSize) { + stack.setCount(stack.getCount() + 1); + grabbed.setCount(grabbed.getCount() - 1); + } +} + void SlotView::clicked(gui::GUI* gui, mousecode button) { if (bound == nullptr) return; - - auto exchangeSlot = std::dynamic_pointer_cast(gui->get(EXCHANGE_SLOT_NAME)); + auto exchangeSlot = + std::dynamic_pointer_cast(gui->get(EXCHANGE_SLOT_NAME)); if (exchangeSlot == nullptr) { return; } @@ -220,57 +275,9 @@ void SlotView::clicked(gui::GUI* gui, mousecode button) { ItemStack& stack = *bound; if (button == mousecode::BUTTON_1) { - if (Events::pressed(keycode::LEFT_SHIFT)) { - if (layout.shareFunc) { - layout.shareFunc(layout.index, stack); - } - if (layout.updateFunc) { - layout.updateFunc(layout.index, stack); - } - return; - } - if (!layout.itemSource && stack.accepts(grabbed)) { - stack.move(grabbed, content->getIndices()); - } else { - if (layout.itemSource) { - if (grabbed.isEmpty()) { - grabbed.set(stack); - } else { - grabbed.clear(); - } - } else { - std::swap(grabbed, stack); - } - } + performLeftClick(stack, grabbed); } else if (button == mousecode::BUTTON_2) { - if (layout.rightClick) { - layout.rightClick(inventoryid, stack); - if (layout.updateFunc) { - layout.updateFunc(layout.index, stack); - } - return; - } - if (layout.itemSource) - return; - if (grabbed.isEmpty()) { - if (!stack.isEmpty()) { - grabbed.set(stack); - int halfremain = stack.getCount() / 2; - grabbed.setCount(stack.getCount() - halfremain); - stack.setCount(halfremain); - } - } else { - auto& stackDef = - content->getIndices()->items.require(stack.getItemId()); - if (stack.isEmpty()) { - stack.set(grabbed); - stack.setCount(1); - grabbed.setCount(grabbed.getCount() - 1); - } else if (stack.accepts(grabbed) && stack.getCount() < stackDef.stackSize) { - stack.setCount(stack.getCount() + 1); - grabbed.setCount(grabbed.getCount() - 1); - } - } + performRightClick(stack, grabbed); } if (layout.updateFunc) { layout.updateFunc(layout.index, stack); diff --git a/src/graphics/ui/elements/InventoryView.hpp b/src/graphics/ui/elements/InventoryView.hpp index 1700c168..9f53e231 100644 --- a/src/graphics/ui/elements/InventoryView.hpp +++ b/src/graphics/ui/elements/InventoryView.hpp @@ -55,6 +55,9 @@ namespace gui { std::wstring tooltip; itemid_t prevItem = 0; + + void performLeftClick(ItemStack& stack, ItemStack& grabbed); + void performRightClick(ItemStack& stack, ItemStack& grabbed); public: SlotView(SlotLayout layout); From 69df0eba1b1ac5ee3ead3bf9d3025c2afb7bfefd Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 25 Nov 2024 07:12:40 +0300 Subject: [PATCH 3/4] add 'taking' and 'placing' xml properties to slot and slots-grid --- src/graphics/ui/elements/InventoryView.cpp | 15 +++++++++++---- src/graphics/ui/elements/InventoryView.hpp | 3 +++ src/graphics/ui/gui_xml.cpp | 8 ++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index f3687799..78751cea 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -209,7 +209,7 @@ bool SlotView::isHighlighted() const { } void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { - if (Events::pressed(keycode::LEFT_SHIFT)) { + if (layout.taking && Events::pressed(keycode::LEFT_SHIFT)) { if (layout.shareFunc) { layout.shareFunc(layout.index, stack); } @@ -218,7 +218,7 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { } return; } - if (!layout.itemSource && stack.accepts(grabbed)) { + if (!layout.itemSource && stack.accepts(grabbed) && layout.placing) { stack.move(grabbed, content->getIndices()); } else { if (layout.itemSource) { @@ -227,7 +227,11 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { } else { grabbed.clear(); } - } else { + } else if (grabbed.isEmpty()) { + if (layout.taking) { + std::swap(grabbed, stack); + } + } else if (layout.taking && layout.placing) { std::swap(grabbed, stack); } } @@ -244,7 +248,7 @@ void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { if (layout.itemSource) return; if (grabbed.isEmpty()) { - if (!stack.isEmpty()) { + if (!stack.isEmpty() && layout.taking) { grabbed.set(stack); int halfremain = stack.getCount() / 2; grabbed.setCount(stack.getCount() - halfremain); @@ -253,6 +257,9 @@ void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { return; } auto& stackDef = content->getIndices()->items.require(stack.getItemId()); + if (!layout.placing) { + return; + } if (stack.isEmpty()) { stack.set(grabbed); stack.setCount(1); diff --git a/src/graphics/ui/elements/InventoryView.hpp b/src/graphics/ui/elements/InventoryView.hpp index 9f53e231..e37e6681 100644 --- a/src/graphics/ui/elements/InventoryView.hpp +++ b/src/graphics/ui/elements/InventoryView.hpp @@ -34,6 +34,9 @@ namespace gui { slotcallback rightClick; int padding = 0; + bool taking = true; + bool placing = true; + SlotLayout( int index, glm::vec2 position, diff --git a/src/graphics/ui/gui_xml.cpp b/src/graphics/ui/gui_xml.cpp index ffdf0e16..c25b65b2 100644 --- a/src/graphics/ui/gui_xml.cpp +++ b/src/graphics/ui/gui_xml.cpp @@ -483,6 +483,8 @@ static slotcallback readSlotFunc(InventoryView* view, UiXmlReader& reader, xml:: static void readSlot(InventoryView* view, UiXmlReader& reader, xml::xmlelement element) { int index = element->attr("index", "0").asInt(); 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); if (element->has("pos")) { layout.position = element->attr("pos").asVec2(); @@ -496,6 +498,8 @@ static void readSlot(InventoryView* view, UiXmlReader& reader, xml::xmlelement e if (element->has("onrightclick")) { layout.rightClick = readSlotFunc(view, reader, element, "onrightclick"); } + layout.taking = taking; + layout.placing = placing; auto slot = view->addSlot(layout); reader.readUINode(reader, element, *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 count = element->attr("count", "0").asInt(); 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(); if (interval < 0) { 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.padding = padding; + layout.taking = taking; + layout.placing = placing; int idx = 0; for (int row = 0; row < rows; row++) { From e34620b9bd7eeee525bc3b408854375356f86c3a Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 25 Nov 2024 07:21:11 +0300 Subject: [PATCH 4/4] update doc/*/xml-ui-layouts.md --- doc/en/xml-ui-layouts.md | 5 ++++- doc/ru/xml-ui-layouts.md | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/en/xml-ui-layouts.md b/doc/en/xml-ui-layouts.md index 788814e5..d598faa2 100644 --- a/doc/en/xml-ui-layouts.md +++ b/doc/en/xml-ui-layouts.md @@ -142,6 +142,8 @@ Element must be in direct sub-element of *inventory*. - `sharefunc` - Lua event called on LMB + Shift. Inventory id and slot index passed as arguments. - `updatefunc` - Lua event called on slot content update.Inventory id and slot index passed as arguments. - `onrightclick` - Lua event called on RMB click. Inventory id and slot index passed as arguments. +- `taking` - the ability to take an item from a slot. +- `placing` - the ability to put an item in a slot. ## *slots-grid* @@ -154,4 +156,5 @@ Element must be in direct sub-element of *inventory*. - `sharefunc` - Lua event called on LMB + Shift. Inventory id and slot index passed as arguments. - `updatefunc` - Lua event called on slot content update.Inventory id and slot index passed as arguments. - `onrightclick` - Lua event called on RMB click. Inventory id and slot index passed as arguments. - +- `taking` - the ability to take an item from a slot. +- `placing` - the ability to put an item in a slot. diff --git a/doc/ru/xml-ui-layouts.md b/doc/ru/xml-ui-layouts.md index 28777950..f4502a94 100644 --- a/doc/ru/xml-ui-layouts.md +++ b/doc/ru/xml-ui-layouts.md @@ -143,6 +143,8 @@ - `sharefunc` - lua событие вызываемое при использовании ЛКМ + Shift. Передается id инвентаря и индекс слота - `updatefunc` - lua событие вызываемое при изменении содержимого слота - `onrightclick` - lua событие вызываемое при использовании ПКМ. Передается id инвентаря и индекс слота +- `taking` - возможность взять предмет из слота. +- `placing` - возможность положить предмет в слот. ## Сетка слотов - *slots-grid* @@ -156,3 +158,5 @@ - `sharefunc` - lua событие вызываемое при использовании ЛКМ + Shift. Передается id инвентаря и индекс слота - `updatefunc` - lua событие вызываемое при изменении содержимого слота - `onrightclick` - lua событие вызываемое при использовании ПКМ. Передается id инвентаря и индекс слота +- `taking` - возможность взять предмет из слота. +- `placing` - возможность положить предмет в слот.