2014-03-01 21:02:44 +00:00
|
|
|
#include "MenuComponent.h"
|
2014-03-12 03:00:08 +00:00
|
|
|
#include "ButtonComponent.h"
|
|
|
|
|
2014-03-12 23:24:34 +00:00
|
|
|
#define BUTTON_GRID_HEIGHT ((float)Font::get(FONT_SIZE_MEDIUM)->getHeight() + 32)
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
using namespace Eigen;
|
2014-03-01 21:02:44 +00:00
|
|
|
|
|
|
|
MenuComponent::MenuComponent(Window* window, const char* title) : GuiComponent(window),
|
2014-03-12 03:00:08 +00:00
|
|
|
mBackground(window), mGrid(window, Vector2i(1, 3))
|
|
|
|
{
|
|
|
|
addChild(&mBackground);
|
|
|
|
addChild(&mGrid);
|
|
|
|
|
2014-03-01 21:02:44 +00:00
|
|
|
mBackground.setImagePath(":/frame.png");
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
// set up title which will never change
|
2014-03-12 23:24:34 +00:00
|
|
|
mTitle = std::make_shared<TextComponent>(mWindow, strToUpper(title), Font::get(FONT_SIZE_LARGE), 0x555555FF, true);
|
2014-03-12 03:00:08 +00:00
|
|
|
mGrid.setEntry(mTitle, Vector2i(0, 0), false);
|
2014-03-01 21:02:44 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
// set up list which will never change (externally, anyway)
|
|
|
|
mList = std::make_shared<ComponentList>(mWindow);
|
|
|
|
mGrid.setEntry(mList, Vector2i(0, 1), true);
|
2014-03-01 21:02:44 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
updateGrid();
|
2014-03-12 23:24:34 +00:00
|
|
|
updateSize();
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
mGrid.resetCursor();
|
2014-03-01 21:02:44 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 23:24:34 +00:00
|
|
|
void MenuComponent::updateSize()
|
|
|
|
{
|
|
|
|
float height = mTitle->getSize().y() + mList->getTotalRowHeight() + BUTTON_GRID_HEIGHT + 2;
|
|
|
|
if(height > Renderer::getScreenHeight() * 0.7f)
|
|
|
|
height = Renderer::getScreenHeight() * 0.7f;
|
|
|
|
|
|
|
|
setSize(Renderer::getScreenWidth() * 0.4f, height);
|
|
|
|
}
|
|
|
|
|
2014-03-01 21:02:44 +00:00
|
|
|
void MenuComponent::onSizeChanged()
|
|
|
|
{
|
2014-03-04 22:48:33 +00:00
|
|
|
mBackground.fitTo(mSize, Eigen::Vector3f::Zero(), Eigen::Vector2f(-32, -32));
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
// update grid row/col sizes
|
|
|
|
mGrid.setRowHeightPerc(0, mTitle->getSize().y() / mSize.y());
|
2014-03-12 23:24:34 +00:00
|
|
|
mGrid.setRowHeightPerc(2, (BUTTON_GRID_HEIGHT) / mSize.y());
|
2014-03-12 03:00:08 +00:00
|
|
|
|
|
|
|
mGrid.setSize(mSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuComponent::addButton(const std::string& name, const std::string& helpText, const std::function<void()>& callback)
|
|
|
|
{
|
2014-03-12 23:24:34 +00:00
|
|
|
mButtons.push_back(std::make_shared<ButtonComponent>(mWindow, strToUpper(name), helpText, callback));
|
2014-03-12 03:00:08 +00:00
|
|
|
updateGrid();
|
2014-03-12 23:24:34 +00:00
|
|
|
updateSize();
|
2014-03-12 03:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MenuComponent::updateGrid()
|
|
|
|
{
|
|
|
|
if(mButtonGrid)
|
|
|
|
mGrid.removeEntry(mButtonGrid);
|
2014-03-01 21:02:44 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
if(mButtons.size())
|
|
|
|
{
|
|
|
|
mButtonGrid = std::make_shared<ComponentGrid>(mWindow, Vector2i(mButtons.size(), 1));
|
|
|
|
|
|
|
|
float buttonGridWidth = 16.0f * mButtons.size(); // initialize to padding
|
|
|
|
for(int i = 0; i < (int)mButtons.size(); i++)
|
|
|
|
{
|
|
|
|
mButtonGrid->setEntry(mButtons.at(i), Vector2i(i, 0), true, false);
|
|
|
|
buttonGridWidth += mButtons.at(i)->getSize().x();
|
|
|
|
}
|
|
|
|
|
|
|
|
mButtonGrid->setSize(buttonGridWidth, mButtons.at(0)->getSize().y());
|
2014-03-01 21:02:44 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
mGrid.setEntry(mButtonGrid, Vector2i(0, 2), true, false);
|
|
|
|
}else{
|
|
|
|
mButtonGrid.reset();
|
|
|
|
}
|
2014-03-01 21:02:44 +00:00
|
|
|
}
|