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>
|
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
2020-01-30 15:31:49 +09:00
|
|
|
#include <gk/resource/ResourceHandler.hpp>
|
|
|
|
|
2018-06-21 01:32:55 +02:00
|
|
|
#include "ItemWidget.hpp"
|
2018-06-26 01:35:19 +02:00
|
|
|
#include "Registry.hpp"
|
2020-01-30 15:31:49 +09:00
|
|
|
#include "TextureAtlas.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)
|
2020-01-30 15:31:49 +09:00
|
|
|
: Widget(18, 18, parent), m_inventory(inventory), m_x(x), m_y(y),
|
|
|
|
m_textureAtlas(gk::ResourceHandler::getInstance().get<TextureAtlas>("atlas-blocks"))
|
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-21 09:00:53 +02:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:34 +02:00
|
|
|
void ItemWidget::update() {
|
2020-01-30 16:03:49 +09:00
|
|
|
if (stack().item().isBlock()) {
|
|
|
|
const Block &block = Registry::getInstance().getBlock(stack().item().id());
|
|
|
|
if (block.drawType() != BlockDrawType::XShape) {
|
|
|
|
m_cube.updateVertexBuffer(block);
|
|
|
|
m_isImage = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
updateImage();
|
2019-01-13 21:53:59 +01:00
|
|
|
}
|
2020-01-30 16:03:49 +09:00
|
|
|
else
|
|
|
|
updateImage();
|
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
|
|
|
|
2020-01-30 16:03:49 +09:00
|
|
|
void ItemWidget::updateImage() {
|
|
|
|
if (m_image.width() == 0) {
|
|
|
|
m_image.load(m_textureAtlas.texture());
|
|
|
|
m_image.setPosition(1, 1, 0);
|
|
|
|
m_image.setClipRect(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2020-01-30 21:35:51 +09:00
|
|
|
gk::FloatRect clipRect = m_textureAtlas.getTexCoords(stack().item().tiles().getTextureForFace(0), false);
|
2020-01-30 16:03:49 +09:00
|
|
|
m_image.setClipRect(clipRect.x, clipRect.y, clipRect.width, clipRect.height);
|
|
|
|
m_image.setScale(16.0f / clipRect.width, 16.0f / clipRect.height);
|
|
|
|
|
|
|
|
m_isImage = true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-01-30 16:03:49 +09:00
|
|
|
if (stack().item().id()) {
|
|
|
|
if (!m_isImage)
|
|
|
|
target.draw(m_cube, states);
|
|
|
|
else
|
|
|
|
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
|
|
|
}
|
|
|
|
|