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
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#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);
|
2018-06-23 01:32:23 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 06:19:40 +01:00
|
|
|
else if (event.type == SDL_MOUSEBUTTONDOWN) {
|
|
|
|
updatePosition(event.button.x, event.button.y);
|
2018-06-23 01:32:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 20:22:10 +09:00
|
|
|
void MouseItemWidget::updateCurrentItem(const ItemWidget *currentItemWidget) {
|
2018-06-25 22:30:38 +02:00
|
|
|
if (currentItemWidget) {
|
|
|
|
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
|
|
|
|
|
|
|
if (currentItemWidget->stack().item().isFuel())
|
|
|
|
m_tooltipInfoText.setText("Burn time: " + std::to_string(currentItemWidget->stack().item().burnTime()) + " ticks");
|
|
|
|
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 {
|
2018-06-25 22:30:38 +02:00
|
|
|
ItemWidget::draw(target, states);
|
|
|
|
|
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
|
|
|
|
2018-06-25 22:30:38 +02:00
|
|
|
setPosition(x / m_parent->getScale().x, y / m_parent->getScale().y, 0);
|
2018-06-23 01:32:23 +02:00
|
|
|
}
|
|
|
|
|