2018-06-23 01:32:23 +02:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* OpenMiner
|
2020-02-25 01:42:10 +09:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
2020-02-25 01:42:10 +09:00
|
|
|
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
|
|
|
|
*
|
|
|
|
* This file is part of OpenMiner.
|
2018-06-23 01:32:23 +02:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is free software; you can redistribute it and/or
|
2020-02-08 18:34:26 +09:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2018-06-23 01:32:23 +02:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is distributed in the hope that it will be useful,
|
2020-02-08 18:34:26 +09:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2018-06-23 01:32:23 +02:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2020-02-25 01:42:10 +09:00
|
|
|
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
|
2020-02-08 18:34:26 +09:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2018-06-23 01:32:23 +02:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
2020-03-01 15:45:09 +01:00
|
|
|
#include "InventoryWidget.hpp"
|
2018-06-23 01:32:23 +02:00
|
|
|
#include "MouseItemWidget.hpp"
|
|
|
|
|
2018-06-25 22:30:38 +02:00
|
|
|
MouseItemWidget::MouseItemWidget(Widget *parent) : ItemWidget(m_inventory, 0, 0, parent) {
|
2018-12-29 02:23:23 +01:00
|
|
|
m_tooltipBackground.setColor(gk::Color{255, 255, 255, 240});
|
2018-06-28 08:50:57 +02:00
|
|
|
m_tooltipBackground.setPosition(20, 17, 0);
|
2018-06-30 04:17:08 +02:00
|
|
|
|
|
|
|
m_tooltipText.setPosition(26, 24, 0);
|
|
|
|
|
|
|
|
m_tooltipInfoText.setPosition(26, 35, 0);
|
|
|
|
m_tooltipInfoText.setColor({180, 180, 180});
|
2018-06-25 22:30:38 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 06:19:40 +01:00
|
|
|
void MouseItemWidget::onEvent(const SDL_Event &event) {
|
|
|
|
if (event.type == SDL_MOUSEMOTION) {
|
|
|
|
updatePosition(event.motion.x, event.motion.y);
|
2020-03-01 15:45:09 +01:00
|
|
|
}
|
2018-12-28 06:19:40 +01:00
|
|
|
else if (event.type == SDL_MOUSEBUTTONDOWN) {
|
|
|
|
updatePosition(event.button.x, event.button.y);
|
2020-03-01 15:45:09 +01:00
|
|
|
|
|
|
|
if (getStack().amount() == 0) {
|
|
|
|
if (event.button.button == SDL_BUTTON_LEFT) {
|
|
|
|
leftClickBehaviour();
|
|
|
|
}
|
|
|
|
else if (event.button.button == SDL_BUTTON_RIGHT) {
|
|
|
|
rightClickBehaviour();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_isDragging = false;
|
|
|
|
}
|
2020-03-01 17:52:35 +01:00
|
|
|
else if (event.button.button == SDL_BUTTON_LEFT || event.button.button == SDL_BUTTON_RIGHT) {
|
|
|
|
startDragging(event.button.button == SDL_BUTTON_LEFT);
|
2020-03-01 15:45:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (event.type == SDL_MOUSEBUTTONUP) {
|
|
|
|
updatePosition(event.button.x, event.button.y);
|
|
|
|
|
2020-03-01 17:44:53 +01:00
|
|
|
if (m_isDragging && m_draggedSlots.size() <= 1) {
|
2020-03-01 15:45:09 +01:00
|
|
|
if (event.button.button == SDL_BUTTON_LEFT) {
|
|
|
|
leftClickBehaviour();
|
|
|
|
}
|
|
|
|
else if (event.button.button == SDL_BUTTON_RIGHT) {
|
|
|
|
rightClickBehaviour();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 17:52:35 +01:00
|
|
|
stopDragging();
|
2018-06-23 01:32:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 15:45:09 +01:00
|
|
|
void MouseItemWidget::leftClickBehaviour() {
|
|
|
|
if (m_currentInventoryWidget && m_currentInventoryWidget->currentItemWidget() && m_currentInventoryWidget->inventory()) {
|
2020-03-29 13:51:58 +02:00
|
|
|
if (m_currentInventoryWidget->doItemMatchFilter(m_inventory.getStack(0, 0).item())) {
|
|
|
|
ItemWidget *currentItemWidget = m_currentInventoryWidget->currentItemWidget();
|
|
|
|
if (!m_currentInventoryWidget->inventory()->isUnlimited())
|
|
|
|
swapItems(*currentItemWidget, m_currentInventoryWidget->isReadOnly());
|
|
|
|
else if (getStack().amount() == 0 && currentItemWidget->stack().amount() != 0)
|
|
|
|
setStack(currentItemWidget->stack().item().stringID(), 64);
|
2020-03-01 15:45:09 +01:00
|
|
|
|
2020-03-29 13:51:58 +02:00
|
|
|
m_currentInventoryWidget->sendUpdatePacket();
|
|
|
|
}
|
2020-03-01 15:45:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseItemWidget::rightClickBehaviour() {
|
|
|
|
if (m_currentInventoryWidget && m_currentInventoryWidget->currentItemWidget() && m_currentInventoryWidget->inventory()) {
|
|
|
|
if (!m_currentInventoryWidget->isReadOnly()) {
|
2020-03-29 13:51:58 +02:00
|
|
|
if (m_currentInventoryWidget->doItemMatchFilter(m_inventory.getStack(0, 0).item())) {
|
|
|
|
ItemWidget *currentItemWidget = m_currentInventoryWidget->currentItemWidget();
|
|
|
|
if (!m_currentInventoryWidget->inventory()->isUnlimited())
|
|
|
|
putItem(*currentItemWidget);
|
|
|
|
else if (getStack().amount() == 0 && currentItemWidget->stack().amount() != 0)
|
|
|
|
setStack(currentItemWidget->stack().item().stringID(), 1);
|
|
|
|
}
|
2020-03-01 15:45:09 +01:00
|
|
|
|
|
|
|
m_currentInventoryWidget->sendUpdatePacket();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 17:52:35 +01:00
|
|
|
void MouseItemWidget::startDragging(bool isLeftClickDrag) {
|
|
|
|
m_isDragging = true;
|
|
|
|
m_isLeftClickDrag = isLeftClickDrag;
|
|
|
|
m_draggedStack = getStack();
|
|
|
|
m_dragOrigin = nullptr;
|
2020-03-01 17:44:53 +01:00
|
|
|
|
2020-03-01 17:52:35 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2020-03-01 17:44:53 +01:00
|
|
|
|
2020-03-01 17:52:35 +01:00
|
|
|
void MouseItemWidget::stopDragging() {
|
|
|
|
m_isDragging = false;
|
2020-03-01 17:44:53 +01:00
|
|
|
|
2020-03-01 17:52:35 +01:00
|
|
|
for (auto &it : m_draggedSlots)
|
|
|
|
it.first->setChanged(true);
|
2020-03-01 17:44:53 +01:00
|
|
|
|
2020-03-01 17:52:35 +01:00
|
|
|
m_draggedSlots.clear();
|
|
|
|
}
|
2020-03-01 17:44:53 +01:00
|
|
|
|
2020-03-01 17:52:35 +01:00
|
|
|
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;
|
2020-03-01 17:44:53 +01:00
|
|
|
|
2020-03-01 17:52:35 +01:00
|
|
|
m_draggedSlots.emplace(std::make_pair(newItemWidget, newItemWidget->stack()));
|
|
|
|
}
|
|
|
|
}
|
2020-03-01 17:44:53 +01:00
|
|
|
|
2020-03-01 17:52:35 +01:00
|
|
|
if (m_draggedSlots.size() > 1) {
|
|
|
|
if (m_isLeftClickDrag) {
|
|
|
|
for (auto &it : m_draggedSlots) {
|
|
|
|
u16 splitAmount = m_draggedStack.amount() / m_draggedSlots.size();
|
2020-03-01 17:44:53 +01:00
|
|
|
|
2020-03-01 17:52:35 +01:00
|
|
|
if (splitAmount > 0) {
|
|
|
|
it.first->setStack(m_draggedStack.item().stringID(), it.second.amount() + splitAmount);
|
|
|
|
it.first->update();
|
2020-03-01 17:44:53 +01:00
|
|
|
}
|
2020-03-01 15:45:09 +01:00
|
|
|
}
|
2020-03-01 17:52:35 +01:00
|
|
|
|
|
|
|
u16 remainingAmount = (m_draggedStack.amount() == 1) ? 0 : m_draggedStack.amount() % m_draggedSlots.size();
|
|
|
|
|
|
|
|
setStack(remainingAmount ? m_draggedStack.item().stringID() : "_:air", remainingAmount);
|
2020-03-01 15:45:09 +01:00
|
|
|
}
|
2020-03-01 17:52:35 +01:00
|
|
|
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) {
|
2020-03-29 13:51:58 +02:00
|
|
|
bool doItemMatchFilter = !m_currentInventoryWidget || m_currentInventoryWidget->doItemMatchFilter(m_draggedStack.item());
|
|
|
|
if (m_isDragging && currentItemWidget != m_currentItemWidget && doItemMatchFilter)
|
2020-03-01 17:52:35 +01:00
|
|
|
draggingBehaviour(currentItemWidget);
|
2020-03-01 15:45:09 +01:00
|
|
|
|
2018-06-25 22:30:38 +02:00
|
|
|
m_currentItemWidget = (currentItemWidget->stack().item().id()) ? currentItemWidget : nullptr;
|
2018-12-28 06:57:34 +01:00
|
|
|
m_tooltipText.setText(currentItemWidget->stack().item().label() + " [" + std::to_string(currentItemWidget->stack().item().id()) + "]");
|
2018-06-30 04:17:08 +02:00
|
|
|
|
2020-03-29 15:12:39 +02:00
|
|
|
if (currentItemWidget->stack().item().hasGroup("group:om_fuel"))
|
|
|
|
m_tooltipInfoText.setText("Burn time: " + std::to_string(currentItemWidget->stack().item().getGroupValue("group:om_fuel")) + " ticks");
|
2018-06-30 04:17:08 +02:00
|
|
|
else
|
|
|
|
m_tooltipInfoText.setText("");
|
2018-06-25 22:30:38 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_currentItemWidget = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 03:39:02 +02:00
|
|
|
void MouseItemWidget::swapItems(ItemWidget &widget, bool isReadOnly) {
|
2020-02-17 15:54:19 +09:00
|
|
|
std::string widgetItemName = widget.stack().item().stringID();
|
2018-06-26 06:03:30 +02:00
|
|
|
u32 widgetItemAmount = widget.stack().amount();
|
|
|
|
|
2020-02-17 15:54:19 +09:00
|
|
|
if (!isReadOnly || stack().item().id() == 0 || stack().item().stringID() == widgetItemName) {
|
|
|
|
if (stack().item().stringID() != widgetItemName) {
|
|
|
|
widget.setStack(stack().item().stringID(), stack().amount());
|
2018-12-28 21:23:26 +01:00
|
|
|
setStack(widgetItemName, widgetItemAmount);
|
2018-06-25 03:39:02 +02:00
|
|
|
}
|
|
|
|
else if (!isReadOnly) {
|
2018-12-28 21:23:26 +01:00
|
|
|
widget.setStack(widgetItemName, widgetItemAmount + stack().amount());
|
|
|
|
setStack("", 0);
|
2018-06-25 03:39:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-02-17 15:54:19 +09:00
|
|
|
setStack(stack().item().stringID(), stack().amount() + widgetItemAmount);
|
2018-12-28 21:23:26 +01:00
|
|
|
widget.setStack("", 0);
|
2018-06-25 03:39:02 +02:00
|
|
|
}
|
2018-06-24 03:17:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseItemWidget::putItem(ItemWidget &widget) {
|
2020-02-17 15:54:19 +09:00
|
|
|
std::string widgetItemName = widget.stack().item().stringID();
|
2018-06-26 06:03:30 +02:00
|
|
|
u32 widgetItemID = widget.stack().item().id();
|
|
|
|
u32 widgetItemAmount = widget.stack().amount();
|
|
|
|
|
|
|
|
if (!widgetItemID && stack().item().id()) {
|
2020-02-17 15:54:19 +09:00
|
|
|
widget.setStack(stack().item().stringID(), 1);
|
|
|
|
setStack(stack().amount() > 1 ? stack().item().stringID() : "", stack().amount() - 1);
|
2018-06-24 03:17:06 +02:00
|
|
|
}
|
2018-06-26 06:03:30 +02:00
|
|
|
else if (widgetItemID && widgetItemID == stack().item().id()) {
|
2020-02-17 15:54:19 +09:00
|
|
|
widget.setStack(stack().item().stringID(), widgetItemAmount + 1);
|
|
|
|
setStack(stack().amount() > 1 ? stack().item().stringID() : "", stack().amount() - 1);
|
2018-06-24 03:17:06 +02:00
|
|
|
}
|
|
|
|
else if (stack().item().id() == 0) {
|
2018-12-28 21:23:26 +01:00
|
|
|
setStack(widgetItemName, ceil(widgetItemAmount / 2.0));
|
|
|
|
widget.setStack(widgetItemAmount > 1 ? widgetItemName : "", widgetItemAmount / 2);
|
2018-06-24 03:17:06 +02:00
|
|
|
}
|
2018-06-23 01:32:23 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 02:23:23 +01:00
|
|
|
void MouseItemWidget::draw(gk::RenderTarget &target, gk::RenderStates states) const {
|
2020-03-01 15:45:09 +01:00
|
|
|
if (getStack().amount() > 0)
|
|
|
|
ItemWidget::draw(target, states);
|
2018-06-25 22:30:38 +02:00
|
|
|
|
2018-12-25 01:45:10 +01:00
|
|
|
states.transform *= getTransform();
|
2018-06-25 22:30:38 +02:00
|
|
|
|
|
|
|
if (m_currentItemWidget) {
|
|
|
|
target.draw(m_tooltipBackground, states);
|
|
|
|
target.draw(m_tooltipText, states);
|
2018-06-30 04:17:08 +02:00
|
|
|
target.draw(m_tooltipInfoText, states);
|
2018-06-25 22:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 01:32:23 +02:00
|
|
|
void MouseItemWidget::updatePosition(float x, float y) {
|
2018-06-25 22:30:38 +02:00
|
|
|
x -= m_parent->getPosition().x + 10 * m_parent->getScale().x;
|
|
|
|
y -= m_parent->getPosition().y + 10 * m_parent->getScale().y;
|
2018-06-23 01:32:23 +02:00
|
|
|
|
2020-03-11 03:53:04 +01:00
|
|
|
setPosition(x / m_parent->getScale().x, y / m_parent->getScale().y, -20);
|
2018-06-23 01:32:23 +02:00
|
|
|
}
|
|
|
|
|