2018-06-23 01:32:23 +02:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* Filename: MouseItemWidget.cpp
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Created: 23/06/2018 00:51:15
|
|
|
|
*
|
|
|
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#include "MouseItemWidget.hpp"
|
|
|
|
|
2018-06-25 22:30:38 +02:00
|
|
|
MouseItemWidget::MouseItemWidget(Widget *parent) : ItemWidget(m_inventory, 0, 0, parent) {
|
2018-06-30 04:17:08 +02:00
|
|
|
m_tooltipBackground.setColor(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 22:30:38 +02:00
|
|
|
void MouseItemWidget::update(const ItemWidget *currentItemWidget) {
|
|
|
|
if (currentItemWidget) {
|
|
|
|
m_currentItemWidget = (currentItemWidget->stack().item().id()) ? currentItemWidget : nullptr;
|
|
|
|
m_tooltipText.setText(currentItemWidget->stack().item().name() + " [" + 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) {
|
2018-06-26 06:03:30 +02:00
|
|
|
u32 widgetItemID = widget.stack().item().id();
|
|
|
|
u32 widgetItemAmount = widget.stack().amount();
|
|
|
|
|
|
|
|
if (!isReadOnly || stack().item().id() == 0 || stack().item().id() == widgetItemID) {
|
|
|
|
if (stack().item().id() != widgetItemID) {
|
2018-06-25 03:39:02 +02:00
|
|
|
widget.setStack(stack().item().id(), stack().amount());
|
2018-06-26 06:03:30 +02:00
|
|
|
setStack(widgetItemID, widgetItemAmount);
|
2018-06-25 03:39:02 +02:00
|
|
|
}
|
|
|
|
else if (!isReadOnly) {
|
2018-06-26 06:03:30 +02:00
|
|
|
widget.setStack(widgetItemID, widgetItemAmount + stack().amount());
|
2018-06-25 03:39:02 +02:00
|
|
|
setStack(0, 0);
|
|
|
|
}
|
|
|
|
else {
|
2018-06-26 06:03:30 +02:00
|
|
|
setStack(stack().item().id(), stack().amount() + widgetItemAmount);
|
2018-06-25 03:39:02 +02:00
|
|
|
widget.setStack(0, 0);
|
|
|
|
}
|
2018-06-24 03:17:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MouseItemWidget::putItem(ItemWidget &widget) {
|
2018-06-26 06:03:30 +02:00
|
|
|
u32 widgetItemID = widget.stack().item().id();
|
|
|
|
u32 widgetItemAmount = widget.stack().amount();
|
|
|
|
|
|
|
|
if (!widgetItemID && stack().item().id()) {
|
2018-06-24 03:17:06 +02:00
|
|
|
widget.setStack(stack().item().id(), 1);
|
2018-06-24 03:20:12 +02:00
|
|
|
setStack(stack().amount() > 1 ? stack().item().id() : 0, 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()) {
|
|
|
|
widget.setStack(stack().item().id(), widgetItemAmount + 1);
|
2018-06-24 07:52:32 +02:00
|
|
|
setStack(stack().amount() > 1 ? stack().item().id() : 0, stack().amount() - 1);
|
2018-06-24 03:17:06 +02:00
|
|
|
}
|
|
|
|
else if (stack().item().id() == 0) {
|
2018-06-26 06:03:30 +02:00
|
|
|
setStack(widgetItemID, ceil(widgetItemAmount / 2.0));
|
|
|
|
widget.setStack(widgetItemAmount > 1 ? widgetItemID : 0, widgetItemAmount / 2);
|
2018-06-24 03:17:06 +02:00
|
|
|
}
|
2018-06-23 01:32:23 +02:00
|
|
|
}
|
|
|
|
|
2018-06-25 22:30:38 +02:00
|
|
|
void MouseItemWidget::draw(RenderTarget &target, RenderStates states) const {
|
|
|
|
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
|
|
|
}
|
|
|
|
|