Root/src/menusettingint.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 "menusettingint.h"
22
23#include "gmenu2x.h"
24#include "iconbutton.h"
25#include "surface.h"
26#include "utilities.h"
27
28#include <sstream>
29
30using std::bind;
31using std::string;
32using std::stringstream;
33using std::unique_ptr;
34
35MenuSettingInt::MenuSettingInt(
36        GMenu2X& gmenu2x,
37        const string &name, const string &description,
38        int *value, int min, int max, int increment)
39    : MenuSetting(gmenu2x, name, description)
40{
41    _value = value;
42    originalValue = *value;
43    this->min = min;
44    this->max = max;
45    this->increment = increment;
46    setValue(this->value());
47
48    //Delegates
49    IconButton::Action actionInc = bind(&MenuSettingInt::inc, this);
50    IconButton::Action actionDec = bind(&MenuSettingInt::dec, this);
51
52    buttonBox.add(unique_ptr<IconButton>(new IconButton(
53            gmenu2x, "skin:imgs/buttons/l.png",
54            "", actionDec)));
55    buttonBox.add(unique_ptr<IconButton>(new IconButton(
56            gmenu2x, "skin:imgs/buttons/left.png",
57            gmenu2x.tr["Decrease"], actionDec)));
58
59    buttonBox.add(unique_ptr<IconButton>(new IconButton(
60            gmenu2x, "skin:imgs/buttons/r.png",
61            "", actionInc)));
62    buttonBox.add(unique_ptr<IconButton>(new IconButton(
63            gmenu2x, "skin:imgs/buttons/right.png",
64            gmenu2x.tr["Increase"], actionInc)));
65}
66
67void MenuSettingInt::draw(int valueX, int y, int h)
68{
69    Surface& s = *gmenu2x.s;
70    MenuSetting::draw(valueX, y, h);
71    gmenu2x.font->write(s, strvalue, valueX, y, Font::HAlignLeft, Font::VAlignTop);
72}
73
74bool MenuSettingInt::handleButtonPress(InputManager::Button button)
75{
76    switch (button) {
77        case InputManager::LEFT:
78            dec();
79            break;
80        case InputManager::RIGHT:
81            inc();
82            break;
83        case InputManager::ALTLEFT:
84            setValue(value() - 10 * increment);
85            break;
86        case InputManager::ALTRIGHT:
87            setValue(value() + 10 * increment);
88            break;
89        default:
90            return false;
91    }
92    return true;
93}
94
95void MenuSettingInt::inc()
96{
97    setValue(value() + increment);
98}
99
100void MenuSettingInt::dec()
101{
102    setValue(value() - increment);
103}
104
105void MenuSettingInt::setValue(int value)
106{
107    *_value = constrain(value,min,max);
108    stringstream ss;
109    ss << *_value;
110    strvalue = "";
111    ss >> strvalue;
112}
113
114int MenuSettingInt::value()
115{
116    return *_value;
117}
118
119bool MenuSettingInt::edited()
120{
121    return originalValue != value();
122}
123

Archive Download this file



interactive