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-2019 The ManaPlus Developers
7  * Copyright (C) 2019-2021 Andrei Karas
8  *
9  * This file is part of The ManaPlus Client.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 #include "gui/userpalette.h"
26 
27 #include "configuration.h"
28 
29 #include "utils/foreach.h"
30 #include "utils/gettext.h"
31 
32 #include "debug.h"
33 
35 
36 const std::string ColorTypeNames[CAST_SIZE(
38 {
39  "",
40  "ColorBeing",
41  "ColorFriend",
42  "ColorDisregarded",
43  "ColorIgnored",
44  "ColorErased",
45  "ColorEnemy",
46  "ColorPlayer",
47  "ColorSelf",
48  "ColorGM",
49  "ColorNPC",
50  "ColorMonster",
51  "ColorPet",
52  "ColorMercenary",
53  "ColorHomunculus",
54  "ColorSkillUnit",
55  "ColorParty",
56  "ColorGuild",
57  "ColorTeam1",
58  "ColorTeam2",
59  "ColorTeam3",
60  "",
61  "ColorParticle",
62  "ColorPickupInfo",
63  "ColorExpInfo",
64  "",
65  "ColorPlayerHp",
66  "ColorPlayerHp2",
67  "ColorMonsterHp",
68  "ColorMonsterHp2",
69  "ColorHomunHp",
70  "ColorHomunHp2",
71  "ColorMercHp",
72  "ColorMercHp2",
73  "ColorElementalHp",
74  "ColorElementalHp2",
75  "",
76  "ColorHitPlayerMonster",
77  "ColorHitMonsterPlayer",
78  "ColorHitPlayerPlayer",
79  "ColorHitCritical",
80  "ColorHitLocalPlayerMonster",
81  "ColorHitLocalPlayerCritical",
82  "ColorHitLocalPlayerMiss",
83  "ColorMiss",
84  "",
85  "ColorPortalHighlight",
86  "ColorCollisionHighlight",
87  "ColorCollisionAirHighlight",
88  "ColorCollisionWaterHighlight",
89  "ColorCollisionMonsterHighlight",
90  "ColorCollisionGroundtopHighlight",
91  "ColorWalkableTileHighlight",
92  "ColorNet",
93  "",
94  "ColorAttackRange",
95  "ColorAttackRangeBorder",
96  "ColorMonsterAttackRange",
97  "ColorSkillAttackRange",
98  "",
99  "ColorFloorItemText",
100  "ColorHomePlace",
101  "ColorHomePlaceBorder",
102  "ColorRoadPoint",
103 };
104 
105 std::string UserPalette::getConfigName(const std::string &typeName)
106 {
107  std::string res("Color" + typeName);
108  size_t pos = 5;
109  for (size_t i = 0; i < typeName.length(); i++)
110  {
111  if (i == 0 || typeName[i] == '_')
112  {
113  if (i > 0)
114  i++;
115 
116  res[pos] = typeName[i];
117  }
118  else
119  {
120  res[pos] = CAST_S8(tolower(typeName[i]));
121  }
122  pos ++;
123  }
124  res.erase(pos, res.length() - pos);
125  return res;
126 }
127 
130  ListModel()
131 {
138 
140  // TRANSLATORS: palette label
141  _("Beings"));
143  0xffffff,
145  // TRANSLATORS: palette color
146  _("Being"),
149  0xb0ffb0,
151  // TRANSLATORS: palette color
152  _("Friend names"),
155  0xa00000,
157  // TRANSLATORS: palette color
158  _("Disregarded names"),
161  0xff0000,
163  // TRANSLATORS: palette color
164  _("Ignored names"),
167  0xff0000,
169  // TRANSLATORS: palette color
170  _("Erased names"),
173  0xff4040,
175  // TRANSLATORS: palette color
176  _("Enemy"),
179  0xffffff,
181  // TRANSLATORS: palette color
182  _("Other players names"),
185  0xff8040,
187  // TRANSLATORS: palette color
188  _("Own name"),
191  0x00ff00,
193  // TRANSLATORS: palette color
194  _("GM names"),
197  0xc8c8ff,
199  // TRANSLATORS: palette color
200  _("NPCs"),
203  0xff4040,
205  // TRANSLATORS: palette color
206  _("Monsters"),
209  0xffffff,
211  // TRANSLATORS: palette color
212  _("Pets"),
215  0xffffff,
217  // TRANSLATORS: palette color
218  _("Mercenary"),
221  0xffffff,
223  // TRANSLATORS: palette color
224  _("Homunculus"),
227  0xffffff,
229  // TRANSLATORS: palette color
230  _("Skill unit"),
233  0xff00d8,
235  // TRANSLATORS: palette color
236  _("Party members"),
239  0xff00d8,
241  // TRANSLATORS: palette color
242  _("Guild members"),
245  0x0000ff,
247  // TRANSLATORS: palette color
248  strprintf(_("Team %d"), 1),
251  0x00a020,
253  // TRANSLATORS: palette color
254  strprintf(_("Team %d"), 2),
257  0xffff20,
259  // TRANSLATORS: palette color
260  strprintf(_("Team %d"), 3),
263  // TRANSLATORS: palette label
264  _("Particles"));
266  0xffffff,
268  // TRANSLATORS: palette color
269  _("Particle effects"),
272  0x28dc28,
274  // TRANSLATORS: palette color
275  _("Pickup notification"),
278  0xffff00,
280  // TRANSLATORS: palette color
281  _("Exp notification"),
284  // TRANSLATORS: palette label
285  _("Hp bars"));
287  0x00ff00,
289  // TRANSLATORS: palette color
290  _("Player HP bar"), 50);
292  0xff0000,
294  // TRANSLATORS: palette color
295  _("Player HP bar (second color)"),
296  50);
298  0x00ff00,
300  // TRANSLATORS: palette color
301  _("Monster HP bar"),
302  50);
304  0xff0000,
306  // TRANSLATORS: palette color
307  _("Monster HP bar (second color)"),
308  50);
310  0x00ff00,
312  // TRANSLATORS: palette color
313  _("Homunculus HP bar"),
314  50);
316  0xff0000,
318  // TRANSLATORS: palette color
319  _("Homunculus HP bar (second color)"),
320  50);
322  0x00ff00,
324  // TRANSLATORS: palette color
325  _("Mercenary HP bar"),
326  50);
328  0xff0000,
330  // TRANSLATORS: palette color
331  _("Mercenary HP bar (second color)"),
332  50);
333 
335  0x00ff00,
337  // TRANSLATORS: palette color
338  _("Elemental HP bar"),
339  50);
341  0xff0000,
343  // TRANSLATORS: palette color
344  _("Elemental HP bar (second color)"),
345  50);
347  // TRANSLATORS: palette label
348  _("Hits"));
350  0x0064ff,
352  // TRANSLATORS: palette color
353  _("Player hits monster"),
356  0xff3232,
358  // TRANSLATORS: palette color
359  _("Monster hits player"),
362  0xff5050,
364  // TRANSLATORS: palette color
365  _("Other player hits local player"),
368  0xff0000,
370  // TRANSLATORS: palette color
371  _("Critical Hit"),
374  0x00ff00,
376  // TRANSLATORS: palette color
377  _("Local player hits monster"),
380  0xff0000,
382  // TRANSLATORS: palette color
383  _("Local player critical hit"),
386  0x00ffa6,
388  // TRANSLATORS: palette color
389  _("Local player miss"),
391  addColor(UserColorId::MISS, 0xffff00,
393  // TRANSLATORS: palette color
394  _("Misses"),
397  // TRANSLATORS: palette label
398  _("Tiles"));
400  0xC80000,
402  // TRANSLATORS: palette color
403  _("Portal highlight"),
406  0x0000C8,
408  // TRANSLATORS: palette color
409  _("Default collision highlight"),
410  64);
412  0xe0e0ff,
414  // TRANSLATORS: palette color
415  _("Air collision highlight"),
416  64);
418  0x2050e0,
420  // TRANSLATORS: palette color
421  _("Water collision highlight"),
422  64);
424  0x2050e0,
426  // TRANSLATORS: palette color
427  _("Monster collision highlight"),
428  64);
430  0xffff00,
432  // TRANSLATORS: palette color
433  _("Special ground collision highlight"),
434  20);
436  0x00D000,
438  // TRANSLATORS: palette color
439  _("Walkable highlight"),
440  255);
442  0x000000,
444  // TRANSLATORS: palette color
445  _("Tiles border"), 64);
447  // TRANSLATORS: palette label
448  _("Ranges"));
450  0xffffff,
452  // TRANSLATORS: palette color
453  _("Local player attack range"),
454  5);
456  0x0,
458  // TRANSLATORS: palette color
459  _("Local player attack range border"),
460  76);
462  0xff0000,
464  // TRANSLATORS: palette color
465  _("Monster attack range"),
466  20);
468  0x0,
470  // TRANSLATORS: palette color
471  _("Skill attack range border"),
472  76);
474  // TRANSLATORS: palette label
475  _("Other"));
477  0xffffff,
479  // TRANSLATORS: palette color
480  _("Floor item amount color"),
481  100);
483  0xffffff,
485  // TRANSLATORS: palette color
486  _("Home place"),
487  20);
489  0xffff00,
491  // TRANSLATORS: palette color
492  _("Home place border"),
493  200);
495  0x000000,
497  // TRANSLATORS: palette color
498  _("Road point"), 100);
499  commit(true);
500 }
501 
503 {
504  FOR_EACH (Colors::const_iterator, col, mColors)
505  {
506  if (col->grad == GradientType::LABEL)
507  continue;
508  const std::string &configName = ColorTypeNames[col->type];
509  config.setValue(configName + "Gradient",
510  CAST_S32(col->committedGrad));
511  config.setValue(configName + "Delay", col->delay);
512 
513  if (col->grad == GradientType::STATIC ||
514  col->grad == GradientType::PULSE)
515  {
516  char buffer[20];
517  snprintf(buffer, sizeof(buffer), "0x%06x", col->getRGB());
518  buffer[19] = 0;
519  config.setValue(configName, std::string(buffer));
520  }
521  }
522 }
523 
525  const int r,
526  const int g,
527  const int b)
528 {
529  Color &color = mColors[CAST_SIZE(type)].color;
530  color.r = r;
531  color.g = g;
532  color.b = b;
533 }
534 
536  const GradientTypeT grad)
537 {
538  ColorElem *const elem = &mColors[CAST_SIZE(type)];
539 
540  if (elem->grad != GradientType::STATIC && grad == GradientType::STATIC)
541  {
542  const size_t sz = mGradVector.size();
543  for (size_t i = 0; i < sz; i++)
544  {
545  if (mGradVector[i] == elem)
546  {
547  mGradVector.erase(mGradVector.begin() + i);
548  break;
549  }
550  }
551  }
552  else if (elem->grad == GradientType::STATIC &&
553  grad != GradientType::STATIC)
554  {
555  mGradVector.push_back(elem);
556  }
557 
558  if (elem->grad != grad)
559  elem->grad = grad;
560 }
561 
562 std::string UserPalette::getElementAt(int i)
563 {
564  if (i < 0 || i >= getNumberOfElements())
565  return "";
566 
567  return mColors[i].text;
568 }
569 
570 void UserPalette::commit(const bool commitNonStatic)
571 {
572  FOR_EACH (Colors::iterator, i, mColors)
573  {
574  i->committedGrad = i->grad;
575  i->committedDelay = i->delay;
576  if (commitNonStatic || i->grad == GradientType::STATIC)
577  i->committedColor = i->color;
578  else if (i->grad == GradientType::PULSE)
579  i->committedColor = i->testColor;
580  }
581 }
582 
584 {
585  FOR_EACH (Colors::iterator, i, mColors)
586  {
587  if (i->grad != i->committedGrad)
588  setGradient(static_cast<UserColorIdT>(i->type), i->committedGrad);
589 
590  const Color &committedColor = i->committedColor;
591  setGradientDelay(static_cast<UserColorIdT>(i->type),
592  i->committedDelay);
593  setColor(static_cast<UserColorIdT>(i->type),
594  committedColor.r,
595  committedColor.g,
596  committedColor.b);
597 
598  if (i->grad == GradientType::PULSE)
599  {
600  Color &testColor = i->testColor;
601  testColor.r = committedColor.r;
602  testColor.g = committedColor.g;
603  testColor.b = committedColor.b;
604  }
605  }
606 }
607 
609 {
610  if (i < 0 || i >= getNumberOfElements())
611  return 0;
612 
613  return mColors[i].type;
614 }
615 
617  const unsigned rgb,
618  GradientTypeT grad,
619  const std::string &text,
620  int delay)
621 {
622  const unsigned maxType = sizeof(ColorTypeNames)
623  / sizeof(ColorTypeNames[0]);
624 
625  if (CAST_U32(type) >= maxType)
626  return;
627 
628  const std::string &configName = ColorTypeNames[CAST_SIZE(type)];
629  char buffer[20];
630  snprintf(buffer, sizeof(buffer), "0x%06x", rgb);
631  buffer[19] = 0;
632 
633  const std::string rgbString = config.getValue(
634  configName, std::string(buffer));
635  unsigned int rgbValue = 0;
636  if (rgbString.length() == 8 && rgbString[0] == '0' && rgbString[1] == 'x')
637  rgbValue = atox(rgbString);
638  else
639  rgbValue = atoi(rgbString.c_str());
640  const Color &trueCol = Color(rgbValue);
641  grad = static_cast<GradientTypeT>(config.getValue(
642  configName + "Gradient",
643  CAST_S32(grad)));
644  delay = config.getValueInt(configName + "Delay", delay);
645  mColors[CAST_SIZE(type)].set(CAST_S32(type),
646  trueCol, grad, delay);
647  mColors[CAST_SIZE(type)].text = text;
648 
649  if (grad != GradientType::STATIC)
650  mGradVector.push_back(&mColors[CAST_SIZE(type)]);
651 }
652 
654  const std::string &text)
655 {
656  const unsigned maxType = sizeof(ColorTypeNames)
657  / sizeof(ColorTypeNames[0]);
658 
659  if (CAST_U32(type) >= maxType)
660  return;
661 
662 
663  mColors[CAST_SIZE(type)] = ColorElem();
664  const std::string str(" \342\200\225\342\200\225\342\200\225"
665  "\342\200\225\342\200\225 ");
666  mColors[CAST_SIZE(type)].grad = GradientType::LABEL;
667  mColors[CAST_SIZE(type)].text =
668  std::string(str).append(text).append(str);
669 }
670 
671 int UserPalette::getIdByChar(const signed char c, bool &valid) const
672 {
673  const CharColors::const_iterator it = mCharColors.find(c);
674  if (it != mCharColors.end())
675  {
676  valid = true;
677  return (*it).second;
678  }
679 
680  valid = false;
681  return 0;
682 }
#define CAST_S8
Definition: cast.h:26
#define CAST_S32
Definition: cast.h:30
#define CAST_U32
Definition: cast.h:31
#define CAST_SIZE
Definition: cast.h:34
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
int getValueInt(const std::string &key, const int deflt) const
std::string getValue(const std::string &key, const std::string &deflt) const
void setValue(const std::string &key, const std::string &value)
Colors mColors
Definition: palette.h:154
CharColors mCharColors
Definition: palette.h:155
std::vector< ColorElem * > mGradVector
Definition: palette.h:156
void addLabel(const UserColorIdT type, const std::string &text)
std::string getElementAt(int i)
void rollback()
static std::string getConfigName(const std::string &typeName)
void setColor(const UserColorIdT type, const int r, const int g, const int b)
void commit()
Definition: userpalette.h:133
void setGradientDelay(const UserColorIdT type, const int delay)
Definition: userpalette.h:109
void setGradient(const UserColorIdT type, const GradientTypeT grad)
int getIdByChar(const signed char c, bool &valid) const
int getColorTypeAt(const int i)
void addColor(const UserColorIdT type, const unsigned rgb, GradientTypeT grad, const std::string &text, int delay)
int getNumberOfElements()
Definition: userpalette.h:118
Configuration config
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define _(s)
Definition: gettext.h:35
GradientType ::T GradientTypeT
Definition: gradienttype.h:38
@ HIT_PLAYER_PLAYER
Definition: usercolorid.h:72
@ 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
#define GRADIENT_DELAY
Definition: palette.h:39
unsigned int atox(const std::string &str)
Definition: stringutils.cpp:78
std::string strprintf(const char *const format,...)
GradientTypeT grad
Definition: palette.h:127
UserColorId ::T UserColorIdT
Definition: usercolorid.h:99
const std::string ColorTypeNames[static_cast< size_t >(UserColorId::USER_COLOR_LAST)]
Definition: userpalette.cpp:37
UserPalette * userPalette
Definition: userpalette.cpp:34