Root/src/settingsdialog.cpp

Source at commit 00d3c3b57008bddf8770c51271db9d766acaf854 created 7 years 5 months ago.
By Maarten ter Huurne, Gave Layer class a protected constructor
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 "settingsdialog.h"
22
23#include "gmenu2x.h"
24#include "menusetting.h"
25
26#include <SDL.h>
27
28using namespace std;
29
30SettingsDialog::SettingsDialog(
31        GMenu2X& gmenu2x, InputManager &inputMgr_,
32        const string &text_, const string &icon)
33    : Dialog(gmenu2x)
34    , inputMgr(inputMgr_)
35    , text(text_)
36{
37    if (!icon.empty() && gmenu2x.sc[icon] != NULL) {
38        this->icon = icon;
39    } else {
40        this->icon = "icons/generic.png";
41    }
42}
43
44bool SettingsDialog::exec() {
45    OffscreenSurface bg(*gmenu2x.bg);
46    bg.convertToDisplayFormat();
47
48    bool close = false;
49    uint i, sel = 0, firstElement = 0;
50
51    const int topBarHeight = gmenu2x.skinConfInt["topBarHeight"];
52    uint rowHeight = gmenu2x.font->getLineSpacing() + 1; // gp2x=15+1 / pandora=19+1
53    uint numRows = (gmenu2x.resY - topBarHeight - 20) / rowHeight;
54
55    uint maxNameWidth = 0;
56    for (auto it = settings.begin(); it != settings.end(); it++) {
57        maxNameWidth = max(maxNameWidth, (uint) gmenu2x.font->getTextWidth((*it)->getName()));
58    }
59
60    while (!close) {
61        OutputSurface& s = *gmenu2x.s;
62
63        bg.blit(s, 0, 0);
64
65        gmenu2x.drawTopBar(s);
66        //link icon
67        drawTitleIcon(s, icon);
68        writeTitle(s, text);
69
70        gmenu2x.drawBottomBar(s);
71
72        if (sel>firstElement+numRows-1) firstElement=sel-numRows+1;
73        if (sel<firstElement) firstElement=sel;
74
75        //selection
76        uint iY = topBarHeight + 2 + (sel - firstElement) * rowHeight;
77
78        //selected option
79        settings[sel]->drawSelected(maxNameWidth + 15, iY, rowHeight);
80
81        for (i=firstElement; i<settings.size() && i<firstElement+numRows; i++) {
82            iY = i-firstElement;
83            settings[i]->draw(maxNameWidth + 15, iY * rowHeight + topBarHeight + 2, rowHeight);
84        }
85
86        gmenu2x.drawScrollBar(numRows, settings.size(), firstElement);
87
88        //description
89        writeSubTitle(s, settings[sel]->getDescription());
90
91        s.flip();
92
93        InputManager::Button button = inputMgr.waitForPressedButton();
94        if (!settings[sel]->handleButtonPress(button)) {
95            switch (button) {
96                case InputManager::SETTINGS:
97                    close = true;
98                    break;
99                case InputManager::UP:
100                    if (sel == 0) {
101                        sel = settings.size() - 1;
102                    } else {
103                        sel -= 1;
104                    }
105                    break;
106                case InputManager::DOWN:
107                    sel += 1;
108                    if (sel>=settings.size()) sel = 0;
109                default:
110                    break;
111            }
112        }
113    }
114
115    return any_of(settings.begin(), settings.end(),
116            [](unique_ptr<MenuSetting>& setting){ return setting->edited(); });
117}
118

Archive Download this file



interactive