Mana
Loading...
Searching...
No Matches
setup_colors.cpp
Go to the documentation of this file.
1/*
2 * Configurable text colors
3 * Copyright (C) 2008 Douglas Boffey <dougaboffey@netscape.net>
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "gui/setup_colors.h"
23
24#include "gui/gui.h"
25
27#include "gui/widgets/label.h"
28#include "gui/widgets/layout.h"
29#include "gui/widgets/listbox.h"
31#include "gui/widgets/slider.h"
34
36
37#include "utils/gettext.h"
38#include "utils/stringutils.h"
39
40#include <string>
41#include <cmath>
42
43const std::string Setup_Colors::rawmsg = _("This is what the color looks like");
44
46 mSelected(-1)
47{
48 setName(_("Colors"));
49
51 mColorBox->addSelectionListener(this);
52
54 mScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
55
57
59
61 mPreviewBox->setHeight(20);
62 mPreviewBox->setScrollPolicy(gcn::ScrollArea::SHOW_NEVER,
63 gcn::ScrollArea::SHOW_NEVER);
64
65 mGradTypeLabel = new Label(_("Type:"));
66
67 mGradTypeSlider = new Slider(0, 3);
68 mGradTypeSlider->setWidth(180);
69 mGradTypeSlider->setActionEventId("slider_grad");
70 mGradTypeSlider->setValue(0);
71 mGradTypeSlider->addActionListener(this);
72 mGradTypeSlider->setEnabled(false);
73
74 mGradTypeText = new Label;
75
76 // Initialize with widest label for layout purposes
77 const char *longText = _("Static");
78 int longWidth = getFont()->getWidth(longText);
79
80 auto maybeLonger = [&] (const char *text) {
81 const int width = getFont()->getWidth(text);
82 if (width > longWidth)
83 {
84 longText = text;
85 longWidth = width;
86 }
87 };
88 maybeLonger(_("Pulse"));
89 maybeLonger(_("Rainbow"));
90 maybeLonger(_("Spectrum"));
91
92 mGradTypeText->setCaption(longText);
93
94 mGradDelayLabel = new Label(_("Delay:"));
95
97 mGradDelayText->setWidth(40);
98 mGradDelayText->setRange(20, 100);
100 mGradDelayText->setEnabled(false);
101
102 mGradDelaySlider = new Slider(20, 100);
103 mGradDelaySlider->setWidth(180);
105 mGradDelaySlider->setActionEventId("slider_graddelay");
106 mGradDelaySlider->addActionListener(this);
107 mGradDelaySlider->setEnabled(false);
108
109 mRedLabel = new Label(_("Red:"));
110
111 mRedText = new TextField;
112 mRedText->setWidth(40);
113 mRedText->setRange(0, 255);
114 mRedText->setNumeric(true);
115 mRedText->setEnabled(false);
116
117 mRedSlider = new Slider(0, 255);
118 mRedSlider->setWidth(180);
119 mRedSlider->setValue(mRedText->getValue());
120 mRedSlider->setActionEventId("slider_red");
121 mRedSlider->addActionListener(this);
122 mRedSlider->setEnabled(false);
123
124 mGreenLabel = new Label(_("Green:"));
125
126 mGreenText = new TextField;
127 mGreenText->setWidth(40);
128 mGreenText->setRange(0, 255);
129 mGreenText->setNumeric(true);
130 mGreenText->setEnabled(false);
131
132 mGreenSlider = new Slider(0, 255);
133 mGreenSlider->setWidth(180);
134 mGreenSlider->setValue(mGreenText->getValue());
135 mGreenSlider->setActionEventId("slider_green");
136 mGreenSlider->addActionListener(this);
137 mGreenSlider->setEnabled(false);
138
139 mBlueLabel = new Label(_("Blue:"));
140
141 mBlueText = new TextField;
142 mBlueText->setWidth(40);
143 mBlueText->setRange(0, 255);
144 mBlueText->setNumeric(true);
145 mBlueText->setEnabled(false);
146
147 mBlueSlider = new Slider(0, 255);
148 mBlueSlider->setWidth(180);
149 mBlueSlider->setValue(mBlueText->getValue());
150 mBlueSlider->setActionEventId("slider_blue");
151 mBlueSlider->addActionListener(this);
152 mBlueSlider->setEnabled(false);
153
154 setOpaque(false);
155
156 // Do the layout
157 place(0, 0, mScroll, 6, 6).setPadding(2);
158 place(0, 6, mPreviewBox, 6).setPadding(2);
159 place(0, 7, mGradTypeLabel, 3);
161 place(4, 7, mGradTypeText, 2).setPadding(1);
162 place(0, 8, mRedLabel, 3);
164 place(5, 8, mRedText).setPadding(1);
165 place(0, 9, mGreenLabel, 3);
167 place(5, 9, mGreenText).setPadding(1);
168 place(0, 10, mBlueLabel, 3);
170 place(5, 10, mBlueText).setPadding(1);
171 place(0, 11, mGradDelayLabel, 3);
174
175 mGradTypeText->setCaption(std::string());
176}
177
179{
180 if (mPreviewBox->getContent() == mPreview)
181 delete mTextPreview;
182 else
183 delete mPreview;
184}
185
186void Setup_Colors::action(const gcn::ActionEvent &event)
187{
188 if (event.getId() == "slider_grad")
189 {
190 updateColor();
192 return;
193 }
194
195 if (event.getId() == "slider_graddelay")
196 {
197 mGradDelayText->setText(toString(std::floor(mGradDelaySlider->getValue())));
198 updateColor();
199 return;
200 }
201
202 if (event.getId() == "slider_red")
203 {
204 mRedText->setText(toString(std::floor(mRedSlider->getValue())));
205 updateColor();
206 return;
207 }
208
209 if (event.getId() == "slider_green")
210 {
211 mGreenText->setText(toString(std::floor(mGreenSlider->getValue())));
212 updateColor();
213 return;
214 }
215
216 if (event.getId() == "slider_blue")
217 {
218 mBlueText->setText(toString(std::floor(mBlueSlider->getValue())));
219 updateColor();
220 return;
221 }
222}
223
224void Setup_Colors::valueChanged(const gcn::SelectionEvent &)
225{
226 mSelected = mColorBox->getSelected();
227 const int type = userPalette->getColorTypeAt(mSelected);
228 const gcn::Color *col = &userPalette->getColor(type);
230 const int delay = userPalette->getGradientDelay(type);
231
233 mPreviewBox->setContent(mTextPreview);
237
238 switch (type)
239 {
247 mTextPreview->setShadow(false);
248 break;
249 default:
250 mTextPreview->setShadow(true);
251 break;
252 }
253
254 if (grad != Palette::STATIC && grad != Palette::PULSE)
255 { // If nonstatic color, don't display the current, but the committed
256 // color at the sliders
257 col = &userPalette->getCommittedColor(type);
258 }
259 else if (grad == Palette::PULSE)
260 {
261 col = &userPalette->getTestColor(type);
262 }
263
265 setEntry(mRedSlider, mRedText, col->r);
268
269 mGradTypeSlider->setValue(grad);
271 mGradTypeSlider->setEnabled(true);
272}
273
274void Setup_Colors::setEntry(gcn::Slider *s, TextField *t, int value)
275{
276 s->setValue(value);
277 char buffer[100];
278 snprintf(buffer, 100, "%d", value);
279 t->setText(buffer);
280}
281
283{
285}
286
288{
291 const gcn::Color *col = &userPalette->getColor(type);
293 const int delay = userPalette->getGradientDelay(type);
295 setEntry(mRedSlider, mRedText, col->r);
298}
299
300#if 0
301void Setup_Colors::listen(const TextField *tf)
302{
303 if (tf == mGradDelayText)
304 mGradDelaySlider->setValue(tf->getValue());
305 else if (tf == mRedText)
306 mRedSlider->setValue(tf->getValue());
307 else if (tf == mGreenText)
308 mGreenSlider->setValue(tf->getValue());
309 else if (tf == mBlueText)
310 mBlueSlider->setValue(tf->getValue());
311
312 updateColor();
313}
314#endif
315
317{
318 if (mSelected == -1)
319 return;
320
321 mSelected = mColorBox->getSelected();
324
325 mGradTypeText->setCaption(
326 (grad == Palette::STATIC) ? _("Static") :
327 (grad == Palette::PULSE) ? _("Pulse") :
328 (grad == Palette::RAINBOW) ? _("Rainbow") : _("Spectrum"));
329
330 const bool enable = (grad == Palette::STATIC || grad == Palette::PULSE);
331 const bool delayEnable = (grad != Palette::STATIC);
332
333 mGradDelayText->setEnabled(delayEnable);
334 mGradDelaySlider->setEnabled(delayEnable);
335
336 mRedText->setEnabled(enable);
337 mRedSlider->setEnabled(enable);
338 mGreenText->setEnabled(enable);
339 mGreenSlider->setEnabled(enable);
340 mBlueText->setEnabled(enable);
341 mBlueSlider->setEnabled(enable);
342}
343
345{
346 if (mSelected == -1)
347 return;
348
350 auto grad = static_cast<Palette::GradientType>((int)
351 mGradTypeSlider->getValue());
352 int delay = (int) mGradDelaySlider->getValue();
353 userPalette->setGradient(type, grad);
354 userPalette->setGradientDelay(type, delay);
355
356 if (grad == Palette::STATIC)
357 {
358 userPalette->setColor(type,
359 static_cast<int>(mRedSlider->getValue()),
360 static_cast<int>(mGreenSlider->getValue()),
361 static_cast<int>(mBlueSlider->getValue()));
362 }
363 else if (grad == Palette::PULSE)
364 {
365 userPalette->setTestColor(type, gcn::Color(
366 static_cast<int>(mRedSlider->getValue()),
367 static_cast<int>(mGreenSlider->getValue()),
368 static_cast<int>(mBlueSlider->getValue())));
369 }
370}
A simple browser box able to handle links and forward events to the parent conteiner.
Definition browserbox.h:74
void clearRows()
Remove all rows.
@ AUTO_WRAP
Maybe it needs a fix or to be redone.
Definition browserbox.h:79
LayoutCell & place(int x, int y, gcn::Widget *wg, int w=1, int h=1)
Adds a widget to the container and sets it at given cell.
Definition container.cpp:46
Label widget.
Definition label.h:34
LayoutCell & setPadding(int p)
Sets the padding around the cell content.
Definition layout.h:179
LayoutCell & setVAlign(Alignment a)
Sets the vertical alignment of the cell content.
Definition layout.h:191
A list box, meant to be used inside a scroll area.
Definition listbox.h:36
GradientType getGradientType(int type) const
Gets the GradientType associated with the specified type.
Definition palette.h:94
const gcn::Color & getColor(int type) const
Gets the color associated with the type.
Definition palette.h:72
GradientType
Colors can be static or can alter over time.
Definition palette.h:53
@ STATIC
Definition palette.h:54
@ PULSE
Definition palette.h:55
@ RAINBOW
Definition palette.h:57
int getGradientDelay(int type) const
Gets the gradient delay for the specified type.
Definition palette.h:105
A scroll area.
Definition scrollarea.h:38
void setName(const std::string &name)
Sets the name displayed on the tab.
Definition setuptab.h:54
static const std::string rawmsg
void valueChanged(const gcn::SelectionEvent &event) override
gcn::ListBox * mColorBox
BrowserBox * mPreview
gcn::Label * mRedLabel
TextField * mGradDelayText
gcn::Slider * mGradTypeSlider
gcn::ScrollArea * mPreviewBox
gcn::Slider * mRedSlider
gcn::ScrollArea * mScroll
void setEntry(gcn::Slider *s, TextField *t, int value)
TextField * mBlueText
gcn::Slider * mGreenSlider
void apply() override
Called when the Apply button is pressed in the setup window.
gcn::Label * mGradTypeLabel
gcn::Label * mGreenLabel
TextPreview * mTextPreview
void action(const gcn::ActionEvent &event) override
gcn::Slider * mBlueSlider
gcn::Label * mGradTypeText
gcn::Label * mBlueLabel
gcn::Label * mGradDelayLabel
TextField * mRedText
TextField * mGreenText
gcn::Slider * mGradDelaySlider
void updateGradType()
void cancel() override
Called when the Cancel button is pressed in the setup window.
~Setup_Colors() override
Slider widget.
Definition slider.h:32
A text field.
Definition textfield.h:72
int getValue() const
Return the value for a numeric field.
void setRange(int min, int max)
Set the range on the field if it is numeric.
Definition textfield.h:98
void setNumeric(bool numeric)
Determine whether the field should be numeric or not.
Definition textfield.cpp:83
Preview widget for particle colors, etc.
Definition textpreview.h:32
void setFont(gcn::Font *font)
Sets the font to render the text in.
Definition textpreview.h:51
void setOutline(bool outline)
Sets whether to use an outline while rendering.
Definition textpreview.h:71
void setTextColor(const gcn::Color *color)
Sets the color the text is printed in.
Definition textpreview.h:41
void setShadow(bool shadow)
Sets whether to use a shadow while rendering.
Definition textpreview.h:61
void setColor(int type, int r, int g, int b)
Sets the color for the specified type.
void rollback()
Rollback the colors.
void setGradient(int type, Palette::GradientType grad)
Sets the gradient type for the specified color.
int getColorTypeAt(int i)
Gets the ColorType used by the color for the element at index i in the current color model.
void commit()
Commit the colors.
void setTestColor(int type, const gcn::Color &color)
Sets the test color associated with the specified type.
Definition userpalette.h:92
const gcn::Color & getCommittedColor(int type)
Gets the committed color associated with the specified type.
Definition userpalette.h:69
const gcn::Color & getTestColor(int type)
Gets the test color associated with the specified type.
Definition userpalette.h:81
void setGradientDelay(int type, int delay)
Sets the gradient delay for the specified color.
UserPalette * userPalette
Definition client.cpp:103
#define _(s)
Definition gettext.h:38
gcn::Font * boldFont
Bolded text font.
Definition gui.cpp:54
std::string toString(const T &arg)
Converts the given value to a string using std::stringstream.
Definition stringutils.h:68