2018-06-21 01:32:55 +02:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* Filename: ItemWidget.cpp
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Created: 21/06/2018 01:11:11
|
|
|
|
*
|
|
|
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#include "ItemWidget.hpp"
|
2018-06-26 01:35:19 +02:00
|
|
|
#include "Registry.hpp"
|
2018-06-21 01:32:55 +02:00
|
|
|
|
2018-06-23 03:26:34 +02:00
|
|
|
ItemWidget::ItemWidget(Inventory &inventory, u16 x, u16 y, Widget *parent)
|
2018-06-25 22:30:38 +02:00
|
|
|
: Widget(18, 18, parent), m_inventory(inventory), m_x(x), m_y(y)
|
2018-06-23 03:26:34 +02:00
|
|
|
{
|
2018-06-26 05:08:32 +02:00
|
|
|
m_cube.setPosition(-12, -13, 0);
|
2018-06-26 01:52:28 +02:00
|
|
|
// m_cube.setPosition(8.5, 14, 0);
|
|
|
|
// m_cube.setRotation(-172, glm::vec3{0.42, -0.2, 1});
|
2018-06-26 01:35:19 +02:00
|
|
|
|
|
|
|
m_image.load("texture-items");
|
|
|
|
m_image.setPosition(1, 1, 0);
|
|
|
|
m_image.setClipRect(0, 0, 0, 0);
|
2018-06-21 09:00:53 +02:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:34 +02:00
|
|
|
void ItemWidget::update() {
|
2018-06-26 01:35:19 +02:00
|
|
|
if (stack().item().isBlock())
|
|
|
|
m_cube.updateVertexBuffer(Registry::getInstance().getBlock(stack().item().id()));
|
|
|
|
else
|
2018-06-25 16:32:32 +02:00
|
|
|
m_image.setClipRect(stack().item().textureID() * 16, stack().item().textureID() / 16 * 16, 16, 16);
|
2018-06-24 03:17:06 +02:00
|
|
|
|
|
|
|
m_text.setText(std::to_string(stack().amount()));
|
|
|
|
m_text.setPosition(16 - 4 - 6 * floor(log10(stack().amount())), 16 - 6, 0);
|
2018-06-23 03:26:34 +02:00
|
|
|
}
|
2018-06-23 00:34:02 +02:00
|
|
|
|
2018-06-24 03:17:06 +02:00
|
|
|
void ItemWidget::setStack(unsigned int id, unsigned int amount) {
|
|
|
|
m_inventory.setStack(m_x, m_y, id, amount);
|
2018-06-23 03:26:34 +02:00
|
|
|
update();
|
2018-06-21 01:32:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ItemWidget::draw(RenderTarget &target, RenderStates states) const {
|
2018-06-21 02:55:11 +02:00
|
|
|
applyTransform(states);
|
|
|
|
|
2018-06-26 01:52:28 +02:00
|
|
|
if (stack().item().isBlock() && stack().item().id())
|
2018-06-26 01:35:19 +02:00
|
|
|
target.draw(m_cube, states);
|
2018-06-26 01:52:28 +02:00
|
|
|
else if (stack().amount() >= 1)
|
2018-06-26 01:35:19 +02:00
|
|
|
target.draw(m_image, states);
|
2018-06-24 03:17:06 +02:00
|
|
|
|
|
|
|
if (stack().item().id() && stack().amount() > 1)
|
|
|
|
target.draw(m_text, states);
|
2018-06-21 01:32:55 +02:00
|
|
|
}
|
|
|
|
|