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 <SDL_gfxPrimitives.h>
26
27#include <unistd.h>
28
29using namespace std;
30
31MessageBox::MessageBox(GMenu2X *gmenu2x, const string &text, const string &icon) {
32    this->gmenu2x = gmenu2x;
33    this->text = text;
34    this->icon = icon;
35
36    for (uint i = 0; i < BUTTON_TYPE_SIZE; i++) {
37        buttons[i] = "";
38        buttonLabels[i] = "";
39        buttonPositions[i].h = gmenu2x->font->getHeight();
40    }
41
42    //Default enabled button
43    buttons[InputManager::ACCEPT] = "OK";
44
45    //Default labels
46    buttonLabels[InputManager::UP] = "up";
47    buttonLabels[InputManager::DOWN] = "down";
48    buttonLabels[InputManager::LEFT] = "left";
49    buttonLabels[InputManager::RIGHT] = "right";
50    buttonLabels[InputManager::CANCEL] = "cancel";
51    buttonLabels[InputManager::ACCEPT] = "accept";
52    buttonLabels[InputManager::ALTLEFT] = "l";
53    buttonLabels[InputManager::ALTRIGHT] = "r";
54    buttonLabels[InputManager::SETTINGS] = "start";
55    buttonLabels[InputManager::MENU] = "select";
56}
57
58void MessageBox::setButton(InputManager::Button button, const string &label) {
59    buttons[button] = label;
60}
61
62int MessageBox::exec() {
63    Surface bg(gmenu2x->s);
64    //Darken background
65    bg.box(0, 0, gmenu2x->resX, gmenu2x->resY, 0,0,0,200);
66
67    SDL_Rect box;
68    box.h = gmenu2x->font->getHeight()*3 +4;
69    box.w = gmenu2x->font->getTextWidth(text) + 24 + (gmenu2x->sc[icon] != NULL ? 37 : 0);
70    box.x = gmenu2x->halfX - box.w/2 -2;
71    box.y = gmenu2x->halfY - box.h/2 -2;
72
73    //outer box
74    bg.box(box, gmenu2x->skinConfColors[COLOR_MESSAGE_BOX_BG]);
75    //draw inner rectangle
76    bg.rectangle(box.x+2, box.y+2, box.w-4, box.h-gmenu2x->font->getHeight(),
77    gmenu2x->skinConfColors[COLOR_MESSAGE_BOX_BORDER]);
78    //icon+text
79    if (gmenu2x->sc[icon] != NULL)
80        gmenu2x->sc[icon]->blitCenter( &bg, box.x+25, box.y+gmenu2x->font->getHeight()+3 );
81    bg.write( gmenu2x->font, text, box.x+(gmenu2x->sc[icon] != NULL ? 47 : 10), box.y+gmenu2x->font->getHeight()+3, Font::HAlignLeft, Font::VAlignMiddle );
82
83    int btnX = gmenu2x->halfX+box.w/2-6;
84    for (uint i = 0; i < BUTTON_TYPE_SIZE; i++) {
85        if (!buttons[i].empty()) {
86            buttonPositions[i].y = box.y+box.h-4;
87            buttonPositions[i].w = btnX;
88
89            btnX = gmenu2x->drawButtonRight(&bg, buttonLabels[i], buttons[i], btnX, buttonPositions[i].y);
90
91            buttonPositions[i].x = btnX;
92            buttonPositions[i].w = buttonPositions[i].x-btnX-6;
93        }
94    }
95
96    bg.convertToDisplayFormat();
97    bg.blit(gmenu2x->s,0,0);
98    gmenu2x->s->flip();
99
100    int result = -1;
101    while (result < 0) {
102        InputManager::Button button;
103        if (gmenu2x->input.pollButton(&button)
104                && !buttons[button].empty()) {
105            result = button;
106        }
107
108        usleep(LOOP_DELAY);
109    }
110
111    return result;
112}
113

Archive Download this file



interactive