1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-07-04 21:05:27 +03:00
gmenu2x/src/messagebox.cpp
Maarten ter Huurne 0810a26589 Removed unnecessary includes of SDL_gfx header
Only surface.cpp actually uses SDL_gfx functions.
2014-07-19 00:43:05 +02:00

111 lines
4.0 KiB
C++

/***************************************************************************
* Copyright (C) 2006 by Massimiliano Torromeo *
* massimiliano.torromeo@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "messagebox.h"
#include "gmenu2x.h"
#include "surface.h"
#include <unistd.h>
using namespace std;
MessageBox::MessageBox(GMenu2X *gmenu2x, const string &text, const string &icon) {
this->gmenu2x = gmenu2x;
this->text = text;
this->icon = icon;
for (uint i = 0; i < BUTTON_TYPE_SIZE; i++) {
buttons[i] = "";
buttonLabels[i] = "";
buttonPositions[i].h = gmenu2x->font->getLineSpacing();
}
//Default enabled button
buttons[InputManager::ACCEPT] = "OK";
//Default labels
buttonLabels[InputManager::UP] = "up";
buttonLabels[InputManager::DOWN] = "down";
buttonLabels[InputManager::LEFT] = "left";
buttonLabels[InputManager::RIGHT] = "right";
buttonLabels[InputManager::CANCEL] = "cancel";
buttonLabels[InputManager::ACCEPT] = "accept";
buttonLabels[InputManager::ALTLEFT] = "l";
buttonLabels[InputManager::ALTRIGHT] = "r";
buttonLabels[InputManager::SETTINGS] = "start";
buttonLabels[InputManager::MENU] = "select";
}
void MessageBox::setButton(InputManager::Button button, const string &label) {
buttons[button] = label;
}
int MessageBox::exec() {
Surface bg(gmenu2x->s);
//Darken background
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;
//outer box
bg.box(box, 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]);
//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 );
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].w = btnX;
btnX = gmenu2x->drawButtonRight(&bg, buttonLabels[i], buttons[i], btnX, buttonPositions[i].y);
buttonPositions[i].x = btnX;
buttonPositions[i].w = buttonPositions[i].x-btnX-6;
}
}
bg.convertToDisplayFormat();
bg.blit(gmenu2x->s,0,0);
gmenu2x->s->flip();
int result = -1;
while (result < 0) {
InputManager::Button button;
if (gmenu2x->input.pollButton(&button)
&& !buttons[button].empty()) {
result = button;
}
usleep(LOOP_DELAY);
}
return result;
}