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-2019 The ManaPlus Developers
5  * Copyright (C) 2019-2021 Andrei Karas
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 #include "gui/gui.h"
26 #include "gui/userpalette.h"
27 
28 #include "gui/fonts/font.h"
29 
30 #include "gui/widgets/browserbox.h"
33 #include "gui/widgets/label.h"
35 #include "gui/widgets/listbox.h"
36 #include "gui/widgets/scrollarea.h"
37 #include "gui/widgets/slider.h"
38 #include "gui/widgets/textfield.h"
40 
41 #include "utils/delete2.h"
42 #include "utils/gettext.h"
43 #include "utils/stringutils.h"
44 
45 #include <cmath>
46 
47 #include "debug.h"
48 
49 const char *const Setup_Colors::rawmsg =
50  // TRANSLATORS: color selection preview message
51  N_("This is what the color looks like");
52 
53 Setup_Colors::Setup_Colors(const Widget2 *const widget) :
54  SetupTab(widget),
56  mColorBox(CREATEWIDGETR(ListBox, this, userPalette, "")),
57  mScroll(new ScrollArea(this, mColorBox,
58  Opaque_true, "setup_colors_background.xml")),
59  mPreview(new BrowserBox(this, Opaque_true,
60  "browserbox.xml")),
61  mTextPreview(new TextPreview(this, gettext(rawmsg))),
62  mPreviewBox(new ScrollArea(this, mPreview, Opaque_true,
63  "setup_colors_preview_background.xml")),
64  mSelected(-1),
65  // TRANSLATORS: colors tab. label.
66  mGradTypeLabel(new Label(this, _("Type:"))),
67  mGradTypeSlider(new Slider(this, 0.0, 3.0, 1.0)),
68  mGradTypeText(new Label(this)),
69  // TRANSLATORS: colors tab. label.
70  mGradDelayLabel(new Label(this, _("Delay:"))),
71  mGradDelaySlider(new Slider(this, 20.0, 100.0, 1.0)),
72  mGradDelayText(new TextField(this, std::string(), LoseFocusOnTab_true,
73  nullptr, std::string(), false)),
74  // TRANSLATORS: colors tab. label.
75  mRedLabel(new Label(this, _("Red:"))),
76  mRedSlider(new Slider(this, 0.0, 255.0, 1.0)),
77  mRedText(new TextField(this, std::string(), LoseFocusOnTab_true,
78  nullptr, std::string(), false)),
79  // TRANSLATORS: colors tab. label.
80  mGreenLabel(new Label(this, _("Green:"))),
81  mGreenSlider(new Slider(this, 0.0, 255.0, 1.0)),
82  mGreenText(new TextField(this, std::string(), LoseFocusOnTab_true,
83  nullptr, std::string(), false)),
84  // TRANSLATORS: colors tab. label.
85  mBlueLabel(new Label(this, _("Blue:"))),
86  mBlueSlider(new Slider(this, 0.0, 255.0, 1.0)),
87  mBlueText(new TextField(this, std::string(), LoseFocusOnTab_true,
88  nullptr, std::string(), false))
89 {
90  // TRANSLATORS: settings colors tab name
91  setName(_("Colors"));
95 
96  // don't do anything with links
97  mPreview->setLinkHandler(nullptr);
98 
102 
104  mGradTypeSlider->setActionEventId("slider_grad");
107  mGradTypeSlider->setEnabled(false);
108 
109  // TRANSLATORS: color type
110  std::string longText = _("Static");
111 
112  const Font *const font = getFont();
113  // TRANSLATORS: color type
114  if (getFont()->getWidth(_("Pulse")) > font->getWidth(longText))
115  {
116  // TRANSLATORS: color type
117  longText = _("Pulse");
118  }
119  // TRANSLATORS: color type
120  if (getFont()->getWidth(_("Rainbow")) > font->getWidth(longText))
121  {
122  // TRANSLATORS: color type
123  longText = _("Rainbow");
124  }
125  // TRANSLATORS: color type
126  if (getFont()->getWidth(_("Spectrum")) > font->getWidth(longText))
127  {
128  // TRANSLATORS: color type
129  longText = _("Spectrum");
130  }
131 
132  mGradTypeText->setCaption(longText);
133 
135  mGradDelayText->setRange(20, 100);
136  mGradDelayText->setNumeric(true);
137  mGradDelayText->setEnabled(false);
138 
141  mGradDelaySlider->setActionEventId("slider_graddelay");
144 
145  mRedText->setWidth(40);
146  mRedText->setRange(0, 255);
147  mRedText->setNumeric(true);
148  mRedText->setEnabled(false);
149 
150  mRedSlider->setWidth(180);
152  mRedSlider->setActionEventId("slider_red");
154  mRedSlider->setEnabled(false);
155 
156  mGreenText->setWidth(40);
157  mGreenText->setRange(0, 255);
158  mGreenText->setNumeric(true);
159  mGreenText->setEnabled(false);
160 
161  mGreenSlider->setWidth(180);
163  mGreenSlider->setActionEventId("slider_green");
165  mGreenSlider->setEnabled(false);
166 
167  mBlueText->setWidth(40);
168  mBlueText->setRange(0, 255);
169  mBlueText->setNumeric(true);
170  mBlueText->setEnabled(false);
171 
172  mBlueSlider->setWidth(180);
174  mBlueSlider->setActionEventId("slider_blue");
176  mBlueSlider->setEnabled(false);
177 
179 
180  // Do the layout
181  LayoutHelper h(this);
182  ContainerPlacer place = h.getPlacer(0, 0);
183 
184  place(0, 0, mScroll, 6, 6).setPadding(2);
185  place(0, 6, mPreviewBox, 6, 1).setPadding(2);
186  place(0, 7, mGradTypeLabel, 3, 1);
187  place(3, 7, mGradTypeSlider, 1, 1);
188  place(4, 7, mGradTypeText, 2, 1).setPadding(1);
189  place(0, 8, mRedLabel, 3, 1);
190  place(3, 8, mRedSlider, 1, 1);
191  place(5, 8, mRedText, 1, 1).setPadding(1);
192  place(0, 9, mGreenLabel, 3, 1);
193  place(3, 9, mGreenSlider, 1, 1);
194  place(5, 9, mGreenText, 1, 1).setPadding(1);
195  place(0, 10, mBlueLabel, 3, 1);
196  place(3, 10, mBlueSlider, 1, 1);
197  place(5, 10, mBlueText, 1, 1).setPadding(1);
198  place(0, 11, mGradDelayLabel, 3, 1);
199  place(3, 11, mGradDelaySlider, 1, 1);
200  place(5, 11, mGradDelayText, 1, 1).setPadding(1);
201 
203 
204  setDimension(Rect(0, 0, 365, 350));
205 }
206 
208 {
209  if ((mPreviewBox != nullptr) && mPreviewBox->getContent() == mPreview)
211  else
213 }
214 
216 {
217  const std::string &eventId = event.getId();
218  if (eventId == "slider_grad")
219  {
220  updateGradType();
221  updateColor();
222  return;
223  }
224 
225  if (eventId == "slider_graddelay")
226  {
228  std::floor(mGradDelaySlider->getValue())));
229  updateColor();
230  return;
231  }
232  if (eventId == "slider_red")
233  {
234  mRedText->setText(toString(std::floor(mRedSlider->getValue())));
235  updateColor();
236  return;
237  }
238  if (eventId == "slider_green")
239  {
240  mGreenText->setText(toString(std::floor(mGreenSlider->getValue())));
241  updateColor();
242  return;
243  }
244  if (eventId == "slider_blue")
245  {
246  mBlueText->setText(toString(std::floor(mBlueSlider->getValue())));
247  updateColor();
248  return;
249  }
250 }
251 
253 {
254  if (userPalette == nullptr)
255  return;
256 
258  const UserColorIdT type = static_cast<UserColorIdT>(
260  const Color *col = &userPalette->getColor(type, 255U);
261  const GradientTypeT grad = userPalette->getGradientType(type);
262  const int delay = userPalette->getGradientDelay(type);
263  const Visible showControls = fromBool(grad != GradientType::LABEL,
264  Visible);
265  mPreview->setVisible(showControls);
266  mPreviewBox->setVisible(showControls);
267  mTextPreview->setVisible(showControls);
268  mGradTypeLabel->setVisible(showControls);
269  mGradTypeSlider->setVisible(showControls);
270  mGradTypeText->setVisible(showControls);
271  mGradDelayLabel->setVisible(showControls);
272  mGradDelaySlider->setVisible(showControls);
273  mGradDelayText->setVisible(showControls);
274  mRedLabel->setVisible(showControls);
275  mRedSlider->setVisible(showControls);
276  mRedText->setVisible(showControls);
277  mGreenLabel->setVisible(showControls);
278  mGreenSlider->setVisible(showControls);
279  mGreenText->setVisible(showControls);
280  mBlueLabel->setVisible(showControls);
281  mBlueSlider->setVisible(showControls);
282  mBlueText->setVisible(showControls);
283 
284  mPreview->clearRows();
288  mTextPreview->setTextBGColor(nullptr);
290  mTextPreview->setShadow(true);
291  mTextPreview->setOutline(true);
292  mTextPreview->useTextAlpha(false);
293 
294 // probably need combite both switches and add all mssing color ids.
295 
296  PRAGMA45(GCC diagnostic push)
297  PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
298  switch (type)
299  {
308  case UserColorId::NET:
309  mTextPreview->setBGColor(col);
311  mTextPreview->setOutline(false);
312  mTextPreview->setShadow(false);
313  break;
316  if (gui != nullptr)
319  mTextPreview->setOutline(false);
320  mTextPreview->setShadow(false);
321  break;
328  case UserColorId::MISS:
336  mTextPreview->setShadow(false);
337  break;
338  default:
339  break;
340  }
341 
342  switch (type)
343  {
369  case UserColorId::NET:
370  // TRANSLATORS: colors tab. label.
371  mGradDelayLabel->setCaption(_("Alpha:"));
372  mGradDelayText->setRange(0, 255);
373  mGradDelaySlider->setScale(0, 255);
374  break;
375  default:
376  // TRANSLATORS: colors tab. label.
377  mGradDelayLabel->setCaption(_("Delay:"));
378  mGradDelayText->setRange(20, 100);
379  mGradDelaySlider->setScale(20, 100);
380  break;
381  }
382  PRAGMA45(GCC diagnostic pop)
383  if (grad != GradientType::STATIC && grad != GradientType::PULSE)
384  { // If nonstatic color, don't display the current, but the committed
385  // color at the sliders
386  col = &userPalette->getCommittedColor(type);
387  }
388  else if (grad == GradientType::PULSE)
389  {
390  col = &userPalette->getTestColor(type);
391  }
392 
394  setEntry(mRedSlider, mRedText, col->r);
397 
399  updateGradType();
401 }
402 
403 void Setup_Colors::setEntry(Slider *const s, TextField *const t,
404  const int value)
405 {
406  if (s != nullptr)
407  s->setValue(value);
408  if (t != nullptr)
409  t->setText(toString(value));
410 }
411 
413 {
414  if (userPalette != nullptr)
415  userPalette->commit();
416 }
417 
419 {
420  if (userPalette == nullptr)
421  return;
422 
424  const UserColorIdT type = static_cast<UserColorIdT>(
426  const Color *const col = &userPalette->getColor(type, 255U);
428  userPalette->getGradientType(type)));
429  const int delay = userPalette->getGradientDelay(type);
431  setEntry(mRedSlider, mRedText, col->r);
434 }
435 
437 {
438  if (mSelected == -1 || (userPalette == nullptr))
439  return;
440 
442  const UserColorIdT type = static_cast<UserColorIdT>(
444  const GradientTypeT grad = userPalette->getGradientType(type);
445 
447  // TRANSLATORS: color type
448  (grad == GradientType::STATIC) ? _("Static") :
449  // TRANSLATORS: color type
450  (grad == GradientType::PULSE) ? _("Pulse") :
451  // TRANSLATORS: color type
452  (grad == GradientType::RAINBOW) ? _("Rainbow") : _("Spectrum"));
453 
454  const bool enable = (grad == GradientType::STATIC ||
455  grad == GradientType::PULSE);
456  const bool delayEnable = true;
457 
458  mGradDelayText->setEnabled(delayEnable);
459  mGradDelaySlider->setEnabled(delayEnable);
460 
461  mRedText->setEnabled(enable);
462  mRedSlider->setEnabled(enable);
463  mGreenText->setEnabled(enable);
464  mGreenSlider->setEnabled(enable);
465  mBlueText->setEnabled(enable);
466  mBlueSlider->setEnabled(enable);
467 }
468 
470 {
471  if (mSelected == -1 || (userPalette == nullptr))
472  return;
473 
474  const UserColorIdT type = static_cast<UserColorIdT>(
476  const GradientTypeT grad = static_cast<GradientTypeT>(
478  const int delay = CAST_S32(mGradDelaySlider->getValue());
479  userPalette->setGradient(type, grad);
480  userPalette->setGradientDelay(type, delay);
481 
482  if (grad == GradientType::STATIC)
483  {
484  userPalette->setColor(type,
488  }
489  else if (grad == GradientType::PULSE)
490  {
495  255U));
496  }
497 }
#define fromBool(val, name)
Definition: booldefines.h:49
#define CAST_S32
Definition: cast.h:30
void setOpaque(Opaque opaque)
void setLinkHandler(LinkHandler *linkHandler)
Definition: browserbox.cpp:166
void clearRows()
Definition: browserbox.cpp:359
void setOpaque(Opaque opaque)
Definition: browserbox.h:75
Definition: color.h:76
unsigned int b
Definition: color.h:245
unsigned int r
Definition: color.h:235
unsigned int g
Definition: color.h:240
Definition: font.h:90
int getWidth(const std::string &text) const
Definition: font.cpp:334
Font * getFont() const
Definition: gui.h:160
Definition: label.h:91
void setCaption(const std::string &caption)
Definition: label.cpp:264
ContainerPlacer getPlacer(const int x, const int y)
int getSelected() const
Definition: listbox.h:168
void addSelectionListener(SelectionListener *const selectionListener)
Definition: listbox.cpp:435
Definition: rect.h:74
void setScrollPolicy(const ScrollPolicy hPolicy, const ScrollPolicy vPolicy)
void setHorizontalScrollPolicy(const ScrollPolicy hPolicy)
void setContent(Widget *widget)
Widget * getContent()
void setHeight(int height)
void setName(const std::string &name)
Definition: setuptab.h:68
ListBox * mColorBox
Definition: setup_colors.h:59
Slider * mRedSlider
Definition: setup_colors.h:75
BrowserBox * mPreview
Definition: setup_colors.h:61
void updateColor() const
TextField * mGradDelayText
Definition: setup_colors.h:72
ScrollArea * mPreviewBox
Definition: setup_colors.h:63
Label * mGradDelayLabel
Definition: setup_colors.h:70
static void setEntry(Slider *const s, TextField *const t, const int value)
TextField * mBlueText
Definition: setup_colors.h:84
Label * mBlueLabel
Definition: setup_colors.h:82
Slider * mGradTypeSlider
Definition: setup_colors.h:67
void action(const ActionEvent &event)
TextPreview * mTextPreview
Definition: setup_colors.h:62
Label * mGradTypeText
Definition: setup_colors.h:68
Setup_Colors(const Widget2 *const widget)
void valueChanged(const SelectionEvent &event)
Label * mGradTypeLabel
Definition: setup_colors.h:66
TextField * mRedText
Definition: setup_colors.h:76
Label * mRedLabel
Definition: setup_colors.h:74
Slider * mGradDelaySlider
Definition: setup_colors.h:71
Slider * mBlueSlider
Definition: setup_colors.h:83
TextField * mGreenText
Definition: setup_colors.h:80
ScrollArea * mScroll
Definition: setup_colors.h:60
void updateGradType()
Slider * mGreenSlider
Definition: setup_colors.h:79
Label * mGreenLabel
Definition: setup_colors.h:78
static const char *const rawmsg
Definition: setup_colors.h:57
Definition: slider.h:89
double getValue() const
Definition: slider.h:204
void setScale(const double scaleStart, const double scaleEnd)
Definition: slider.cpp:468
void setValue(const double value)
Definition: slider.cpp:474
void setNumeric(const bool numeric)
Definition: textfield.cpp:269
void setText(const std::string &text)
Definition: textfield.cpp:803
void setRange(const int min, const int max)
Definition: textfield.h:137
int getValue() const
Definition: textfield.cpp:286
void setTextColor(const Color *color)
Definition: textpreview.h:46
void setShadow(const bool shadow)
Definition: textpreview.h:90
void setTextBGColor(const Color *color)
Definition: textpreview.h:66
void useTextAlpha(const bool alpha)
Definition: textpreview.h:57
void setOpaque(const Opaque opaque)
Definition: textpreview.h:116
void setBGColor(const Color *color)
Definition: textpreview.h:74
void setFont(Font *const font)
Definition: textpreview.h:82
void setOutline(const bool outline)
Definition: textpreview.h:98
const Color & getTestColor(const UserColorIdT type) const
Definition: userpalette.h:74
void rollback()
GradientTypeT getGradientType(const UserColorIdT type) const
Definition: userpalette.h:185
int getGradientDelay(const UserColorIdT type) const
Definition: userpalette.h:196
void setColor(const UserColorIdT type, const int r, const int g, const int b)
void commit()
Definition: userpalette.h:133
const Color & getColor(UserColorIdT type, const unsigned int alpha)
Definition: userpalette.h:160
void setGradientDelay(const UserColorIdT type, const int delay)
Definition: userpalette.h:109
void setGradient(const UserColorIdT type, const GradientTypeT grad)
int getColorTypeAt(const int i)
void setTestColor(const UserColorIdT type, const Color &color)
Definition: userpalette.h:84
const Color & getCommittedColor(const UserColorIdT type) const
Definition: userpalette.h:61
void setVisible(Visible visible)
Definition: widget.cpp:225
void setWidth(const int width)
Definition: widget.cpp:133
void setEnabled(const bool enabled)
Definition: widget.h:352
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
void setDimension(const Rect &dimension)
Definition: widget.cpp:169
Font * getFont() const
Definition: widget.cpp:331
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
int getWidth() const
Definition: widget.h:221
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:36
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
#define N_(s)
Definition: gettext.h:36
#define _(s)
Definition: gettext.h:35
GradientType ::T GradientTypeT
Definition: gradienttype.h:38
PRAGMA45(GCC diagnostic push) PRAGMA45(GCC diagnostic ignored "-Wredundant-decls") PRAGMA45(GCC diagnostic pop) class TestMain
Gui * gui
Definition: gui.cpp:111
Font * boldFont
Definition: gui.cpp:112
#define nullptr
Definition: localconsts.h:45
#define A_UNUSED
Definition: localconsts.h:160
const bool LoseFocusOnTab_true
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
@ AIR_COLLISION_HIGHLIGHT
Definition: usercolorid.h:81
@ HIT_PLAYER_MONSTER
Definition: usercolorid.h:70
@ MONSTER_COLLISION_HIGHLIGHT
Definition: usercolorid.h:83
@ HOME_PLACE_BORDER
Definition: usercolorid.h:95
@ GROUNDTOP_COLLISION_HIGHLIGHT
Definition: usercolorid.h:84
@ HIT_LOCAL_PLAYER_MONSTER
Definition: usercolorid.h:74
@ HIT_LOCAL_PLAYER_MISS
Definition: usercolorid.h:76
@ HIT_MONSTER_PLAYER
Definition: usercolorid.h:71
@ MONSTER_ATTACK_RANGE
Definition: usercolorid.h:90
@ HIT_LOCAL_PLAYER_CRITICAL
Definition: usercolorid.h:75
@ ATTACK_RANGE_BORDER
Definition: usercolorid.h:89
@ SKILL_ATTACK_RANGE
Definition: usercolorid.h:91
@ COLLISION_HIGHLIGHT
Definition: usercolorid.h:80
@ WATER_COLLISION_HIGHLIGHT
Definition: usercolorid.h:82
@ PORTAL_HIGHLIGHT
Definition: usercolorid.h:79
@ WALKABLE_HIGHLIGHT
Definition: usercolorid.h:85
const bool Opaque_false
Definition: opaque.h:30
const bool Opaque_true
Definition: opaque.h:30
UserColorId ::T UserColorIdT
Definition: usercolorid.h:99
UserPalette * userPalette
Definition: userpalette.cpp:34
bool Visible
Definition: visible.h:30