2014-03-01 00:52:32 +00:00
|
|
|
#include "ComponentGrid.h"
|
2013-06-19 01:12:30 +00:00
|
|
|
#include "../Log.h"
|
|
|
|
#include "../Renderer.h"
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
using namespace GridFlags;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
ComponentGrid::ComponentGrid(Window* window, const Eigen::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()];
|
|
|
|
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-03-12 03:00:08 +00:00
|
|
|
void ComponentGrid::setColWidthPerc(int col, float width)
|
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;
|
|
|
|
onSizeChanged();
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
void ComponentGrid::setRowHeightPerc(int row, float height)
|
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;
|
|
|
|
onSizeChanged();
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
void ComponentGrid::setEntry(const std::shared_ptr<GuiComponent>& comp, const Eigen::Vector2i& pos, bool canFocus, bool resize, const Eigen::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
|
|
|
|
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
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
for(auto it = mCells.begin(); it != mCells.end(); 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
|
|
|
|
Eigen::Vector2f size(0, 0);
|
|
|
|
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
|
|
|
|
Eigen::Vector3f pos(0, 0, 0);
|
|
|
|
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-12 03:00:08 +00:00
|
|
|
Eigen::Vector2f pos;
|
|
|
|
Eigen::Vector2f size;
|
|
|
|
for(auto it = mCells.begin(); it != mCells.end(); it++)
|
2013-08-21 19:49:33 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
if(!it->border)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// find component position + size
|
|
|
|
pos << 0, 0;
|
|
|
|
size << 0, 0;
|
|
|
|
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-12 03:00:08 +00:00
|
|
|
if(it->border & BORDER_TOP)
|
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-12 03:00:08 +00:00
|
|
|
if(it->border & BORDER_BOTTOM)
|
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));
|
|
|
|
}
|
|
|
|
if(it->border & BORDER_LEFT)
|
|
|
|
{
|
|
|
|
mLines.push_back(Vert(pos.x(), pos.y()));
|
|
|
|
mLines.push_back(Vert(pos.x(), pos.y() + size.y()));
|
|
|
|
}
|
|
|
|
if(it->border & BORDER_RIGHT)
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
Renderer::buildGLColorArray((GLubyte*)mLineColors.data(), 0xC6C7C6FF, mLines.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentGrid::onSizeChanged()
|
|
|
|
{
|
|
|
|
for(auto it = mCells.begin(); it != mCells.end(); it++)
|
|
|
|
updateCellComponent(*it);
|
|
|
|
|
|
|
|
updateSeparators();
|
2013-08-21 19:49:33 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
ComponentGrid::GridEntry* ComponentGrid::getCellAt(int x, int y)
|
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());
|
|
|
|
|
|
|
|
for(auto it = mCells.begin(); it != mCells.end(); 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
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
GridEntry* cursorEntry = getCellAt(mCursor);
|
|
|
|
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))
|
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
return moveCursor(Eigen::Vector2i(0, 1));
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
if(config->isMappedTo("up", input))
|
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
return moveCursor(Eigen::Vector2i(0, -1));
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
2013-08-18 14:16:11 +00:00
|
|
|
if(config->isMappedTo("left", input))
|
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
return moveCursor(Eigen::Vector2i(-1, 0));
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|
|
|
|
if(config->isMappedTo("right", input))
|
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
return moveCursor(Eigen::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
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
for(auto it = mCells.begin(); it != mCells.end(); it++)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
if(it->canFocus)
|
|
|
|
{
|
|
|
|
Eigen::Vector2i origCursor = mCursor;
|
|
|
|
mCursor = it->pos;
|
|
|
|
onCursorMoved(origCursor, mCursor);
|
|
|
|
break;
|
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
bool ComponentGrid::moveCursor(Eigen::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
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
const Eigen::Vector2i origCursor = mCursor;
|
2013-08-18 14:16:11 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
GridEntry* currentCursorEntry = getCellAt(mCursor);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::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;
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Vector2i curDirPos = mCursor;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
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
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
GridEntry* cursorEntry = getCellAt(mCursor);
|
|
|
|
if(cursorEntry)
|
|
|
|
cursorEntry->component->onFocusLost();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentGrid::onFocusGained()
|
|
|
|
{
|
|
|
|
GridEntry* cursorEntry = getCellAt(mCursor);
|
|
|
|
if(cursorEntry)
|
|
|
|
cursorEntry->component->onFocusGained();
|
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
bool ComponentGrid::cursorValid()
|
|
|
|
{
|
2014-03-15 17:18:50 +00:00
|
|
|
GridEntry* e = getCellAt(mCursor);
|
|
|
|
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
|
|
|
|
GridEntry* cursorEntry = getCellAt(mCursor);
|
|
|
|
for(auto it = mCells.begin(); it != mCells.end(); 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-01 00:52:32 +00:00
|
|
|
void ComponentGrid::render(const Eigen::Affine3f& parentTrans)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Affine3f 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
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
glDrawArrays(GL_LINES, 0, mLines.size());
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
void ComponentGrid::onCursorMoved(Eigen::Vector2i from, Eigen::Vector2i to)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
GridEntry* cell = getCellAt(from);
|
|
|
|
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
|
|
|
{
|
2014-03-12 03:00:08 +00:00
|
|
|
for(auto it = mCells.begin(); it != mCells.end(); it++)
|
|
|
|
{
|
|
|
|
if(it->component == comp)
|
|
|
|
{
|
|
|
|
Eigen::Vector2i oldCursor = mCursor;
|
|
|
|
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;
|
2014-03-12 03:00:08 +00:00
|
|
|
GridEntry* e = getCellAt(mCursor);
|
|
|
|
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;
|
2014-01-25 23:34:29 +00:00
|
|
|
for(auto it = prompts.begin(); it != prompts.end(); it++)
|
|
|
|
{
|
|
|
|
if(it->first == "up/down/left/right")
|
|
|
|
{
|
|
|
|
canScrollHoriz = false;
|
|
|
|
canScrollVert = false;
|
|
|
|
break;
|
|
|
|
}else if(it->first == "up/down")
|
|
|
|
{
|
|
|
|
canScrollVert = false;
|
|
|
|
}else if(it->first == "left/right")
|
|
|
|
{
|
|
|
|
canScrollHoriz = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(canScrollHoriz && canScrollVert)
|
2014-03-24 01:33:27 +00:00
|
|
|
prompts.push_back(HelpPrompt("up/down/left/right", "move"));
|
2014-01-25 23:34:29 +00:00
|
|
|
else if(canScrollHoriz)
|
2014-03-24 01:33:27 +00:00
|
|
|
prompts.push_back(HelpPrompt("left/right", "move"));
|
2014-01-25 23:34:29 +00:00
|
|
|
else if(canScrollVert)
|
2014-03-24 01:33:27 +00:00
|
|
|
prompts.push_back(HelpPrompt("up/down", "move"));
|
2014-01-25 23:34:29 +00:00
|
|
|
|
|
|
|
return prompts;
|
2013-08-18 14:16:11 +00:00
|
|
|
}
|