Merge branch 'main' into user-properties

This commit is contained in:
MihailRis 2024-11-25 08:18:29 +03:00
commit f4e1f4cfaf
5 changed files with 90 additions and 55 deletions

View File

@ -142,6 +142,8 @@ Element must be in direct sub-element of *inventory*.
- `sharefunc` - Lua event called on <btn>LMB</btn> + <btn>Shift</btn>. Inventory id and slot index passed as arguments. - `sharefunc` - Lua event called on <btn>LMB</btn> + <btn>Shift</btn>. Inventory id and slot index passed as arguments.
- `updatefunc` - Lua event called on slot content update.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 <btn>RMB</btn> click. Inventory id and slot index passed as arguments. - `onrightclick` - Lua event called on <btn>RMB</btn> 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* ## *slots-grid*
@ -154,4 +156,5 @@ Element must be in direct sub-element of *inventory*.
- `sharefunc` - Lua event called on <btn>LMB</btn> + <btn>Shift</btn>. Inventory id and slot index passed as arguments. - `sharefunc` - Lua event called on <btn>LMB</btn> + <btn>Shift</btn>. Inventory id and slot index passed as arguments.
- `updatefunc` - Lua event called on slot content update.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 <btn>RMB</btn> click. Inventory id and slot index passed as arguments. - `onrightclick` - Lua event called on <btn>RMB</btn> 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.

View File

@ -143,6 +143,8 @@
- `sharefunc` - lua событие вызываемое при использовании ЛКМ + Shift. Передается id инвентаря и индекс слота - `sharefunc` - lua событие вызываемое при использовании ЛКМ + Shift. Передается id инвентаря и индекс слота
- `updatefunc` - lua событие вызываемое при изменении содержимого слота - `updatefunc` - lua событие вызываемое при изменении содержимого слота
- `onrightclick` - lua событие вызываемое при использовании ПКМ. Передается id инвентаря и индекс слота - `onrightclick` - lua событие вызываемое при использовании ПКМ. Передается id инвентаря и индекс слота
- `taking` - возможность взять предмет из слота.
- `placing` - возможность положить предмет в слот.
## Сетка слотов - *slots-grid* ## Сетка слотов - *slots-grid*
@ -156,3 +158,5 @@
- `sharefunc` - lua событие вызываемое при использовании ЛКМ + Shift. Передается id инвентаря и индекс слота - `sharefunc` - lua событие вызываемое при использовании ЛКМ + Shift. Передается id инвентаря и индекс слота
- `updatefunc` - lua событие вызываемое при изменении содержимого слота - `updatefunc` - lua событие вызываемое при изменении содержимого слота
- `onrightclick` - lua событие вызываемое при использовании ПКМ. Передается id инвентаря и индекс слота - `onrightclick` - lua событие вызываемое при использовании ПКМ. Передается id инвентаря и индекс слота
- `taking` - возможность взять предмет из слота.
- `placing` - возможность положить предмет в слот.

View File

@ -112,7 +112,7 @@ SlotView::SlotView(
layout(std::move(layout)) layout(std::move(layout))
{ {
setColor(glm::vec4(0, 0, 0, 0.2f)); setColor(glm::vec4(0, 0, 0, 0.2f));
setTooltipDelay(0.05f); setTooltipDelay(0.0f);
} }
void SlotView::draw(const DrawContext* pctx, Assets* assets) { void SlotView::draw(const DrawContext* pctx, Assets* assets) {
@ -135,7 +135,7 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
const int slotSize = InventoryView::SLOT_SIZE; const int slotSize = InventoryView::SLOT_SIZE;
const ItemStack& stack = *bound; const ItemStack& stack = *bound;
glm::vec4 tint(1.0f); glm::vec4 tint(1, 1, 1, isEnabled() ? 1 : 0.5f);
glm::vec2 pos = calcPos(); glm::vec2 pos = calcPos();
glm::vec4 color = getColor(); glm::vec4 color = getColor();
@ -208,19 +208,8 @@ bool SlotView::isHighlighted() const {
return highlighted; return highlighted;
} }
void SlotView::clicked(gui::GUI* gui, mousecode button) { void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
if (bound == nullptr) if (layout.taking && Events::pressed(keycode::LEFT_SHIFT)) {
return;
auto exchangeSlot = std::dynamic_pointer_cast<SlotView>(gui->get(EXCHANGE_SLOT_NAME));
if (exchangeSlot == nullptr) {
return;
}
ItemStack& grabbed = exchangeSlot->getStack();
ItemStack& stack = *bound;
if (button == mousecode::BUTTON_1) {
if (Events::pressed(keycode::LEFT_SHIFT)) {
if (layout.shareFunc) { if (layout.shareFunc) {
layout.shareFunc(layout.index, stack); layout.shareFunc(layout.index, stack);
} }
@ -229,7 +218,7 @@ void SlotView::clicked(gui::GUI* gui, mousecode button) {
} }
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) {
@ -238,11 +227,17 @@ void SlotView::clicked(gui::GUI* gui, mousecode button) {
} 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);
} }
} }
} else if (button == mousecode::BUTTON_2) { }
void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) {
if (layout.rightClick) { if (layout.rightClick) {
layout.rightClick(inventoryid, stack); layout.rightClick(inventoryid, stack);
if (layout.updateFunc) { if (layout.updateFunc) {
@ -253,15 +248,18 @@ void SlotView::clicked(gui::GUI* gui, mousecode button) {
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);
stack.setCount(halfremain); stack.setCount(halfremain);
} }
} else { 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);
@ -271,6 +269,22 @@ void SlotView::clicked(gui::GUI* gui, mousecode button) {
grabbed.setCount(grabbed.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<SlotView>(gui->get(EXCHANGE_SLOT_NAME));
if (exchangeSlot == nullptr) {
return;
}
ItemStack& grabbed = exchangeSlot->getStack();
ItemStack& stack = *bound;
if (button == mousecode::BUTTON_1) {
performLeftClick(stack, grabbed);
} else if (button == mousecode::BUTTON_2) {
performRightClick(stack, grabbed);
} }
if (layout.updateFunc) { if (layout.updateFunc) {
layout.updateFunc(layout.index, stack); layout.updateFunc(layout.index, stack);

View File

@ -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,
@ -55,6 +58,9 @@ namespace gui {
std::wstring tooltip; std::wstring tooltip;
itemid_t prevItem = 0; itemid_t prevItem = 0;
void performLeftClick(ItemStack& stack, ItemStack& grabbed);
void performRightClick(ItemStack& stack, ItemStack& grabbed);
public: public:
SlotView(SlotLayout layout); SlotView(SlotLayout layout);

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++) {