2020-09-18 16:16:12 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-07 08:57:49 +00:00
|
|
|
//
|
2020-09-18 16:16:12 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-21 12:25:28 +00:00
|
|
|
// GuiTextEditPopup.cpp
|
2020-06-07 08:57:49 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// Simple text edit popup with a title, a text input box and OK and Cancel buttons.
|
2020-06-07 08:57:49 +00:00
|
|
|
//
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "guis/GuiTextEditPopup.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "Window.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "components/ButtonComponent.h"
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/MenuComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "components/TextEditComponent.h"
|
2020-09-18 16:16:12 +00:00
|
|
|
#include "guis/GuiMsgBox.h"
|
2020-06-07 08:57:49 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
GuiTextEditPopup::GuiTextEditPopup(Window* window,
|
|
|
|
const HelpStyle& helpstyle,
|
|
|
|
const std::string& title,
|
|
|
|
const std::string& initValue,
|
|
|
|
const std::function<void(const std::string&)>& okCallback,
|
|
|
|
bool multiLine,
|
|
|
|
const std::string& acceptBtnText,
|
|
|
|
const std::string& saveConfirmationText)
|
|
|
|
: GuiComponent(window)
|
|
|
|
, mHelpStyle(helpstyle)
|
|
|
|
, mBackground(window, ":/graphics/frame.svg")
|
2021-08-17 16:41:45 +00:00
|
|
|
, mGrid(window, glm::ivec2{1, 3})
|
2021-07-07 18:31:46 +00:00
|
|
|
, mMultiLine(multiLine)
|
|
|
|
, mInitValue(initValue)
|
|
|
|
, mOkCallback(okCallback)
|
|
|
|
, mSaveConfirmationText(saveConfirmationText)
|
2014-03-21 16:10:19 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
addChild(&mBackground);
|
|
|
|
addChild(&mGrid);
|
2014-03-21 16:10:19 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
mTitle = std::make_shared<TextComponent>(mWindow, Utils::String::toUpper(title),
|
2021-07-07 18:31:46 +00:00
|
|
|
Font::get(FONT_SIZE_MEDIUM), 0x555555FF, ALIGN_CENTER);
|
2014-03-21 16:10:19 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
mText = std::make_shared<TextEditComponent>(mWindow);
|
|
|
|
mText->setValue(initValue);
|
2014-03-21 16:10:19 +00:00
|
|
|
|
2021-03-27 09:26:13 +00:00
|
|
|
std::vector<std::shared_ptr<ButtonComponent>> buttons;
|
2020-06-21 12:25:28 +00:00
|
|
|
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, acceptBtnText, acceptBtnText,
|
2021-07-07 18:31:46 +00:00
|
|
|
[this, okCallback] {
|
|
|
|
okCallback(mText->getValue());
|
|
|
|
delete this;
|
|
|
|
}));
|
2020-07-30 14:39:16 +00:00
|
|
|
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, "CLEAR", "clear",
|
2021-07-07 18:31:46 +00:00
|
|
|
[this] { mText->setValue(""); }));
|
2020-06-21 12:25:28 +00:00
|
|
|
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, "CANCEL", "discard changes",
|
2021-07-07 18:31:46 +00:00
|
|
|
[this] { delete this; }));
|
2014-03-21 16:10:19 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
mButtonGrid = makeButtonGrid(mWindow, buttons);
|
2014-03-21 16:10:19 +00:00
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
mGrid.setEntry(mTitle, glm::ivec2{0, 0}, false, true);
|
|
|
|
mGrid.setEntry(mText, glm::ivec2{0, 1}, true, false, glm::ivec2{1, 1},
|
2021-07-07 18:31:46 +00:00
|
|
|
GridFlags::BORDER_TOP | GridFlags::BORDER_BOTTOM);
|
2021-08-17 16:41:45 +00:00
|
|
|
mGrid.setEntry(mButtonGrid, glm::ivec2{0, 2}, true, false);
|
2014-03-21 16:10:19 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
float textHeight = mText->getFont()->getHeight();
|
2020-06-07 08:57:49 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
if (multiLine)
|
2021-07-07 18:31:46 +00:00
|
|
|
textHeight *= 6.0f;
|
2020-06-21 12:25:28 +00:00
|
|
|
mText->setSize(0, textHeight);
|
2014-03-21 16:10:19 +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-08-17 18:55:29 +00:00
|
|
|
float width = glm::clamp(0.50f * aspectValue, 0.40f, 0.70f) * Renderer::getScreenWidth();
|
2021-03-09 16:17:33 +00:00
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
setSize(width, mTitle->getFont()->getHeight() + textHeight + mButtonGrid->getSize().y +
|
|
|
|
mButtonGrid->getSize().y / 2.0f);
|
|
|
|
setPosition((Renderer::getScreenWidth() - mSize.x) / 2.0f,
|
|
|
|
(Renderer::getScreenHeight() - mSize.y) / 2.0f);
|
2020-11-07 11:45:57 +00:00
|
|
|
mText->startEditing();
|
2014-03-21 16:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiTextEditPopup::onSizeChanged()
|
|
|
|
{
|
2021-08-17 16:41:45 +00:00
|
|
|
mBackground.fitTo(mSize, glm::vec3{}, glm::vec2{-32.0f, -32.0f});
|
2014-03-21 16:10:19 +00:00
|
|
|
|
2021-08-16 16:25:01 +00:00
|
|
|
mText->setSize(mSize.x - 40.0f, mText->getSize().y);
|
2014-03-25 23:41:50 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// Update grid.
|
2021-08-16 16:25:01 +00:00
|
|
|
mGrid.setRowHeightPerc(0, mTitle->getFont()->getHeight() / mSize.y);
|
|
|
|
mGrid.setRowHeightPerc(2, mButtonGrid->getSize().y / mSize.y);
|
2020-06-21 12:25:28 +00:00
|
|
|
mGrid.setSize(mSize);
|
2014-03-21 16:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiTextEditPopup::input(InputConfig* config, Input input)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (GuiComponent::input(config, input))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Pressing back when not text editing closes us.
|
|
|
|
if (config->isMappedTo("b", input) && input.value) {
|
|
|
|
if (mText->getValue() != mInitValue) {
|
|
|
|
// Changes were made, ask if the user wants to save them.
|
2021-07-07 18:31:46 +00:00
|
|
|
mWindow->pushGui(new GuiMsgBox(
|
|
|
|
mWindow, mHelpStyle, mSaveConfirmationText, "YES",
|
|
|
|
[this] {
|
|
|
|
this->mOkCallback(mText->getValue());
|
|
|
|
delete this;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
"NO",
|
|
|
|
[this] {
|
|
|
|
delete this;
|
|
|
|
return false;
|
|
|
|
}));
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2014-03-24 01:33:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<HelpPrompt> GuiTextEditPopup::getHelpPrompts()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
std::vector<HelpPrompt> prompts = mGrid.getHelpPrompts();
|
|
|
|
prompts.push_back(HelpPrompt("b", "back"));
|
|
|
|
return prompts;
|
2014-03-24 01:33:27 +00:00
|
|
|
}
|