Mana
Loading...
Searching...
No Matches
userpalette.cpp
Go to the documentation of this file.
1/*
2 * Configurable text colors
3 * Copyright (C) 2008 Douglas Boffey <dougaboffey@netscape.net>
4 * Copyright (C) 2009 The Mana World Development Team
5 * Copyright (C) 2009-2012 The Mana Developers
6 *
7 * This file is part of The Mana 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 "configuration.h"
26
27#include "utils/gettext.h"
28#include "utils/stringutils.h"
29
30static constexpr const char *ColorTypeNames[] = {
31 "Being",
32 "Player",
33 "Self",
34 "GM",
35 "NPC",
36 "Monster",
37 "Party",
38 "Guild",
39 "Particle",
40 "Experience",
41 "Pickup",
42 "HitPlayerMonster",
43 "HitMonsterPlayer",
44 "HitCritical",
45 "HitLocalPlayerMonster",
46 "HitLocalPlayerCritical",
47 "HitLocalPlayerMiss",
48 "Miss"
49};
50
52 Palette(USER_COLOR_LAST)
53{
55 mColors[PC] = ColorElem();
57 mColors[GM] = ColorElem();
60
61 addColor(BEING, 0xffffff, STATIC, _("Being"));
62 addColor(PC, 0xffffff, STATIC, _("Other Players' Names"));
63 addColor(SELF, 0xff8040, STATIC, _("Own Name"));
64 addColor(GM, 0x00ff00, STATIC, _("GM Names"));
65 addColor(NPC, 0xc8c8ff, STATIC, _("NPCs"));
66 addColor(MONSTER, 0xff4040, STATIC, _("Monsters"));
67 addColor(PARTY, 0xff00d8, STATIC, _("Party Members"));
68 addColor(GUILD, 0xff00d8, STATIC, _("Guild Members"));
69 addColor(PARTICLE, 0xffffff, STATIC, _("Particle Effects"));
70 addColor(PICKUP_INFO, 0x28dc28, STATIC, _("Pickup Notification"));
71 addColor(EXP_INFO, 0xffff00, STATIC, _("Exp Notification"));
73 _("Other Player Hits Monster"));
74 addColor(HIT_MONSTER_PLAYER, 0xff3232, STATIC, _("Monster Hits Player"));
75 addColor(HIT_CRITICAL, 0xff0000, RAINBOW, _("Critical Hit"));
77 _("Local Player Hits Monster"));
79 _("Local Player Critical Hit"));
81 _("Local Player Miss"));
82 addColor(MISS, 0xffff00, STATIC, _("Misses"));
83 commit(true);
84}
85
87{
88 config.colors.clear();
89
90 // TODO: Don't write out colors when they have the default value
91 for (auto &color : mColors)
92 {
93 auto &configColor = config.colors[ColorTypeNames[color.type]];
94
95 configColor.gradient = color.committedGrad;
96
97 if (color.grad != STATIC && color.delay != GRADIENT_DELAY)
98 configColor.delay = color.delay;
99
100 if (color.grad == STATIC || color.grad == PULSE)
101 configColor.color = strprintf("0x%06x", color.getRGB());
102 }
103}
104
105void UserPalette::setColor(int type, int r, int g, int b)
106{
107 mColors[type].color.r = r;
108 mColors[type].color.g = g;
109 mColors[type].color.b = b;
110}
111
113{
114 ColorElem *elem = &mColors[type];
115 if (elem->grad != STATIC && grad == STATIC)
116 {
117 for (size_t i = 0; i < mGradVector.size(); i++)
118 {
119 if (mGradVector[i] == elem)
120 {
121 mGradVector.erase(mGradVector.begin() + i);
122 break;
123 }
124 }
125 }
126 else if (elem->grad == STATIC && grad != STATIC)
127 {
128 mGradVector.push_back(elem);
129 }
130
131 if (elem->grad != grad)
132 {
133 elem->grad = grad;
134 }
135}
136
138{
139 if (i < 0 || i >= getNumberOfElements())
140 {
141 return "";
142 }
143 return mColors[i].text;
144}
145
146void UserPalette::commit(bool commitNonStatic)
147{
148 for (auto &color : mColors)
149 {
150 color.committedGrad = color.grad;
151 color.committedDelay = color.delay;
152 if (commitNonStatic || color.grad == STATIC)
153 {
154 color.committedColor = color.color;
155 }
156 else if (color.grad == PULSE)
157 {
158 color.committedColor = color.testColor;
159 }
160 }
161}
162
164{
165 for (auto &color : mColors)
166 {
167 if (color.grad != color.committedGrad)
168 {
169 setGradient(color.type, color.committedGrad);
170 }
171 setGradientDelay(color.type, color.committedDelay);
172 setColor(color.type, color.committedColor.r,
173 color.committedColor.g, color.committedColor.b);
174 if (color.grad == PULSE)
175 {
176 color.testColor.r = color.committedColor.r;
177 color.testColor.g = color.committedColor.g;
178 color.testColor.b = color.committedColor.b;
179 }
180 }
181}
182
184{
185 if (i < 0 || i >= getNumberOfElements())
186 {
187 return BEING;
188 }
189
190 return mColors[i].type;
191}
192
193void UserPalette::addColor(int type, unsigned rgb, GradientType grad,
194 const std::string &text, int delay)
195{
196 auto colorIt = config.colors.find(ColorTypeNames[type]);
197 if (colorIt != config.colors.end())
198 {
199 const UserColor &userColor = colorIt->second;
200 rgb = atox(userColor.color);
201 grad = static_cast<GradientType>(userColor.gradient);
202 delay = userColor.delay.value_or(GRADIENT_DELAY);
203 }
204
205 mColors[type].set(type, gcn::Color(rgb), grad, delay);
206 mColors[type].text = text;
207
208 if (grad != STATIC)
209 mGradVector.push_back(&mColors[type]);
210}
Class controlling the game's color palette.
Definition palette.h:42
std::vector< ColorElem * > mGradVector
Definition palette.h:158
std::vector< ColorElem > mColors
Vector containing the colors.
Definition palette.h:157
GradientType
Colors can be static or can alter over time.
Definition palette.h:53
@ STATIC
Definition palette.h:54
@ PULSE
Definition palette.h:55
@ RAINBOW
Definition palette.h:57
void setColor(int type, int r, int g, int b)
Sets the color for the specified type.
void rollback()
Rollback the colors.
void setGradient(int type, Palette::GradientType grad)
Sets the gradient type for the specified color.
int getColorTypeAt(int i)
Gets the ColorType used by the color for the element at index i in the current color model.
@ HIT_LOCAL_PLAYER_MONSTER
Definition userpalette.h:51
@ HIT_LOCAL_PLAYER_MISS
Definition userpalette.h:53
@ HIT_LOCAL_PLAYER_CRITICAL
Definition userpalette.h:52
std::string getElementAt(int i) override
Returns the name of the ith color.
void commit()
Commit the colors.
void setGradientDelay(int type, int delay)
Sets the gradient delay for the specified color.
int getNumberOfElements() override
Returns the number of colors known.
~UserPalette() override
void addColor(int type, unsigned int rgb, GradientType grad, const std::string &text, int delay=GRADIENT_DELAY)
Initialise color.
Config config
Global settings (config.xml)
Definition client.cpp:97
#define _(s)
Definition gettext.h:38
constexpr int GRADIENT_DELAY
Definition palette.h:36
unsigned int atox(const std::string &str)
Converts an ascii hexidecimal string to an integer.
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.
std::map< std::string, UserColor > colors
GradientType grad
Definition palette.h:134
std::string color
std::optional< int > delay