2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-07 08:57:49 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-21 12:25:28 +00:00
|
|
|
// GuiMsgBox.cpp
|
2020-06-07 08:57:49 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// Popup message dialog with a notification text and a choice of one,
|
|
|
|
// two or three buttons.
|
2020-06-07 08:57:49 +00:00
|
|
|
//
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "guis/GuiMsgBox.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/ButtonComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "components/MenuComponent.h"
|
2014-03-15 17:18:50 +00:00
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
#define HORIZONTAL_PADDING_PX 20.0f
|
2014-03-29 01:58:45 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
GuiMsgBox::GuiMsgBox(Window* window,
|
|
|
|
const HelpStyle& helpstyle,
|
|
|
|
const std::string& text,
|
|
|
|
const std::string& name1,
|
|
|
|
const std::function<void()>& func1,
|
|
|
|
const std::string& name2,
|
|
|
|
const std::function<void()>& func2,
|
|
|
|
const std::string& name3,
|
|
|
|
const std::function<void()>& func3,
|
|
|
|
bool disableBackButton,
|
|
|
|
bool deleteOnButtonPress)
|
|
|
|
: GuiComponent(window)
|
|
|
|
, mHelpStyle(helpstyle)
|
|
|
|
, mBackground(window, ":/graphics/frame.svg")
|
2021-08-17 16:41:45 +00:00
|
|
|
, mGrid(window, glm::ivec2{1, 2})
|
2021-07-07 18:31:46 +00:00
|
|
|
, mDisableBackButton(disableBackButton)
|
|
|
|
, mDeleteOnButtonPress(deleteOnButtonPress)
|
2014-03-15 17:18:50 +00:00
|
|
|
{
|
2021-07-02 15:57:52 +00:00
|
|
|
// Adjust the width relative to the aspect ratio of the screen to make the GUI look coherent
|
|
|
|
// regardless of screen type. The 1.778 aspect ratio value is the 16:9 reference.
|
|
|
|
float aspectValue = 1.778f / Renderer::getScreenAspectRatio();
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
float width =
|
2021-08-17 18:55:29 +00:00
|
|
|
floorf(glm::clamp(0.60f * aspectValue, 0.60f, 0.80f) * Renderer::getScreenWidth());
|
2021-07-07 18:31:46 +00:00
|
|
|
float minWidth =
|
2021-08-17 18:55:29 +00:00
|
|
|
floorf(glm::clamp(0.30f * aspectValue, 0.10f, 0.50f) * Renderer::getScreenWidth());
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
mMsg = std::make_shared<TextComponent>(mWindow, text, Font::get(FONT_SIZE_MEDIUM), 0x777777FF,
|
|
|
|
ALIGN_CENTER);
|
2021-08-17 16:41:45 +00:00
|
|
|
mGrid.setEntry(mMsg, glm::ivec2{0, 0}, false, false);
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
// Create the buttons.
|
2021-07-07 18:31:46 +00:00
|
|
|
mButtons.push_back(std::make_shared<ButtonComponent>(
|
|
|
|
mWindow, name1, name1, std::bind(&GuiMsgBox::deleteMeAndCall, this, func1)));
|
2020-06-21 12:25:28 +00:00
|
|
|
if (!name2.empty())
|
2021-07-07 18:31:46 +00:00
|
|
|
mButtons.push_back(std::make_shared<ButtonComponent>(
|
|
|
|
mWindow, name2, name2, std::bind(&GuiMsgBox::deleteMeAndCall, this, func2)));
|
2020-06-21 12:25:28 +00:00
|
|
|
if (!name3.empty())
|
2021-07-07 18:31:46 +00:00
|
|
|
mButtons.push_back(std::make_shared<ButtonComponent>(
|
|
|
|
mWindow, name3, name3, std::bind(&GuiMsgBox::deleteMeAndCall, this, func3)));
|
2020-06-21 12:25:28 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
// Set accelerator automatically (button to press when "B" is pressed).
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mButtons.size() == 1) {
|
|
|
|
mAcceleratorFunc = mButtons.front()->getPressedFunc();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (auto it = mButtons.cbegin(); it != mButtons.cend(); it++) {
|
|
|
|
if (Utils::String::toUpper((*it)->getText()) == "OK" ||
|
2021-07-07 18:31:46 +00:00
|
|
|
Utils::String::toUpper((*it)->getText()) == "NO") {
|
2020-06-21 12:25:28 +00:00
|
|
|
mAcceleratorFunc = (*it)->getPressedFunc();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the buttons into a ComponentGrid.
|
|
|
|
mButtonGrid = makeButtonGrid(mWindow, mButtons);
|
2021-08-17 16:41:45 +00:00
|
|
|
mGrid.setEntry(mButtonGrid, glm::ivec2{0, 1}, true, false, glm::ivec2{1, 1},
|
|
|
|
GridFlags::BORDER_TOP);
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
// Decide final width.
|
2021-08-16 16:25:01 +00:00
|
|
|
if (mMsg->getSize().x < width && mButtonGrid->getSize().x < width) {
|
2020-06-21 12:25:28 +00:00
|
|
|
// mMsg and buttons are narrower than width.
|
2021-08-16 16:25:01 +00:00
|
|
|
width = std::max(mButtonGrid->getSize().x, mMsg->getSize().x);
|
2020-12-28 10:29:32 +00:00
|
|
|
width = std::max(width, minWidth);
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
2021-08-16 16:25:01 +00:00
|
|
|
else if (mButtonGrid->getSize().x > width) {
|
|
|
|
width = mButtonGrid->getSize().x;
|
2021-03-09 16:30:50 +00:00
|
|
|
}
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
// Now that we know width, we can find height.
|
2021-08-16 16:25:01 +00:00
|
|
|
mMsg->setSize(width, 0.0f); // mMsg->getSize.y() now returns the proper length.
|
2021-07-07 18:31:46 +00:00
|
|
|
const float msgHeight =
|
2021-08-16 16:25:01 +00:00
|
|
|
std::max(Font::get(FONT_SIZE_LARGE)->getHeight(), mMsg->getSize().y * 1.225f);
|
|
|
|
setSize(width + HORIZONTAL_PADDING_PX * 2.0f * Renderer::getScreenWidthModifier(),
|
|
|
|
msgHeight + mButtonGrid->getSize().y);
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
// Center for good measure.
|
2021-08-16 16:25:01 +00:00
|
|
|
setPosition((Renderer::getScreenWidth() - mSize.x) / 2.0f,
|
|
|
|
(Renderer::getScreenHeight() - mSize.y) / 2.0f);
|
2020-06-21 12:25:28 +00:00
|
|
|
|
|
|
|
addChild(&mBackground);
|
|
|
|
addChild(&mGrid);
|
2014-03-15 17:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 16:30:50 +00:00
|
|
|
void GuiMsgBox::changeText(const std::string& newText)
|
|
|
|
{
|
|
|
|
mMsg->setText(newText);
|
|
|
|
mMsg->setSize(mMsg->getFont()->sizeText(newText));
|
|
|
|
|
2021-07-02 15:57:52 +00:00
|
|
|
// Adjust the width depending on the aspect ratio of the screen, to make the screen look
|
|
|
|
// somewhat coherent regardless of screen type. The 1.778 aspect ratio value is the 16:9
|
|
|
|
// reference.
|
|
|
|
float aspectValue = 1.778f / Renderer::getScreenAspectRatio();
|
2021-03-09 16:30:50 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
float width =
|
2021-08-17 18:55:29 +00:00
|
|
|
floorf(glm::clamp(0.60f * aspectValue, 0.60f, 0.80f) * Renderer::getScreenWidth());
|
2021-03-09 16:30:50 +00:00
|
|
|
float minWidth = Renderer::getScreenWidth() * 0.3f;
|
|
|
|
|
|
|
|
// Decide final width.
|
2021-08-16 16:25:01 +00:00
|
|
|
if (mMsg->getSize().x < width && mButtonGrid->getSize().x < width) {
|
2021-03-09 16:30:50 +00:00
|
|
|
// mMsg and buttons are narrower than width.
|
2021-08-16 16:25:01 +00:00
|
|
|
width = std::max(mButtonGrid->getSize().x, mMsg->getSize().x);
|
2021-03-09 16:30:50 +00:00
|
|
|
width = std::max(width, minWidth);
|
|
|
|
}
|
2021-08-16 16:25:01 +00:00
|
|
|
else if (mButtonGrid->getSize().x > mSize.x) {
|
|
|
|
width = mButtonGrid->getSize().x;
|
2021-03-09 16:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we know width, we can find height.
|
|
|
|
mMsg->setSize(width, 0); // mMsg->getSize.y() now returns the proper length.
|
2021-07-07 18:31:46 +00:00
|
|
|
const float msgHeight =
|
2021-08-16 16:25:01 +00:00
|
|
|
std::max(Font::get(FONT_SIZE_LARGE)->getHeight(), mMsg->getSize().y * 1.225f);
|
|
|
|
setSize(width + HORIZONTAL_PADDING_PX * 2.0f * Renderer::getScreenWidthModifier(),
|
|
|
|
msgHeight + mButtonGrid->getSize().y);
|
2021-03-09 16:30:50 +00:00
|
|
|
}
|
|
|
|
|
2014-03-15 22:06:16 +00:00
|
|
|
bool GuiMsgBox::input(InputConfig* config, Input input)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
// Special case for when GuiMsgBox comes up to report errors before
|
|
|
|
// anything has been configured.
|
|
|
|
if (config->getDeviceId() == DEVICE_KEYBOARD && !config->isConfigured() && input.value &&
|
|
|
|
(input.id == SDLK_RETURN || input.id == SDLK_ESCAPE || input.id == SDLK_SPACE)) {
|
|
|
|
mAcceleratorFunc();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-15 08:12:19 +00:00
|
|
|
if (!mDisableBackButton) {
|
|
|
|
if (mAcceleratorFunc && config->isMappedTo("b", input) && input.value != 0) {
|
|
|
|
mAcceleratorFunc();
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GuiComponent::input(config, input);
|
2014-03-15 22:06:16 +00:00
|
|
|
}
|
|
|
|
|
2014-03-15 17:18:50 +00:00
|
|
|
void GuiMsgBox::onSizeChanged()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mGrid.setSize(mSize);
|
2021-08-16 16:25:01 +00:00
|
|
|
mGrid.setRowHeightPerc(1, mButtonGrid->getSize().y / mSize.y);
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// Update messagebox size.
|
2021-08-16 16:25:01 +00:00
|
|
|
mMsg->setSize(mSize.x - HORIZONTAL_PADDING_PX * 2.0f * Renderer::getScreenWidthModifier(),
|
2021-07-07 18:31:46 +00:00
|
|
|
mGrid.getRowHeight(0));
|
2020-06-21 12:25:28 +00:00
|
|
|
mGrid.onSizeChanged();
|
2014-03-29 01:58:45 +00:00
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
mBackground.fitTo(mSize, glm::vec3{}, glm::vec2{-32.0f, -32.0f});
|
2014-03-15 17:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiMsgBox::deleteMeAndCall(const std::function<void()>& func)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
auto funcCopy = func;
|
2021-03-09 16:30:50 +00:00
|
|
|
|
|
|
|
if (mDeleteOnButtonPress)
|
|
|
|
delete this;
|
2014-05-16 22:19:25 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (funcCopy)
|
|
|
|
funcCopy();
|
2014-03-15 17:18:50 +00:00
|
|
|
}
|
2014-03-24 01:33:27 +00:00
|
|
|
|
|
|
|
std::vector<HelpPrompt> GuiMsgBox::getHelpPrompts()
|
|
|
|
{
|
2020-07-12 19:05:50 +00:00
|
|
|
std::vector<HelpPrompt> prompts = mGrid.getHelpPrompts();
|
2020-08-15 08:12:19 +00:00
|
|
|
|
|
|
|
// If there is only one button, then remove the "Choose" help symbol
|
|
|
|
// as there is no way to make a choice.
|
|
|
|
if (mButtons.size() == 1)
|
|
|
|
prompts.pop_back();
|
|
|
|
|
|
|
|
if (!mDisableBackButton)
|
|
|
|
prompts.push_back(HelpPrompt("b", "Back"));
|
|
|
|
|
2020-07-12 19:05:50 +00:00
|
|
|
return prompts;
|
2014-03-24 01:33:27 +00:00
|
|
|
}
|