Root/src/menusettingbool.cpp

Source at commit 7b10f9448bb9965e94f9a37a59f0999e9c51e68f created 7 years 5 months ago.
By Maarten ter Huurne, Suppress Clang analyzer warnings about dead assignments
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 "menusettingbool.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::unique_ptr;
33
34MenuSettingBool::MenuSettingBool(
35        GMenu2X& gmenu2x,
36        const string &name, const string &description, int *value)
37    : MenuSetting(gmenu2x, name, description)
38{
39    _ivalue = value;
40    _value = NULL;
41    originalValue = *value != 0;
42    setValue(this->value());
43    initButton();
44}
45
46MenuSettingBool::MenuSettingBool(
47        GMenu2X& gmenu2x,
48        const string &name, const string &description, bool *value)
49    : MenuSetting(gmenu2x, name, description)
50{
51    _value = value;
52    _ivalue = NULL;
53    originalValue = *value;
54    setValue(this->value());
55    initButton();
56}
57
58void MenuSettingBool::initButton()
59{
60    buttonBox.add(unique_ptr<IconButton>(new IconButton(
61            gmenu2x, "skin:imgs/buttons/accept.png",
62            gmenu2x.tr["Switch"],
63            bind(&MenuSettingBool::toggle, this))));
64}
65
66void MenuSettingBool::draw(int valueX, int y, int h)
67{
68    Surface& s = *gmenu2x.s;
69    MenuSetting::draw(valueX, y, h);
70    gmenu2x.font->write(s, strvalue, valueX, y, Font::HAlignLeft, Font::VAlignTop);
71}
72
73bool MenuSettingBool::handleButtonPress(InputManager::Button button)
74{
75    switch (button) {
76        case InputManager::ACCEPT:
77        case InputManager::LEFT:
78        case InputManager::RIGHT:
79            toggle();
80            break;
81        default:
82            return false;
83    }
84    return true;
85}
86
87void MenuSettingBool::toggle()
88{
89    setValue(!value());
90}
91
92void MenuSettingBool::setValue(int value)
93{
94    setValue(value != 0);
95}
96
97void MenuSettingBool::setValue(bool value)
98{
99    if (_value == NULL)
100        *_ivalue = value;
101    else
102        *_value = value;
103    strvalue = value ? "ON" : "OFF";
104}
105
106bool MenuSettingBool::value()
107{
108    if (_value == NULL)
109        return *_ivalue != 0;
110    else
111        return *_value;
112}
113
114bool MenuSettingBool::edited()
115{
116    return originalValue != value();
117}
118

Archive Download this file



interactive