2020-06-06 14:48:05 +00:00
|
|
|
//
|
|
|
|
// ComponentGrid.cpp
|
|
|
|
//
|
2020-06-06 20:04:05 +00:00
|
|
|
// Provides basic layout of components in an X*Y grid.
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/ComponentGrid.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "Settings.h"
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
using namespace GridFlags;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
ComponentGrid::ComponentGrid(
|
|
|
|
Window* window,
|
|
|
|
const Vector2i& gridDimensions)
|
|
|
|
: GuiComponent(window),
|
|
|
|
mGridSize(gridDimensions),
|
|
|
|
mCursor(0, 0)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
assert(gridDimensions.x() > 0 && gridDimensions.y() > 0);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
mCells.reserve(gridDimensions.x() * gridDimensions.y());
|
|
|
|
|
|
|
|
mColWidths = new float[gridDimensions.x()];
|
|
|
|
mRowHeights = new float[gridDimensions.y()];
|
2020-06-06 14:48:05 +00:00
|
|
|
for (int x = 0; x < gridDimensions.x(); x++)
|
2014-03-12 03:00:08 +00:00
|
|
|
mColWidths[x] = 0;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (int y = 0; y < gridDimensions.y(); y++)
|
2014-03-12 03:00:08 +00:00
|
|
|
mRowHeights[y] = 0;
|
2013-09-19 23:41:14 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
ComponentGrid::~ComponentGrid()
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
delete[] mRowHeights;
|
|
|
|
delete[] mColWidths;
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
float ComponentGrid::getColWidth(int col)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
if (mColWidths[col] != 0)
|
2014-03-12 03:00:08 +00:00
|
|
|
return mColWidths[col] * mSize.x();
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Calculate automatic width.
|
2014-03-12 03:00:08 +00:00
|
|
|
float freeWidthPerc = 1;
|
|
|
|
int between = 0;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (int x = 0; x < mGridSize.x(); x++) {
|
|
|
|
freeWidthPerc -= mColWidths[x]; // If it's 0 it won't do anything.
|
|
|
|
if (mColWidths[x] == 0)
|
2014-03-12 03:00:08 +00:00
|
|
|
between++;
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
return (freeWidthPerc * mSize.x()) / between;
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
float ComponentGrid::getRowHeight(int row)
|
2013-09-19 23:41:14 +00:00
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
if (mRowHeights[row] != 0)
|
2014-03-12 03:00:08 +00:00
|
|
|
return mRowHeights[row] * mSize.y();
|
2013-09-19 23:41:14 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Calculate automatic height.
|
2014-03-12 03:00:08 +00:00
|
|
|
float freeHeightPerc = 1;
|
|
|
|
int between = 0;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (int y = 0; y < mGridSize.y(); y++) {
|
|
|
|
freeHeightPerc -= mRowHeights[y]; // If it's 0 it won't do anything.
|
|
|
|
if (mRowHeights[y] == 0)
|
2014-03-12 03:00:08 +00:00
|
|
|
between++;
|
2013-09-19 23:41:14 +00:00
|
|
|
}
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
return (freeHeightPerc * mSize.y()) / between;
|
2013-09-19 23:41:14 +00:00
|
|
|
}
|
|
|
|
|
2014-05-14 22:47:28 +00:00
|
|
|
void ComponentGrid::setColWidthPerc(int col, float width, bool update)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-25 17:13:41 +00:00
|
|
|
assert(width >= 0 && width <= 1);
|
2014-03-12 03:00:08 +00:00
|
|
|
assert(col >= 0 && col < mGridSize.x());
|
|
|
|
mColWidths[col] = width;
|
2014-05-14 22:47:28 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (update)
|
2014-05-14 22:47:28 +00:00
|
|
|
onSizeChanged();
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2014-05-14 22:47:28 +00:00
|
|
|
void ComponentGrid::setRowHeightPerc(int row, float height, bool update)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-25 17:13:41 +00:00
|
|
|
assert(height >= 0 && height <= 1);
|
2014-03-12 03:00:08 +00:00
|
|
|
assert(row >= 0 && row < mGridSize.y());
|
|
|
|
mRowHeights[row] = height;
|
2014-05-14 22:47:28 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (update)
|
2014-05-14 22:47:28 +00:00
|
|
|
onSizeChanged();
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
void ComponentGrid::setEntry(
|
|
|
|
const std::shared_ptr<GuiComponent>& comp,
|
|
|
|
const Vector2i& pos,
|
|
|
|
bool canFocus,
|
|
|
|
bool resize,
|
|
|
|
const Vector2i& size,
|
|
|
|
unsigned int border,
|
|
|
|
GridFlags::UpdateType updateType)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
assert(pos.x() >= 0 && pos.x() < mGridSize.x() && pos.y() >= 0 && pos.y() < mGridSize.y());
|
|
|
|
assert(comp != nullptr);
|
|
|
|
assert(comp->getParent() == NULL);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
GridEntry entry(pos, size, comp, canFocus, resize, updateType, border);
|
|
|
|
mCells.push_back(entry);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
addChild(comp.get());
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (!cursorValid() && canFocus) {
|
2014-03-15 17:18:50 +00:00
|
|
|
auto origCursor = mCursor;
|
2014-03-12 03:00:08 +00:00
|
|
|
mCursor = pos;
|
2014-03-15 17:18:50 +00:00
|
|
|
onCursorMoved(origCursor, mCursor);
|
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
updateCellComponent(mCells.back());
|
|
|
|
updateSeparators();
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
bool ComponentGrid::removeEntry(const std::shared_ptr<GuiComponent>& comp)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = mCells.cbegin(); it != mCells.cend(); it++) {
|
|
|
|
if (it->component == comp) {
|
2014-03-12 03:00:08 +00:00
|
|
|
removeChild(comp.get());
|
|
|
|
mCells.erase(it);
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
return false;
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
void ComponentGrid::updateCellComponent(const GridEntry& cell)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
// Size.
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f size(0, 0);
|
2020-06-06 14:48:05 +00:00
|
|
|
for (int x = cell.pos.x(); x < cell.pos.x() + cell.dim.x(); x++)
|
2014-03-12 03:00:08 +00:00
|
|
|
size[0] += getColWidth(x);
|
2020-06-06 14:48:05 +00:00
|
|
|
for (int y = cell.pos.y(); y < cell.pos.y() + cell.dim.y(); y++)
|
2014-03-12 03:00:08 +00:00
|
|
|
size[1] += getRowHeight(y);
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (cell.resize)
|
2014-03-12 03:00:08 +00:00
|
|
|
cell.component->setSize(size);
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Position.
|
|
|
|
// Find top left corner.
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector3f pos(0, 0, 0);
|
2020-06-06 14:48:05 +00:00
|
|
|
for (int x = 0; x < cell.pos.x(); x++)
|
2014-03-12 03:00:08 +00:00
|
|
|
pos[0] += getColWidth(x);
|
2020-06-06 14:48:05 +00:00
|
|
|
for (int y = 0; y < cell.pos.y(); y++)
|
2014-03-12 03:00:08 +00:00
|
|
|
pos[1] += getRowHeight(y);
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Center component.
|
2014-03-12 03:00:08 +00:00
|
|
|
pos[0] = pos.x() + (size.x() - cell.component->getSize().x()) / 2;
|
|
|
|
pos[1] = pos.y() + (size.y() - cell.component->getSize().y()) / 2;
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
cell.component->setPosition(pos);
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
void ComponentGrid::updateSeparators()
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
mLines.clear();
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2019-08-08 20:16:11 +00:00
|
|
|
const unsigned int color = Renderer::convertColor(0xC6C7C6FF);
|
2014-03-25 22:47:36 +00:00
|
|
|
bool drawAll = Settings::getInstance()->getBool("DebugGrid");
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f pos;
|
|
|
|
Vector2f size;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = mCells.cbegin(); it != mCells.cend(); it++) {
|
|
|
|
if (!it->border && !drawAll)
|
2014-03-12 03:00:08 +00:00
|
|
|
continue;
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Find component position + size.
|
2017-10-28 20:24:35 +00:00
|
|
|
pos = Vector2f(0, 0);
|
|
|
|
size = Vector2f(0, 0);
|
2020-06-06 14:48:05 +00:00
|
|
|
for (int x = 0; x < it->pos.x(); x++)
|
2014-03-12 03:00:08 +00:00
|
|
|
pos[0] += getColWidth(x);
|
2020-06-06 14:48:05 +00:00
|
|
|
for (int y = 0; y < it->pos.y(); y++)
|
2014-03-12 03:00:08 +00:00
|
|
|
pos[1] += getRowHeight(y);
|
2020-06-06 14:48:05 +00:00
|
|
|
for (int x = it->pos.x(); x < it->pos.x() + it->dim.x(); x++)
|
2014-03-12 03:00:08 +00:00
|
|
|
size[0] += getColWidth(x);
|
2020-06-06 14:48:05 +00:00
|
|
|
for (int y = it->pos.y(); y < it->pos.y() + it->dim.y(); y++)
|
2014-03-12 03:00:08 +00:00
|
|
|
size[1] += getRowHeight(y);
|
2013-08-21 19:49:33 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (it->border & BORDER_TOP || drawAll) {
|
|
|
|
mLines.push_back( { { pos.x(), pos.y() }, { 0.0f, 0.0f }, color } );
|
|
|
|
mLines.push_back( { { pos.x() + size.x(), pos.y() }, { 0.0f, 0.0f }, color } );
|
2013-08-21 19:49:33 +00:00
|
|
|
}
|
2020-06-06 14:48:05 +00:00
|
|
|
if (it->border & BORDER_BOTTOM || drawAll) {
|
|
|
|
mLines.push_back( { { pos.x(), pos.y() + size.y() }, { 0.0f, 0.0f }, color } );
|
|
|
|
mLines.push_back( { { pos.x() + size.x(), mLines.back().pos.y() }, { 0.0f, 0.0f }, color } );
|
2014-03-12 03:00:08 +00:00
|
|
|
}
|
2020-06-06 14:48:05 +00:00
|
|
|
if (it->border & BORDER_LEFT || drawAll) {
|
|
|
|
mLines.push_back( { { pos.x(), pos.y() }, { 0.0f, 0.0f }, color } );
|
|
|
|
mLines.push_back( { { pos.x(), pos.y() + size.y() }, { 0.0f, 0.0f }, color } );
|
2014-03-12 03:00:08 +00:00
|
|
|
}
|
2020-06-06 14:48:05 +00:00
|
|
|
if (it->border & BORDER_RIGHT || drawAll) {
|
|
|
|
mLines.push_back( { { pos.x() + size.x(), pos.y() }, { 0.0f, 0.0f }, color } );
|
|
|
|
mLines.push_back( { { mLines.back().pos.x(), pos.y() + size.y() }, { 0.0f, 0.0f }, color } );
|
2013-08-21 19:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-12 03:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentGrid::onSizeChanged()
|
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = mCells.cbegin(); it != mCells.cend(); it++)
|
2014-03-12 03:00:08 +00:00
|
|
|
updateCellComponent(*it);
|
|
|
|
|
|
|
|
updateSeparators();
|
2013-08-21 19:49:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
const ComponentGrid::GridEntry* ComponentGrid::getCellAt(int x, int y) const
|
2013-08-21 19:49:33 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
assert(x >= 0 && x < mGridSize.x() && y >= 0 && y < mGridSize.y());
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = mCells.cbegin(); it != mCells.cend(); it++) {
|
2014-03-12 03:00:08 +00:00
|
|
|
int xmin = it->pos.x();
|
|
|
|
int xmax = xmin + it->dim.x();
|
|
|
|
int ymin = it->pos.y();
|
|
|
|
int ymax = ymin + it->dim.y();
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (x >= xmin && y >= ymin && x < xmax && y < ymax)
|
2014-03-12 03:00:08 +00:00
|
|
|
return &(*it);
|
2013-08-21 19:49:33 +00:00
|
|
|
}
|
2014-03-12 03:00:08 +00:00
|
|
|
|
|
|
|
return NULL;
|
2013-08-21 19:49:33 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 00:52:32 +00:00
|
|
|
bool ComponentGrid::input(InputConfig* config, Input input)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* cursorEntry = getCellAt(mCursor);
|
2020-06-06 14:48:05 +00:00
|
|
|
if (cursorEntry && cursorEntry->component->input(config, input))
|
2013-06-19 01:12:30 +00:00
|
|
|
return true;
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (!input.value)
|
2013-06-19 01:12:30 +00:00
|
|
|
return false;
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (config->isMappedLike("down", input))
|
2017-10-28 20:24:35 +00:00
|
|
|
return moveCursor(Vector2i(0, 1));
|
2020-06-06 14:48:05 +00:00
|
|
|
|
|
|
|
if (config->isMappedLike("up", input))
|
2017-10-28 20:24:35 +00:00
|
|
|
return moveCursor(Vector2i(0, -1));
|
2020-06-06 14:48:05 +00:00
|
|
|
|
|
|
|
if (config->isMappedLike("left", input))
|
2017-10-28 20:24:35 +00:00
|
|
|
return moveCursor(Vector2i(-1, 0));
|
2020-06-06 14:48:05 +00:00
|
|
|
|
|
|
|
if (config->isMappedLike("right", input))
|
2017-10-28 20:24:35 +00:00
|
|
|
return moveCursor(Vector2i(1, 0));
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:52:32 +00:00
|
|
|
void ComponentGrid::resetCursor()
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
if (!mCells.size())
|
2014-03-12 03:00:08 +00:00
|
|
|
return;
|
2013-09-20 23:55:05 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = mCells.cbegin(); it != mCells.cend(); it++) {
|
|
|
|
if (it->canFocus) {
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2i origCursor = mCursor;
|
2014-03-12 03:00:08 +00:00
|
|
|
mCursor = it->pos;
|
|
|
|
onCursorMoved(origCursor, mCursor);
|
|
|
|
break;
|
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
bool ComponentGrid::moveCursor(Vector2i dir)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
assert(dir.x() || dir.y());
|
2013-08-18 14:16:11 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
const Vector2i origCursor = mCursor;
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* currentCursorEntry = getCellAt(mCursor);
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2i searchAxis(dir.x() == 0, dir.y() == 0);
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
while (mCursor.x() >= 0 && mCursor.y() >= 0 && mCursor.x() < mGridSize.x() &&
|
|
|
|
mCursor.y() < mGridSize.y()) {
|
2013-06-19 01:12:30 +00:00
|
|
|
mCursor = mCursor + dir;
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2i curDirPos = mCursor;
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* cursorEntry;
|
2020-06-06 14:48:05 +00:00
|
|
|
|
|
|
|
// Spread out on search axis+
|
|
|
|
while (mCursor.x() < mGridSize.x() && mCursor.y() < mGridSize.y()
|
|
|
|
&& mCursor.x() >= 0 && mCursor.y() >= 0) {
|
2014-03-12 03:00:08 +00:00
|
|
|
cursorEntry = getCellAt(mCursor);
|
2020-06-06 14:48:05 +00:00
|
|
|
if (cursorEntry && cursorEntry->canFocus && cursorEntry != currentCursorEntry) {
|
2013-08-18 14:16:11 +00:00
|
|
|
onCursorMoved(origCursor, mCursor);
|
2014-03-12 03:00:08 +00:00
|
|
|
return true;
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
mCursor += searchAxis;
|
|
|
|
}
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Now again on search axis-
|
2013-06-19 01:12:30 +00:00
|
|
|
mCursor = curDirPos;
|
2020-06-06 14:48:05 +00:00
|
|
|
while (mCursor.x() >= 0 && mCursor.y() >= 0
|
|
|
|
&& mCursor.x() < mGridSize.x() && mCursor.y() < mGridSize.y()) {
|
2014-03-12 03:00:08 +00:00
|
|
|
cursorEntry = getCellAt(mCursor);
|
2020-06-06 14:48:05 +00:00
|
|
|
|
|
|
|
if (cursorEntry && cursorEntry->canFocus && cursorEntry != currentCursorEntry) {
|
2013-08-18 14:16:11 +00:00
|
|
|
onCursorMoved(origCursor, mCursor);
|
2014-03-12 03:00:08 +00:00
|
|
|
return true;
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
mCursor -= searchAxis;
|
|
|
|
}
|
2013-08-07 05:41:55 +00:00
|
|
|
mCursor = curDirPos;
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Failed to find another focusable element in this direction.
|
2013-06-19 01:12:30 +00:00
|
|
|
mCursor = origCursor;
|
2014-03-12 03:00:08 +00:00
|
|
|
return false;
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
void ComponentGrid::onFocusLost()
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* cursorEntry = getCellAt(mCursor);
|
2020-06-06 14:48:05 +00:00
|
|
|
if (cursorEntry)
|
2014-03-12 03:00:08 +00:00
|
|
|
cursorEntry->component->onFocusLost();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentGrid::onFocusGained()
|
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* cursorEntry = getCellAt(mCursor);
|
2020-06-06 14:48:05 +00:00
|
|
|
if (cursorEntry)
|
2014-03-12 03:00:08 +00:00
|
|
|
cursorEntry->component->onFocusGained();
|
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
bool ComponentGrid::cursorValid()
|
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* e = getCellAt(mCursor);
|
2014-03-15 17:18:50 +00:00
|
|
|
return (e != NULL && e->canFocus);
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 00:52:32 +00:00
|
|
|
void ComponentGrid::update(int deltaTime)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
// Update ALL THE THINGS.
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* cursorEntry = getCellAt(mCursor);
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = mCells.cbegin(); it != mCells.cend(); it++)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
if (it->updateType == UPDATE_ALWAYS ||
|
|
|
|
(it->updateType == UPDATE_WHEN_SELECTED && cursorEntry == &(*it)))
|
2014-03-12 03:00:08 +00:00
|
|
|
it->component->update(deltaTime);
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void ComponentGrid::render(const Transform4x4f& parentTrans)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
Transform4x4f trans = parentTrans * getTransform();
|
2013-10-06 02:56:06 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
renderChildren(trans);
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Draw cell separators.
|
|
|
|
if (mLines.size()) {
|
2014-03-12 03:00:08 +00:00
|
|
|
Renderer::setMatrix(trans);
|
2019-08-08 20:16:11 +00:00
|
|
|
Renderer::bindTexture(0);
|
|
|
|
Renderer::drawLines(&mLines[0], mLines.size());
|
2014-03-12 03:00:08 +00:00
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 00:52:32 +00:00
|
|
|
void ComponentGrid::textInput(const char* text)
|
2013-08-19 15:36:48 +00:00
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* selectedEntry = getCellAt(mCursor);
|
2020-06-06 14:48:05 +00:00
|
|
|
if (selectedEntry != NULL && selectedEntry->canFocus)
|
2014-03-12 03:00:08 +00:00
|
|
|
selectedEntry->component->textInput(text);
|
2013-08-19 15:36:48 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void ComponentGrid::onCursorMoved(Vector2i from, Vector2i to)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* cell = getCellAt(from);
|
2020-06-06 14:48:05 +00:00
|
|
|
if (cell)
|
2014-03-12 03:00:08 +00:00
|
|
|
cell->component->onFocusLost();
|
2013-06-19 21:02:42 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
cell = getCellAt(to);
|
2020-06-06 14:48:05 +00:00
|
|
|
if (cell)
|
2014-03-12 03:00:08 +00:00
|
|
|
cell->component->onFocusGained();
|
|
|
|
|
|
|
|
updateHelpPrompts();
|
2013-06-19 21:02:42 +00:00
|
|
|
}
|
2013-08-18 14:16:11 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
void ComponentGrid::setCursorTo(const std::shared_ptr<GuiComponent>& comp)
|
2013-08-18 14:16:11 +00:00
|
|
|
{
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = mCells.cbegin(); it != mCells.cend(); it++) {
|
|
|
|
if (it->component == comp) {
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2i oldCursor = mCursor;
|
2014-03-12 03:00:08 +00:00
|
|
|
mCursor = it->pos;
|
|
|
|
onCursorMoved(oldCursor, mCursor);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
// Component not found!!
|
2014-03-12 03:00:08 +00:00
|
|
|
assert(false);
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 00:52:32 +00:00
|
|
|
std::vector<HelpPrompt> ComponentGrid::getHelpPrompts()
|
2014-01-25 23:34:29 +00:00
|
|
|
{
|
|
|
|
std::vector<HelpPrompt> prompts;
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* e = getCellAt(mCursor);
|
2020-06-06 14:48:05 +00:00
|
|
|
if (e)
|
2014-03-12 03:00:08 +00:00
|
|
|
prompts = e->component->getHelpPrompts();
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2014-03-13 19:09:50 +00:00
|
|
|
bool canScrollVert = mGridSize.y() > 1;
|
|
|
|
bool canScrollHoriz = mGridSize.x() > 1;
|
2020-06-06 14:48:05 +00:00
|
|
|
for (auto it = prompts.cbegin(); it != prompts.cend(); it++) {
|
|
|
|
if (it->first == "up/down/left/right") {
|
2014-01-25 23:34:29 +00:00
|
|
|
canScrollHoriz = false;
|
|
|
|
canScrollVert = false;
|
|
|
|
break;
|
2020-06-06 14:48:05 +00:00
|
|
|
}
|
|
|
|
else if (it->first == "up/down") {
|
2014-01-25 23:34:29 +00:00
|
|
|
canScrollVert = false;
|
2020-06-06 14:48:05 +00:00
|
|
|
}
|
|
|
|
else if (it->first == "left/right") {
|
2014-01-25 23:34:29 +00:00
|
|
|
canScrollHoriz = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
if (canScrollHoriz && canScrollVert)
|
2014-05-16 21:21:33 +00:00
|
|
|
prompts.push_back(HelpPrompt("up/down/left/right", "choose"));
|
2020-06-06 14:48:05 +00:00
|
|
|
else if (canScrollHoriz)
|
2014-05-16 21:21:33 +00:00
|
|
|
prompts.push_back(HelpPrompt("left/right", "choose"));
|
2020-06-06 14:48:05 +00:00
|
|
|
else if (canScrollVert)
|
2014-05-16 21:21:33 +00:00
|
|
|
prompts.push_back(HelpPrompt("up/down", "choose"));
|
2014-01-25 23:34:29 +00:00
|
|
|
|
|
|
|
return prompts;
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|