ManaPlus
userpalette.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008 Douglas Boffey <[email protected]>
4  * Copyright (C) 2009 The Mana World Development Team
5  * Copyright (C) 2009-2010 The Mana Developers
6  * Copyright (C) 2011-2018 The ManaPlus Developers
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "gui/userpalette.h"
25 
26 #include "configuration.h"
27 
28 #include "utils/foreach.h"
29 #include "utils/gettext.h"
30 
31 #include "debug.h"
32 
34 
35 const std::string ColorTypeNames[CAST_SIZE(
37 {
38  "",
39  "ColorBeing",
40  "ColorFriend",
41  "ColorDisregarded",
42  "ColorIgnored",
43  "ColorErased",
44  "ColorEnemy",
45  "ColorPlayer",
46  "ColorSelf",
47  "ColorGM",
48  "ColorNPC",
49  "ColorMonster",
50  "ColorPet",
51  "ColorMercenary",
52  "ColorHomunculus",
53  "ColorSkillUnit",
54  "ColorParty",
55  "ColorGuild",
56  "ColorTeam1",
57  "ColorTeam2",
58  "ColorTeam3",
59  "",
60  "ColorParticle",
61  "ColorPickupInfo",
62  "ColorExpInfo",
63  "",
64  "ColorPlayerHp",
65  "ColorPlayerHp2",
66  "ColorMonsterHp",
67  "ColorMonsterHp2",
68  "ColorHomunHp",
69  "ColorHomunHp2",
70  "ColorMercHp",
71  "ColorMercHp2",
72  "ColorElementalHp",
73  "ColorElementalHp2",
74  "",
75  "ColorHitPlayerMonster",
76  "ColorHitMonsterPlayer",
77  "ColorHitPlayerPlayer",
78  "ColorHitCritical",
79  "ColorHitLocalPlayerMonster",
80  "ColorHitLocalPlayerCritical",
81  "ColorHitLocalPlayerMiss",
82  "ColorMiss",
83  "",
84  "ColorPortalHighlight",
85  "ColorCollisionHighlight",
86  "ColorCollisionAirHighlight",
87  "ColorCollisionWaterHighlight",
88  "ColorCollisionMonsterHighlight",
89  "ColorCollisionGroundtopHighlight",
90  "ColorWalkableTileHighlight",
91  "ColorNet",
92  "",
93  "ColorAttackRange",
94  "ColorAttackRangeBorder",
95  "ColorMonsterAttackRange",
96  "ColorSkillAttackRange",
97  "",
98  "ColorFloorItemText",
99  "ColorHomePlace",
100  "ColorHomePlaceBorder",
101  "ColorRoadPoint",
102 };
103 
104 std::string UserPalette::getConfigName(const std::string &typeName)
105 {
106  std::string res("Color" + typeName);
107  size_t pos = 5;
108  for (size_t i = 0; i < typeName.length(); i++)
109  {
110  if (i == 0 || typeName[i] == '_')
111  {
112  if (i > 0)
113  i++;
114 
115  res[pos] = typeName[i];
116  }
117  else
118  {
119  res[pos] = CAST_S8(tolower(typeName[i]));
120  }
121  pos ++;
122  }
123  res.erase(pos, res.length() - pos);
124  return res;
125 }
126 
129  ListModel()
130 {
137 
139  // TRANSLATORS: palette label
140  _("Beings"));
142  0xffffff,
144  // TRANSLATORS: palette color
145  _("Being"),
148  0xb0ffb0,
150  // TRANSLATORS: palette color
151  _("Friend names"),
154  0xa00000,
156  // TRANSLATORS: palette color
157  _("Disregarded names"),
160  0xff0000,
162  // TRANSLATORS: palette color
163  _("Ignored names"),
166  0xff0000,
168  // TRANSLATORS: palette color
169  _("Erased names"),
172  0xff4040,
174  // TRANSLATORS: palette color
175  _("Enemy"),
178  0xffffff,
180  // TRANSLATORS: palette color
181  _("Other players names"),
184  0xff8040,
186  // TRANSLATORS: palette color
187  _("Own name"),
190  0x00ff00,
192  // TRANSLATORS: palette color
193  _("GM names"),
196  0xc8c8ff,
198  // TRANSLATORS: palette color
199  _("NPCs"),
202  0xff4040,
204  // TRANSLATORS: palette color
205  _("Monsters"),
208  0xffffff,
210  // TRANSLATORS: palette color
211  _("Pets"),
214  0xffffff,
216  // TRANSLATORS: palette color
217  _("Mercenary"),
220  0xffffff,
222  // TRANSLATORS: palette color
223  _("Homunculus"),
226  0xffffff,
228  // TRANSLATORS: palette color
229  _("Skill unit"),
232  0xff00d8,
234  // TRANSLATORS: palette color
235  _("Party members"),
238  0xff00d8,
240  // TRANSLATORS: palette color
241  _("Guild members"),
244  0x0000ff,
246  // TRANSLATORS: palette color
247  strprintf(_("Team %d"), 1),
250  0x00a020,
252  // TRANSLATORS: palette color
253  strprintf(_("Team %d"), 2),
256  0xffff20,
258  // TRANSLATORS: palette color
259  strprintf(_("Team %d"), 3),
262  // TRANSLATORS: palette label
263  _("Particles"));
265  0xffffff,
267  // TRANSLATORS: palette color
268  _("Particle effects"),
271  0x28dc28,
273  // TRANSLATORS: palette color
274  _("Pickup notification"),
277  0xffff00,
279  // TRANSLATORS: palette color
280  _("Exp notification"),
283  // TRANSLATORS: palette label
284  _("Hp bars"));
286  0x00ff00,
288  // TRANSLATORS: palette color
289  _("Player HP bar"), 50);
291  0xff0000,
293  // TRANSLATORS: palette color
294  _("Player HP bar (second color)"),
295  50);
297  0x00ff00,
299  // TRANSLATORS: palette color
300  _("Monster HP bar"),
301  50);
303  0xff0000,
305  // TRANSLATORS: palette color
306  _("Monster HP bar (second color)"),
307  50);
309  0x00ff00,
311  // TRANSLATORS: palette color
312  _("Homunculus HP bar"),
313  50);
315  0xff0000,
317  // TRANSLATORS: palette color
318  _("Homunculus HP bar (second color)"),
319  50);
321  0x00ff00,
323  // TRANSLATORS: palette color
324  _("Mercenary HP bar"),
325  50);
327  0xff0000,
329  // TRANSLATORS: palette color
330  _("Mercenary HP bar (second color)"),
331  50);
332 
334  0x00ff00,
336  // TRANSLATORS: palette color
337  _("Elemental HP bar"),
338  50);
340  0xff0000,
342  // TRANSLATORS: palette color
343  _("Elemental HP bar (second color)"),
344  50);
346  // TRANSLATORS: palette label
347  _("Hits"));
349  0x0064ff,
351  // TRANSLATORS: palette color
352  _("Player hits monster"),
355  0xff3232,
357  // TRANSLATORS: palette color
358  _("Monster hits player"),
361  0xff5050,
363  // TRANSLATORS: palette color
364  _("Other player hits local player"),
367  0xff0000,
369  // TRANSLATORS: palette color
370  _("Critical Hit"),
373  0x00ff00,
375  // TRANSLATORS: palette color
376  _("Local player hits monster"),
379  0xff0000,
381  // TRANSLATORS: palette color
382  _("Local player critical hit"),
385  0x00ffa6,
387  // TRANSLATORS: palette color
388  _("Local player miss"),
390  addColor(UserColorId::MISS, 0xffff00,
392  // TRANSLATORS: palette color
393  _("Misses"),
396  // TRANSLATORS: palette label
397  _("Tiles"));
399  0xC80000,
401  // TRANSLATORS: palette color
402  _("Portal highlight"),
405  0x0000C8,
407  // TRANSLATORS: palette color
408  _("Default collision highlight"),
409  64);
411  0xe0e0ff,
413  // TRANSLATORS: palette color
414  _("Air collision highlight"),
415  64);
417  0x2050e0,
419  // TRANSLATORS: palette color
420  _("Water collision highlight"),
421  64);
423  0x2050e0,
425  // TRANSLATORS: palette color
426  _("Monster collision highlight"),
427  64);
429  0xffff00,
431  // TRANSLATORS: palette color
432  _("Special ground collision highlight"),
433  20);
435  0x00D000,
437  // TRANSLATORS: palette color
438  _("Walkable highlight"),
439  255);
441  0x000000,
443  // TRANSLATORS: palette color
444  _("Tiles border"), 64);
446  // TRANSLATORS: palette label
447  _("Ranges"));
449  0xffffff,
451  // TRANSLATORS: palette color
452  _("Local player attack range"),
453  5);
455  0x0,
457  // TRANSLATORS: palette color
458  _("Local player attack range border"),
459  76);
461  0xff0000,
463  // TRANSLATORS: palette color
464  _("Monster attack range"),
465  20);
467  0x0,
469  // TRANSLATORS: palette color
470  _("Skill attack range border"),
471  76);
473  // TRANSLATORS: palette label
474  _("Other"));
476  0xffffff,
478  // TRANSLATORS: palette color
479  _("Floor item amount color"),
480  100);
482  0xffffff,
484  // TRANSLATORS: palette color
485  _("Home place"),
486  20);
488  0xffff00,
490  // TRANSLATORS: palette color
491  _("Home place border"),
492  200);
494  0x000000,
496  // TRANSLATORS: palette color
497  _("Road point"), 100);
498  commit(true);
499 }
500 
502 {
503  FOR_EACH (Colors::const_iterator, col, mColors)
504  {
505  if (col->grad == GradientType::LABEL)
506  continue;
507  const std::string &configName = ColorTypeNames[col->type];
508  config.setValue(configName + "Gradient",
509  CAST_S32(col->committedGrad));
510  config.setValue(configName + "Delay", col->delay);
511 
512  if (col->grad == GradientType::STATIC ||
513  col->grad == GradientType::PULSE)
514  {
515  char buffer[20];
516  snprintf(buffer, sizeof(buffer), "0x%06x", col->getRGB());
517  buffer[19] = 0;
518  config.setValue(configName, std::string(buffer));
519  }
520  }
521 }
522 
524  const int r,
525  const int g,
526  const int b)
527 {
528  Color &color = mColors[CAST_SIZE(type)].color;
529  color.r = r;
530  color.g = g;
531  color.b = b;
532 }
533 
535  const GradientTypeT grad)
536 {
537  ColorElem *const elem = &mColors[CAST_SIZE(type)];
538 
539  if (elem->grad != GradientType::STATIC && grad == GradientType::STATIC)
540  {
541  const size_t sz = mGradVector.size();
542  for (size_t i = 0; i < sz; i++)
543  {
544  if (mGradVector[i] == elem)
545  {
546  mGradVector.erase(mGradVector.begin() + i);
547  break;
548  }
549  }
550  }
551  else if (elem->grad == GradientType::STATIC &&
552  grad != GradientType::STATIC)
553  {
554  mGradVector.push_back(elem);
555  }
556 
557  if (elem->grad != grad)
558  elem->grad = grad;
559 }
560 
561 std::string UserPalette::getElementAt(int i)
562 {
563  if (i < 0 || i >= getNumberOfElements())
564  return "";
565 
566  return mColors[i].text;
567 }
568 
569 void UserPalette::commit(const bool commitNonStatic)
570 {
571  FOR_EACH (Colors::iterator, i, mColors)
572  {
573  i->committedGrad = i->grad;
574  i->committedDelay = i->delay;
575  if (commitNonStatic || i->grad == GradientType::STATIC)
576  i->committedColor = i->color;
577  else if (i->grad == GradientType::PULSE)
578  i->committedColor = i->testColor;
579  }
580 }
581 
583 {
584  FOR_EACH (Colors::iterator, i, mColors)
585  {
586  if (i->grad != i->committedGrad)
587  setGradient(static_cast<UserColorIdT>(i->type), i->committedGrad);
588 
589  const Color &committedColor = i->committedColor;
590  setGradientDelay(static_cast<UserColorIdT>(i->type),
591  i->committedDelay);
592  setColor(static_cast<UserColorIdT>(i->type),
593  committedColor.r,
594  committedColor.g,
595  committedColor.b);
596 
597  if (i->grad == GradientType::PULSE)
598  {
599  Color &testColor = i->testColor;
600  testColor.r = committedColor.r;
601  testColor.g = committedColor.g;
602  testColor.b = committedColor.b;
603  }
604  }
605 }
606 
608 {
609  if (i < 0 || i >= getNumberOfElements())
610  return 0;
611 
612  return mColors[i].type;
613 }
614 
616  const unsigned rgb,
617  GradientTypeT grad,
618  const std::string &text,
619  int delay)
620 {
621  const unsigned maxType = sizeof(ColorTypeNames)
622  / sizeof(ColorTypeNames[0]);
623 
624  if (CAST_U32(type) >= maxType)
625  return;
626 
627  const std::string &configName = ColorTypeNames[CAST_SIZE(type)];
628  char buffer[20];
629  snprintf(buffer, sizeof(buffer), "0x%06x", rgb);
630  buffer[19] = 0;
631 
632  const std::string rgbString = config.getValue(
633  configName, std::string(buffer));
634  unsigned int rgbValue = 0;
635  if (rgbString.length() == 8 && rgbString[0] == '0' && rgbString[1] == 'x')
636  rgbValue = atox(rgbString);
637  else
638  rgbValue = atoi(rgbString.c_str());
639  const Color &trueCol = Color(rgbValue);
640  grad = static_cast<GradientTypeT>(config.getValue(
641  configName + "Gradient",
642  CAST_S32(grad)));
643  delay = config.getValueInt(configName + "Delay", delay);
644  mColors[CAST_SIZE(type)].set(CAST_S32(type),
645  trueCol, grad, delay);
646  mColors[CAST_SIZE(type)].text = text;
647 
648  if (grad != GradientType::STATIC)
649  mGradVector.push_back(&mColors[CAST_SIZE(type)]);
650 }
651 
653  const std::string &text)
654 {
655  const unsigned maxType = sizeof(ColorTypeNames)
656  / sizeof(ColorTypeNames[0]);
657 
658  if (CAST_U32(type) >= maxType)
659  return;
660 
661 
662  mColors[CAST_SIZE(type)] = ColorElem();
663  const std::string str(" \342\200\225\342\200\225\342\200\225"
664  "\342\200\225\342\200\225 ");
665  mColors[CAST_SIZE(type)].grad = GradientType::LABEL;
666  mColors[CAST_SIZE(type)].text =
667  std::string(str).append(text).append(str);
668 }
669 
670 int UserPalette::getIdByChar(const signed char c, bool &valid) const
671 {
672  const CharColors::const_iterator it = mCharColors.find(c);
673  if (it != mCharColors.end())
674  {
675  valid = true;
676  return (*it).second;
677  }
678 
679  valid = false;
680  return 0;
681 }
#define CAST_U32
Definition: cast.h:30
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
#define _(s)
Definition: gettext.h:34
void setGradientDelay(const UserColorIdT type, const int delay)
Definition: userpalette.h:108
void rollback()
GradientType ::T GradientTypeT
Definition: gradienttype.h:37
void setGradient(const UserColorIdT type, const GradientTypeT grad)
unsigned int g
Definition: color.h:239
GradientTypeT grad
Definition: palette.h:126
Configuration config
#define GRADIENT_DELAY
Definition: palette.h:38
int getIdByChar(const signed char c, bool &valid) const
static std::string getConfigName(const std::string &typeName)
std::vector< ColorElem * > mGradVector
Definition: palette.h:155
UserPalette * userPalette
Definition: userpalette.cpp:33
int getNumberOfElements()
Definition: userpalette.h:117
#define CAST_S32
Definition: cast.h:29
unsigned int b
Definition: color.h:244
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
const std::string ColorTypeNames[static_cast< size_t >(UserColorId::USER_COLOR_LAST)]
Definition: userpalette.cpp:36
void setColor(const UserColorIdT type, const int r, const int g, const int b)
void addLabel(const UserColorIdT type, const std::string &text)
Colors mColors
Definition: palette.h:153
void commit()
Definition: userpalette.h:132
#define CAST_S8
Definition: cast.h:25
std::string getElementAt(int i)
unsigned int atox(const std::string &str)
Definition: stringutils.cpp:77
std::string getValue(const std::string &key, const std::string &deflt) const
CharColors mCharColors
Definition: palette.h:154
Definition: color.h:74
#define CAST_SIZE
Definition: cast.h:33
int getColorTypeAt(const int i)
int getValueInt(const std::string &key, const int deflt) const
UserColorId ::T UserColorIdT
Definition: usercolorid.h:98
void addColor(const UserColorIdT type, const unsigned rgb, GradientTypeT grad, const std::string &text, int delay)
void setValue(const std::string &key, const std::string &value)
std::string text
unsigned int r
Definition: color.h:234