ComponentGrid.h 3.18 KB
Newer Older
1 2 3 4
#pragma once

#include "../GuiComponent.h"

5
namespace GridFlags
6
{
7
	enum UpdateType
8
	{
9 10 11
		UPDATE_ALWAYS,
		UPDATE_WHEN_SELECTED,
		UPDATE_NEVER
12 13
	};

14
	enum Border : unsigned int
15
	{
16 17 18 19 20 21
		BORDER_NONE = 0,

		BORDER_TOP = 1,
		BORDER_BOTTOM = 2,
		BORDER_LEFT = 4,
		BORDER_RIGHT = 8
22
	};
23
};
24

25 26 27 28 29 30
// Used to arrange a bunch of components in a spreadsheet-esque grid.
class ComponentGrid : public GuiComponent
{
public:
	ComponentGrid(Window* window, const Eigen::Vector2i& gridDimensions);
	virtual ~ComponentGrid();
31

32
	bool removeEntry(const std::shared_ptr<GuiComponent>& comp);
33

34 35
	void setEntry(const std::shared_ptr<GuiComponent>& comp, const Eigen::Vector2i& pos, bool canFocus, bool resize = true, 
		const Eigen::Vector2i& size = Eigen::Vector2i(1, 1), unsigned int border = GridFlags::BORDER_NONE, GridFlags::UpdateType updateType = GridFlags::UPDATE_ALWAYS);
36

Aloshi's avatar
Aloshi committed
37
	void textInput(const char* text) override;
38 39
	bool input(InputConfig* config, Input input) override;
	void update(int deltaTime) override;
40
	void render(const Eigen::Affine3f& parentTrans) override;
41
	void onSizeChanged() override;
42 43 44 45

	void resetCursor();
	bool cursorValid();

46 47 48
	float getColWidth(int col);
	float getRowHeight(int row);

Aloshi's avatar
Aloshi committed
49 50
	void setColWidthPerc(int col, float width, bool update = true); // if update is false, will not call an onSizeChanged() which triggers a (potentially costly) repositioning + resizing of every element
	void setRowHeightPerc(int row, float height, bool update = true); // if update is false, will not call an onSizeChanged() which triggers a (potentially costly) repositioning + resizing of every element
51 52 53 54 55 56 57 58 59 60 61 62

	bool moveCursor(Eigen::Vector2i dir);
	void setCursorTo(const std::shared_ptr<GuiComponent>& comp);

	inline std::shared_ptr<GuiComponent> getSelectedComponent()
	{
		GridEntry* e = getCellAt(mCursor);
		if(e)
			return e->component;
		else
			return nullptr;
	}
Aloshi's avatar
Aloshi committed
63

64 65
	void onFocusLost() override;
	void onFocusGained() override;
66

Aloshi's avatar
Aloshi committed
67 68
	virtual std::vector<HelpPrompt> getHelpPrompts() override;

69
private:
70
	class GridEntry
71 72
	{
	public:
73 74
		Eigen::Vector2i pos;
		Eigen::Vector2i dim;
75
		std::shared_ptr<GuiComponent> component;
76
		bool canFocus;
77 78 79
		bool resize;
		GridFlags::UpdateType updateType;
		unsigned int border;
80

81 82 83 84 85
		GridEntry(const Eigen::Vector2i& p = Eigen::Vector2i::Zero(), const Eigen::Vector2i& d = Eigen::Vector2i::Zero(),
			const std::shared_ptr<GuiComponent>& cmp = nullptr, bool f = false, bool r = true, 
			GridFlags::UpdateType u = GridFlags::UPDATE_ALWAYS, unsigned int b = GridFlags::BORDER_NONE) : 
			pos(p), dim(d), component(cmp), canFocus(f), resize(r), updateType(u), border(b)
		{};
86 87 88 89 90 91 92

		operator bool() const
		{
			return component != NULL;
		}
	};

93 94 95 96 97 98 99 100 101
	float* mRowHeights;
	float* mColWidths;
	
	struct Vert
	{
		Vert(float xi = 0, float yi = 0) : x(xi), y(yi) {};
		float x;
		float y;
	};
102

103 104
	std::vector<Vert> mLines;
	std::vector<unsigned int> mLineColors;
105

106 107 108
	// Update position & size
	void updateCellComponent(const GridEntry& cell);
	void updateSeparators();
109

110 111 112 113
	GridEntry* getCellAt(int x, int y);
	inline GridEntry* getCellAt(const Eigen::Vector2i& pos) { return getCellAt(pos.x(), pos.y()); }
	
	Eigen::Vector2i mGridSize;
114

115
	std::vector<GridEntry> mCells;
116

Aloshi's avatar
Aloshi committed
117
	void onCursorMoved(Eigen::Vector2i from, Eigen::Vector2i to);
118
	Eigen::Vector2i mCursor;
119
};