summaryrefslogtreecommitdiff
path: root/src/gui/userpalette.cpp
blob: d0e6c2c9ab02bdebb32459dcdc4ffba359d0628f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 *  The ManaPlus Client
 *  Copyright (C) 2008  Douglas Boffey <dougaboffey@netscape.net>
 *  Copyright (C) 2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011-2015  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "gui/userpalette.h"

#include "enums/gui/usercolorid.h"

#include "configuration.h"

#include "utils/gettext.h"

#include "debug.h"

UserPalette *userPalette = nullptr;

const std::string ColorTypeNames[] =
{
    "ColorBeing",
    "ColorFriend",
    "ColorDisregarded",
    "ColorIgnored",
    "ColorErased",
    "ColorPlayer",
    "ColorSelf",
    "ColorGM",
    "ColorNPC",
    "ColorMonster",
    "ColorMonsterHp",
    "ColorMonsterHp2",
    "ColorParty",
    "ColorGuild",
    "ColorParticle",
    "ColorPickupInfo",
    "ColorExpInfo",
    "ColorPlayerHp",
    "ColorPlayerHp2",
    "ColorHitPlayerMonster",
    "ColorHitMonsterPlayer",
    "ColorHitPlayerPlayer",
    "ColorHitCritical",
    "ColorHitLocalPlayerMonster",
    "ColorHitLocalPlayerCritical",
    "ColorHitLocalPlayerMiss",
    "ColorMiss",
    "ColorPortalHighlight",
    "ColorCollisionHighlight",
    "ColorCollisionAirHighlight",
    "ColorCollisionWaterHighlight",
    "ColorCollisionGroundtopHighlight",
    "ColorWalkableTileHighlight",
    "ColorAttackRange",
    "ColorAttackRangeBorder",
    "ColorMonsterAttackRange",
    "ColorFloorItemText",
    "ColorHomePlace",
    "ColorHomePlaceBorder",
    "ColorRoadPoint",
    "ColorNet",
    "ColorPet",
    "ColorMercenary",
    "ColorHomunculus"
};

std::string UserPalette::getConfigName(const std::string &typeName)
{
    std::string res("Color" + typeName);
    size_t pos = 5;
    for (size_t i = 0; i < typeName.length(); i++)
    {
        if (i == 0 || typeName[i] == '_')
        {
            if (i > 0)
                i++;

            res[pos] = typeName[i];
        }
        else
        {
           res[pos] = static_cast<signed char>(tolower(typeName[i]));
        }
        pos ++;
    }
    res.erase(pos, res.length() - pos);
    return res;
}

