From b08a285b3751facc1c8a30c992d9c5aa8e6c5a51 Mon Sep 17 00:00:00 2001 From: Nebuleon Fumika Date: Wed, 23 Jul 2014 10:03:03 +0000 Subject: [PATCH] Fix the bounding box of message boxes Multi-line message boxes had the incorrect height. I also took the opportunity to make named constants out of magic numbers making up the various message box dimensions. --- src/font.cpp | 11 +++++++++++ src/font.h | 1 + src/messagebox.cpp | 31 ++++++++++++++++++++----------- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/font.cpp b/src/font.cpp index ce5b35e..96c51e7 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -74,6 +74,17 @@ int Font::getTextWidth(const string &text) } } +int Font::getTextHeight(const string &text) +{ + int nLines = 1; + size_t pos = 0; + while ((pos = text.find('\n', pos)) != string::npos) { + nLines++; + pos++; + } + return nLines * getLineSpacing(); +} + void Font::write(Surface *surface, const string &text, int x, int y, HAlign halign, VAlign valign) { diff --git a/src/font.h b/src/font.h index ff58ac9..1703726 100644 --- a/src/font.h +++ b/src/font.h @@ -23,6 +23,7 @@ public: ~Font(); int getTextWidth(const std::string& text); + int getTextHeight(const std::string& text); int getLineSpacing() { diff --git a/src/messagebox.cpp b/src/messagebox.cpp index 2d26237..d5994ce 100644 --- a/src/messagebox.cpp +++ b/src/messagebox.cpp @@ -26,6 +26,10 @@ using namespace std; +constexpr unsigned int ICON_PADDING = 6; +constexpr unsigned int TEXT_PADDING = 8; +constexpr unsigned int ICON_DIMENSION = 32; + MessageBox::MessageBox(GMenu2X *gmenu2x, const string &text, const string &icon) { this->gmenu2x = gmenu2x; this->text = text; @@ -63,25 +67,30 @@ int MessageBox::exec() { bg.box(0, 0, gmenu2x->resX, gmenu2x->resY, 0,0,0,200); SDL_Rect box; - box.h = gmenu2x->font->getLineSpacing()*3 +4; - box.w = gmenu2x->font->getTextWidth(text) + 24 + (gmenu2x->sc[icon] != NULL ? 37 : 0); - box.x = gmenu2x->halfX - box.w/2 -2; - box.y = gmenu2x->halfY - box.h/2 -2; + int textHeight = gmenu2x->font->getTextHeight(text); + box.h = textHeight + 2 * TEXT_PADDING; + box.w = gmenu2x->font->getTextWidth(text) + 2 * TEXT_PADDING; + if (gmenu2x->sc[icon]) { + box.h = max(box.h, (Uint16) (ICON_DIMENSION + 2 * ICON_PADDING)); + box.w += ICON_DIMENSION + ICON_PADDING; + } + box.x = gmenu2x->halfX - box.w/2; + box.y = gmenu2x->halfY - box.h/2; //outer box - bg.box(box, gmenu2x->skinConfColors[COLOR_MESSAGE_BOX_BG]); + bg.box(box.x - 2, box.y - 2, box.w + 4, box.h + 4, gmenu2x->skinConfColors[COLOR_MESSAGE_BOX_BG]); //draw inner rectangle - bg.rectangle(box.x+2, box.y+2, box.w-4, box.h-gmenu2x->font->getLineSpacing(), - gmenu2x->skinConfColors[COLOR_MESSAGE_BOX_BORDER]); + bg.rectangle(box, gmenu2x->skinConfColors[COLOR_MESSAGE_BOX_BORDER]); //icon+text - if (gmenu2x->sc[icon] != NULL) - gmenu2x->sc[icon]->blitCenter( &bg, box.x+25, box.y+gmenu2x->font->getLineSpacing()+3 ); - bg.write( gmenu2x->font, text, box.x+(gmenu2x->sc[icon] != NULL ? 47 : 10), box.y+gmenu2x->font->getLineSpacing()+3, Font::HAlignLeft, Font::VAlignMiddle ); + if (gmenu2x->sc[icon]) { + gmenu2x->sc[icon]->blitCenter( &bg, box.x + ICON_PADDING + ICON_DIMENSION / 2, box.y + ICON_PADDING + ICON_DIMENSION / 2 ); + } + bg.write( gmenu2x->font, text, box.x + TEXT_PADDING + (gmenu2x->sc[icon] ? ICON_PADDING + ICON_DIMENSION : 0), box.y + (box.h - textHeight) / 2, Font::HAlignLeft, Font::VAlignTop ); int btnX = gmenu2x->halfX+box.w/2-6; for (uint i = 0; i < BUTTON_TYPE_SIZE; i++) { if (!buttons[i].empty()) { - buttonPositions[i].y = box.y+box.h-4; + buttonPositions[i].y = box.y+box.h+8; buttonPositions[i].w = btnX; btnX = gmenu2x->drawButtonRight(&bg, buttonLabels[i], buttons[i], btnX, buttonPositions[i].y);