mirror of
https://github.com/scummvm/scummvm.git
synced 2024-06-11 16:47:13 -04:00
GUI: Handle screen resize while displaying MessageDialog
This commit is contained in:
parent
396719c88f
commit
43bd4edcc1
|
@ -44,10 +44,33 @@ void MessageDialog::init(const Common::U32String &message,
|
|||
Graphics::TextAlign alignment,
|
||||
const char *url,
|
||||
const Common::U32String &extraMessage) {
|
||||
// message widgets will be created in reflowLayout as needed
|
||||
_message = message;
|
||||
_alignment = alignment;
|
||||
_url = url;
|
||||
|
||||
_extraMessage = nullptr;
|
||||
|
||||
// Only use bogus sizes, we do the calculation in reflowLayout
|
||||
if (!defaultButton.empty()) {
|
||||
// Confirm dialog
|
||||
_buttons.push_back(new ButtonWidget(this, 0, 0, 0, 0, defaultButton, Common::U32String(), kDefaultCmd, Common::ASCII_RETURN));
|
||||
}
|
||||
|
||||
int buttonHotKey = altButtons.size() == 1 ? Common::ASCII_ESCAPE : 0;
|
||||
for (size_t i = 0, total = altButtons.size(); i < total; ++i) {
|
||||
_buttons.push_back(new ButtonWidget(this, 0, 0, 0, 0, altButtons[i], Common::U32String(), kAltCmd + i, buttonHotKey));
|
||||
buttonHotKey = 0;
|
||||
}
|
||||
|
||||
if (!extraMessage.empty()) {
|
||||
_extraMessage = new StaticTextWidget(this, 0, 0, 0, 0, extraMessage, Graphics::kTextAlignLeft);
|
||||
}
|
||||
}
|
||||
|
||||
void MessageDialog::reflowLayout() {
|
||||
const int horizontalMargin = 10;
|
||||
const int buttonSpacing = 10;
|
||||
|
||||
const int screenW = g_system->getOverlayWidth();
|
||||
const int screenH = g_system->getOverlayHeight();
|
||||
|
||||
|
@ -59,24 +82,24 @@ void MessageDialog::init(const Common::U32String &message,
|
|||
// Using this, and accounting for the space the button(s) need, we can set
|
||||
// the real size of the dialog
|
||||
Common::Array<Common::U32String> lines;
|
||||
int lineCount;
|
||||
const int horizontalMargin = 10;
|
||||
int maxlineWidth = g_gui.getFont().wordWrapText(message, screenW - 2 * horizontalMargin - 20, lines);
|
||||
const int buttonCount = altButtons.size() + 1;
|
||||
const int buttonSpacing = 10;
|
||||
size_t lineCount;
|
||||
|
||||
int maxlineWidth = g_gui.getFont().wordWrapText(_message, screenW - 2 * horizontalMargin - 20, lines);
|
||||
|
||||
const size_t buttonCount = _buttons.size();
|
||||
const int buttonsTotalWidth = buttonCount * buttonWidth + (buttonCount - 1) * buttonSpacing;
|
||||
|
||||
// Calculate the desired dialog size (maxing out at 300*180 for now)
|
||||
// Calculate the desired dialog size
|
||||
_w = MAX(maxlineWidth, buttonsTotalWidth) + 2 * horizontalMargin;
|
||||
|
||||
lineCount = lines.size();
|
||||
|
||||
_h = 16;
|
||||
if (!defaultButton.empty() || !altButtons.empty())
|
||||
if (buttonCount)
|
||||
_h += buttonHeight + 8;
|
||||
|
||||
// Limit the number of lines so that the dialog still fits on the screen.
|
||||
if (lineCount > (screenH - 20 - _h) / kLineHeight) {
|
||||
if (lineCount > size_t((screenH - 20 - _h) / kLineHeight)) {
|
||||
lineCount = (screenH - 20 - _h) / kLineHeight;
|
||||
}
|
||||
_h += lineCount * kLineHeight;
|
||||
|
@ -86,29 +109,34 @@ void MessageDialog::init(const Common::U32String &message,
|
|||
_y = (screenH - _h) / 2;
|
||||
|
||||
// Each line is represented by one static text item.
|
||||
for (int i = 0; i < lineCount; i++) {
|
||||
new StaticTextWidget(this, horizontalMargin, 10 + i * kLineHeight, maxlineWidth, kLineHeight, lines[i], alignment);
|
||||
// Update existing lines
|
||||
size_t toUpdateLines = MIN<size_t>(lineCount, _lines.size());
|
||||
for (size_t i = 0; i < toUpdateLines; i++) {
|
||||
_lines[i]->setPos(horizontalMargin, 10 + i * kLineHeight);
|
||||
_lines[i]->setSize(maxlineWidth, kLineHeight);
|
||||
_lines[i]->setLabel(lines[i]);
|
||||
}
|
||||
// Create missing lines
|
||||
for (size_t i = toUpdateLines; i < lineCount; i++) {
|
||||
_lines.push_back(new StaticTextWidget(this, horizontalMargin, 10 + i * kLineHeight, maxlineWidth, kLineHeight, lines[i], _alignment));
|
||||
}
|
||||
// Cleanup old useless lines
|
||||
for (size_t i = lineCount, total = _lines.size(); i < total; i++) {
|
||||
this->removeWidget(_lines[i]);
|
||||
delete _lines[i];
|
||||
}
|
||||
_lines.resize(lineCount);
|
||||
|
||||
// Assume defaultButton is always given
|
||||
int buttonPos = (_w - buttonsTotalWidth) / 2;
|
||||
|
||||
if (!defaultButton.empty()) {
|
||||
// Confirm dialog
|
||||
new ButtonWidget(this, buttonPos, _h - buttonHeight - 8, buttonWidth, buttonHeight, defaultButton, Common::U32String(), kDefaultCmd, Common::ASCII_RETURN);
|
||||
for (size_t i = 0; i < buttonCount; ++i) {
|
||||
_buttons[i]->setPos(buttonPos, _h - buttonHeight - 8);
|
||||
_buttons[i]->setSize(buttonWidth, buttonHeight);
|
||||
buttonPos += buttonWidth + buttonSpacing;
|
||||
}
|
||||
|
||||
int buttonHotKey = altButtons.size() == 1 ? Common::ASCII_ESCAPE : 0;
|
||||
for (size_t i = 0, total = altButtons.size(); i < total; ++i) {
|
||||
new ButtonWidget(this, buttonPos, _h - buttonHeight - 8, buttonWidth, buttonHeight, altButtons[i], Common::U32String(), kAltCmd + i, buttonHotKey);
|
||||
buttonHotKey = 0;
|
||||
buttonPos += buttonWidth + buttonSpacing;
|
||||
}
|
||||
|
||||
if (!extraMessage.empty()) {
|
||||
_extraMessage = new StaticTextWidget(this, 10, _h, maxlineWidth, kLineHeight, extraMessage, Graphics::kTextAlignLeft);
|
||||
|
||||
if (_extraMessage) {
|
||||
_extraMessage->setPos(10, _h);
|
||||
_extraMessage->setSize(maxlineWidth, kLineHeight);
|
||||
_h += kLineHeight;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ namespace GUI {
|
|||
|
||||
class CommandSender;
|
||||
class StaticTextWidget;
|
||||
class ButtonWidget;
|
||||
|
||||
enum {
|
||||
kMessageOK = 0,
|
||||
|
@ -59,6 +60,8 @@ public:
|
|||
Graphics::TextAlign alignment = Graphics::kTextAlignCenter);
|
||||
|
||||
void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
|
||||
void reflowLayout() override;
|
||||
|
||||
private:
|
||||
const char *_url;
|
||||
void init(const Common::U32String &message,
|
||||
|
@ -69,6 +72,10 @@ private:
|
|||
const Common::U32String &extraMessage);
|
||||
|
||||
protected:
|
||||
Common::U32String _message;
|
||||
Graphics::TextAlign _alignment;
|
||||
Common::Array<StaticTextWidget *> _lines;
|
||||
Common::Array<ButtonWidget *> _buttons;
|
||||
StaticTextWidget *_extraMessage;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue