2018-06-25 02:39:23 +02:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* OpenMiner
|
|
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
2018-06-25 02:39:23 +02:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* 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-25 02:39:23 +02:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* 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-25 02:39:23 +02:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2018-06-25 02:39:23 +02:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#include "CraftingWidget.hpp"
|
|
|
|
#include "Registry.hpp"
|
|
|
|
|
2019-04-08 12:59:02 +02:00
|
|
|
CraftingWidget::CraftingWidget(ClientCommandHandler &client, Inventory &craftingInventory, Widget *parent)
|
2019-01-26 20:29:13 +01:00
|
|
|
: Widget(parent), m_client(client), m_craftingInventory(craftingInventory)
|
|
|
|
{
|
2020-02-03 14:11:18 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
void CraftingWidget::init(unsigned int offset, unsigned int size) {
|
|
|
|
m_craftingInventoryWidget.init(m_craftingInventory, offset, size * size);
|
2018-06-25 02:39:23 +02:00
|
|
|
m_craftingInventoryWidget.setPosition(29, 16, 0);
|
|
|
|
|
|
|
|
m_craftingResultInventoryWidget.init(m_craftingResultInventory);
|
2018-06-26 05:38:19 +02:00
|
|
|
m_craftingResultInventoryWidget.setPosition(123, 34, 0);
|
2018-06-25 02:39:23 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 06:19:40 +01:00
|
|
|
void CraftingWidget::onMouseEvent(const SDL_Event &event, MouseItemWidget &mouseItemWidget) {
|
2018-06-27 05:39:56 +02:00
|
|
|
m_craftingInventoryWidget.onMouseEvent(event, mouseItemWidget);
|
|
|
|
m_craftingResultInventoryWidget.onMouseEvent(event, mouseItemWidget, true);
|
2018-06-26 06:18:50 +02:00
|
|
|
|
|
|
|
if (m_recipe && !m_craftingResultInventory.getStack(0, 0).item().id()) {
|
2018-06-27 09:07:57 +02:00
|
|
|
for (u8 x = 0 ; x < m_craftingInventory.width() ; ++x) {
|
|
|
|
for (u8 y = 0 ; y < m_craftingInventory.height() ; ++y) {
|
|
|
|
const ItemStack &stack = m_craftingInventory.getStack(x, y);
|
|
|
|
if (stack.item().id()) {
|
2018-12-28 21:23:26 +01:00
|
|
|
m_craftingInventory.setStack(x, y, (stack.amount() > 1) ? stack.item().name() : "", stack.amount() - 1);
|
2018-06-27 09:07:57 +02:00
|
|
|
}
|
2018-06-26 06:18:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_craftingInventoryWidget.init(m_craftingInventory);
|
|
|
|
|
|
|
|
m_recipe = nullptr;
|
|
|
|
}
|
2018-06-25 03:49:53 +02:00
|
|
|
}
|
2018-06-25 02:39:23 +02:00
|
|
|
|
2018-06-25 03:49:53 +02:00
|
|
|
void CraftingWidget::update() {
|
2018-06-30 03:47:39 +02:00
|
|
|
const Recipe *recipe = Registry::getInstance().getRecipe(m_craftingInventory);
|
2018-06-30 04:32:24 +02:00
|
|
|
if (recipe && recipe->type() != "craft")
|
|
|
|
recipe = nullptr;
|
|
|
|
|
|
|
|
if ((!m_recipe || m_recipe != recipe)) {
|
2018-06-25 03:00:49 +02:00
|
|
|
m_recipe = recipe;
|
|
|
|
|
|
|
|
if (m_recipe)
|
2018-12-28 21:23:26 +01:00
|
|
|
m_craftingResultInventory.setStack(0, 0, m_recipe->result().item().name(), m_recipe->result().amount());
|
2018-06-25 03:00:49 +02:00
|
|
|
else
|
2018-12-28 21:23:26 +01:00
|
|
|
m_craftingResultInventory.setStack(0, 0, "", 0);
|
2018-06-25 03:00:49 +02:00
|
|
|
|
2018-06-25 02:39:23 +02:00
|
|
|
m_craftingResultInventoryWidget.init(m_craftingResultInventory);
|
2018-06-25 03:00:49 +02:00
|
|
|
}
|
2018-06-25 02:39:23 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 02:23:23 +01:00
|
|
|
void CraftingWidget::draw(gk::RenderTarget &target, gk::RenderStates states) const {
|
2018-12-25 01:45:10 +01:00
|
|
|
states.transform *= getTransform();
|
2018-06-25 02:39:23 +02:00
|
|
|
|
|
|
|
target.draw(m_craftingInventoryWidget, states);
|
|
|
|
target.draw(m_craftingResultInventoryWidget, states);
|
|
|
|
}
|
|
|
|
|