Root/src/menusettingrgba.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 "menusettingrgba.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::string;
31using std::stringstream;
32using std::unique_ptr;
33
34constexpr unsigned int COMPONENT_WIDTH = 28;
35
36MenuSettingRGBA::MenuSettingRGBA(
37        GMenu2X& gmenu2x,
38        const string &name, const string &description, RGBAColor *value)
39    : MenuSetting(gmenu2x, name, description)
40{
41    edit = false;
42
43    selPart = 0;
44    _value = value;
45    originalValue = *value;
46    this->setR(this->value().r);
47    this->setG(this->value().g);
48    this->setB(this->value().b);
49    this->setA(this->value().a);
50
51    updateButtonBox();
52}
53
54void MenuSettingRGBA::draw(int valueX, int y, int h) {
55    Surface& s = *gmenu2x.s;
56
57    MenuSetting::draw(valueX, y, h);
58    s.rectangle(valueX, y + 1, h - 2, h - 2, 0,0,0,255);
59    s.rectangle(valueX + 1, y + 2, h - 4, h - 4, 255,255,255,255);
60    s.box(valueX + 2, y + 3, h - 6, h - 6, value());
61    auto& font = gmenu2x.font;
62    font->write(s, strR, valueX + h + COMPONENT_WIDTH - 2, y, Font::HAlignRight, Font::VAlignTop);
63    font->write(s, strG, valueX + h + COMPONENT_WIDTH * 2 - 2, y, Font::HAlignRight, Font::VAlignTop);
64    font->write(s, strB, valueX + h + COMPONENT_WIDTH * 3 - 2, y, Font::HAlignRight, Font::VAlignTop);
65    font->write(s, strA, valueX + h + COMPONENT_WIDTH * 4 - 2, y, Font::HAlignRight, Font::VAlignTop);
66}
67
68bool MenuSettingRGBA::handleButtonPress(InputManager::Button button)
69 {
70    if (edit) {
71        switch (button) {
72            case InputManager::LEFT:
73                dec();
74                break;
75            case InputManager::RIGHT:
76                inc();
77                break;
78            case InputManager::ALTLEFT:
79                update_value(-10);
80                break;
81            case InputManager::ALTRIGHT:
82                update_value(10);
83                break;
84            case InputManager::ACCEPT:
85            case InputManager::UP:
86            case InputManager::DOWN:
87                edit = false;
88                updateButtonBox();
89                break;
90            default:
91                return false;
92        }
93    } else {
94        switch (button) {
95            case InputManager::LEFT:
96                leftComponent();
97                break;
98            case InputManager::RIGHT:
99                rightComponent();
100                break;
101            case InputManager::ACCEPT:
102                edit = true;
103                updateButtonBox();
104                break;
105            default:
106                return false;
107        }
108    }
109    return true;
110}
111
112void MenuSettingRGBA::update_value(int value)
113{
114    setSelPart(constrain(getSelPart() + value, 0, 255));
115}
116
117void MenuSettingRGBA::dec()
118{
119    update_value(-1);
120}
121
122void MenuSettingRGBA::inc()
123{
124    update_value(+1);
125}
126
127void MenuSettingRGBA::leftComponent()
128{
129    selPart = constrain(selPart-1,0,3);
130}
131
132void MenuSettingRGBA::rightComponent()
133{
134    selPart = constrain(selPart+1,0,3);
135}
136
137void MenuSettingRGBA::setR(unsigned short r)
138{
139    _value->r = r;
140    stringstream ss;
141    ss << r;
142    ss >> strR;
143}
144
145void MenuSettingRGBA::setG(unsigned short g)
146{
147    _value->g = g;
148    stringstream ss;
149    ss << g;
150    ss >> strG;
151}
152
153void MenuSettingRGBA::setB(unsigned short b)
154{
155    _value->b = b;
156    stringstream ss;
157    ss << b;
158    ss >> strB;
159}
160
161void MenuSettingRGBA::setA(unsigned short a)
162{
163    _value->a = a;
164    stringstream ss;
165    ss << a;
166    ss >> strA;
167}
168
169void MenuSettingRGBA::setSelPart(unsigned short value)
170{
171    switch (selPart) {
172        default: case 0: setR(value); break;
173        case 1: setG(value); break;
174        case 2: setB(value); break;
175        case 3: setA(value); break;
176    }
177}
178
179RGBAColor MenuSettingRGBA::value()
180{
181    return *_value;
182}
183
184unsigned short MenuSettingRGBA::getSelPart()
185{
186    switch (selPart) {
187        default: case 0: return value().r;
188        case 1: return value().g;
189        case 2: return value().b;
190        case 3: return value().a;
191    }
192}
193
194void MenuSettingRGBA::drawSelected(int valueX, int y, int h)
195{
196    int x = valueX + selPart * COMPONENT_WIDTH;
197    RGBAColor color;
198    switch (selPart) {
199        default: /* fallthrough */
200        case 0: color = RGBAColor(255, 0, 0, 255); break;
201        case 1: color = RGBAColor( 0, 255, 0, 255); break;
202        case 2: color = RGBAColor( 0, 0, 255, 255); break;
203        case 3: color = RGBAColor(128, 128, 128, 255); break;
204    }
205    gmenu2x.s->box( x + h, y, COMPONENT_WIDTH, h, color );
206
207    MenuSetting::drawSelected(valueX, y, h);
208}
209
210bool MenuSettingRGBA::edited()
211{
212    return originalValue.r != value().r || originalValue.g != value().g || originalValue.b != value().b || originalValue.a != value().a;
213}
214
215void MenuSettingRGBA::updateButtonBox()
216{
217    buttonBox.clear();
218    if (edit) {
219        buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, "skin:imgs/buttons/l.png")));
220        buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, "skin:imgs/buttons/left.png", gmenu2x.tr["Decrease"])));
221        buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, "skin:imgs/buttons/r.png")));
222        buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, "skin:imgs/buttons/right.png", gmenu2x.tr["Increase"])));
223        buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, "skin:imgs/buttons/accept.png", gmenu2x.tr["Confirm"])));
224    } else {
225        buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, "skin:imgs/buttons/left.png")));
226        buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, "skin:imgs/buttons/right.png", gmenu2x.tr["Change color component"])));
227        buttonBox.add(unique_ptr<IconButton>(new IconButton(gmenu2x, "skin:imgs/buttons/accept.png", gmenu2x.tr["Edit"])));
228    }
229}
230

Archive Download this file



interactive