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 23:33:01 +02:00
|
|
|
m_cube.setPosition(-12.7, -14.6, 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() {
|
2019-01-13 21:53:59 +01:00
|
|
|
const u16 tileSize = 32;
|
|
|
|
|
2018-06-26 01:35:19 +02:00
|
|
|
if (stack().item().isBlock())
|
|
|
|
m_cube.updateVertexBuffer(Registry::getInstance().getBlock(stack().item().id()));
|
2019-01-13 21:53:59 +01:00
|
|
|
else {
|
|
|
|
m_image.setClipRect(stack().item().textureID() % 16 * tileSize, stack().item().textureID() / 16 * tileSize, tileSize, tileSize);
|
|
|
|
m_image.setScale(16.0f / tileSize, 16.0f / tileSize);
|
|
|
|
}
|
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-12-28 21:23:26 +01:00
|
|
|
void ItemWidget::setStack(const std::string &name, unsigned int amount) {
|
|
|
|
m_inventory.setStack(m_x, m_y, name, amount);
|
2018-06-23 03:26:34 +02:00
|
|
|
update();
|
2018-06-21 01:32:55 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 02:23:23 +01:00
|
|
|
void ItemWidget::draw(gk::RenderTarget &target, gk::RenderStates states) const {
|
2018-12-25 01:45:10 +01:00
|
|
|
states.transform *= getTransform();
|
2018-06-21 02:55:11 +02:00
|
|
|
|
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
|
|
|
|
2018-06-26 06:03:30 +02:00
|
|
|
if (stack().amount() != 1)
|
2018-06-24 03:17:06 +02:00
|
|
|
target.draw(m_text, states);
|
2018-06-21 01:32:55 +02:00
|
|
|
}
|
|
|
|
|