InventoryView update

This commit is contained in:
MihailRis
2024-02-06 19:00:57 +03:00
parent 52e60c979b
commit 8dbbea7b6d
3 changed files with 211 additions and 309 deletions
+93 -127
View File
@@ -18,133 +18,8 @@ class ContentIndices;
class LevelFrontend;
class Inventory;
typedef std::function<void(ItemStack&)> itemsharefunc;
typedef std::function<void(ItemStack&, ItemStack&)> slotcallback;
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;
std::vector<InventoryPanel> panels;
public:
InventoryLayout(glm::vec2 size);
void add(SlotLayout slot);
void add(InventoryPanel panel);
void setSize(glm::vec2 size);
void setOrigin(glm::vec2 origin);
glm::vec2 getSize() const;
glm::vec2 getOrigin() const;
std::vector<SlotLayout>& getSlots();
std::vector<InventoryPanel>& getPanels();
};
class InventoryBuilder {
std::unique_ptr<InventoryLayout> layout;
public:
InventoryBuilder();
void addGrid(
int cols, int count,
glm::vec2 coord,
int padding,
bool addpanel,
SlotLayout slotLayout);
void add(SlotLayout slotLayout);
void add(InventoryPanel panel);
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(const GfxContext* pctx, Assets* assets) override;
void setHighlighted(bool flag);
bool isHighlighted() const;
virtual void clicked(gui::GUI*, int) override;
virtual void focus(gui::GUI*) override;
};
class InventoryView : public gui::Container {
const Content* content;
const ContentIndices* indices;
std::shared_ptr<Inventory> inventory;
std::unique_ptr<InventoryLayout> layout;
LevelFrontend* frontend;
InventoryInteraction* interaction;
std::vector<SlotView*> slots;
public:
InventoryView(
const Content* content,
LevelFrontend* frontend,
InventoryInteraction* interaction,
std::shared_ptr<Inventory> inventory,
std::unique_ptr<InventoryLayout> layout);
virtual ~InventoryView();
void build();
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
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 = ITEM_ICON_SIZE;
};
using itemsharefunc = std::function<void(ItemStack&)>;
using slotcallback = std::function<void(ItemStack&, ItemStack&)>;
class InventoryInteraction {
ItemStack grabbedItem;
@@ -156,4 +31,95 @@ public:
}
};
struct SlotLayout {
int index;
glm::vec2 position;
bool background;
bool itemSource;
itemsharefunc shareFunc;
slotcallback rightClick;
SlotLayout(int index,
glm::vec2 position,
bool background,
bool itemSource,
itemsharefunc shareFunc,
slotcallback rightClick);
};
class SlotView : public gui::UINode {
LevelFrontend* frontend;
InventoryInteraction& interaction;
const Content* const content;
SlotLayout layout;
bool highlighted = false;
ItemStack* bound = nullptr;
public:
SlotView(LevelFrontend* frontend,
InventoryInteraction& interaction,
SlotLayout layout);
virtual void draw(const GfxContext* pctx, Assets* assets) override;
void setHighlighted(bool flag);
bool isHighlighted() const;
virtual void clicked(gui::GUI*, int) override;
virtual void focus(gui::GUI*) override;
void bind(ItemStack& stack);
const SlotLayout& getLayout() const;
};
class InventoryView : public gui::Container {
const Content* content;
const ContentIndices* indices;
std::shared_ptr<Inventory> inventory;
LevelFrontend* frontend;
InventoryInteraction& interaction;
std::vector<SlotView*> slots;
glm::vec2 origin {};
public:
InventoryView(LevelFrontend* frontend, InventoryInteraction& interaction);
virtual ~InventoryView();
void setInventory(std::shared_ptr<Inventory> inventory);
virtual void setCoord(glm::vec2 coord) override;
void setOrigin(glm::vec2 origin);
glm::vec2 getOrigin() const;
void setSelected(int index);
void bind(std::shared_ptr<Inventory> inventory);
void addSlot(SlotLayout layout);
static const int SLOT_INTERVAL = 4;
static const int SLOT_SIZE = ITEM_ICON_SIZE;
};
class InventoryBuilder {
std::shared_ptr<InventoryView> view;
LevelFrontend* frontend;
InventoryInteraction& interaction;
public:
InventoryBuilder(LevelFrontend* frontend, InventoryInteraction& interaction);
void addGrid(
int cols, int count,
glm::vec2 coord,
int padding,
bool addpanel,
SlotLayout slotLayout);
void add(SlotLayout slotLayout);
std::shared_ptr<InventoryView> build();
};
#endif // FRONTEND_INVENTORY_VIEW_H_