[MouseItemWidget] Added. [WorkbenchWidget] Now using MouseItemWidget to move items across inventories.
This commit is contained in:
parent
28071a6b89
commit
a9f182ec61
@ -15,16 +15,16 @@
|
||||
#define INVENTORYWIDGET_HPP_
|
||||
|
||||
#include "Inventory.hpp"
|
||||
#include "ItemWidget.hpp"
|
||||
#include "MouseItemWidget.hpp"
|
||||
#include "SDLHeaders.hpp"
|
||||
|
||||
class InventoryWidget : public Widget {
|
||||
public:
|
||||
InventoryWidget(Widget *parent = nullptr) : Widget(parent) {}
|
||||
|
||||
void onEvent(const SDL_Event &event);
|
||||
void init(const Inventory &inventory);
|
||||
|
||||
void update(const Inventory &inventory);
|
||||
void onEvent(const SDL_Event &event, MouseItemWidget &mouseItemWidget);
|
||||
|
||||
private:
|
||||
void draw(RenderTarget &target, RenderStates states) const override;
|
||||
@ -33,7 +33,6 @@ class InventoryWidget : public Widget {
|
||||
u16 m_inventoryHeight = 0;
|
||||
|
||||
std::vector<ItemWidget> m_itemWidgets;
|
||||
ItemWidget m_mouseItemWidget{0, this};
|
||||
|
||||
int m_selectedItem = -1;
|
||||
};
|
||||
|
31
include/gui/MouseItemWidget.hpp
Normal file
31
include/gui/MouseItemWidget.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* Filename: MouseItemWidget.hpp
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* Created: 23/06/2018 00:50:38
|
||||
*
|
||||
* Author: Quentin Bazin, <quent42340@gmail.com>
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
#ifndef MOUSEITEMWIDGET_HPP_
|
||||
#define MOUSEITEMWIDGET_HPP_
|
||||
|
||||
#include "ItemWidget.hpp"
|
||||
|
||||
class MouseItemWidget : public ItemWidget {
|
||||
public:
|
||||
MouseItemWidget(Widget *parent) : ItemWidget(0, parent) {}
|
||||
|
||||
void onEvent(const SDL_Event &event);
|
||||
|
||||
void swapItems(ItemWidget &widget);
|
||||
|
||||
private:
|
||||
void updatePosition(float x, float y);
|
||||
};
|
||||
|
||||
#endif // MOUSEITEMWIDGET_HPP_
|
@ -26,6 +26,8 @@ class Widget : public IDrawable, public Transformable {
|
||||
|
||||
bool isPointInWidget(float x, float y);
|
||||
|
||||
const Widget *parent() { return m_parent; }
|
||||
|
||||
unsigned int width() const { return m_width; }
|
||||
unsigned int height() const { return m_height; }
|
||||
|
||||
|
@ -27,8 +27,16 @@ class WorkbenchWidget : public Widget {
|
||||
|
||||
Image m_background;
|
||||
|
||||
Inventory m_craftingInventory{3, 3};
|
||||
InventoryWidget m_craftingInventoryWidget{this};
|
||||
|
||||
Inventory m_inventory{9, 3};
|
||||
InventoryWidget m_inventoryWidget{this};
|
||||
|
||||
Inventory m_hotbarInventory{9, 1};
|
||||
InventoryWidget m_hotbarInventoryWidget{this};
|
||||
|
||||
MouseItemWidget m_mouseItemWidget{this};
|
||||
};
|
||||
|
||||
#endif // WORKBENCHWIDGET_HPP_
|
||||
|
@ -13,15 +13,13 @@
|
||||
*/
|
||||
#include "InventoryWidget.hpp"
|
||||
|
||||
void InventoryWidget::update(const Inventory &inventory) {
|
||||
void InventoryWidget::init(const Inventory &inventory) {
|
||||
m_itemWidgets.clear();
|
||||
|
||||
for (u16 y = 0 ; y < inventory.height() ; ++y)
|
||||
for (u16 x = 0 ; x < inventory.width() ; ++x)
|
||||
m_itemWidgets.emplace_back(inventory.items().at(x + y * inventory.width()), this).setPosition(x * 18, y * 18, 0);
|
||||
|
||||
// m_mouseItemWidget.setPosition(-10000, -10000, 0);
|
||||
|
||||
m_width = inventory.width() * 18;
|
||||
m_height = inventory.height() * 18;
|
||||
|
||||
@ -29,37 +27,14 @@ void InventoryWidget::update(const Inventory &inventory) {
|
||||
m_inventoryHeight = inventory.height();
|
||||
}
|
||||
|
||||
void InventoryWidget::onEvent(const SDL_Event &event) {
|
||||
void InventoryWidget::onEvent(const SDL_Event &event, MouseItemWidget &mouseItemWidget) {
|
||||
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_mouseItemWidget.setItem(m_itemWidgets.at(m_selectedItem).item());
|
||||
m_mouseItemWidget.setPosition(event.button.x / 3 - getPosition().x - m_parent->getPosition().x - 8,
|
||||
event.button.y / 3 - getPosition().y - m_parent->getPosition().y - 8, 0);
|
||||
|
||||
m_itemWidgets.at(m_selectedItem).setItem(0);
|
||||
}
|
||||
else {
|
||||
u16 mouseItem = m_mouseItemWidget.item();
|
||||
m_mouseItemWidget.setItem(m_itemWidgets.at(i).item());
|
||||
m_itemWidgets.at(i).setItem(mouseItem);
|
||||
|
||||
if (m_mouseItemWidget.item())
|
||||
m_selectedItem = i;
|
||||
else
|
||||
m_selectedItem = -1;
|
||||
}
|
||||
mouseItemWidget.swapItems(m_itemWidgets.at(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (event.type == SDL_MOUSEMOTION && m_selectedItem != -1) {
|
||||
m_mouseItemWidget.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 {
|
||||
@ -68,7 +43,5 @@ void InventoryWidget::draw(RenderTarget &target, RenderStates states) const {
|
||||
for (std::size_t i = 0 ; i < m_itemWidgets.size() ; ++i) {
|
||||
target.draw(m_itemWidgets[i], states);
|
||||
}
|
||||
|
||||
target.draw(m_mouseItemWidget, states);
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@
|
||||
ItemWidget::ItemWidget(u16 id, Widget *parent) : Widget(16, 16, parent) {
|
||||
m_image.load("texture-blocks");
|
||||
m_image.setScale(2.0f / 3.0f, 2.0f / 3.0f, 1.0f);
|
||||
m_image.setPosition(3, 0, 0);
|
||||
m_image.setPosition(3, 3, 0);
|
||||
|
||||
setItem(id);
|
||||
}
|
||||
|
38
source/gui/MouseItemWidget.cpp
Normal file
38
source/gui/MouseItemWidget.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* Filename: MouseItemWidget.cpp
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* Created: 23/06/2018 00:51:15
|
||||
*
|
||||
* Author: Quentin Bazin, <quent42340@gmail.com>
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
#include "MouseItemWidget.hpp"
|
||||
|
||||
void MouseItemWidget::onEvent(const SDL_Event &event) {
|
||||
if (event.type == SDL_MOUSEMOTION && item() != 0) {
|
||||
updatePosition(event.motion.x, event.motion.y);
|
||||
}
|
||||
|
||||
if (event.type == SDL_MOUSEBUTTONDOWN) {
|
||||
updatePosition(event.button.x, event.button.y);
|
||||
}
|
||||
}
|
||||
|
||||
void MouseItemWidget::swapItems(ItemWidget &widget) {
|
||||
u16 widgetItem = widget.item();
|
||||
widget.setItem(item());
|
||||
setItem(widgetItem);
|
||||
}
|
||||
|
||||
void MouseItemWidget::updatePosition(float x, float y) {
|
||||
x = x / 3 - m_parent->getPosition().x - 8;
|
||||
y = y / 3 - m_parent->getPosition().y - 8;
|
||||
|
||||
setPosition(x, y, 0);
|
||||
}
|
||||
|
@ -18,11 +18,21 @@ WorkbenchWidget::WorkbenchWidget(Widget *parent) : Widget(176, 166, parent) {
|
||||
m_background.load("texture-workbench");
|
||||
m_background.setClipRect(0, 0, 176, 166);
|
||||
|
||||
for (u16 i = 0 ; i < 12 ; ++i)
|
||||
m_inventory.setItem(i % m_inventory.width(), i / m_inventory.width(), i + 1);
|
||||
for (u16 i = 0 ; i < 12 ; ++i) {
|
||||
if (i < 9)
|
||||
m_hotbarInventory.setItem(i, 0, i + 1);
|
||||
else
|
||||
m_inventory.setItem(i - 9, 0, i + 1);
|
||||
}
|
||||
|
||||
m_inventoryWidget.update(m_inventory);
|
||||
m_inventoryWidget.setPosition(7, 86, 0);
|
||||
m_craftingInventoryWidget.init(m_craftingInventory);
|
||||
m_craftingInventoryWidget.setPosition(29, 16, 0);
|
||||
|
||||
m_inventoryWidget.init(m_inventory);
|
||||
m_inventoryWidget.setPosition(7, 83, 0);
|
||||
|
||||
m_hotbarInventoryWidget.init(m_hotbarInventory);
|
||||
m_hotbarInventoryWidget.setPosition(7, 141, 0);
|
||||
|
||||
// setScale(3, 3, 1);
|
||||
setPosition(SCREEN_WIDTH / 3.0 / 2.0 - m_background.clipRect().width / 2.0,
|
||||
@ -30,13 +40,22 @@ WorkbenchWidget::WorkbenchWidget(Widget *parent) : Widget(176, 166, parent) {
|
||||
}
|
||||
|
||||
void WorkbenchWidget::onEvent(const SDL_Event &event) {
|
||||
m_inventoryWidget.onEvent(event);
|
||||
m_craftingInventoryWidget.onEvent(event, m_mouseItemWidget);
|
||||
m_inventoryWidget.onEvent(event, m_mouseItemWidget);
|
||||
m_hotbarInventoryWidget.onEvent(event, m_mouseItemWidget);
|
||||
|
||||
m_mouseItemWidget.onEvent(event);
|
||||
}
|
||||
|
||||
void WorkbenchWidget::draw(RenderTarget &target, RenderStates states) const {
|
||||
applyTransform(states);
|
||||
|
||||
target.draw(m_background, states);
|
||||
|
||||
target.draw(m_craftingInventoryWidget, states);
|
||||
target.draw(m_inventoryWidget, states);
|
||||
target.draw(m_hotbarInventoryWidget, states);
|
||||
|
||||
target.draw(m_mouseItemWidget, 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 + 180 / 3.0 * (i - 1) - 8, backgroundY + 16, 0);
|
||||
widget.setPosition(backgroundX + 16 + 180 / 3.0 * (i - 1) - 8, backgroundY + 7, 0);
|
||||
widget.setScale(3, 3, 1);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user