2013-06-19 01:12:30 +00:00
|
|
|
#include "ComponentListComponent.h"
|
|
|
|
#include "../Log.h"
|
|
|
|
#include "../Renderer.h"
|
|
|
|
|
|
|
|
#define INITIAL_CELL_SIZE 12
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
ComponentListComponent::ComponentListComponent(Window* window, Eigen::Vector2i gridDimensions) : GuiComponent(window), mGrid(NULL), mColumnWidths(NULL), mRowHeights(NULL)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
mEntries.reserve(gridDimensions.x() * gridDimensions.y());
|
2013-06-19 01:12:30 +00:00
|
|
|
makeCells(gridDimensions);
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void ComponentListComponent::makeCells(Eigen::Vector2i size)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
|
|
|
if(mGrid)
|
|
|
|
delete[] mGrid;
|
|
|
|
if(mColumnWidths)
|
|
|
|
delete[] mColumnWidths;
|
|
|
|
if(mRowHeights)
|
|
|
|
delete[] mRowHeights;
|
|
|
|
|
|
|
|
mGridSize = size;
|
2013-07-10 11:29:43 +00:00
|
|
|
mGrid = new ComponentEntry*[size.x() * size.y()];
|
|
|
|
std::fill(mGrid, mGrid + (size.x() * size.y()), (ComponentEntry*)NULL);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
mColumnWidths = new unsigned int[size.x()];
|
|
|
|
std::fill(mColumnWidths, mColumnWidths + size.x(), INITIAL_CELL_SIZE);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
mRowHeights = new unsigned int[size.y()];
|
|
|
|
std::fill(mRowHeights, mRowHeights + size.y(), INITIAL_CELL_SIZE);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
updateSize();
|
|
|
|
resetCursor();
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void ComponentListComponent::setEntry(Eigen::Vector2i pos, Eigen::Vector2i size, GuiComponent* component, bool canFocus, AlignmentType align,
|
|
|
|
Eigen::Matrix<bool, 1, 2> autoFit, UpdateBehavior updateType)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
if(pos.x() > mGridSize.x() || pos.y() > mGridSize.y() || pos.x() < 0 || pos.y() < 0)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
|
|
|
LOG(LogError) << "Tried to set entry beyond grid size!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(component == NULL)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Tried to add NULL component to ComponentList!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
ComponentEntry entry(Eigen::Vector2i(pos.x(), pos.y()), Eigen::Vector2i(size.x(), size.y()), component, updateType, canFocus, align);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
mEntries.push_back(entry);
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
for(int y = pos.y(); y < pos.y() + size.y(); y++)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
for(int x = pos.x(); x < pos.x() + size.x(); x++)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
|
|
|
setCell(x, y, &mEntries.back());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(component->getParent() != NULL)
|
|
|
|
LOG(LogError) << "ComponentListComponent ruining an existing parent-child relationship! Call a social worker!";
|
|
|
|
component->setParent(this);
|
|
|
|
|
|
|
|
if(!cursorValid() && canFocus)
|
2013-07-10 11:29:43 +00:00
|
|
|
mCursor = pos;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
//update the column width and row height
|
2013-07-10 11:29:43 +00:00
|
|
|
if(autoFit.x() && (int)getColumnWidth(pos.x()) < component->getSize().x())
|
|
|
|
setColumnWidth(pos.x(), (unsigned int)component->getSize().x());
|
|
|
|
if(autoFit.y() && (int)getRowHeight(pos.y()) < component->getSize().y())
|
|
|
|
setRowHeight(pos.y(), (unsigned int)component->getSize().y());
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
component->setPosition(getCellOffset(pos));
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentListComponent::setRowHeight(int row, unsigned int size)
|
|
|
|
{
|
|
|
|
mRowHeights[row] = size;
|
|
|
|
updateSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentListComponent::setColumnWidth(int col, unsigned int size)
|
|
|
|
{
|
|
|
|
mColumnWidths[col] = size;
|
|
|
|
updateSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ComponentListComponent::getRowHeight(int row) { return mRowHeights[row]; }
|
|
|
|
unsigned int ComponentListComponent::getColumnWidth(int col) { return mColumnWidths[col]; }
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Vector3f ComponentListComponent::getCellOffset(Eigen::Vector2i pos)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Vector3f offset(0, 0, 0);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
for(int y = 0; y < pos.y(); y++)
|
|
|
|
offset[1] += getRowHeight(y);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
for(int x = 0; x < pos.x(); x++)
|
|
|
|
offset[0] += getColumnWidth(x);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
ComponentEntry* entry = getCell(pos.x(), pos.y());
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2013-07-17 04:18:30 +00:00
|
|
|
Eigen::Vector2i gridSize(0, 0);
|
2013-07-10 11:29:43 +00:00
|
|
|
for(int x = pos.x(); x < pos.x() + entry->dim[0]; x++)
|
|
|
|
gridSize[0] += getColumnWidth(x);
|
|
|
|
for(int y = pos.y(); y < pos.y() + entry->dim[1]; y++)
|
|
|
|
gridSize[1] += getRowHeight(y);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
//if AlignCenter, add half of cell width - half of control width
|
|
|
|
if(entry->alignment == AlignCenter)
|
2013-07-10 11:29:43 +00:00
|
|
|
offset[0] += gridSize.x() / 2 - entry->component->getSize().x() / 2;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
//if AlignRight, add cell width - control width
|
|
|
|
if(entry->alignment == AlignRight)
|
2013-07-10 11:29:43 +00:00
|
|
|
offset[0] += gridSize.x() - entry->component->getSize().x();
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
//always center on the Y axis
|
2013-07-10 11:29:43 +00:00
|
|
|
offset[1] += gridSize.y() / 2 - entry->component->getSize().y() / 2;
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentListComponent::setCell(unsigned int x, unsigned int y, ComponentEntry* entry)
|
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
if(x >= (unsigned int)mGridSize.x() || y >= (unsigned int)mGridSize.y())
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
|
|
|
LOG(LogError) << "Invalid setCell - position " << x << ", " << y << " out of bounds!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
mGrid[y * mGridSize.x() + x] = entry;
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ComponentListComponent::ComponentEntry* ComponentListComponent::getCell(unsigned int x, unsigned int y)
|
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
if(x >= (unsigned int)mGridSize.x() || y >= (unsigned int)mGridSize.y())
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
|
|
|
LOG(LogError) << "Invalid getCell - position " << x << ", " << y << " out of bounds!";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
return mGrid[y * mGridSize.x() + x];
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentListComponent::updateSize()
|
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
mSize = Eigen::Vector2f(0, 0);
|
|
|
|
for(int x = 0; x < mGridSize.x(); x++)
|
|
|
|
mSize.x() += getColumnWidth(x);
|
|
|
|
for(int y = 0; y < mGridSize.y(); y++)
|
|
|
|
mSize.y() += getRowHeight(y);
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentListComponent::updateComponentOffsets()
|
|
|
|
{
|
|
|
|
for(auto iter = mEntries.begin(); iter != mEntries.end(); iter++)
|
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
iter->component->setPosition(getCellOffset(iter->pos));
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ComponentListComponent::input(InputConfig* config, Input input)
|
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
if(cursorValid() && getCell(mCursor.x(), mCursor.y())->component->input(config, input))
|
2013-06-19 01:12:30 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!input.value)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(config->isMappedTo("down", input))
|
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
moveCursor(Eigen::Vector2i(0, 1));
|
2013-06-19 01:12:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(config->isMappedTo("up", input))
|
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
moveCursor(Eigen::Vector2i(0, -1));
|
2013-06-19 01:12:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentListComponent::resetCursor()
|
|
|
|
{
|
|
|
|
if(mEntries.size() == 0)
|
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
mCursor = Eigen::Vector2i(-1, -1);
|
2013-06-19 01:12:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
mCursor << mEntries.at(0).pos[0], mEntries.at(0).pos[1];
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void ComponentListComponent::moveCursor(Eigen::Vector2i dir)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
if(dir.x() != 0 && dir.y() != 0)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
|
|
|
LOG(LogError) << "Invalid cursor move dir!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!cursorValid())
|
|
|
|
{
|
|
|
|
resetCursor();
|
|
|
|
if(!cursorValid())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Vector2i origCursor = 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
|
|
|
|
|
|
|
//spread out on search axis+
|
2013-07-10 11:29:43 +00:00
|
|
|
while(mCursor.x() < mGridSize.x() && mCursor.y() < mGridSize.y())
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
if(cursorValid() && getCell(mCursor.x(), mCursor.y())->canFocus)
|
2013-06-19 01:12:30 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
mCursor += searchAxis;
|
|
|
|
}
|
|
|
|
|
|
|
|
//now again on search axis-
|
|
|
|
mCursor = curDirPos;
|
2013-07-10 11:29:43 +00:00
|
|
|
while(mCursor.x() >= 0 && mCursor.y() >= 0)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
if(cursorValid() && getCell(mCursor.x(), mCursor.y())->canFocus)
|
2013-06-19 01:12:30 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ComponentListComponent::cursorValid()
|
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
if(mCursor.x() < 0 || mCursor.y() < 0 || mCursor.x() >= mGridSize.x() || mCursor.y() >= mGridSize.y())
|
2013-06-19 01:12:30 +00:00
|
|
|
return false;
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
return getCell(mCursor.x(), mCursor.y()) != NULL;
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentListComponent::update(int deltaTime)
|
|
|
|
{
|
|
|
|
for(auto iter = mEntries.begin(); iter != mEntries.end(); iter++)
|
|
|
|
{
|
|
|
|
if(iter->updateType == UpdateAlways)
|
|
|
|
{
|
|
|
|
iter->component->update(deltaTime);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
if(iter->updateType == UpdateFocused && cursorValid() && getCell(mCursor.x(), mCursor.y())->component == iter->component)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
|
|
|
iter->component->update(deltaTime);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void ComponentListComponent::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-07-17 04:18:30 +00:00
|
|
|
Renderer::setMatrix(trans);
|
2013-07-10 11:29:43 +00:00
|
|
|
|
|
|
|
Renderer::drawRect(0, 0, (int)getSize().x(), (int)getSize().y(), 0xFFFFFFAA);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
for(auto iter = mEntries.begin(); iter != mEntries.end(); iter++)
|
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
iter->component->render(trans);
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 04:18:30 +00:00
|
|
|
|
2013-06-19 01:12:30 +00:00
|
|
|
//draw cell outlines
|
2013-07-17 04:18:30 +00:00
|
|
|
/*Renderer::setMatrix(trans);
|
|
|
|
Eigen::Vector2i pos(0, 0);
|
|
|
|
for(int x = 0; x < mGridSize.x(); x++)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2013-07-17 04:18:30 +00:00
|
|
|
for(int y = 0; y < mGridSize.y(); y++)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2013-07-17 04:18:30 +00:00
|
|
|
Renderer::drawRect(pos.x(), pos.y(), getColumnWidth(x), 2, 0x000000AA);
|
|
|
|
Renderer::drawRect(pos.x(), pos.y(), 2, getRowHeight(y), 0x000000AA);
|
|
|
|
Renderer::drawRect(pos.x() + getColumnWidth(x), pos.y(), 2, getRowHeight(y), 0x000000AA);
|
|
|
|
Renderer::drawRect(pos.x(), pos.y() + getRowHeight(y) - 2, getColumnWidth(x), 2, 0x000000AA);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2013-07-17 04:18:30 +00:00
|
|
|
pos[1] += getRowHeight(y);
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 04:18:30 +00:00
|
|
|
pos[1] = 0;
|
|
|
|
pos[0] += getColumnWidth(x);
|
2013-06-19 01:12:30 +00:00
|
|
|
}*/
|
|
|
|
|
|
|
|
//draw cursor
|
|
|
|
if(cursorValid())
|
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
ComponentEntry* entry = getCell(mCursor.x(), mCursor.y());
|
2013-07-17 04:18:30 +00:00
|
|
|
Eigen::Affine3f entryTrans = trans * entry->component->getTransform();
|
|
|
|
Renderer::setMatrix(entryTrans);
|
|
|
|
|
|
|
|
Renderer::drawRect(0, 0, 4, 4, 0xFF0000FF);
|
|
|
|
Renderer::drawRect(0, 0, (int)entry->component->getSize().x(), (int)entry->component->getSize().y(), 0x0000AA88);
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void ComponentListComponent::onPositionChanged()
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
|
|
|
updateComponentOffsets();
|
|
|
|
}
|
2013-06-19 21:02:42 +00:00
|
|
|
|
|
|
|
GuiComponent* ComponentListComponent::getSelectedComponent()
|
|
|
|
{
|
|
|
|
if(!cursorValid())
|
|
|
|
return NULL;
|
2013-07-10 11:29:43 +00:00
|
|
|
return getCell(mCursor.x(), mCursor.y())->component;
|
2013-06-19 21:02:42 +00:00
|
|
|
}
|