Root/src/messagebox.cpp

1/***************************************************************************
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#include "messagebox.h"
22#include "gmenu2x.h"
23#include "surface.h"
24
25#include <unistd.h>
26
27using namespace std;
28
29constexpr unsigned int ICON_PADDING = 6;
30constexpr unsigned int TEXT_PADDING = 8;
31constexpr unsigned int ICON_DIMENSION = 32;
32
33MessageBox::MessageBox(GMenu2X& gmenu2x, const string &text, const string &icon)
34    : gmenu2x(gmenu2x)
35    , text(text)
36    , icon(icon)
37{
38    for (uint i = 0; i < BUTTON_TYPE_SIZE; i++) {
39        buttons[i] = "";
40        buttonLabels[i] = "";
41        buttonPositions[i].h = gmenu2x.font->getLineSpacing();
42    }
43
44    //Default enabled button
45    buttons[InputManager::ACCEPT] = "OK";
46
47    //Default labels
48    buttonLabels[InputManager::UP] = "up";
49    buttonLabels[InputManager::DOWN] = "down";
50    buttonLabels[InputManager::LEFT] = "left";
51    buttonLabels[InputManager::RIGHT] = "right";
52    buttonLabels[InputManager::CANCEL] = "cancel";
53    buttonLabels[InputManager::ACCEPT] = "accept";
54    buttonLabels[InputManager::ALTLEFT] = "l";
55    buttonLabels[InputManager::ALTRIGHT] = "r";
56    buttonLabels[InputManager::SETTINGS] = "start";
57    buttonLabels[InputManager::MENU] = "select";
58}
59
60void MessageBox::setButton(InputManager::Button button, const string &label) {
61    buttons[button] = label;
62}
63
64int MessageBox::exec() {
65    OutputSurface& s = *gmenu2x.s;
66    OffscreenSurface bg(s);
67    //Darken background
68    bg.box(0, 0, gmenu2x.resX, gmenu2x.resY, 0,0,0,200);
69
70    SDL_Rect box;
71    int textHeight = gmenu2x.font->getTextHeight(text);
72    box.h = textHeight + 2 * TEXT_PADDING;
73    box.w = gmenu2x.font->getTextWidth(text) + 2 * TEXT_PADDING;
74    if (gmenu2x.sc[icon]) {
75        box.h = max(box.h, (Uint16) (ICON_DIMENSION + 2 * ICON_PADDING));
76        box.w += ICON_DIMENSION + ICON_PADDING;
77    }
78    box.x = gmenu2x.halfX - box.w / 2;
79    box.y = gmenu2x.halfY - box.h / 2;
80
81    //outer box
82    bg.box(box.x - 2, box.y - 2, box.w + 4, box.h + 4, gmenu2x.skinConfColors[COLOR_MESSAGE_BOX_BG]);
83    //draw inner rectangle
84    bg.rectangle(box, gmenu2x.skinConfColors[COLOR_MESSAGE_BOX_BORDER]);
85    //icon+text
86    if (gmenu2x.sc[icon]) {
87        gmenu2x.sc[icon]->blitCenter(bg, box.x + ICON_PADDING + ICON_DIMENSION / 2, box.y + ICON_PADDING + ICON_DIMENSION / 2);
88    }
89    gmenu2x.font->write(bg, text, box.x + TEXT_PADDING + (gmenu2x.sc[icon] ? ICON_PADDING + ICON_DIMENSION : 0), box.y + (box.h - textHeight) / 2, Font::HAlignLeft, Font::VAlignTop);
90
91    int btnX = gmenu2x.halfX + box.w / 2 - 6;
92    for (uint i = 0; i < BUTTON_TYPE_SIZE; i++) {
93        if (!buttons[i].empty()) {
94            buttonPositions[i].y = box.y+box.h+8;
95            buttonPositions[i].w = btnX;
96
97            btnX = gmenu2x.drawButtonRight(bg, buttonLabels[i], buttons[i], btnX, buttonPositions[i].y);
98
99            buttonPositions[i].x = btnX;
100            buttonPositions[i].w = buttonPositions[i].x-btnX-6;
101        }
102    }
103
104    bg.convertToDisplayFormat();
105    bg.blit(s, 0, 0);
106    s.flip();
107
108    int result = -1;
109    while (result < 0) {
110        InputManager::Button button;
111        if (gmenu2x.input.pollButton(&button)
112                && !buttons[button].empty()) {
113            result = button;
114        }
115
116        usleep(LOOP_DELAY);
117    }
118
119    return result;
120}
121

Archive Download this file



interactive