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-2017 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"));
147  0xb0ffb0,
149  // TRANSLATORS: palette color
150  _("Friend names"));
152  0xa00000,
154  // TRANSLATORS: palette color
155  _("Disregarded names"));
157  0xff0000,
159  // TRANSLATORS: palette color
160  _("Ignored names"));
162  0xff0000,
164  // TRANSLATORS: palette color
165  _("Erased names"));
167  0xff4040,
169  // TRANSLATORS: palette color
170  _("Enemy"));
172  0xffffff,
174  // TRANSLATORS: palette color
175  _("Other players names"));
177  0xff8040,
179  // TRANSLATORS: palette color
180  _("Own name"));
182  0x00ff00,
184  // TRANSLATORS: palette color
185  _("GM names"));
187  0xc8c8ff,
189  // TRANSLATORS: palette color
190  _("NPCs"));
192  0xff4040,
194  // TRANSLATORS: palette color
195  _("Monsters"));
197  0xffffff,
199  // TRANSLATORS: palette color
200  _("Pets"));
202  0xffffff,
204  // TRANSLATORS: palette color
205  _("Mercenary"));
207  0xffffff,
209  // TRANSLATORS: palette color
210  _("Homunculus"));
212  0xffffff,
214  // TRANSLATORS: palette color
215  _("Skill unit"));
217  0xff00d8,
219  // TRANSLATORS: palette color
220  _("Party members"));
222  0xff00d8,
224  // TRANSLATORS: palette color
225  _("Guild members"));
227  0x0000ff,
229  // TRANSLATORS: palette color
230  strprintf(_("Team %d"), 1));
232  0x00a020,
234  // TRANSLATORS: palette color
235  strprintf(_("Team %d"), 2));
237  0xffff20,
239  // TRANSLATORS: palette color
240  strprintf(_("Team %d"), 3));
242  // TRANSLATORS: palette label
243  _("Particles"));
245  0xffffff,
247  // TRANSLATORS: palette color
248  _("Particle effects"));
250  0x28dc28,
252  // TRANSLATORS: palette color
253  _("Pickup notification"));
255  0xffff00,
257  // TRANSLATORS: palette color
258  _("Exp notification"));
260  // TRANSLATORS: palette label
261  _("Hp bars"));
263  0x00ff00,
265  // TRANSLATORS: palette color
266  _("Player HP bar"), 50);
268  0xff0000,
270  // TRANSLATORS: palette color
271  _("Player HP bar (second color)"),
272  50);
274  0x00ff00,
276  // TRANSLATORS: palette color
277  _("Monster HP bar"),
278  50);
280  0xff0000,
282  // TRANSLATORS: palette color
283  _("Monster HP bar (second color)"),
284  50);
286  0x00ff00,
288  // TRANSLATORS: palette color
289  _("Homunculus HP bar"),
290  50);
292  0xff0000,
294  // TRANSLATORS: palette color
295  _("Homunculus HP bar (second color)"),
296  50);
298  0x00ff00,
300  // TRANSLATORS: palette color
301  _("Mercenary HP bar"),
302  50);
304  0xff0000,
306  // TRANSLATORS: palette color
307  _("Mercenary HP bar (second color)"),
308  50);
309 
311  0x00ff00,
313  // TRANSLATORS: palette color
314  _("Elemental HP bar"),
315  50);
317  0xff0000,
319  // TRANSLATORS: palette color
320  _("Elemental HP bar (second color)"),
321  50);
323  // TRANSLATORS: palette label
324  _("Hits"));
326  0x0064ff,
328  // TRANSLATORS: palette color
329  _("Player hits monster"));
331  0xff3232,
333  // TRANSLATORS: palette color
334  _("Monster hits player"));
336  0xff5050,
338  // TRANSLATORS: palette color
339  _("Other player hits local player"));
341  0xff0000,
343  // TRANSLATORS: palette color
344  _("Critical Hit"));
346  0x00ff00,
348  // TRANSLATORS: palette color
349  _("Local player hits monster"));
351  0xff0000,
353  // TRANSLATORS: palette color
354  _("Local player critical hit"));
356  0x00ffa6,
358  // TRANSLATORS: palette color
359  _("Local player miss"));
360  addColor(UserColorId::MISS, 0xffff00,
362  // TRANSLATORS: palette color
363  _("Misses"));
365  // TRANSLATORS: palette label
366  _("Tiles"));
368  0xC80000,
370  // TRANSLATORS: palette color
371  _("Portal highlight"));
373  0x0000C8,
375  // TRANSLATORS: palette color
376  _("Default collision highlight"),
377  64);
379  0xe0e0ff,
381  // TRANSLATORS: palette color
382  _("Air collision highlight"),
383  64);
385  0x2050e0,
387  // TRANSLATORS: palette color
388  _("Water collision highlight"),
389  64);
391  0x2050e0,
393  // TRANSLATORS: palette color
394  _("Monster collision highlight"),
395  64);
397  0xffff00,
399  // TRANSLATORS: palette color
400  _("Special ground collision highlight"),
401  20);
403  0x00D000,
405  // TRANSLATORS: palette color
406  _("Walkable highlight"),
407  255);
409  0x000000,
411  // TRANSLATORS: palette color
412  _("Tiles border"), 64);
414  // TRANSLATORS: palette label
415  _("Ranges"));
417  0xffffff,
419  // TRANSLATORS: palette color
420  _("Local player attack range"),
421  5);
423  0x0,
425  // TRANSLATORS: palette color
426  _("Local player attack range border"),
427  76);
429  0xff0000,
431  // TRANSLATORS: palette color
432  _("Monster attack range"),
433  20);
435  0x0,
437  // TRANSLATORS: palette color
438  _("Skill attack range border"),
439  76);
441  // TRANSLATORS: palette label
442  _("Other"));
444  0xffffff,
446  // TRANSLATORS: palette color
447  _("Floor item amount color"),
448  100);
450  0xffffff,
452  // TRANSLATORS: palette color
453  _("Home place"),
454  20);
456  0xffff00,
458  // TRANSLATORS: palette color
459  _("Home place border"),
460  200);
462  0x000000,
464  // TRANSLATORS: palette color
465  _("Road point"), 100);
466  commit(true);
467 }
468 
470 {
471  FOR_EACH (Colors::const_iterator, col, mColors)
472  {
473  if (col->grad == GradientType::LABEL)
474  continue;
475  const std::string &configName = ColorTypeNames[col->type];
476  config.setValue(configName + "Gradient",
477  CAST_S32(col->committedGrad));
478  config.setValue(configName + "Delay", col->delay);
479 
480  if (col->grad == GradientType::STATIC ||
481  col->grad == GradientType::PULSE)
482  {
483  char buffer[20];
484  snprintf(buffer, sizeof(buffer), "0x%06x", col->getRGB());
485  buffer[19] = 0;
486  config.setValue(configName, std::string(buffer));
487  }
488  }
489 }
490 
492  const int r,
493  const int g,
494  const int b)
495 {
496  Color &color = mColors[CAST_SIZE(type)].color;
497  color.r = r;
498  color.g = g;
499  color.b = b;
500 }
501 
503  const GradientTypeT grad)
504 {
505  ColorElem *const elem = &mColors[CAST_SIZE(type)];
506 
507  if (elem->grad != GradientType::STATIC && grad == GradientType::STATIC)
508  {
509  const size_t sz = mGradVector.size();
510  for (size_t i = 0; i < sz; i++)
511  {
512  if (mGradVector[i] == elem)
513  {
514  mGradVector.erase(mGradVector.begin() + i);
515  break;
516  }
517  }
518  }
519  else if (elem->grad == GradientType::STATIC &&
520  grad != GradientType::STATIC)
521  {
522  mGradVector.push_back(elem);
523  }
524 
525  if (elem->grad != grad)
526  elem->grad = grad;
527 }
528 
529 std::string UserPalette::getElementAt(int i)
530 {
531  if (i < 0 || i >= getNumberOfElements())
532  return "";
533 
534  return mColors[i].text;
535 }
536 
537 void UserPalette::commit(const bool commitNonStatic)
538 {
539  FOR_EACH (Colors::iterator, i, mColors)
540  {
541  i->committedGrad = i->grad;
542  i->committedDelay = i->delay;
543  if (commitNonStatic || i->grad == GradientType::STATIC)
544  i->committedColor = i->color;
545  else if (i->grad == GradientType::PULSE)
546  i->committedColor = i->testColor;
547  }
548 }
549 
551 {
552  FOR_EACH (Colors::iterator, i, mColors)
553  {
554  if (i->grad != i->committedGrad)
555  setGradient(static_cast<UserColorIdT>(i->type), i->committedGrad);
556 
557  const Color &committedColor = i->committedColor;
558  setGradientDelay(static_cast<UserColorIdT>(i->type),
559  i->committedDelay);
560  setColor(static_cast<UserColorIdT>(i->type),
561  committedColor.r,
562  committedColor.g,
563  committedColor.b);
564 
565  if (i->grad == GradientType::PULSE)
566  {
567  Color &testColor = i->testColor;
568  testColor.r = committedColor.r;
569  testColor.g = committedColor.g;
570  testColor.b = committedColor.b;
571  }
572  }
573 }
574 
576 {
577  if (i < 0 || i >= getNumberOfElements())
578  return 0;
579 
580  return mColors[i].type;
581 }
582 
584  const unsigned rgb,
585  GradientTypeT grad,
586  const std::string &text,
587  int delay)
588 {
589  const unsigned maxType = sizeof(ColorTypeNames)
590  / sizeof(ColorTypeNames[0]);
591 
592  if (CAST_U32(type) >= maxType)
593  return;
594 
595  const std::string &configName = ColorTypeNames[CAST_SIZE(type)];
596  char buffer[20];
597  snprintf(buffer, sizeof(buffer), "0x%06x", rgb);
598  buffer[19] = 0;
599 
600  const std::string rgbString = config.getValue(
601  configName, std::string(buffer));
602  unsigned int rgbValue = 0;
603  if (rgbString.length() == 8 && rgbString[0] == '0' && rgbString[1] == 'x')
604  rgbValue = atox(rgbString);
605  else
606  rgbValue = atoi(rgbString.c_str());
607  const Color &trueCol = Color(rgbValue);
608  grad = static_cast<GradientTypeT>(config.getValue(
609  configName + "Gradient",
610  CAST_S32(grad)));
611  delay = config.getValueInt(configName + "Delay", delay);
612  mColors[CAST_SIZE(type)].set(CAST_S32(type),
613  trueCol, grad, delay);
614  mColors[CAST_SIZE(type)].text = text;
615 
616  if (grad != GradientType::STATIC)
617  mGradVector.push_back(&mColors[CAST_SIZE(type)]);
618 }
619 
621  const std::string &text)
622 {
623  const unsigned maxType = sizeof(ColorTypeNames)
624  / sizeof(ColorTypeNames[0]);
625 
626  if (CAST_U32(type) >= maxType)
627  return;
628 
629 
630  mColors[CAST_SIZE(type)] = ColorElem();
631  const std::string str(" \342\200\225\342\200\225\342\200\225"
632  "\342\200\225\342\200\225 ");
633  mColors[CAST_SIZE(type)].grad = GradientType::LABEL;
634  mColors[CAST_SIZE(type)].text =
635  std::string(str).append(text).append(str);
636 }
637 
638 int UserPalette::getIdByChar(const signed char c, bool &valid) const
639 {
640  const CharColors::const_iterator it = mCharColors.find(c);
641  if (it != mCharColors.end())
642  {
643  valid = true;
644  return (*it).second;
645  }
646 
647  valid = false;
648  return 0;
649 }
#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
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
void addColor(const UserColorIdT type, const unsigned rgb, GradientTypeT grad, const std::string &text, int delay=40)
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 setValue(const std::string &key, const std::string &value)
std::string text
unsigned int r
Definition: color.h:234