Root/src/settingsdialog.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 "settingsdialog.h"
25
26using namespace std;
27
28SettingsDialog::SettingsDialog(
29        GMenu2X *gmenu2x_, InputManager &inputMgr_, Touchscreen &ts_,
30        const string &text_, const string &icon)
31    : Dialog(gmenu2x_)
32    , inputMgr(inputMgr_)
33    , ts(ts_)
34    , text(text_)
35{
36    if (icon!="" && gmenu2x->sc[icon] != NULL)
37        this->icon = icon;
38    else
39        this->icon = "icons/generic.png";
40}
41
42SettingsDialog::~SettingsDialog() {
43    for (uint i=0; i<voices.size(); i++)
44        free(voices[i]);
45}
46
47bool SettingsDialog::exec() {
48    //Surface bg (gmenu2x->confStr["wallpaper"],false);
49    Surface bg(gmenu2x->bg);
50
51    bool close = false, ts_pressed = false;
52    uint i, sel = 0, iY, firstElement = 0, action;
53    voices[sel]->adjustInput();
54
55    SDL_Rect clipRect = {0, gmenu2x->skinConfInt["topBarHeight"]+1, gmenu2x->resX-9, gmenu2x->resY-gmenu2x->skinConfInt["topBarHeight"]-25};
56    SDL_Rect touchRect = {2, gmenu2x->skinConfInt["topBarHeight"]+4, gmenu2x->resX-12, clipRect.h};
57    uint rowHeight = gmenu2x->font->getHeight()+1; // gp2x=15+1 / pandora=19+1
58    uint numRows = (gmenu2x->resY-gmenu2x->skinConfInt["topBarHeight"]-20)/rowHeight;
59
60    while (!close) {
61        action = SD_NO_ACTION;
62        if (gmenu2x->f200) ts.poll();
63
64        bg.blit(gmenu2x->s,0,0);
65
66        gmenu2x->drawTopBar(gmenu2x->s);
67        //link icon
68        drawTitleIcon(icon);
69        writeTitle(text);
70
71        gmenu2x->drawBottomBar(gmenu2x->s);
72
73        if (sel>firstElement+numRows-1) firstElement=sel-numRows+1;
74        if (sel<firstElement) firstElement=sel;
75
76        //selection
77        iY = sel-firstElement;
78        iY = gmenu2x->skinConfInt["topBarHeight"]+2+(iY*rowHeight);
79        gmenu2x->s->setClipRect(clipRect);
80        if (sel<voices.size())
81            gmenu2x->s->box(1, iY, 148, rowHeight-2, gmenu2x->skinConfColors[COLOR_SELECTION_BG]);
82        gmenu2x->s->clearClipRect();
83
84        //selected option
85        voices[sel]->drawSelected(iY);
86
87        gmenu2x->s->setClipRect(clipRect);
88        if (ts_pressed && !ts.pressed()) ts_pressed = false;
89        if (gmenu2x->f200 && ts.pressed() && !ts.inRect(touchRect)) ts_pressed = false;
90        for (i=firstElement; i<voices.size() && i<firstElement+numRows; i++) {
91            iY = i-firstElement;
92            voices[i]->draw(iY*rowHeight+gmenu2x->skinConfInt["topBarHeight"]+2);
93            if (gmenu2x->f200 && ts.pressed() && ts.inRect(touchRect.x, touchRect.y+(iY*rowHeight), touchRect.w, rowHeight)) {
94                ts_pressed = true;
95                sel = i;
96            }
97        }
98        gmenu2x->s->clearClipRect();
99
100        gmenu2x->drawScrollBar(numRows,voices.size(),firstElement,clipRect.y+1,clipRect.h);
101
102        //description
103        writeSubTitle(voices[sel]->getDescription());
104
105        gmenu2x->s->flip();
106        voices[sel]->handleTS();
107
108        bevent_t event;
109        do {
110            inputMgr.waitForEvent(&event);
111        } while (event.state != PRESSED);
112
113        switch (event.button) {
114            case SETTINGS:
115                close = true;
116                break;
117            case UP:
118                if (sel==0)
119                    sel = voices.size()-1;
120                else
121                    sel -= 1;
122                gmenu2x->setInputSpeed();
123                voices[sel]->adjustInput();
124                break;
125            case DOWN:
126                sel += 1;
127                if (sel>=voices.size()) sel = 0;
128                gmenu2x->setInputSpeed();
129                voices[sel]->adjustInput();
130                break;
131            default:
132                voices[sel]->manageInput(&event);
133                break;
134        }
135
136        /*
137        inputMgr.update();
138        if ( inputMgr[ACTION_START] ) action = SD_ACTION_CLOSE;
139        if ( inputMgr[ACTION_UP ] ) action = SD_ACTION_UP;
140        if ( inputMgr[ACTION_DOWN ] ) action = SD_ACTION_DOWN;
141
142        switch (action) {
143            case SD_ACTION_CLOSE: close = true; break;
144            case SD_ACTION_UP: {
145                if (sel==0)
146                    sel = voices.size()-1;
147                else
148                    sel -= 1;
149                gmenu2x->setInputSpeed();
150                voices[sel]->adjustInput();
151            } break;
152            case SD_ACTION_DOWN: {
153                sel += 1;
154                if (sel>=voices.size()) sel = 0;
155                gmenu2x->setInputSpeed();
156                voices[sel]->adjustInput();
157            } break;
158        }
159        */
160    }
161
162    gmenu2x->setInputSpeed();
163    return true;
164}
165
166void SettingsDialog::addSetting(MenuSetting* set) {
167    voices.push_back(set);
168}
169
170bool SettingsDialog::edited() {
171    for (uint i=0; i<voices.size(); i++)
172        if (voices[i]->edited()) return true;
173    return false;
174}
175

Archive Download this file



interactive