Inventory (WIP part I)

This commit is contained in:
MihailRis
2024-01-21 15:23:46 +03:00
parent fe5cab0db0
commit 5142f3b4e7
13 changed files with 864 additions and 175 deletions
+121 -17
View File
@@ -5,46 +5,150 @@
#include <functional>
#include <glm/glm.hpp>
#include "../frontend/gui/UINode.h"
#include "../frontend/gui/panels.h"
#include "../frontend/gui/controls.h"
#include "../items/ItemStack.h"
#include "../typedefs.h"
class Batch2D;
class Assets;
class GfxContext;
class Content;
class ContentIndices;
class LevelFrontend;
class Inventory;
typedef std::function<void(itemid_t)> slotconsumer;
typedef std::function<void(ItemStack&)> itemsharefunc;
typedef std::function<void(ItemStack&, ItemStack&)> slotcallback;
class InventoryView {
class InventoryInteraction;
struct SlotLayout {
glm::vec2 position;
bool background;
bool itemSource;
itemsharefunc shareFunc;
slotcallback rightClick;
SlotLayout(glm::vec2 position,
bool background,
bool itemSource,
itemsharefunc shareFunc,
slotcallback rightClick);
};
// temporary unused
struct InventoryPanel {
glm::vec2 position;
glm::vec2 size;
glm::vec4 color;
InventoryPanel(glm::vec2 position,
glm::vec2 size,
glm::vec4 color);
};
class InventoryLayout {
glm::vec2 size;
glm::vec2 origin;
std::vector<SlotLayout> slots;
public:
InventoryLayout(glm::vec2 size);
void add(SlotLayout slot);
void setSize(glm::vec2 size);
void setOrigin(glm::vec2 origin);
glm::vec2 getSize() const;
glm::vec2 getOrigin() const;
std::vector<SlotLayout>& getSlots();
};
class InventoryBuilder {
std::unique_ptr<InventoryLayout> layout;
public:
InventoryBuilder();
void addGrid(
int cols, int rows,
glm::vec2 coord,
int padding,
SlotLayout slotLayout);
std::unique_ptr<InventoryLayout> build();
};
class SlotView : public gui::UINode {
LevelFrontend* frontend;
InventoryInteraction* interaction;
const Content* const content;
ItemStack& stack;
bool highlighted = false;
SlotLayout layout;
public:
SlotView(ItemStack& stack,
LevelFrontend* frontend,
InventoryInteraction* interaction,
const Content* content,
SlotLayout layout);
virtual void draw(Batch2D* batch, Assets* assets) override;
void setHighlighted(bool flag);
bool isHighlighted() const;
virtual void clicked(gui::GUI*, int) override;
};
class InventoryView : public gui::Container {
const Content* content;
const ContentIndices* indices;
std::vector<itemid_t> items;
slotconsumer consumer = nullptr;
std::shared_ptr<Inventory> inventory;
std::unique_ptr<InventoryLayout> layout;
LevelFrontend* frontend;
InventoryInteraction* interaction;
std::vector<SlotView*> slots;
int scroll = 0;
int columns;
uint iconSize = 48;
uint interval = 4;
glm::ivec2 padding {interval, interval};
glm::ivec2 position {0, 0};
public:
InventoryView(
int columns,
const Content* content,
LevelFrontend* frontend,
std::vector<itemid_t> items);
InventoryInteraction* interaction,
std::shared_ptr<Inventory> inventory,
std::unique_ptr<InventoryLayout> layout);
virtual ~InventoryView();
virtual void actAndDraw(const GfxContext* ctx);
void build();
void setItems(std::vector<itemid_t> items);
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual void drawBackground(Batch2D* batch, Assets* assets) override;
void setPosition(int x, int y);
int getWidth() const;
int getHeight() const;
void setSlotConsumer(slotconsumer consumer);
void setInventory(std::shared_ptr<Inventory> inventory);
virtual void setCoord(glm::vec2 coord) override;
InventoryLayout* getLayout() const;
void setSelected(int index);
static const int SLOT_INTERVAL = 4;
static const int SLOT_SIZE = 48;
};
class InventoryInteraction {
ItemStack grabbedItem;
public:
InventoryInteraction() = default;
ItemStack& getGrabbedItem() {
return grabbedItem;
}
};
#endif // FRONTEND_INVENTORY_VIEW_H_