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 "Renderer.h"
|
|
|
|
#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
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
ComponentGrid::ComponentGrid(Window* window, const Vector2i& gridDimensions) : GuiComponent(window),
|
2014-03-12 03:00:08 +00:00
|
|
|
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()];
|
|
|
|
for(int x = 0; x < gridDimensions.x(); x++)
|
|
|
|
mColWidths[x] = 0;
|
|
|
|
for(int y = 0; y < gridDimensions.y(); y++)
|
|
|
|
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
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
if(mColWidths[col] != 0)
|
|
|
|
return mColWidths[col] * mSize.x();
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
// calculate automatic width
|
|
|
|
float freeWidthPerc = 1;
|
|
|
|
int between = 0;
|
|
|
|
for(int x = 0; x < mGridSize.x(); x++)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
freeWidthPerc -= mColWidths[x]; // if it's 0 it won't do anything
|
|
|
|
if(mColWidths[x] == 0)
|
|
|
|
between++;
|
2013-06-19 01:12:30 +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
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
if(mRowHeights[row] != 0)
|
|
|
|
return mRowHeights[row] * mSize.y();
|
2013-09-19 23:41:14 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
// calculate automatic height
|
|
|
|
float freeHeightPerc = 1;
|
|
|
|
int between = 0;
|
|
|
|
for(int y = 0; y < mGridSize.y(); y++)
|
2013-09-19 23:41:14 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
freeHeightPerc -= mRowHeights[y]; // if it's 0 it won't do anything
|
|
|
|
if(mRowHeights[y] == 0)
|
|
|
|
between++;
|
2013-09-19 23:41:14 +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
|
|
|
|
|
|
|
if(update)
|
|
|
|
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
|
|
|
|
|
|
|
if(update)
|
|
|
|
onSizeChanged();
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void ComponentGrid::setEntry(const std::shared_ptr<GuiComponent>& comp, const Vector2i& pos, bool canFocus, bool resize, const Vector2i& size,
|
2014-03-12 03:00:08 +00:00
|
|
|
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
|
|
|
|
2014-03-12 03:00:08 +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
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mCells.cbegin(); it != mCells.cend(); it++)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
if(it->component == comp)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
// size
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2f size(0, 0);
|
2014-03-12 03:00:08 +00:00
|
|
|
for(int x = cell.pos.x(); x < cell.pos.x() + cell.dim.x(); x++)
|
|
|
|
size[0] += getColWidth(x);
|
|
|
|
for(int y = cell.pos.y(); y < cell.pos.y() + cell.dim.y(); y++)
|
|
|
|
size[1] += getRowHeight(y);
|
|
|
|
|
|
|
|
if(cell.resize)
|
|
|
|
cell.component->setSize(size);
|
|
|
|
|
|
|
|
// position
|
|
|
|
// find top left corner
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector3f pos(0, 0, 0);
|
2014-03-12 03:00:08 +00:00
|
|
|
for(int x = 0; x < cell.pos.x(); x++)
|
|
|
|
pos[0] += getColWidth(x);
|
|
|
|
for(int y = 0; y < cell.pos.y(); y++)
|
|
|
|
pos[1] += getRowHeight(y);
|
|
|
|
|
|
|
|
// center component
|
|
|
|
pos[0] = pos.x() + (size.x() - cell.component->getSize().x()) / 2;
|
|
|
|
pos[1] = pos.y() + (size.y() - cell.component->getSize().y()) / 2;
|
|
|
|
|
|
|
|
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
|
|
|
|
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;
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mCells.cbegin(); it != mCells.cend(); it++)
|
2013-08-21 19:49:33 +00:00
|
|
|
{
|
2014-03-25 22:47:36 +00:00
|
|
|
if(!it->border && !drawAll)
|
2014-03-12 03:00:08 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// find component position + size
|
2017-10-28 20:24:35 +00:00
|
|
|
pos = Vector2f(0, 0);
|
|
|
|
size = Vector2f(0, 0);
|
2014-03-12 03:00:08 +00:00
|
|
|
for(int x = 0; x < it->pos.x(); x++)
|
|
|
|
pos[0] += getColWidth(x);
|
|
|
|
for(int y = 0; y < it->pos.y(); y++)
|
|
|
|
pos[1] += getRowHeight(y);
|
|
|
|
for(int x = it->pos.x(); x < it->pos.x() + it->dim.x(); x++)
|
|
|
|
size[0] += getColWidth(x);
|
|
|
|
for(int y = it->pos.y(); y < it->pos.y() + it->dim.y(); y++)
|
|
|
|
size[1] += getRowHeight(y);
|
2013-08-21 19:49:33 +00:00
|
|
|
|
2014-03-25 22:47:36 +00:00
|
|
|
if(it->border & BORDER_TOP || drawAll)
|
2013-08-21 19:49:33 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
mLines.push_back(Vert(pos.x(), pos.y()));
|
|
|
|
mLines.push_back(Vert(pos.x() + size.x(), pos.y()));
|
2013-08-21 19:49:33 +00:00
|
|
|
}
|
2014-03-25 22:47:36 +00:00
|
|
|
if(it->border & BORDER_BOTTOM || drawAll)
|
2013-08-21 19:49:33 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
mLines.push_back(Vert(pos.x(), pos.y() + size.y()));
|
|
|
|
mLines.push_back(Vert(pos.x() + size.x(), mLines.back().y));
|
|
|
|
}
|
2014-03-25 22:47:36 +00:00
|
|
|
if(it->border & BORDER_LEFT || drawAll)
|
2014-03-12 03:00:08 +00:00
|
|
|
{
|
|
|
|
mLines.push_back(Vert(pos.x(), pos.y()));
|
|
|
|
mLines.push_back(Vert(pos.x(), pos.y() + size.y()));
|
|
|
|
}
|
2014-03-25 22:47:36 +00:00
|
|
|
if(it->border & BORDER_RIGHT || drawAll)
|
2014-03-12 03:00:08 +00:00
|
|
|
{
|
|
|
|
mLines.push_back(Vert(pos.x() + size.x(), pos.y()));
|
|
|
|
mLines.push_back(Vert(mLines.back().x, pos.y() + size.y()));
|
2013-08-21 19:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
mLineColors.reserve(mLines.size());
|
2017-11-17 14:58:52 +00:00
|
|
|
Renderer::buildGLColorArray((GLubyte*)mLineColors.data(), 0xC6C7C6FF, (unsigned int)mLines.size());
|
2014-03-12 03:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentGrid::onSizeChanged()
|
|
|
|
{
|
2017-11-11 14:56:22 +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());
|
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mCells.cbegin(); it != mCells.cend(); it++)
|
2013-08-21 19:49:33 +00:00
|
|
|
{
|
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();
|
|
|
|
|
|
|
|
if(x >= xmin && y >= ymin && x < xmax && y < ymax)
|
|
|
|
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);
|
2014-03-12 03:00:08 +00:00
|
|
|
if(cursorEntry && cursorEntry->component->input(config, input))
|
2013-06-19 01:12:30 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!input.value)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(config->isMappedTo("down", input))
|
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
return moveCursor(Vector2i(0, 1));
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
if(config->isMappedTo("up", input))
|
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
return moveCursor(Vector2i(0, -1));
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
2013-08-18 14:16:11 +00:00
|
|
|
if(config->isMappedTo("left", input))
|
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
return moveCursor(Vector2i(-1, 0));
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
|
|
|
if(config->isMappedTo("right", input))
|
|
|
|
{
|
2017-10-28 20:24:35 +00:00
|
|
|
return moveCursor(Vector2i(1, 0));
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
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
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
if(!mCells.size())
|
|
|
|
return;
|
2013-09-20 23:55:05 +00:00
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mCells.cbegin(); it != mCells.cend(); it++)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
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;
|
2013-08-18 14:16:11 +00:00
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* currentCursorEntry = getCellAt(mCursor);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
Vector2i searchAxis(dir.x() == 0, dir.y() == 0);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2013-07-10 11:29:43 +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;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* cursorEntry;
|
2013-06-19 01:12:30 +00:00
|
|
|
//spread out on search axis+
|
2014-03-12 03:00:08 +00:00
|
|
|
while(mCursor.x() < mGridSize.x() && mCursor.y() < mGridSize.y()
|
|
|
|
&& mCursor.x() >= 0 && mCursor.y() >= 0)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
cursorEntry = getCellAt(mCursor);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//now again on search axis-
|
|
|
|
mCursor = curDirPos;
|
2014-03-12 03:00:08 +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
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
cursorEntry = getCellAt(mCursor);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
//failed to find another focusable element in this direction
|
|
|
|
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);
|
2014-03-12 03:00:08 +00:00
|
|
|
if(cursorEntry)
|
|
|
|
cursorEntry->component->onFocusLost();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentGrid::onFocusGained()
|
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* cursorEntry = getCellAt(mCursor);
|
2014-03-12 03:00:08 +00:00
|
|
|
if(cursorEntry)
|
|
|
|
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
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
// update ALL THE THINGS
|
2017-11-11 14:56:22 +00:00
|
|
|
const GridEntry* cursorEntry = getCellAt(mCursor);
|
|
|
|
for(auto it = mCells.cbegin(); it != mCells.cend(); it++)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
if(it->updateType == UPDATE_ALWAYS || (it->updateType == UPDATE_WHEN_SELECTED && cursorEntry == &(*it)))
|
|
|
|
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);
|
2013-07-17 04:18:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
// draw cell separators
|
|
|
|
if(mLines.size())
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
Renderer::setMatrix(trans);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glEnableClientState(GL_COLOR_ARRAY);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
glVertexPointer(2, GL_FLOAT, 0, &mLines[0].x);
|
|
|
|
glColorPointer(4, GL_UNSIGNED_BYTE, 0, mLineColors.data());
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2017-11-17 14:58:52 +00:00
|
|
|
glDrawArrays(GL_LINES, 0, (GLsizei)mLines.size());
|
2014-03-12 03:00:08 +00:00
|
|
|
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glDisableClientState(GL_COLOR_ARRAY);
|
|
|
|
}
|
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);
|
2014-03-21 02:47:45 +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);
|
2014-03-12 03:00:08 +00:00
|
|
|
if(cell)
|
|
|
|
cell->component->onFocusLost();
|
2013-06-19 21:02:42 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
cell = getCellAt(to);
|
|
|
|
if(cell)
|
|
|
|
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
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mCells.cbegin(); it != mCells.cend(); it++)
|
2014-03-12 03:00:08 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
// component not found!!
|
|
|
|
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);
|
2014-03-12 03:00:08 +00:00
|
|
|
if(e)
|
|
|
|
prompts = e->component->getHelpPrompts();
|
2014-01-25 23:34:29 +00:00
|
|
|
|
2014-03-13 19:09:50 +00:00
|
|
|
bool canScrollVert = mGridSize.y() > 1;
|
|
|
|
bool canScrollHoriz = mGridSize.x() > 1;
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = prompts.cbegin(); it != prompts.cend(); it++)
|
2014-01-25 23:34:29 +00:00
|
|
|
{
|
2017-08-22 23:21:33 +00:00
|
|
|
if(it->first == "up/down/left/right")
|
2014-01-25 23:34:29 +00:00
|
|
|
{
|
|
|
|
canScrollHoriz = false;
|
|
|
|
canScrollVert = false;
|
|
|
|
break;
|
2017-08-22 23:21:33 +00:00
|
|
|
}else if(it->first == "up/down")
|
2014-01-25 23:34:29 +00:00
|
|
|
{
|
|
|
|
canScrollVert = false;
|
2017-08-22 23:21:33 +00:00
|
|
|
}else if(it->first == "left/right")
|
2014-01-25 23:34:29 +00:00
|
|
|
{
|
|
|
|
canScrollHoriz = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(canScrollHoriz && canScrollVert)
|
2014-05-16 21:21:33 +00:00
|
|
|
prompts.push_back(HelpPrompt("up/down/left/right", "choose"));
|
2014-01-25 23:34:29 +00:00
|
|
|
else if(canScrollHoriz)
|
2014-05-16 21:21:33 +00:00
|
|
|
prompts.push_back(HelpPrompt("left/right", "choose"));
|
2014-01-25 23:34:29 +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
|
|
|
}
|