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 <SDL.h>
22#include <SDL_gfxPrimitives.h>
23
24#include "messagebox.h"
25
26using namespace std;
27
28MessageBox::MessageBox(GMenu2X *gmenu2x, const string &text, const string &icon) {
29    this->gmenu2x = gmenu2x;
30    this->text = text;
31    this->icon = icon;
32
33    buttons.resize(19);
34    buttonLabels.resize(19);
35    buttonPositions.resize(19);
36    for (uint x=0; x<buttons.size(); x++) {
37        buttons[x] = "";
38        buttonLabels[x] = "";
39        buttonPositions[x].h = gmenu2x->font->getHeight();
40    }
41
42    //Default enabled button
43    buttons[ACCEPT] = "OK";
44
45    //Default labels
46    buttonLabels[UP] = "up";
47    buttonLabels[DOWN] = "down";
48    buttonLabels[LEFT] = "left";
49    buttonLabels[RIGHT] = "right";
50    buttonLabels[CANCEL] = "a";
51    buttonLabels[ACCEPT] = "b";
52    buttonLabels[CLEAR] = "x";
53    buttonLabels[MANUAL] = "y";
54    buttonLabels[ALTLEFT] = "l";
55    buttonLabels[ALTRIGHT] = "r";
56    buttonLabels[SETTINGS] = "start";
57    buttonLabels[MENU] = "select";
58    buttonLabels[VOLUP] = "vol+";
59    buttonLabels[VOLDOWN] = "vol-";
60}
61
62void MessageBox::setButton(int action, const string &btn) {
63    buttons[action] = btn;
64}
65
66int MessageBox::exec() {
67    int result = -1;
68
69    Surface bg(gmenu2x->s);
70    //Darken background
71    bg.box(0, 0, gmenu2x->resX, gmenu2x->resY, 0,0,0,200);
72
73    SDL_Rect box;
74    box.h = gmenu2x->font->getHeight()*3 +4;
75    box.w = gmenu2x->font->getTextWidth(text) + 24 + (gmenu2x->sc[icon] != NULL ? 37 : 0);
76    box.x = gmenu2x->halfX - box.w/2 -2;
77    box.y = gmenu2x->halfY - box.h/2 -2;
78
79    //outer box
80    bg.box(box, gmenu2x->skinConfColors[COLOR_MESSAGE_BOX_BG]);
81    //draw inner rectangle
82    bg.rectangle(box.x+2, box.y+2, box.w-4, box.h-gmenu2x->font->getHeight(),
83    gmenu2x->skinConfColors[COLOR_MESSAGE_BOX_BORDER]);
84    //icon+text
85    if (gmenu2x->sc[icon] != NULL)
86        gmenu2x->sc[icon]->blitCenter( &bg, box.x+25, box.y+gmenu2x->font->getHeight()+3 );
87    bg.write( gmenu2x->font, text, box.x+(gmenu2x->sc[icon] != NULL ? 47 : 10), box.y+gmenu2x->font->getHeight()+3, ASFont::HAlignLeft, ASFont::VAlignMiddle );
88
89    int btnX = gmenu2x->halfX+box.w/2-6;
90    for (uint i=0; i<buttons.size(); i++) {
91        if (buttons[i] != "") {
92            buttonPositions[i].y = box.y+box.h-4;
93            buttonPositions[i].w = btnX;
94
95            btnX = gmenu2x->drawButtonRight(&bg, buttonLabels[i], buttons[i], btnX, buttonPositions[i].y);
96
97            buttonPositions[i].x = btnX;
98            buttonPositions[i].w = buttonPositions[i].x-btnX-6;
99        }
100    }
101
102    bg.blit(gmenu2x->s,0,0);
103    gmenu2x->s->flip();
104
105    bevent_t event;
106    while (result<0) {
107        //touchscreen
108        if (gmenu2x->f200) {
109            if (gmenu2x->ts.poll()) {
110                for (uint i=0; i<buttons.size(); i++)
111                    if (buttons[i]!="" && gmenu2x->ts.inRect(buttonPositions[i])) {
112                        result = i;
113                        i = buttons.size();
114                    }
115            }
116        }
117/*
118        gmenu2x->input.update();
119        for (uint i=0; i<buttons.size(); i++)
120            if (buttons[i]!="" && gmenu2x->input[i]) result = i;
121
122        */
123
124        if (gmenu2x->input.pollEvent(&event) && (event.state == PRESSED) && (buttons[event.button] != "")) result = event.button;
125
126        usleep(LOOP_DELAY);
127    }
128
129    return result;
130}
131

Archive Download this file



interactive