2020-06-06 14:48:05 +00:00
|
|
|
//
|
|
|
|
// ComponentGrid.h
|
|
|
|
//
|
|
|
|
// Providing basic layout of other components in an X*Y grid.
|
|
|
|
//
|
|
|
|
|
2013-06-19 01:12:30 +00:00
|
|
|
#pragma once
|
2017-10-31 17:12:50 +00:00
|
|
|
#ifndef ES_CORE_COMPONENTS_COMPONENT_GRID_H
|
|
|
|
#define ES_CORE_COMPONENTS_COMPONENT_GRID_H
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
#include "math/Vector2i.h"
|
2019-08-08 20:16:11 +00:00
|
|
|
#include "renderers/Renderer.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "GuiComponent.h"
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
namespace GridFlags
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
enum UpdateType {
|
2014-03-12 03:00:08 +00:00
|
|
|
UPDATE_ALWAYS,
|
|
|
|
UPDATE_WHEN_SELECTED,
|
|
|
|
UPDATE_NEVER
|
2013-06-19 01:12:30 +00:00
|
|
|
};
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
enum Border : unsigned int {
|
2014-03-12 03:00:08 +00:00
|
|
|
BORDER_NONE = 0,
|
|
|
|
|
|
|
|
BORDER_TOP = 1,
|
|
|
|
BORDER_BOTTOM = 2,
|
|
|
|
BORDER_LEFT = 4,
|
|
|
|
BORDER_RIGHT = 8
|
2013-06-19 01:12:30 +00:00
|
|
|
};
|
2014-03-12 03:00:08 +00:00
|
|
|
};
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
// Used to arrange a bunch of components in a spreadsheet-esque grid.
|
|
|
|
class ComponentGrid : public GuiComponent
|
|
|
|
{
|
|
|
|
public:
|
2017-10-28 20:24:35 +00:00
|
|
|
ComponentGrid(Window* window, const Vector2i& gridDimensions);
|
2014-03-12 03:00:08 +00:00
|
|
|
virtual ~ComponentGrid();
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
bool removeEntry(const std::shared_ptr<GuiComponent>& comp);
|
2013-09-19 23:41:14 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
void setEntry(
|
|
|
|
const std::shared_ptr<GuiComponent>& comp,
|
|
|
|
const Vector2i& pos,
|
|
|
|
bool canFocus,
|
|
|
|
bool resize = true,
|
|
|
|
const Vector2i& size = Vector2i(1, 1),
|
|
|
|
unsigned int border = GridFlags::BORDER_NONE,
|
|
|
|
GridFlags::UpdateType updateType = GridFlags::UPDATE_ALWAYS);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2013-08-19 15:36:48 +00:00
|
|
|
void textInput(const char* text) override;
|
2013-06-19 01:12:30 +00:00
|
|
|
bool input(InputConfig* config, Input input) override;
|
|
|
|
void update(int deltaTime) override;
|
2017-10-28 20:24:35 +00:00
|
|
|
void render(const Transform4x4f& parentTrans) override;
|
2014-03-12 03:00:08 +00:00
|
|
|
void onSizeChanged() override;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
void resetCursor();
|
|
|
|
bool cursorValid();
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
float getColWidth(int col);
|
|
|
|
float getRowHeight(int row);
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// If update is false, will not call an onSizeChanged() which triggers
|
|
|
|
// a (potentially costly) repositioning + resizing of every element.
|
|
|
|
void setColWidthPerc(int col, float width, bool update = true);
|
|
|
|
// Dito.
|
|
|
|
void setRowHeightPerc(int row, float height, bool update = true);
|
2014-03-12 03:00:08 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
bool moveCursor(Vector2i dir);
|
2014-03-12 03:00:08 +00:00
|
|
|
void setCursorTo(const std::shared_ptr<GuiComponent>& comp);
|
|
|
|
|
|
|
|
inline std::shared_ptr<GuiComponent> getSelectedComponent()
|
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* e = getCellAt(mCursor);
|
2014-03-12 03:00:08 +00:00
|
|
|
if(e)
|
|
|
|
return e->component;
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-06-19 21:02:42 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
void onFocusLost() override;
|
|
|
|
void onFocusGained() override;
|
2013-09-19 23:41:14 +00:00
|
|
|
|
2014-01-25 23:34:29 +00:00
|
|
|
virtual std::vector<HelpPrompt> getHelpPrompts() override;
|
|
|
|
|
2013-06-19 01:12:30 +00:00
|
|
|
private:
|
2014-03-12 03:00:08 +00:00
|
|
|
class GridEntry
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2i pos;
|
|
|
|
Vector2i dim;
|
2014-03-12 03:00:08 +00:00
|
|
|
std::shared_ptr<GuiComponent> component;
|
2013-06-19 01:12:30 +00:00
|
|
|
bool canFocus;
|
2014-03-12 03:00:08 +00:00
|
|
|
bool resize;
|
|
|
|
GridFlags::UpdateType updateType;
|
|
|
|
unsigned int border;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
GridEntry(const Vector2i& p = Vector2i::Zero(), const Vector2i& d = Vector2i::Zero(),
|
2019-08-25 15:23:02 +00:00
|
|
|
const std::shared_ptr<GuiComponent>& cmp = nullptr, bool f = false, bool r = true,
|
2020-06-06 14:48:05 +00:00
|
|
|
GridFlags::UpdateType u = GridFlags::UPDATE_ALWAYS, unsigned int b =
|
|
|
|
GridFlags::BORDER_NONE) :
|
2014-03-12 03:00:08 +00:00
|
|
|
pos(p), dim(d), component(cmp), canFocus(f), resize(r), updateType(u), border(b)
|
|
|
|
{};
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return component != NULL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
float* mRowHeights;
|
|
|
|
float* mColWidths;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2019-08-08 20:16:11 +00:00
|
|
|
std::vector<Renderer::Vertex> mLines;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Update position & size.
|
2014-03-12 03:00:08 +00:00
|
|
|
void updateCellComponent(const GridEntry& cell);
|
|
|
|
void updateSeparators();
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* getCellAt(int x, int y) const;
|
2020-06-06 14:48:05 +00:00
|
|
|
inline const GridEntry* getCellAt(const Vector2i& pos) const
|
|
|
|
{ return getCellAt(pos.x(), pos.y()); }
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2i mGridSize;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
std::vector<GridEntry> mCells;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void onCursorMoved(Vector2i from, Vector2i to);
|
|
|
|
Vector2i mCursor;
|
2013-06-19 01:12:30 +00:00
|
|
|
};
|
2017-10-31 17:12:50 +00:00
|
|
|
|
|
|
|
#endif // ES_CORE_COMPONENTS_COMPONENT_GRID_H
|