[Widget] Base class was needed to get widget real positions. [InventoryWidget] Drag n'drop is working.
This commit is contained in:
parent
4fe6868bcb
commit
be7b857b84
@ -14,20 +14,24 @@
|
||||
#ifndef INVENTORYWIDGET_HPP_
|
||||
#define INVENTORYWIDGET_HPP_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "Inventory.hpp"
|
||||
#include "ItemWidget.hpp"
|
||||
#include "Transformable.hpp"
|
||||
#include "SDLHeaders.hpp"
|
||||
|
||||
class InventoryWidget : public IDrawable, public Transformable {
|
||||
class InventoryWidget : public Widget {
|
||||
public:
|
||||
InventoryWidget(Widget *parent = nullptr) : Widget(parent) {}
|
||||
|
||||
void onEvent(const SDL_Event &event);
|
||||
|
||||
void update(const Inventory &inventory);
|
||||
|
||||
private:
|
||||
void draw(RenderTarget &target, RenderStates states) const override;
|
||||
|
||||
std::vector<ItemWidget> m_itemWidgets;
|
||||
|
||||
int m_selectedItem = -1;
|
||||
};
|
||||
|
||||
#endif // INVENTORYWIDGET_HPP_
|
||||
|
@ -15,16 +15,17 @@
|
||||
#define ITEMWIDGET_HPP_
|
||||
|
||||
#include "Image.hpp"
|
||||
#include "Widget.hpp"
|
||||
|
||||
class ItemWidget : public IDrawable, public Transformable {
|
||||
class ItemWidget : public Widget {
|
||||
public:
|
||||
ItemWidget(u16 id);
|
||||
ItemWidget(u16 id, Widget *parent = nullptr);
|
||||
|
||||
void setItem(u16 id);
|
||||
|
||||
private:
|
||||
void draw(RenderTarget &target, RenderStates states) const override;
|
||||
|
||||
u16 m_id;
|
||||
|
||||
Image m_image;
|
||||
};
|
||||
|
||||
|
39
include/gui/Widget.hpp
Normal file
39
include/gui/Widget.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* Filename: Widget.hpp
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* Created: 21/06/2018 07:19:27
|
||||
*
|
||||
* Author: Quentin Bazin, <quent42340@gmail.com>
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
#ifndef WIDGET_HPP_
|
||||
#define WIDGET_HPP_
|
||||
|
||||
#include "IDrawable.hpp"
|
||||
#include "SDLHeaders.hpp"
|
||||
#include "Transformable.hpp"
|
||||
|
||||
class Widget : public IDrawable, public Transformable {
|
||||
public:
|
||||
Widget(Widget *parent = nullptr) : m_parent(parent) {}
|
||||
Widget(unsigned int width, unsigned int height, Widget *parent = nullptr)
|
||||
: m_parent(parent), m_width(width), m_height(height) {}
|
||||
|
||||
bool isPointInWidget(float x, float y);
|
||||
|
||||
unsigned int width() const { return m_width; }
|
||||
unsigned int height() const { return m_height; }
|
||||
|
||||
protected:
|
||||
Widget *m_parent = nullptr;
|
||||
|
||||
unsigned int m_width = 0;
|
||||
unsigned int m_height = 0;
|
||||
};
|
||||
|
||||
#endif // WIDGET_HPP_
|
@ -15,14 +15,10 @@
|
||||
#define WORKBENCHWIDGET_HPP_
|
||||
|
||||
#include "InventoryWidget.hpp"
|
||||
#include "SDLHeaders.hpp"
|
||||
#include "Transformable.hpp"
|
||||
|
||||
#include "Inventory.hpp"
|
||||
|
||||
class WorkbenchWidget : public IDrawable, public Transformable {
|
||||
class WorkbenchWidget : public Widget {
|
||||
public:
|
||||
WorkbenchWidget();
|
||||
WorkbenchWidget(Widget *parent = nullptr);
|
||||
|
||||
void onEvent(const SDL_Event &event);
|
||||
|
||||
@ -32,7 +28,7 @@ class WorkbenchWidget : public IDrawable, public Transformable {
|
||||
Image m_background;
|
||||
|
||||
Inventory m_inventory{9, 3};
|
||||
InventoryWidget m_inventoryWidget;
|
||||
InventoryWidget m_inventoryWidget{this};
|
||||
};
|
||||
|
||||
#endif // WORKBENCHWIDGET_HPP_
|
||||
|
@ -15,11 +15,12 @@
|
||||
#define APPLICATIONSTATE_HPP_
|
||||
|
||||
#include "IDrawable.hpp"
|
||||
#include "Transformable.hpp"
|
||||
#include "SDLHeaders.hpp"
|
||||
|
||||
class ApplicationStateStack;
|
||||
|
||||
class ApplicationState : public IDrawable {
|
||||
class ApplicationState : public IDrawable, public Transformable {
|
||||
public:
|
||||
ApplicationState(ApplicationState *parent = nullptr) : m_parent(parent) {}
|
||||
ApplicationState(const ApplicationState &) = delete;
|
||||
|
@ -36,9 +36,7 @@ void Transformable::setScale(float factorX, float factorY, float factorZ) {
|
||||
|
||||
void Transformable::applyTransform(RenderStates &states) const {
|
||||
glm::mat4 &modelMatrix = const_cast<Transformable *>(this)->m_modelMatrix;
|
||||
modelMatrix = glm::mat4{1};
|
||||
modelMatrix = glm::translate(modelMatrix, m_position);
|
||||
modelMatrix = glm::scale(modelMatrix, m_scale);
|
||||
modelMatrix = glm::translate(glm::mat4{1}, m_position) * glm::scale(glm::mat4{1}, m_scale);
|
||||
|
||||
if (states.modelMatrix) {
|
||||
const_cast<Transformable *>(this)->m_tmpMatrix = *states.modelMatrix * m_modelMatrix;
|
||||
|
@ -18,9 +18,35 @@ void InventoryWidget::update(const Inventory &inventory) {
|
||||
|
||||
u16 i = 0;
|
||||
for (u16 id : inventory.items())
|
||||
m_itemWidgets.emplace_back(id).setPosition((++i - 1) * 18, 0, 0);
|
||||
m_itemWidgets.emplace_back(id, this).setPosition((++i - 1) * 18, 0, 0);
|
||||
|
||||
setPosition(10.5, 86.5, 0);
|
||||
m_width = inventory.width() * 18;
|
||||
m_height = inventory.height() * 18;
|
||||
}
|
||||
|
||||
void InventoryWidget::onEvent(const SDL_Event &event) {
|
||||
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
|
||||
for (std::size_t i = 0 ; i < m_itemWidgets.size() ; ++i) {
|
||||
if (m_itemWidgets[i].isPointInWidget(event.button.x / 3.0, event.button.y / 3.0)) {
|
||||
if (m_selectedItem == -1) {
|
||||
m_selectedItem = i;
|
||||
|
||||
m_itemWidgets.at(m_selectedItem).setPosition(event.motion.x / 3 - getPosition().x - m_parent->getPosition().x - 8,
|
||||
event.motion.y / 3 - getPosition().y - m_parent->getPosition().y - 8, 0);
|
||||
}
|
||||
else {
|
||||
m_itemWidgets.at(m_selectedItem).setPosition(i * 18, 0, 0);
|
||||
|
||||
m_selectedItem = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (event.type == SDL_MOUSEMOTION && m_selectedItem != -1) {
|
||||
m_itemWidgets.at(m_selectedItem).setPosition(event.motion.x / 3 - getPosition().x - m_parent->getPosition().x - 8,
|
||||
event.motion.y / 3 - getPosition().y - m_parent->getPosition().y - 8, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void InventoryWidget::draw(RenderTarget &target, RenderStates states) const {
|
||||
|
@ -13,12 +13,16 @@
|
||||
*/
|
||||
#include "ItemWidget.hpp"
|
||||
|
||||
ItemWidget::ItemWidget(u16 id) {
|
||||
m_id = id;
|
||||
|
||||
ItemWidget::ItemWidget(u16 id, Widget *parent) : Widget(16, 16, parent) {
|
||||
m_image.load("texture-blocks");
|
||||
m_image.setClipRect(m_id * 16, 0, 16, 16);
|
||||
m_image.setScale(2.0f / 3.0f, 2.0f / 3.0f, 1.0f);
|
||||
m_image.setPosition(3, 0, 0);
|
||||
|
||||
setItem(id);
|
||||
}
|
||||
|
||||
void ItemWidget::setItem(u16 id) {
|
||||
m_image.setClipRect(id * 16, 0, 16, 16);
|
||||
}
|
||||
|
||||
void ItemWidget::draw(RenderTarget &target, RenderStates states) const {
|
||||
|
41
source/gui/Widget.cpp
Normal file
41
source/gui/Widget.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* Filename: Widget.cpp
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* Created: 21/06/2018 07:55:39
|
||||
*
|
||||
* Author: Quentin Bazin, <quent42340@gmail.com>
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
#include "Rect.hpp"
|
||||
#include "Widget.hpp"
|
||||
|
||||
#include "Debug.hpp"
|
||||
|
||||
bool Widget::isPointInWidget(float x, float y) {
|
||||
FloatRect aabb;
|
||||
aabb.x = getPosition().x;
|
||||
aabb.y = getPosition().y;
|
||||
aabb.width = m_width * getScale().x;
|
||||
aabb.height = m_height * getScale().y;
|
||||
|
||||
Widget *parent = m_parent;
|
||||
while (parent) {
|
||||
aabb.x += parent->getPosition().x;
|
||||
aabb.y += parent->getPosition().y;
|
||||
|
||||
aabb.width *= parent->getScale().x;
|
||||
aabb.height *= parent->getScale().y;
|
||||
|
||||
parent = parent->m_parent;
|
||||
}
|
||||
|
||||
// DEBUG("Checking aabb:", aabb.x, aabb.y, aabb.width, aabb.height, "|", x, y);
|
||||
|
||||
return aabb.intersects(FloatRect{x, y, 1, 1});
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
#include "Config.hpp"
|
||||
#include "WorkbenchWidget.hpp"
|
||||
|
||||
WorkbenchWidget::WorkbenchWidget() {
|
||||
WorkbenchWidget::WorkbenchWidget(Widget *parent) : Widget(176, 166, parent) {
|
||||
m_background.load("texture-workbench");
|
||||
m_background.setClipRect(0, 0, 176, 166);
|
||||
|
||||
@ -22,14 +22,15 @@ WorkbenchWidget::WorkbenchWidget() {
|
||||
m_inventory.addItem(i);
|
||||
|
||||
m_inventoryWidget.update(m_inventory);
|
||||
m_inventoryWidget.setPosition(7, 86, 0);
|
||||
|
||||
setPosition(SCREEN_WIDTH / 2.0 - m_background.clipRect().width * 3 / 2.0,
|
||||
SCREEN_HEIGHT / 2.0 - m_background.clipRect().height * 3 / 2.0, 0);
|
||||
|
||||
setScale(3, 3, 1);
|
||||
// setScale(3, 3, 1);
|
||||
setPosition(SCREEN_WIDTH / 3.0 / 2.0 - m_background.clipRect().width / 2.0,
|
||||
SCREEN_HEIGHT / 3.0 / 2.0 - m_background.clipRect().height / 2.0, 0);
|
||||
}
|
||||
|
||||
void WorkbenchWidget::onEvent(const SDL_Event &event) {
|
||||
m_inventoryWidget.onEvent(event);
|
||||
}
|
||||
|
||||
void WorkbenchWidget::draw(RenderTarget &target, RenderStates states) const {
|
||||
@ -37,13 +38,5 @@ void WorkbenchWidget::draw(RenderTarget &target, RenderStates states) const {
|
||||
|
||||
target.draw(m_background, states);
|
||||
target.draw(m_inventoryWidget, states);
|
||||
|
||||
// Image image;
|
||||
// image.load(m_blocksTexture);
|
||||
// image.setClipRect(i * 16, 0, 16, 16);
|
||||
// image.setPosRect(m_background.posRect().x + 10.5 * 3 + (i - 1) * 27 * 2,
|
||||
// m_background.posRect().y + 86.5 * 3,
|
||||
// 16 * 2, 16 * 2);
|
||||
// target.draw(image, states);
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ Hotbar::Hotbar() {
|
||||
|
||||
for (u16 i = 1 ; i < 10 ; ++i) {
|
||||
ItemWidget &widget = m_items.emplace_back(i);
|
||||
widget.setPosition(backgroundX + 16 + 182 * 3 / 9 * (i - 1), backgroundY + 16, 0);
|
||||
widget.setPosition(backgroundX + 16 + 180 / 3.0 * (i - 1) - 8, backgroundY + 16, 0);
|
||||
widget.setScale(3, 3, 1);
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,8 @@ InventoryState::InventoryState(ApplicationState *parent) : ApplicationState(pare
|
||||
Mouse::setCursorGrabbed(false);
|
||||
Mouse::setCursorVisible(true);
|
||||
Mouse::resetToWindowCenter();
|
||||
|
||||
setScale(3, 3, 1);
|
||||
}
|
||||
|
||||
void InventoryState::onEvent(const SDL_Event &event) {
|
||||
@ -46,6 +48,8 @@ void InventoryState::update() {
|
||||
}
|
||||
|
||||
void InventoryState::draw(RenderTarget &target, RenderStates states) const {
|
||||
applyTransform(states);
|
||||
|
||||
if (m_parent)
|
||||
target.draw(*m_parent);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user