diff --git a/client/source/gui/MouseItemWidget.cpp b/client/source/gui/MouseItemWidget.cpp index 80531358..37665279 100644 --- a/client/source/gui/MouseItemWidget.cpp +++ b/client/source/gui/MouseItemWidget.cpp @@ -54,16 +54,8 @@ void MouseItemWidget::onEvent(const SDL_Event &event) { m_isDragging = false; } - else { - m_isDragging = true; - m_isLeftClickDrag = event.button.button == SDL_BUTTON_LEFT; - m_draggedStack = getStack(); - m_dragOrigin = nullptr; - - if (m_currentItemWidget && (m_currentItemWidget->stack().amount() == 0 || m_currentItemWidget->stack().item().stringID() == m_draggedStack.item().stringID())) { - m_dragOrigin = m_currentItemWidget; - m_draggedSlots[m_currentItemWidget] = m_currentItemWidget->stack(); - } + else if (event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_RIGHT) { + startDragging(event.button.button == SDL_BUTTON_LEFT); } } else if (event.type == SDL_MOUSEBUTTONUP) { @@ -78,12 +70,7 @@ void MouseItemWidget::onEvent(const SDL_Event &event) { } } - m_isDragging = false; - - for (auto &it : m_draggedSlots) - it.first->setChanged(true); - - m_draggedSlots.clear(); + stopDragging(); } } @@ -113,51 +100,75 @@ void MouseItemWidget::rightClickBehaviour() { } } +void MouseItemWidget::startDragging(bool isLeftClickDrag) { + m_isDragging = true; + m_isLeftClickDrag = isLeftClickDrag; + m_draggedStack = getStack(); + m_dragOrigin = nullptr; + + if (m_currentItemWidget && (m_currentItemWidget->stack().amount() == 0 || m_currentItemWidget->stack().item().stringID() == m_draggedStack.item().stringID())) { + m_dragOrigin = m_currentItemWidget; + m_draggedSlots[m_currentItemWidget] = m_currentItemWidget->stack(); + } +} + +void MouseItemWidget::stopDragging() { + m_isDragging = false; + + for (auto &it : m_draggedSlots) + it.first->setChanged(true); + + m_draggedSlots.clear(); +} + +void MouseItemWidget::draggingBehaviour(ItemWidget *newItemWidget) { + if (newItemWidget->stack().amount() == 0 || newItemWidget->stack().item().stringID() == m_draggedStack.item().stringID()) { + auto it = m_draggedSlots.find(newItemWidget); + if (it == m_draggedSlots.end()) { + if (m_draggedSlots.empty()) + m_dragOrigin = newItemWidget; + + m_draggedSlots.emplace(std::make_pair(newItemWidget, newItemWidget->stack())); + } + } + + if (m_draggedSlots.size() > 1) { + if (m_isLeftClickDrag) { + for (auto &it : m_draggedSlots) { + u16 splitAmount = m_draggedStack.amount() / m_draggedSlots.size(); + + if (splitAmount > 0) { + it.first->setStack(m_draggedStack.item().stringID(), it.second.amount() + splitAmount); + it.first->update(); + } + } + + u16 remainingAmount = (m_draggedStack.amount() == 1) ? 0 : m_draggedStack.amount() % m_draggedSlots.size(); + + setStack(remainingAmount ? m_draggedStack.item().stringID() : "_:air", remainingAmount); + } + else if (m_draggedStack.amount() > 0 && (newItemWidget->stack().amount() == 0 || newItemWidget->stack().item().stringID() == m_draggedStack.item().stringID())) { + if (m_dragOrigin) { + m_dragOrigin->setStack(m_draggedStack.item().stringID(), m_dragOrigin->stack().amount() + 1); + + m_draggedStack.setAmount(m_draggedStack.amount() - 1); + setStack(m_draggedStack.amount() ? m_draggedStack.item().stringID() : "_:air", m_draggedStack.amount()); + + m_dragOrigin = nullptr; + } + + newItemWidget->setStack(m_draggedStack.item().stringID(), newItemWidget->stack().amount() + 1); + + m_draggedStack.setAmount(m_draggedStack.amount() - 1); + setStack(m_draggedStack.amount() ? m_draggedStack.item().stringID() : "_:air", m_draggedStack.amount()); + } + } +} + void MouseItemWidget::updateCurrentItem(ItemWidget *currentItemWidget) { if (currentItemWidget) { - if (m_isDragging && currentItemWidget != m_currentItemWidget) { - if (currentItemWidget->stack().amount() == 0 || currentItemWidget->stack().item().stringID() == m_draggedStack.item().stringID()) { - auto it = m_draggedSlots.find(currentItemWidget); - if (it == m_draggedSlots.end()) { - if (m_draggedSlots.empty()) - m_dragOrigin = currentItemWidget; - - m_draggedSlots.emplace(std::make_pair(currentItemWidget, currentItemWidget->stack())); - } - } - - if (m_draggedSlots.size() > 1) { - if (m_isLeftClickDrag) { - for (auto &it : m_draggedSlots) { - u16 splitAmount = m_draggedStack.amount() / m_draggedSlots.size(); - - if (splitAmount > 0) { - it.first->setStack(m_draggedStack.item().stringID(), it.second.amount() + splitAmount); - it.first->update(); - } - } - - u16 remainingAmount = (m_draggedStack.amount() == 1) ? 0 : m_draggedStack.amount() % m_draggedSlots.size(); - - setStack(remainingAmount ? m_draggedStack.item().stringID() : "_:air", remainingAmount); - } - else if (m_draggedStack.amount() > 0 && (currentItemWidget->stack().amount() == 0 || currentItemWidget->stack().item().stringID() == m_draggedStack.item().stringID())) { - if (m_dragOrigin) { - m_dragOrigin->setStack(m_draggedStack.item().stringID(), m_dragOrigin->stack().amount() + 1); - - m_draggedStack.setAmount(m_draggedStack.amount() - 1); - setStack(m_draggedStack.amount() ? m_draggedStack.item().stringID() : "_:air", m_draggedStack.amount()); - - m_dragOrigin = nullptr; - } - - currentItemWidget->setStack(m_draggedStack.item().stringID(), currentItemWidget->stack().amount() + 1); - - m_draggedStack.setAmount(m_draggedStack.amount() - 1); - setStack(m_draggedStack.amount() ? m_draggedStack.item().stringID() : "_:air", m_draggedStack.amount()); - } - } - } + if (m_isDragging && currentItemWidget != m_currentItemWidget) + draggingBehaviour(currentItemWidget); m_currentItemWidget = (currentItemWidget->stack().item().id()) ? currentItemWidget : nullptr; m_tooltipText.setText(currentItemWidget->stack().item().label() + " [" + std::to_string(currentItemWidget->stack().item().id()) + "]"); diff --git a/client/source/gui/MouseItemWidget.hpp b/client/source/gui/MouseItemWidget.hpp index 25765b14..28b95da4 100644 --- a/client/source/gui/MouseItemWidget.hpp +++ b/client/source/gui/MouseItemWidget.hpp @@ -43,6 +43,10 @@ class MouseItemWidget : public ItemWidget { void leftClickBehaviour(); void rightClickBehaviour(); + void startDragging(bool isLeftClickDrag); + void stopDragging(); + void draggingBehaviour(ItemWidget *newItemWidget); + const ItemWidget *currentItemWidget() const { return m_currentItemWidget; } void updateCurrentItem(ItemWidget *currentItemWidget);