set slot tooltip delay to 0 & refactor

This commit is contained in:
MihailRis
2024-11-24 17:56:14 +03:00
parent e8ee3e04b1
commit 4e25362f9b
2 changed files with 63 additions and 53 deletions
+60 -53
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) {
@@ -208,11 +208,66 @@ bool SlotView::isHighlighted() const {
return highlighted; 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) { void SlotView::clicked(gui::GUI* gui, mousecode button) {
if (bound == nullptr) if (bound == nullptr)
return; return;
auto exchangeSlot =
auto exchangeSlot = std::dynamic_pointer_cast<SlotView>(gui->get(EXCHANGE_SLOT_NAME)); std::dynamic_pointer_cast<SlotView>(gui->get(EXCHANGE_SLOT_NAME));
if (exchangeSlot == nullptr) { if (exchangeSlot == nullptr) {
return; return;
} }
@@ -220,57 +275,9 @@ void SlotView::clicked(gui::GUI* gui, mousecode button) {
ItemStack& stack = *bound; ItemStack& stack = *bound;
if (button == mousecode::BUTTON_1) { if (button == mousecode::BUTTON_1) {
if (Events::pressed(keycode::LEFT_SHIFT)) { performLeftClick(stack, grabbed);
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);
}
}
} else if (button == mousecode::BUTTON_2) { } else if (button == mousecode::BUTTON_2) {
if (layout.rightClick) { performRightClick(stack, grabbed);
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);
}
}
} }
if (layout.updateFunc) { if (layout.updateFunc) {
layout.updateFunc(layout.index, stack); layout.updateFunc(layout.index, stack);
@@ -55,6 +55,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);