ManaPlus
setup_colors.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008 Douglas Boffey <[email protected]>
4  * Copyright (C) 2011-2018 The ManaPlus Developers
5  *
6  * This file is part of The ManaPlus 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 
23 
24 #include "gui/gui.h"
25 #include "gui/userpalette.h"
26 
27 #include "gui/fonts/font.h"
28 
29 #include "gui/widgets/browserbox.h"
32 #include "gui/widgets/label.h"
34 #include "gui/widgets/listbox.h"
35 #include "gui/widgets/scrollarea.h"
36 #include "gui/widgets/slider.h"
37 #include "gui/widgets/textfield.h"
39 
40 #include "utils/delete2.h"
41 #include "utils/gettext.h"
42 #include "utils/stringutils.h"
43 
44 #include <cmath>
45 
46 #include "debug.h"
47 
48 const char *const Setup_Colors::rawmsg =
49  // TRANSLATORS: color selection preview message
50  N_("This is what the color looks like");
51 
52 Setup_Colors::Setup_Colors(const Widget2 *const widget) :
53  SetupTab(widget),
55  mColorBox(CREATEWIDGETR(ListBox, this, userPalette, "")),
56  mScroll(new ScrollArea(this, mColorBox,
57  Opaque_true, "setup_colors_background.xml")),
58  mPreview(new BrowserBox(this, Opaque_true,
59  "browserbox.xml")),
60  mTextPreview(new TextPreview(this, gettext(rawmsg))),
61  mPreviewBox(new ScrollArea(this, mPreview, Opaque_true,
62  "setup_colors_preview_background.xml")),
63  mSelected(-1),
64  // TRANSLATORS: colors tab. label.
65  mGradTypeLabel(new Label(this, _("Type:"))),
66  mGradTypeSlider(new Slider(this, 0.0, 3.0, 1.0)),
67  mGradTypeText(new Label(this)),
68  // TRANSLATORS: colors tab. label.
69  mGradDelayLabel(new Label(this, _("Delay:"))),
70  mGradDelaySlider(new Slider(this, 20.0, 100.0, 1.0)),
71  mGradDelayText(new TextField(this, std::string(), LoseFocusOnTab_true,
72  nullptr, std::string(), false)),
73  // TRANSLATORS: colors tab. label.
74  mRedLabel(new Label(this, _("Red:"))),
75  mRedSlider(new Slider(this, 0.0, 255.0, 1.0)),
76  mRedText(new TextField(this, std::string(), LoseFocusOnTab_true,
77  nullptr, std::string(), false)),
78  // TRANSLATORS: colors tab. label.
79  mGreenLabel(new Label(this, _("Green:"))),
80  mGreenSlider(new Slider(this, 0.0, 255.0, 1.0)),
81  mGreenText(new TextField(this, std::string(), LoseFocusOnTab_true,
82  nullptr, std::string(), false)),
83  // TRANSLATORS: colors tab. label.
84  mBlueLabel(new Label(this, _("Blue:"))),
85  mBlueSlider(new Slider(this, 0.0, 255.0, 1.0)),
86  mBlueText(new TextField(this, std::string(), LoseFocusOnTab_true,
87  nullptr, std::string(), false))
88 {
89  // TRANSLATORS: settings colors tab name
90  setName(_("Colors"));
94 
95  // don't do anything with links
96  mPreview->setLinkHandler(nullptr);
97 
101 
103  mGradTypeSlider->setActionEventId("slider_grad");
106  mGradTypeSlider->setEnabled(false);
107 
108  // TRANSLATORS: color type
109  std::string longText = _("Static");
110 
111  const Font *const font = getFont();
112  // TRANSLATORS: color type
113  if (getFont()->getWidth(_("Pulse")) > font->getWidth(longText))
114  {
115  // TRANSLATORS: color type
116  longText = _("Pulse");
117  }
118  // TRANSLATORS: color type
119  if (getFont()->getWidth(_("Rainbow")) > font->getWidth(longText))
120  {
121  // TRANSLATORS: color type
122  longText = _("Rainbow");
123  }
124  // TRANSLATORS: color type
125  if (getFont()->getWidth(_("Spectrum")) > font->getWidth(longText))
126  {
127  // TRANSLATORS: color type
128  longText = _("Spectrum");
129  }
130 
131  mGradTypeText->setCaption(longText);
132 
134  mGradDelayText->setRange(20, 100);
135  mGradDelayText->setNumeric(true);
136  mGradDelayText->setEnabled(false);
137 
140  mGradDelaySlider->setActionEventId("slider_graddelay");
143 
144  mRedText->setWidth(40);
145  mRedText->setRange(0, 255);
146  mRedText->setNumeric(true);
147  mRedText->setEnabled(false);
148 
149  mRedSlider->setWidth(180);
151  mRedSlider->setActionEventId("slider_red");
153  mRedSlider->setEnabled(false);
154 
155  mGreenText->setWidth(40);
156  mGreenText->setRange(0, 255);
157  mGreenText->setNumeric(true);
158  mGreenText->setEnabled(false);
159 
160  mGreenSlider->setWidth(180);
162  mGreenSlider->setActionEventId("slider_green");
164  mGreenSlider->setEnabled(false);
165 
166  mBlueText->setWidth(40);
167  mBlueText->setRange(0, 255);
168  mBlueText->setNumeric(true);
169  mBlueText->setEnabled(false);
170 
171  mBlueSlider->setWidth(180);
173  mBlueSlider->setActionEventId("slider_blue");
175  mBlueSlider->setEnabled(false);
176 
178 
179  // Do the layout
180  LayoutHelper h(this);
181  ContainerPlacer place = h.getPlacer(0, 0);
182 
183  place(0, 0, mScroll, 6, 6).setPadding(2);
184  place(0, 6, mPreviewBox, 6, 1).setPadding(2);
185  place(0, 7, mGradTypeLabel, 3, 1);
186  place(3, 7, mGradTypeSlider, 1, 1);
187  place(4, 7, mGradTypeText, 2, 1).setPadding(1);
188  place(0, 8, mRedLabel, 3, 1);
189  place(3, 8, mRedSlider, 1, 1);
190  place(5, 8, mRedText, 1, 1).setPadding(1);
191  place(0, 9, mGreenLabel, 3, 1);
192  place(3, 9, mGreenSlider, 1, 1);
193  place(5, 9, mGreenText, 1, 1).setPadding(1);
194  place(0, 10, mBlueLabel, 3, 1);
195  place(3, 10, mBlueSlider, 1, 1);
196  place(5, 10, mBlueText, 1, 1).setPadding(1);
197  place(0, 11, mGradDelayLabel, 3, 1);
198  place(3, 11, mGradDelaySlider, 1, 1);
199  place(5, 11, mGradDelayText, 1, 1).setPadding(1);
200 
202 
203  setDimension(Rect(0, 0, 365, 350));
204 }
205 
207 {
208  if ((mPreviewBox != nullptr) && mPreviewBox->getContent() == mPreview)
210  else
212 }
213 
215 {
216  const std::string &eventId = event.getId();
217  if (eventId == "slider_grad")
218  {
219  updateGradType();
220  updateColor();
221  return;
222  }
223 
224  if (eventId == "slider_graddelay")
225  {
227  std::floor(mGradDelaySlider->getValue())));
228  updateColor();
229  return;
230  }
231  if (eventId == "slider_red")
232  {
233  mRedText->setText(toString(std::floor(mRedSlider->getValue())));
234  updateColor();
235  return;
236  }
237  if (eventId == "slider_green")
238  {
239  mGreenText->setText(toString(std::floor(mGreenSlider->getValue())));
240  updateColor();
241  return;
242  }
243  if (eventId == "slider_blue")
244  {
245  mBlueText->setText(toString(std::floor(mBlueSlider->getValue())));
246  updateColor();
247  return;
248  }
249 }
250 
252 {
253  if (userPalette == nullptr)
254  return;
255 
257  const UserColorIdT type = static_cast<UserColorIdT>(
259  const Color *col = &userPalette->getColor(type, 255U);
260  const GradientTypeT grad = userPalette->getGradientType(type);
261  const int delay = userPalette->getGradientDelay(type);
262  const Visible showControls = fromBool(grad != GradientType::LABEL,
263  Visible);
264  mPreview->setVisible(showControls);
265  mPreviewBox->setVisible(showControls);
266  mTextPreview->setVisible(showControls);
267  mGradTypeLabel->setVisible(showControls);
268  mGradTypeSlider->setVisible(showControls);
269  mGradTypeText->setVisible(showControls);
270  mGradDelayLabel->setVisible(showControls);
271  mGradDelaySlider->setVisible(showControls);
272  mGradDelayText->setVisible(showControls);
273  mRedLabel->setVisible(showControls);
274  mRedSlider->setVisible(showControls);
275  mRedText->setVisible(showControls);
276  mGreenLabel->setVisible(showControls);
277  mGreenSlider->setVisible(showControls);
278  mGreenText->setVisible(showControls);
279  mBlueLabel->setVisible(showControls);
280  mBlueSlider->setVisible(showControls);
281  mBlueText->setVisible(showControls);
282 
283  mPreview->clearRows();
287  mTextPreview->setTextBGColor(nullptr);
289  mTextPreview->setShadow(true);
290  mTextPreview->setOutline(true);
291  mTextPreview->useTextAlpha(false);
292 
293 // probably need combite both switches and add all mssing color ids.
294 
295  PRAGMA45(GCC diagnostic push)
296  PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
297  switch (type)
298  {
307  case UserColorId::NET:
308  mTextPreview->setBGColor(col);
310  mTextPreview->setOutline(false);
311  mTextPreview->setShadow(false);
312  break;
315  if (gui != nullptr)
318  mTextPreview->setOutline(false);
319  mTextPreview->setShadow(false);
320  break;
327  case UserColorId::MISS:
335  mTextPreview->setShadow(false);
336  break;
337  default:
338  break;
339  }
340 
341  switch (type)
342  {
368  case UserColorId::NET:
369  // TRANSLATORS: colors tab. label.
370  mGradDelayLabel->setCaption(_("Alpha:"));
371  mGradDelayText->setRange(0, 255);
372  mGradDelaySlider->setScale(0, 255);
373  break;
374  default:
375  // TRANSLATORS: colors tab. label.
376  mGradDelayLabel->setCaption(_("Delay:"));
377  mGradDelayText->setRange(20, 100);
378  mGradDelaySlider->setScale(20, 100);
379  break;
380  }
381  PRAGMA45(GCC diagnostic pop)
382  if (grad != GradientType::STATIC && grad != GradientType::PULSE)
383  { // If nonstatic color, don't display the current, but the committed
384  // color at the sliders
385  col = &userPalette->getCommittedColor(type);
386  }
387  else if (grad == GradientType::PULSE)
388  {
389  col = &userPalette->getTestColor(type);
390  }
391 
393  setEntry(mRedSlider, mRedText, col->r);
396 
398  updateGradType();
400 }
401 
402 void Setup_Colors::setEntry(Slider *const s, TextField *const t,
403  const int value)
404 {
405  if (s != nullptr)
406  s->setValue(value);
407  if (t != nullptr)
408  t->setText(toString(value));
409 }
410 
412 {
413  if (userPalette != nullptr)
414  userPalette->commit();
415 }
416 
418 {
419  if (userPalette == nullptr)
420  return;
421 
423  const UserColorIdT type = static_cast<UserColorIdT>(
425  const Color *const col = &userPalette->getColor(type, 255U);
427  userPalette->getGradientType(type)));
428  const int delay = userPalette->getGradientDelay(type);
430  setEntry(mRedSlider, mRedText, col->r);
433 }
434 
436 {
437  if (mSelected == -1 || (userPalette == nullptr))
438  return;
439 
441  const UserColorIdT type = static_cast<UserColorIdT>(
443  const GradientTypeT grad = userPalette->getGradientType(type);
444 
446  // TRANSLATORS: color type
447  (grad == GradientType::STATIC) ? _("Static") :
448  // TRANSLATORS: color type
449  (grad == GradientType::PULSE) ? _("Pulse") :
450  // TRANSLATORS: color type
451  (grad == GradientType::RAINBOW) ? _("Rainbow") : _("Spectrum"));
452 
453  const bool enable = (grad == GradientType::STATIC ||
454  grad == GradientType::PULSE);
455  const bool delayEnable = true;
456 
457  mGradDelayText->setEnabled(delayEnable);
458  mGradDelaySlider->setEnabled(delayEnable);
459 
460  mRedText->setEnabled(enable);
461  mRedSlider->setEnabled(enable);
462  mGreenText->setEnabled(enable);
463  mGreenSlider->setEnabled(enable);
464  mBlueText->setEnabled(enable);
465  mBlueSlider->setEnabled(enable);
466 }
467 
469 {
470  if (mSelected == -1 || (userPalette == nullptr))
471  return;
472 
473  const UserColorIdT type = static_cast<UserColorIdT>(
475  const GradientTypeT grad = static_cast<GradientTypeT>(
477  const int delay = CAST_S32(mGradDelaySlider->getValue());
478  userPalette->setGradient(type, grad);
479  userPalette->setGradientDelay(type, delay);
480 
481  if (grad == GradientType::STATIC)
482  {
483  userPalette->setColor(type,
487  }
488  else if (grad == GradientType::PULSE)
489  {
494  255U));
495  }
496 }
Font * getFont() const
Definition: widget.cpp:330
static const char *const rawmsg
Definition: setup_colors.h:56
TextPreview * mTextPreview
Definition: setup_colors.h:61
#define _(s)
Definition: gettext.h:34
const Color & getColor(UserColorIdT type, const unsigned int alpha)
Definition: userpalette.h:159
int getWidth() const
Definition: widget.h:220
void setFont(Font *const font)
Definition: textpreview.h:81
Font * boldFont
Definition: gui.cpp:111
void valueChanged(const SelectionEvent &event)
Slider * mBlueSlider
Definition: setup_colors.h:82
void updateColor() const
int getGradientDelay(const UserColorIdT type) const
Definition: userpalette.h:195
const bool LoseFocusOnTab_true
void setWidth(const int width)
Definition: widget.cpp:132
ScrollArea * mPreviewBox
Definition: setup_colors.h:62
Gui * gui
Definition: gui.cpp:110
Definition: font.h:88
Slider * mGreenSlider
Definition: setup_colors.h:78
void setGradientDelay(const UserColorIdT type, const int delay)
Definition: userpalette.h:108
#define N_(s)
Definition: gettext.h:35
PRAGMA45(GCC diagnostic push) PRAGMA45(GCC diagnostic ignored "-Wunused-result") int TestLauncher
void rollback()
GradientType ::T GradientTypeT
Definition: gradienttype.h:37
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
void setVisible(Visible visible)
Definition: widget.cpp:224
void setScrollPolicy(const ScrollPolicy hPolicy, const ScrollPolicy vPolicy)
void setGradient(const UserColorIdT type, const GradientTypeT grad)
unsigned int g
Definition: color.h:239
Definition: rect.h:72
void setShadow(const bool shadow)
Definition: textpreview.h:89
void addSelectionListener(SelectionListener *const selectionListener)
Definition: listbox.cpp:434
void setDimension(const Rect &dimension)
Definition: widget.cpp:168
ListBox * mColorBox
Definition: setup_colors.h:58
static void setEntry(Slider *const s, TextField *const t, const int value)
void setOutline(const bool outline)
Definition: textpreview.h:97
void setNumeric(const bool numeric)
Definition: textfield.cpp:268
Font * getFont() const
Definition: gui.h:159
void action(const ActionEvent &event)
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
void setOpaque(const Opaque opaque)
Definition: textpreview.h:115
Label * mGradTypeLabel
Definition: setup_colors.h:65
#define delete2(var)
Definition: delete2.h:24
void setHeight(int height)
void setBGColor(const Color *color)
Definition: textpreview.h:73
const Color & getTestColor(const UserColorIdT type) const
Definition: userpalette.h:73
void setScale(const double scaleStart, const double scaleEnd)
Definition: slider.cpp:467
void updateGradType()
UserPalette * userPalette
Definition: userpalette.cpp:33
void setText(const std::string &text)
Definition: textfield.cpp:802
int getValue() const
Definition: textfield.cpp:285
TextField * mGreenText
Definition: setup_colors.h:79
#define new
Definition: debug_new.h:147
Slider * mRedSlider
Definition: setup_colors.h:74
#define CAST_S32
Definition: cast.h:29
unsigned int b
Definition: color.h:244
void setCaption(const std::string &caption)
Definition: label.cpp:261
Label * mGreenLabel
Definition: setup_colors.h:77
Definition: label.h:87
#define fromBool(val, name)
Definition: booldefines.h:48
double getValue() const
Definition: slider.h:203
void setColor(const UserColorIdT type, const int r, const int g, const int b)
int getSelected() const
Definition: listbox.h:167
#define nullptr
Definition: localconsts.h:44
void commit()
Definition: userpalette.h:132
const bool Opaque_false
Definition: opaque.h:29
const Color & getCommittedColor(const UserColorIdT type) const
Definition: userpalette.h:60
TextField * mBlueText
Definition: setup_colors.h:83
Slider * mGradDelaySlider
Definition: setup_colors.h:70
void setTestColor(const UserColorIdT type, const Color &color)
Definition: userpalette.h:83
void setContent(Widget *widget)
Label * mBlueLabel
Definition: setup_colors.h:81
GradientTypeT getGradientType(const UserColorIdT type) const
Definition: userpalette.h:184
ContainerPlacer getPlacer(const int x, const int y)
void setOpaque(Opaque opaque)
void setEnabled(const bool enabled)
Definition: widget.h:351
void useTextAlpha(const bool alpha)
Definition: textpreview.h:56
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
#define A_UNUSED
Definition: localconsts.h:159
Label * mGradDelayLabel
Definition: setup_colors.h:69
void setLinkHandler(LinkHandler *linkHandler)
Definition: browserbox.cpp:165
BrowserBox * mPreview
Definition: setup_colors.h:60
void setName(const std::string &name)
Definition: setuptab.h:67
void setValue(const double value)
Definition: slider.cpp:473
const bool Opaque_true
Definition: opaque.h:29
int getWidth(const std::string &text) const
Definition: font.cpp:333
Definition: color.h:74
TextField * mGradDelayText
Definition: setup_colors.h:71
Setup_Colors(const Widget2 *const widget)
void setOpaque(Opaque opaque)
Definition: browserbox.h:74
int getColorTypeAt(const int i)
Label * mRedLabel
Definition: setup_colors.h:73
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:35
void setTextColor(const Color *color)
Definition: textpreview.h:45
Definition: slider.h:85
void setTextBGColor(const Color *color)
Definition: textpreview.h:65
void setRange(const int min, const int max)
Definition: textfield.h:136
UserColorId ::T UserColorIdT
Definition: usercolorid.h:98
ScrollArea * mScroll
Definition: setup_colors.h:59
void clearRows()
Definition: browserbox.cpp:358
unsigned int r
Definition: color.h:234
TextField * mRedText
Definition: setup_colors.h:75
Slider * mGradTypeSlider
Definition: setup_colors.h:66
Label * mGradTypeText
Definition: setup_colors.h:67
void setHorizontalScrollPolicy(const ScrollPolicy hPolicy)
Widget * getContent()