2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-21 12:25:28 +00:00
|
|
|
// ComponentGrid.h
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// Provides basic layout of components in an X*Y grid.
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
|
|
|
|
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-21 12:25:28 +00:00
|
|
|
enum UpdateType {
|
|
|
|
UPDATE_ALWAYS,
|
|
|
|
UPDATE_WHEN_SELECTED,
|
|
|
|
UPDATE_NEVER
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Border : unsigned int {
|
|
|
|
BORDER_NONE = 0,
|
|
|
|
BORDER_TOP = 1,
|
|
|
|
BORDER_BOTTOM = 2,
|
|
|
|
BORDER_LEFT = 4,
|
|
|
|
BORDER_RIGHT = 8
|
|
|
|
};
|
2014-03-12 03:00:08 +00:00
|
|
|
};
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2021-01-13 18:48:31 +00:00
|
|
|
// Provides basic layout of components in an X*Y grid.
|
2014-03-12 03:00:08 +00:00
|
|
|
class ComponentGrid : public GuiComponent
|
|
|
|
{
|
|
|
|
public:
|
2020-06-21 12:25:28 +00:00
|
|
|
ComponentGrid(Window* window, const Vector2i& gridDimensions);
|
|
|
|
virtual ~ComponentGrid();
|
|
|
|
|
|
|
|
bool removeEntry(const std::shared_ptr<GuiComponent>& comp);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-12-16 22:59:00 +00:00
|
|
|
void textInput(const std::string& text) override;
|
2020-06-21 12:25:28 +00:00
|
|
|
bool input(InputConfig* config, Input input) override;
|
|
|
|
void update(int deltaTime) override;
|
|
|
|
void render(const Transform4x4f& parentTrans) override;
|
|
|
|
void onSizeChanged() override;
|
|
|
|
|
|
|
|
void resetCursor();
|
|
|
|
bool cursorValid();
|
|
|
|
|
|
|
|
float getColWidth(int col);
|
|
|
|
float getRowHeight(int row);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
bool moveCursor(Vector2i dir);
|
|
|
|
void setCursorTo(const std::shared_ptr<GuiComponent>& comp);
|
|
|
|
|
|
|
|
inline std::shared_ptr<GuiComponent> getSelectedComponent()
|
|
|
|
{
|
|
|
|
const GridEntry* e = getCellAt(mCursor);
|
2020-07-13 18:58:25 +00:00
|
|
|
if (e)
|
2020-06-21 12:25:28 +00:00
|
|
|
return e->component;
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onFocusLost() override;
|
|
|
|
void onFocusGained() override;
|
|
|
|
|
|
|
|
virtual std::vector<HelpPrompt> getHelpPrompts() override;
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2013-06-19 01:12:30 +00:00
|
|
|
private:
|
2020-06-21 12:25:28 +00:00
|
|
|
class GridEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Vector2i pos;
|
|
|
|
Vector2i dim;
|
|
|
|
std::shared_ptr<GuiComponent> component;
|
|
|
|
bool canFocus;
|
|
|
|
bool resize;
|
|
|
|
GridFlags::UpdateType updateType;
|
|
|
|
unsigned int border;
|
|
|
|
|
2020-11-08 21:58:06 +00:00
|
|
|
GridEntry(
|
|
|
|
const Vector2i& p = Vector2i::Zero(),
|
|
|
|
const Vector2i& d = 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)
|
2020-06-21 12:25:28 +00:00
|
|
|
{};
|
|
|
|
|
|
|
|
operator bool() const
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
return component != nullptr;
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-14 20:54:26 +00:00
|
|
|
// Update position and size.
|
2020-06-21 12:25:28 +00:00
|
|
|
void updateCellComponent(const GridEntry& cell);
|
|
|
|
void updateSeparators();
|
|
|
|
|
2021-01-14 20:54:26 +00:00
|
|
|
void onCursorMoved(Vector2i from, Vector2i to);
|
2020-06-21 12:25:28 +00:00
|
|
|
const GridEntry* getCellAt(int x, int y) const;
|
2021-01-14 20:54:26 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
inline const GridEntry* getCellAt(const Vector2i& pos) const
|
|
|
|
{ return getCellAt(pos.x(), pos.y()); }
|
|
|
|
|
2021-01-14 20:54:26 +00:00
|
|
|
std::vector<Renderer::Vertex> mLines;
|
2020-06-21 12:25:28 +00:00
|
|
|
Vector2i mGridSize;
|
|
|
|
std::vector<GridEntry> mCells;
|
|
|
|
Vector2i mCursor;
|
2021-01-14 20:54:26 +00:00
|
|
|
|
|
|
|
float* mRowHeights;
|
|
|
|
float* mColWidths;
|
2013-06-19 01:12:30 +00:00
|
|
|
};
|
2017-10-31 17:12:50 +00:00
|
|
|
|
|
|
|
#endif // ES_CORE_COMPONENTS_COMPONENT_GRID_H
|