1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-06-30 21:02:21 +03:00

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.
This commit is contained in:
Nebuleon Fumika 2014-07-23 10:03:03 +00:00 committed by Maarten ter Huurne
parent a468b33d1e
commit b08a285b37
3 changed files with 32 additions and 11 deletions

View File

@ -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)
{

View File

@ -23,6 +23,7 @@ public:
~Font();
int getTextWidth(const std::string& text);
int getTextHeight(const std::string& text);
int getLineSpacing()
{

View File

@ -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);