ES-DE/es-core/src/components/MenuComponent.cpp

134 lines
3.9 KiB
C++
Raw Normal View History

#include "components/MenuComponent.h"
2017-11-01 22:21:10 +00:00
#include "components/ButtonComponent.h"
2014-03-22 21:02:25 +00:00
#define BUTTON_GRID_VERT_PADDING 32
#define BUTTON_GRID_HORIZ_PADDING 10
#define TITLE_HEIGHT (mTitle->getFont()->getLetterHeight() + TITLE_VERT_PADDING)
2017-08-30 00:47:04 +00:00
MenuComponent::MenuComponent(Window* window, const char* title, const std::shared_ptr<Font>& titleFont) : GuiComponent(window),
mBackground(window), mGrid(window, Vector2i(1, 3))
2017-08-30 00:47:04 +00:00
{
addChild(&mBackground);
addChild(&mGrid);
mBackground.setImagePath(":/frame.png");
// set up title
mTitle = std::make_shared<TextComponent>(mWindow);
2017-08-15 02:34:34 +00:00
mTitle->setHorizontalAlignment(ALIGN_CENTER);
mTitle->setColor(0x555555FF);
setTitle(title, titleFont);
mGrid.setEntry(mTitle, Vector2i(0, 0), false);
// set up list which will never change (externally, anyway)
mList = std::make_shared<ComponentList>(mWindow);
mGrid.setEntry(mList, Vector2i(0, 1), true);
updateGrid();
updateSize();
mGrid.resetCursor();
}
void MenuComponent::setTitle(const char* title, const std::shared_ptr<Font>& font)
{
mTitle->setText(strToUpper(title));
mTitle->setFont(font);
}
float MenuComponent::getButtonGridHeight() const
{
return (mButtonGrid ? mButtonGrid->getSize().y() : Font::get(FONT_SIZE_MEDIUM)->getHeight() + BUTTON_GRID_VERT_PADDING);
}
void MenuComponent::updateSize()
{
const float maxHeight = Renderer::getScreenHeight() * 0.75f;
float height = TITLE_HEIGHT + mList->getTotalRowHeight() + getButtonGridHeight() + 2;
if(height > maxHeight)
{
height = TITLE_HEIGHT + getButtonGridHeight();
int i = 0;
while(i < mList->size())
{
float rowHeight = mList->getRowHeight(i);
if(height + rowHeight < maxHeight)
height += rowHeight;
else
break;
i++;
}
}
2017-11-13 22:16:38 +00:00
float width = (float) Math::min(Renderer::getScreenHeight(), (unsigned int) (Renderer::getScreenWidth() * 0.90f));
2017-08-30 00:47:04 +00:00
setSize(width, height);
}
void MenuComponent::onSizeChanged()
{
mBackground.fitTo(mSize, Vector3f::Zero(), Vector2f(-32, -32));
// update grid row/col sizes
mGrid.setRowHeightPerc(0, TITLE_HEIGHT / mSize.y());
mGrid.setRowHeightPerc(2, getButtonGridHeight() / mSize.y());
2017-08-30 00:47:04 +00:00
mGrid.setSize(mSize);
}
void MenuComponent::addButton(const std::string& name, const std::string& helpText, const std::function<void()>& callback)
{
mButtons.push_back(std::make_shared<ButtonComponent>(mWindow, strToUpper(name), helpText, callback));
updateGrid();
updateSize();
}
void MenuComponent::updateGrid()
{
if(mButtonGrid)
mGrid.removeEntry(mButtonGrid);
mButtonGrid.reset();
if(mButtons.size())
{
2014-03-15 17:18:50 +00:00
mButtonGrid = makeButtonGrid(mWindow, mButtons);
mGrid.setEntry(mButtonGrid, Vector2i(0, 2), true, false);
}
}
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)
{
std::shared_ptr<ComponentGrid> buttonGrid = std::make_shared<ComponentGrid>(window, Vector2i(buttons.size(), 2));
2014-03-15 17:18:50 +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++)
{
buttonGrid->setColWidthPerc(i, (buttons.at(i)->getSize().x() + BUTTON_GRID_HORIZ_PADDING) / buttonGridWidth);
2014-03-15 17:18:50 +00:00
}
2017-08-30 00:47:04 +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");
2017-11-13 22:16:38 +00:00
bracket->setResize(0, Math::round(Font::get(FONT_SIZE_MEDIUM)->getLetterHeight()));
2014-03-22 21:02:25 +00:00
return bracket;
}