diff --git a/es-core/src/guis/GuiMsgBox.cpp b/es-core/src/guis/GuiMsgBox.cpp index 2ccfa2549..d7d98eae1 100644 --- a/es-core/src/guis/GuiMsgBox.cpp +++ b/es-core/src/guis/GuiMsgBox.cpp @@ -18,15 +18,20 @@ GuiMsgBox::GuiMsgBox(Window* window, const HelpStyle& helpstyle, const std::stri const std::string& name1, const std::function& func1, const std::string& name2, const std::function& func2, const std::string& name3, const std::function& func3, - bool disableBackButton) + bool disableBackButton, + bool deleteOnButtonPress) : GuiComponent(window), mHelpStyle(helpstyle), mBackground(window, ":/graphics/frame.svg"), mGrid(window, Vector2i(1, 2)), - mDisableBackButton(disableBackButton) + mDisableBackButton(disableBackButton), + mDeleteOnButtonPress(deleteOnButtonPress) { - float width = Renderer::getScreenWidth() * 0.6f; // Max width. - float minWidth = Renderer::getScreenWidth() * 0.3f; // Minimum width. + // For narrower displays (e.g. in 4:3 ratio), allow the window to fill 80% of the screen + // width rather than the 60% allowed for wider displays. + float width = Renderer::getScreenWidth() * + ((Renderer::getScreenAspectRatio() < 1.4f) ? 0.8f : 0.6f); + float minWidth = Renderer::getScreenWidth() * 0.3f; mMsg = std::make_shared(mWindow, text, Font::get(FONT_SIZE_MEDIUM), 0x777777FF, ALIGN_CENTER); @@ -66,11 +71,14 @@ GuiMsgBox::GuiMsgBox(Window* window, const HelpStyle& helpstyle, const std::stri width = std::max(mButtonGrid->getSize().x(), mMsg->getSize().x()); width = std::max(width, minWidth); } + else if (mButtonGrid->getSize().x() > width) { + width = mButtonGrid->getSize().x(); + } // Now that we know width, we can find height. mMsg->setSize(width, 0); // mMsg->getSize.y() now returns the proper length. const float msgHeight = std::max(Font::get(FONT_SIZE_LARGE)->getHeight(), - mMsg->getSize().y()*1.225f); + mMsg->getSize().y() * 1.225f); setSize(width + HORIZONTAL_PADDING_PX * 2 * Renderer::getScreenWidthModifier(), msgHeight + mButtonGrid->getSize().y()); @@ -82,6 +90,36 @@ GuiMsgBox::GuiMsgBox(Window* window, const HelpStyle& helpstyle, const std::stri addChild(&mGrid); } +void GuiMsgBox::changeText(const std::string& newText) +{ + mMsg->setText(newText); + mMsg->setSize(mMsg->getFont()->sizeText(newText)); + + // For narrower displays (e.g. in 4:3 ratio), allow the window to fill 80% of the screen + // width rather than the 60% allowed for wider displays. + float width = Renderer::getScreenWidth() * + ((Renderer::getScreenAspectRatio() < 1.4f) ? 0.8f : 0.6f); + + float minWidth = Renderer::getScreenWidth() * 0.3f; + + // Decide final width. + if (mMsg->getSize().x() < width && mButtonGrid->getSize().x() < width) { + // mMsg and buttons are narrower than width. + width = std::max(mButtonGrid->getSize().x(), mMsg->getSize().x()); + width = std::max(width, minWidth); + } + else if (mButtonGrid->getSize().x() > mSize.x()) { + width = mButtonGrid->getSize().x(); + } + + // Now that we know width, we can find height. + mMsg->setSize(width, 0); // mMsg->getSize.y() now returns the proper length. + const float msgHeight = std::max(Font::get(FONT_SIZE_LARGE)->getHeight(), + mMsg->getSize().y() * 1.225f); + setSize(width + HORIZONTAL_PADDING_PX * 2 * Renderer::getScreenWidthModifier(), + msgHeight + mButtonGrid->getSize().y()); +} + bool GuiMsgBox::input(InputConfig* config, Input input) { // Special case for when GuiMsgBox comes up to report errors before @@ -118,7 +156,9 @@ void GuiMsgBox::onSizeChanged() void GuiMsgBox::deleteMeAndCall(const std::function& func) { auto funcCopy = func; - delete this; + + if (mDeleteOnButtonPress) + delete this; if (funcCopy) funcCopy(); diff --git a/es-core/src/guis/GuiMsgBox.h b/es-core/src/guis/GuiMsgBox.h index 14f51de02..9997c4d8b 100644 --- a/es-core/src/guis/GuiMsgBox.h +++ b/es-core/src/guis/GuiMsgBox.h @@ -20,11 +20,20 @@ class TextComponent; class GuiMsgBox : public GuiComponent { public: - GuiMsgBox(Window* window, const HelpStyle& helpstyle, const std::string& text, - const std::string& name1 = "OK", const std::function& func1 = nullptr, - const std::string& name2 = "", const std::function& func2 = nullptr, - const std::string& name3 = "", const std::function& func3 = nullptr, - bool disableBackButton = false); + GuiMsgBox( + Window* window, + const HelpStyle& helpstyle, + const std::string& text, + const std::string& name1 = "OK", + const std::function& func1 = nullptr, + const std::string& name2 = "", + const std::function& func2 = nullptr, + const std::string& name3 = "", + const std::function& func3 = nullptr, + bool disableBackButton = false, + bool deleteOnButtonPress = true); + + void changeText(const std::string& newText); bool input(InputConfig* config, Input input) override; void onSizeChanged() override; @@ -44,6 +53,7 @@ private: std::shared_ptr mButtonGrid; std::function mAcceleratorFunc; bool mDisableBackButton; + bool mDeleteOnButtonPress; }; #endif // ES_CORE_GUIS_GUI_MSG_BOX_H