2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-21 12:25:28 +00:00
|
|
|
// MenuComponent.cpp
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// Basic component for building a menu.
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/MenuComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2020-06-06 11:10:33 +00:00
|
|
|
#include "Settings.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "components/ButtonComponent.h"
|
2014-03-12 03:00:08 +00:00
|
|
|
|
2021-01-13 18:45:56 +00:00
|
|
|
#define BUTTON_GRID_VERT_PADDING 32.0f
|
|
|
|
#define BUTTON_GRID_HORIZ_PADDING 10.0f
|
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
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
MenuComponent::MenuComponent(Window* window,
|
|
|
|
std::string title,
|
|
|
|
const std::shared_ptr<Font>& titleFont)
|
|
|
|
: GuiComponent(window)
|
|
|
|
, mBackground(window)
|
|
|
|
, mGrid(window, Vector2i(1, 3))
|
|
|
|
, mNeedsSaving(false)
|
2017-08-30 00:47:04 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
addChild(&mBackground);
|
|
|
|
addChild(&mGrid);
|
2014-03-12 03:00:08 +00:00
|
|
|
|
2021-01-14 21:56:49 +00:00
|
|
|
mBackground.setImagePath(":/graphics/frame.svg");
|
2014-03-01 21:02:44 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// Set up title.
|
|
|
|
mTitle = std::make_shared<TextComponent>(mWindow);
|
|
|
|
mTitle->setHorizontalAlignment(ALIGN_CENTER);
|
|
|
|
mTitle->setColor(0x555555FF);
|
|
|
|
setTitle(title, titleFont);
|
|
|
|
mGrid.setEntry(mTitle, Vector2i(0, 0), false);
|
2014-03-01 21:02:44 +00:00
|
|
|
|
2020-06-21 12:25:28 +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
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
updateGrid();
|
|
|
|
updateSize();
|
2014-03-12 23:24:34 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
mGrid.resetCursor();
|
2014-03-01 21:02:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 11:10:33 +00:00
|
|
|
MenuComponent::~MenuComponent()
|
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
// Save when destroyed.
|
2020-06-21 12:25:28 +00:00
|
|
|
save();
|
2020-06-06 11:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MenuComponent::save()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (!mSaveFuncs.size())
|
|
|
|
return;
|
2020-06-06 11:10:33 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
for (auto it = mSaveFuncs.cbegin(); it != mSaveFuncs.cend(); it++)
|
|
|
|
(*it)();
|
2020-06-06 11:10:33 +00:00
|
|
|
|
2020-11-05 17:18:11 +00:00
|
|
|
if (mNeedsSaving) {
|
|
|
|
Settings::getInstance()->saveFile();
|
|
|
|
mNeedsSaving = false;
|
|
|
|
}
|
2020-06-06 11:10:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 19:27:41 +00:00
|
|
|
void MenuComponent::setTitle(std::string title, const std::shared_ptr<Font>& font)
|
2014-04-19 19:15:44 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mTitle->setText(Utils::String::toUpper(title));
|
|
|
|
mTitle->setFont(font);
|
2014-04-19 19:15:44 +00:00
|
|
|
}
|
|
|
|
|
2014-03-21 16:54:48 +00:00
|
|
|
float MenuComponent::getButtonGridHeight() const
|
|
|
|
{
|
2021-08-16 16:25:01 +00:00
|
|
|
return (mButtonGrid ? mButtonGrid->getSize().y :
|
2021-07-07 18:31:46 +00:00
|
|
|
Font::get(FONT_SIZE_MEDIUM)->getHeight() +
|
|
|
|
(BUTTON_GRID_VERT_PADDING * Renderer::getScreenHeightModifier()));
|
2014-03-21 16:54:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 23:24:34 +00:00
|
|
|
void MenuComponent::updateSize()
|
|
|
|
{
|
2021-05-23 08:40:11 +00:00
|
|
|
const float maxHeight = Renderer::getScreenHeight() * 0.80f;
|
2021-01-29 16:59:05 +00:00
|
|
|
float height = TITLE_HEIGHT + mList->getTotalRowHeight() + getButtonGridHeight() +
|
2021-07-07 18:31:46 +00:00
|
|
|
(2.0f * Renderer::getScreenHeightModifier());
|
2020-06-21 12:25:28 +00:00
|
|
|
if (height > maxHeight) {
|
|
|
|
height = TITLE_HEIGHT + getButtonGridHeight();
|
|
|
|
int i = 0;
|
|
|
|
while (i < mList->size()) {
|
2021-01-29 16:59:05 +00:00
|
|
|
// Add the separator height to the row height so that it also gets properly rendered.
|
|
|
|
float rowHeight = mList->getRowHeight(i) + (1 * Renderer::getScreenHeightModifier());
|
2020-06-21 12:25:28 +00:00
|
|
|
if (height + rowHeight < maxHeight)
|
|
|
|
height += rowHeight;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
float width =
|
|
|
|
static_cast<float>(std::min(static_cast<int>(Renderer::getScreenHeight() * 1.05f),
|
|
|
|
static_cast<int>(Renderer::getScreenWidth() * 0.90f)));
|
2020-06-21 12:25:28 +00:00
|
|
|
setSize(width, height);
|
2014-03-12 23:24:34 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 21:02:44 +00:00
|
|
|
void MenuComponent::onSizeChanged()
|
|
|
|
{
|
2021-08-16 16:25:01 +00:00
|
|
|
mBackground.fitTo(mSize, glm::vec3({}), glm::vec2(-32.0f, -32.0f));
|
2014-03-04 22:48:33 +00:00
|
|
|
|
2021-05-23 08:40:11 +00:00
|
|
|
// Update grid row/column sizes.
|
2021-08-16 16:25:01 +00:00
|
|
|
mGrid.setRowHeightPerc(0, TITLE_HEIGHT / mSize.y);
|
|
|
|
mGrid.setRowHeightPerc(2, getButtonGridHeight() / mSize.y);
|
2017-08-30 00:47:04 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
mGrid.setSize(mSize);
|
2014-03-12 03:00:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
void MenuComponent::addButton(const std::string& name,
|
2021-07-07 18:31:46 +00:00
|
|
|
const std::string& helpText,
|
|
|
|
const std::function<void()>& callback)
|
2014-03-12 03:00:08 +00:00
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
mButtons.push_back(std::make_shared<ButtonComponent>(mWindow, Utils::String::toUpper(name),
|
|
|
|
helpText, callback));
|
2020-06-21 12:25:28 +00:00
|
|
|
updateGrid();
|
|
|
|
updateSize();
|
2014-03-12 03:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MenuComponent::updateGrid()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mButtonGrid)
|
|
|
|
mGrid.removeEntry(mButtonGrid);
|
2014-03-01 21:02:44 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
mButtonGrid.reset();
|
2014-03-18 21:05:56 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mButtons.size()) {
|
|
|
|
mButtonGrid = makeButtonGrid(mWindow, mButtons);
|
|
|
|
mGrid.setEntry(mButtonGrid, Vector2i(0, 2), true, false);
|
|
|
|
}
|
2014-03-01 21:02:44 +00:00
|
|
|
}
|
2014-03-13 19:09:50 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
std::shared_ptr<ComponentGrid> makeButtonGrid(
|
|
|
|
Window* window, const std::vector<std::shared_ptr<ButtonComponent>>& buttons)
|
2014-03-15 17:18:50 +00:00
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
std::shared_ptr<ComponentGrid> buttonGrid =
|
|
|
|
std::make_shared<ComponentGrid>(window, Vector2i(static_cast<int>(buttons.size()), 2));
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-01-13 18:45:56 +00:00
|
|
|
// Initialize to padding.
|
2021-07-07 18:31:46 +00:00
|
|
|
float buttonGridWidth =
|
|
|
|
BUTTON_GRID_HORIZ_PADDING * Renderer::getScreenWidthModifier() * buttons.size();
|
2020-11-17 22:06:54 +00:00
|
|
|
for (int i = 0; i < static_cast<int>(buttons.size()); i++) {
|
2020-06-21 12:25:28 +00:00
|
|
|
buttonGrid->setEntry(buttons.at(i), Vector2i(i, 0), true, false);
|
2021-08-16 16:25:01 +00:00
|
|
|
buttonGridWidth += buttons.at(i)->getSize().x;
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < buttons.size(); i++)
|
2021-07-07 18:31:46 +00:00
|
|
|
buttonGrid->setColWidthPerc(
|
2021-08-16 16:25:01 +00:00
|
|
|
i, (buttons.at(i)->getSize().x +
|
2021-07-07 18:31:46 +00:00
|
|
|
BUTTON_GRID_HORIZ_PADDING * Renderer::getScreenWidthModifier()) /
|
|
|
|
buttonGridWidth);
|
|
|
|
|
|
|
|
buttonGrid->setSize(buttonGridWidth,
|
2021-08-16 16:25:01 +00:00
|
|
|
buttons.at(0)->getSize().y +
|
|
|
|
(BUTTON_GRID_VERT_PADDING * Renderer::getScreenHeightModifier()) +
|
|
|
|
2.0f);
|
2020-06-21 12:25:28 +00:00
|
|
|
// Spacer row to deal with dropshadow to make buttons look centered.
|
2021-08-16 16:25:01 +00:00
|
|
|
buttonGrid->setRowHeightPerc(1, 2.0f / buttonGrid->getSize().y);
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
return buttonGrid;
|
2014-03-15 17:18:50 +00:00
|
|
|
}
|
2014-03-22 21:02:25 +00:00
|
|
|
|
|
|
|
std::shared_ptr<ImageComponent> makeArrow(Window* window)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
auto bracket = std::make_shared<ImageComponent>(window);
|
2020-06-21 17:35:43 +00:00
|
|
|
bracket->setImage(":/graphics/arrow.svg");
|
2020-12-28 10:29:32 +00:00
|
|
|
bracket->setResize(0, std::round(Font::get(FONT_SIZE_MEDIUM)->getLetterHeight()));
|
2020-06-21 12:25:28 +00:00
|
|
|
return bracket;
|
2014-03-22 21:02:25 +00:00
|
|
|
}
|