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
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "GuiComponent.h"
|
2019-08-08 20:16:11 +00:00
|
|
|
#include "renderers/Renderer.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 {
|
2021-07-07 18:31:46 +00:00
|
|
|
UPDATE_ALWAYS, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0).
|
2020-06-21 12:25:28 +00:00
|
|
|
UPDATE_WHEN_SELECTED,
|
|
|
|
UPDATE_NEVER
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Border : unsigned int {
|
|
|
|
BORDER_NONE = 0,
|
|
|
|
BORDER_TOP = 1,
|
|
|
|
BORDER_BOTTOM = 2,
|
|
|
|
BORDER_LEFT = 4,
|
|
|
|
BORDER_RIGHT = 8
|
|
|
|
};
|
2021-07-07 18:31:46 +00:00
|
|
|
}; // namespace GridFlags
|
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:
|
2021-08-17 16:41:45 +00:00
|
|
|
ComponentGrid(Window* window, const glm::ivec2& gridDimensions);
|
2020-06-21 12:25:28 +00:00
|
|
|
virtual ~ComponentGrid();
|
|
|
|
|
|
|
|
bool removeEntry(const std::shared_ptr<GuiComponent>& comp);
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
void setEntry(const std::shared_ptr<GuiComponent>& comp,
|
2021-08-17 16:41:45 +00:00
|
|
|
const glm::ivec2& pos,
|
2021-07-07 18:31:46 +00:00
|
|
|
bool canFocus,
|
|
|
|
bool resize = true,
|
2021-08-17 16:41:45 +00:00
|
|
|
const glm::ivec2& size = glm::ivec2{1, 1},
|
2021-07-07 18:31:46 +00:00
|
|
|
unsigned int border = GridFlags::BORDER_NONE,
|
|
|
|
GridFlags::UpdateType updateType = GridFlags::UPDATE_ALWAYS);
|
2020-06-21 12:25:28 +00:00
|
|
|
|
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;
|
2021-08-15 17:30:31 +00:00
|
|
|
void render(const glm::mat4& parentTrans) override;
|
2020-06-21 12:25:28 +00:00
|
|
|
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);
|
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
bool moveCursor(glm::ivec2 dir);
|
2020-06-21 12:25:28 +00:00
|
|
|
void setCursorTo(const std::shared_ptr<GuiComponent>& comp);
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
std::shared_ptr<GuiComponent> getSelectedComponent()
|
2020-06-21 12:25:28 +00:00
|
|
|
{
|
|
|
|
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:
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::ivec2 pos;
|
|
|
|
glm::ivec2 dim;
|
2020-06-21 12:25:28 +00:00
|
|
|
std::shared_ptr<GuiComponent> component;
|
|
|
|
bool canFocus;
|
|
|
|
bool resize;
|
|
|
|
GridFlags::UpdateType updateType;
|
|
|
|
unsigned int border;
|
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
GridEntry(const glm::ivec2& p = glm::ivec2{},
|
|
|
|
const glm::ivec2& d = glm::ivec2{},
|
2021-07-07 18:31:46 +00:00
|
|
|
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
|
|
|
{
|
|
|
|
}
|
2021-07-07 18:31:46 +00:00
|
|
|
|
|
|
|
operator bool() const { 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-08-17 16:41:45 +00:00
|
|
|
void onCursorMoved(glm::ivec2 from, glm::ivec2 to);
|
2020-06-21 12:25:28 +00:00
|
|
|
const GridEntry* getCellAt(int x, int y) const;
|
2021-08-17 16:41:45 +00:00
|
|
|
const GridEntry* getCellAt(const glm::ivec2& pos) const { return getCellAt(pos.x, pos.y); }
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-01-16 21:52:10 +00:00
|
|
|
std::vector<std::vector<float>> mSeparators;
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::ivec2 mGridSize;
|
2020-06-21 12:25:28 +00:00
|
|
|
std::vector<GridEntry> mCells;
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::ivec2 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
|