2014-06-20 01:30:09 +00:00
|
|
|
#include "components/MenuComponent.h"
|
|
|
|
#include "components/ButtonComponent.h"
|
2014-03-12 03:00:08 +00:00
|
|
|
|
2014-03-22 21:02:25 +00:00
|
|
|
#define BUTTON_GRID_VERT_PADDING 32
|
|
|
|
#define BUTTON_GRID_HORIZ_PADDING 10
|
2014-03-12 23:24:34 +00:00
|
|
|
|
2014-04-19 20:21:15 +00:00
|
|
|
#define TITLE_HEIGHT (mTitle->getFont()->getLetterHeight() + TITLE_VERT_PADDING)
|
2014-04-19 19:15:44 +00:00
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
using namespace Eigen;
|
2014-03-01 21:02:44 +00:00
|
|
|
|
2014-04-19 19:15:44 +00:00
|
|
|
MenuComponent::MenuComponent(Window* window, const char* title, const std::shared_ptr<Font>& titleFont) : 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-04-19 19:15:44 +00:00
|
|
|
// set up title
|
|
|
|
mTitle = std::make_shared<TextComponent>(mWindow);
|
2014-05-01 19:47:33 +00:00
|
|
|
mTitle->setAlignment(ALIGN_CENTER);
|
2014-04-19 19:15:44 +00:00
|
|
|
mTitle->setColor(0x555555FF);
|
|
|
|
setTitle(title, titleFont);
|
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-04-19 19:15:44 +00:00
|
|
|
void MenuComponent::setTitle(const char* title, const std::shared_ptr<Font>& font)
|
|
|
|
{
|
|
|
|
mTitle->setText(strToUpper(title));
|
|
|
|
mTitle->setFont(font);
|
|
|
|
}
|
|
|
|
|
2014-03-21 16:54:48 +00:00
|
|
|
float MenuComponent::getButtonGridHeight() const
|
|
|
|
{
|
|
|
|
return (mButtonGrid ? mButtonGrid->getSize().y() : Font::get(FONT_SIZE_MEDIUM)->getHeight() + BUTTON_GRID_VERT_PADDING);
|
|
|
|
}
|
|
|
|
|
2014-03-12 23:24:34 +00:00
|
|
|
void MenuComponent::updateSize()
|
|
|
|
{
|
2014-05-13 18:49:52 +00:00
|
|
|
const float maxHeight = Renderer::getScreenHeight() * 0.7f;
|
2014-04-19 19:15:44 +00:00
|
|
|
float height = TITLE_HEIGHT + mList->getTotalRowHeight() + getButtonGridHeight() + 2;
|
2014-05-13 18:49:52 +00:00
|
|
|
if(height > maxHeight)
|
|
|
|
{
|
2014-05-21 21:33:10 +00:00
|
|
|
height = TITLE_HEIGHT + getButtonGridHeight();
|
2014-05-13 18:49:52 +00:00
|
|
|
int i = 0;
|
2014-05-21 21:33:10 +00:00
|
|
|
while(i < mList->size())
|
2014-05-13 18:49:52 +00:00
|
|
|
{
|
|
|
|
float rowHeight = mList->getRowHeight(i);
|
|
|
|
if(height + rowHeight < maxHeight)
|
|
|
|
height += rowHeight;
|
2014-05-21 21:33:10 +00:00
|
|
|
else
|
|
|
|
break;
|
2014-05-13 18:49:52 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 23:24:34 +00:00
|
|
|
|
2014-03-15 22:06:16 +00:00
|
|
|
setSize(Renderer::getScreenWidth() * 0.5f, height);
|
2014-03-12 23:24:34 +00:00
|
|
|
}
|
|
|
|
|
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
|
2014-04-19 19:15:44 +00:00
|
|
|
mGrid.setRowHeightPerc(0, TITLE_HEIGHT / mSize.y());
|
2014-03-21 16:54:48 +00:00
|
|
|
mGrid.setRowHeightPerc(2, getButtonGridHeight() / 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-18 21:05:56 +00:00
|
|
|
mButtonGrid.reset();
|
|
|
|
|
2014-03-12 03:00:08 +00:00
|
|
|
if(mButtons.size())
|
|
|
|
{
|
2014-03-15 17:18:50 +00:00
|
|
|
mButtonGrid = makeButtonGrid(mWindow, mButtons);
|
2014-03-12 03:00:08 +00:00
|
|
|
mGrid.setEntry(mButtonGrid, Vector2i(0, 2), true, false);
|
|
|
|
}
|
2014-03-01 21:02:44 +00:00
|
|
|
}
|
2014-03-13 19:09:50 +00:00
|
|
|
|
|
|
|
std::vector<HelpPrompt> MenuComponent::getHelpPrompts()
|
|
|
|
{
|
|
|
|
return mGrid.getHelpPrompts();
|
|
|
|
}
|
2014-03-15 17:18:50 +00:00
|
|
|
|
|
|
|
std::shared_ptr<ComponentGrid> makeButtonGrid(Window* window, const std::vector< std::shared_ptr<ButtonComponent> >& buttons)
|
|
|
|
{
|
2014-03-22 21:55:18 +00:00
|
|
|
std::shared_ptr<ComponentGrid> buttonGrid = std::make_shared<ComponentGrid>(window, Vector2i(buttons.size(), 2));
|
2014-03-15 17:18:50 +00:00
|
|
|
|
2014-03-21 16:54:48 +00:00
|
|
|
float buttonGridWidth = (float)BUTTON_GRID_HORIZ_PADDING * buttons.size(); // initialize to padding
|
2014-03-15 17:18:50 +00:00
|
|
|
for(int i = 0; i < (int)buttons.size(); i++)
|
|
|
|
{
|
|
|
|
buttonGrid->setEntry(buttons.at(i), Vector2i(i, 0), true, false);
|
|
|
|
buttonGridWidth += buttons.at(i)->getSize().x();
|
|
|
|
}
|
|
|
|
for(unsigned int i = 0; i < buttons.size(); i++)
|
|
|
|
{
|
2014-03-21 16:54:48 +00:00
|
|
|
buttonGrid->setColWidthPerc(i, (buttons.at(i)->getSize().x() + BUTTON_GRID_HORIZ_PADDING) / buttonGridWidth);
|
2014-03-15 17:18:50 +00:00
|
|
|
}
|
2014-03-22 21:55:18 +00:00
|
|
|
|
|
|
|
buttonGrid->setSize(buttonGridWidth, buttons.at(0)->getSize().y() + BUTTON_GRID_VERT_PADDING + 2);
|
|
|
|
buttonGrid->setRowHeightPerc(1, 2 / buttonGrid->getSize().y()); // spacer row to deal with dropshadow to make buttons look centered
|
2014-03-15 17:18:50 +00:00
|
|
|
|
|
|
|
return buttonGrid;
|
|
|
|
}
|
2014-03-22 21:02:25 +00:00
|
|
|
|
|
|
|
std::shared_ptr<ImageComponent> makeArrow(Window* window)
|
|
|
|
{
|
|
|
|
auto bracket = std::make_shared<ImageComponent>(window);
|
|
|
|
bracket->setImage(":/arrow.svg");
|
|
|
|
bracket->setResize(0, round(Font::get(FONT_SIZE_MEDIUM)->getLetterHeight()));
|
|
|
|
return bracket;
|
|
|
|
}
|