UserPalette::UserPalette() :
    Palette(UserColorId::USER_COLOR_LAST)
{
    mColors[UserColorId::BEING] = ColorElem();
    mColors[UserColorId::PC] = ColorElem();
    mColors[UserColorId::SELF] = ColorElem();
    mColors[UserColorId::GM] = ColorElem();
    mColors[UserColorId::NPC] = ColorElem();
    mColors[UserColorId::MONSTER] = ColorElem();

    // TRANSLATORS: palette color
    addColor(UserColorId::BEING, 0xffffff, GradientType::STATIC, _("Being"));
    // TRANSLATORS: palette color
    addColor(UserColorId::FRIEND, 0xb0ffb0, GradientType::STATIC, _("Friend names"));
    // TRANSLATORS: palette color
    addColor(UserColorId::DISREGARDED, 0xa00000, GradientType::STATIC, _("Disregarded names"));
    // TRANSLATORS: palette color
    addColor(UserColorId::IGNORED, 0xff0000, GradientType::STATIC, _("Ignored names"));
    // TRANSLATORS: palette color
    addColor(UserColorId::ERASED, 0xff0000, GradientType::STATIC, _("Erased names"));
    // TRANSLATORS: palette color
    addColor(UserColorId::PC, 0xffffff, GradientType::STATIC, _("Other players names"));
    // TRANSLATORS: palette color
    addColor(UserColorId::SELF, 0xff8040, GradientType::STATIC, _("Own name"));
    // TRANSLATORS: palette color
    addColor(UserColorId::GM, 0x00ff00, GradientType::STATIC, _("GM names"));
    // TRANSLATORS: palette color
    addColor(UserColorId::NPC, 0xc8c8ff, GradientType::STATIC, _("NPCs"));
    // TRANSLATORS: palette color
    addColor(UserColorId::MONSTER, 0xff4040, GradientType::STATIC, _("Monsters"));
    // TRANSLATORS: palette color
    addColor(UserColorId::MONSTER_HP, 0x00ff00, GradientType::STATIC, _("Monster HP bar"), 50);
    addColor(UserColorId::MONSTER_HP2, 0xff0000, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Monster HP bar (second color)"), 50);
    // TRANSLATORS: palette color
    addColor(UserColorId::PARTY, 0xff00d8, GradientType::STATIC, _("Party members"));
    // TRANSLATORS: palette color
    addColor(UserColorId::GUILD, 0xff00d8, GradientType::STATIC, _("Guild members"));
    // TRANSLATORS: palette color
    addColor(UserColorId::PARTICLE, 0xffffff, GradientType::STATIC, _("Particle effects"));
    // TRANSLATORS: palette color
    addColor(UserColorId::PICKUP_INFO, 0x28dc28, GradientType::STATIC, _("Pickup notification"));
    // TRANSLATORS: palette color
    addColor(UserColorId::EXP_INFO, 0xffff00, GradientType::STATIC, _("Exp notification"));
    // TRANSLATORS: palette color
    addColor(UserColorId::PLAYER_HP, 0x00ff00, GradientType::STATIC, _("Player HP bar"), 50);
    // TRANSLATORS: palette color
    addColor(UserColorId::PLAYER_HP2, 0xff0000, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Player HP bar (second color)"), 50);
    // TRANSLATORS: palette color
    addColor(UserColorId::HIT_PLAYER_MONSTER, 0x0064ff, GradientType::STATIC, _("Player hits monster"));
    // TRANSLATORS: palette color
    addColor(UserColorId::HIT_MONSTER_PLAYER, 0xff3232, GradientType::STATIC, _("Monster hits player"));
    // TRANSLATORS: palette color
    addColor(UserColorId::HIT_PLAYER_PLAYER, 0xff5050, GradientType::STATIC,
       // TRANSLATORS: palette color
       _("Other player hits local player"));
    // TRANSLATORS: palette color
    addColor(UserColorId::HIT_CRITICAL, 0xff0000, GradientType::RAINBOW, _("Critical Hit"));
    // TRANSLATORS: palette color
    addColor(UserColorId::HIT_LOCAL_PLAYER_MONSTER, 0x00ff00, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Local player hits monster"));
    addColor(UserColorId::HIT_LOCAL_PLAYER_CRITICAL, 0xff0000, GradientType::RAINBOW,
        // TRANSLATORS: palette color
        _("Local player critical hit"));
    addColor(UserColorId::HIT_LOCAL_PLAYER_MISS, 0x00ffa6, GradientType::STATIC,
    // TRANSLATORS: palette color
        _("Local player miss"));
    // TRANSLATORS: palette color
    addColor(UserColorId::MISS, 0xffff00, GradientType::STATIC, _("Misses"));
    // TRANSLATORS: palette color
    addColor(UserColorId::PORTAL_HIGHLIGHT, 0xC80000, GradientType::STATIC, _("Portal highlight"));
    addColor(UserColorId::COLLISION_HIGHLIGHT, 0x0000C8, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Default collision highlight"), 64);
    addColor(UserColorId::AIR_COLLISION_HIGHLIGHT, 0xe0e0ff, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Air collision highlight"), 64);
    addColor(UserColorId::WATER_COLLISION_HIGHLIGHT, 0x2050e0, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Water collision highlight"), 64);
    addColor(UserColorId::GROUNDTOP_COLLISION_HIGHLIGHT, 0xffff00, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Special ground collision highlight"), 20);
    addColor(UserColorId::WALKABLE_HIGHLIGHT, 0x00D000, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Walkable highlight"), 255);
    addColor(UserColorId::ATTACK_RANGE, 0xffffff, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Local player attack range"), 5);
    addColor(UserColorId::ATTACK_RANGE_BORDER, 0x0, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Local player attack range border"), 76);
    addColor(UserColorId::MONSTER_ATTACK_RANGE, 0xff0000, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Monster attack range"), 20);

    addColor(UserColorId::FLOOR_ITEM_TEXT, 0xffffff, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Floor item amount color"), 100);
    addColor(UserColorId::HOME_PLACE, 0xffffff, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Home place"), 20);
    addColor(UserColorId::HOME_PLACE_BORDER, 0xffff00, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Home place border"), 200);
    addColor(UserColorId::ROAD_POINT, 0x000000, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Road point"), 100);
    addColor(UserColorId::NET, 0x000000, GradientType::STATIC,
        // TRANSLATORS: palette color
        _("Tiles border"), 64);
    // TRANSLATORS: palette color
    addColor(UserColorId::PET, 0xffffff, GradientType::STATIC, _("Pets"));
    // TRANSLATORS: palette color
    addColor(UserColorId::MERCENARY, 0xffffff, GradientType::STATIC, _("Mercenary"));
    // TRANSLATORS: palette color
    addColor(UserColorId::HOMUNCULUS, 0xffffff, GradientType::STATIC, _("Homunculus"));
    commit(true);
}

UserPalette::~UserPalette()
{
    FOR_EACH (Colors::const_iterator, col, mColors)
    {
        const std::string &configName = ColorTypeNames[col->type];
        config.setValue(configName + "Gradient",
            static_cast<int>(col->committedGrad));
        config.setValue(configName + "Delay", col->delay);

        if (col->grad == GradientType::STATIC ||
            col->grad == GradientType::PULSE)
        {
            char buffer[20];
            snprintf(buffer, sizeof(buffer), "0x%06x", col->getRGB());
            buffer[19] = 0;
            config.setValue(configName, std::string(buffer));
        }
    }
}

void UserPalette::setColor(const int type,
                           const int r,
                           const int g,
                           const int b)
{
    Color &color = mColors[type].color;
    color.r = r;
    color.g = g;
    color.b = b;
}

void UserPalette::setGradient(const int type, const GradientTypeT grad)
{
    ColorElem *const elem = &mColors[type];

    if (elem->grad != GradientType::STATIC && grad == GradientType::STATIC)
    {
        const size_t sz = mGradVector.size();
        for (size_t i = 0; i < sz; i++)
        {
            if (mGradVector[i] == elem)
            {
                mGradVector.erase(mGradVector.begin() + i);
                break;
            }
        }
    }
    else if (elem->grad == GradientType::STATIC &&
             grad != GradientType::STATIC)
    {
        mGradVector.push_back(elem);
    }

    if (elem->grad != grad)
        elem->grad = grad;
}

std::string UserPalette::getElementAt(int i)
{
    if (i < 0 || i >= getNumberOfElements())
        return "";

    return mColors[i].text;
}

void UserPalette::commit(const bool commitNonStatic)
{
    FOR_EACH (Colors::iterator, i, mColors)
    {
        i->committedGrad = i->grad;
        i->committedDelay = i->delay;
        if (commitNonStatic || i->grad == GradientType::STATIC)
            i->committedColor = i->color;
        else if (i->grad == GradientType::PULSE)
            i->committedColor = i->testColor;
    }
}

void UserPalette::rollback()
{
    FOR_EACH (Colors::iterator, i, mColors)
    {
        if (i->grad != i->committedGrad)
            setGradient(i->type, i->committedGrad);

        const Color &committedColor = i->committedColor;
        setGradientDelay(i->type, i->committedDelay);
        setColor(i->type, committedColor.r,
                 committedColor.g, committedColor.b);

        if (i->grad == GradientType::PULSE)
        {
            Color &testColor = i->testColor;
            testColor.r = committedColor.r;
            testColor.g = committedColor.g;
            testColor.b = committedColor.b;
        }
    }
}

int UserPalette::getColorTypeAt(const int i)
{
    if (i < 0 || i >= getNumberOfElements())
        return UserColorId::BEING;

    return mColors[i].type;
}

void UserPalette::addColor(const unsigned type,
                           const unsigned rgb,
                           GradientTypeT grad,
                           const std::string &text,
                           int delay)
{
    const unsigned maxType = sizeof(ColorTypeNames)
        / sizeof(ColorTypeNames[0]);

    if (type >= maxType)
        return;

    const std::string &configName = ColorTypeNames[type];
    char buffer[20];
    snprintf(buffer, sizeof(buffer), "0x%06x", rgb);
    buffer[19] = 0;

    const std::string rgbString = config.getValue(
        configName, std::string(buffer));
    unsigned int rgbValue = 0;
    if (rgbString.length() == 8 && rgbString[0] == '0' && rgbString[1] == 'x')
        rgbValue = atox(rgbString);
    else
        rgbValue = atoi(rgbString.c_str());
    const Color &trueCol = Color(rgbValue);
    grad = static_cast<GradientTypeT>(config.getValue(
        configName + "Gradient",
        static_cast<int>(grad)));
    delay = config.getValueInt(configName + "Delay", delay);
    mColors[type].set(type, trueCol, grad, delay);
    mColors[type].text = text;

    if (grad != GradientType::STATIC)
        mGradVector.push_back(&mColors[type]);
}