GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/setup_colors.cpp Lines: 106 253 41.9 %
Date: 2017-11-29 Branches: 141 360 39.2 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008  Douglas Boffey <[email protected]>
4
 *  Copyright (C) 2011-2017  The ManaPlus Developers
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "gui/widgets/tabs/setup_colors.h"
23
24
#include "gui/gui.h"
25
#include "gui/userpalette.h"
26
27
#include "gui/fonts/font.h"
28
29
#include "gui/widgets/browserbox.h"
30
#include "gui/widgets/containerplacer.h"
31
#include "gui/widgets/createwidget.h"
32
#include "gui/widgets/label.h"
33
#include "gui/widgets/layouthelper.h"
34
#include "gui/widgets/listbox.h"
35
#include "gui/widgets/scrollarea.h"
36
#include "gui/widgets/slider.h"
37
#include "gui/widgets/textfield.h"
38
#include "gui/widgets/textpreview.h"
39
40
#include "utils/delete2.h"
41
#include "utils/gettext.h"
42
#include "utils/stringutils.h"
43
44
#include <cmath>
45
46
#include "debug.h"
47
48
const char *const Setup_Colors::rawmsg =
49
    // TRANSLATORS: color selection preview message
50
    N_("This is what the color looks like");
51
52
4
Setup_Colors::Setup_Colors(const Widget2 *const widget) :
53
    SetupTab(widget),
54
    SelectionListener(),
55


16
    mColorBox(CREATEWIDGETR(ListBox, this, userPalette, "")),
56
4
    mScroll(new ScrollArea(this, mColorBox,
57

8
        Opaque_true, "setup_colors_background.xml")),
58
    mPreview(new BrowserBox(this, Opaque_true,
59

4
        "browserbox.xml")),
60

8
    mTextPreview(new TextPreview(this, gettext(rawmsg))),
61
4
    mPreviewBox(new ScrollArea(this, mPreview, Opaque_true,
62

8
        "setup_colors_preview_background.xml")),
63
    mSelected(-1),
64
    // TRANSLATORS: colors tab. label.
65

8
    mGradTypeLabel(new Label(this, _("Type:"))),
66

4
    mGradTypeSlider(new Slider(this, 0.0, 3.0, 1.0)),
67

4
    mGradTypeText(new Label(this)),
68
    // TRANSLATORS: colors tab. label.
69

8
    mGradDelayLabel(new Label(this, _("Delay:"))),
70

4
    mGradDelaySlider(new Slider(this, 20.0, 100.0, 1.0)),
71

4
    mGradDelayText(new TextField(this)),
72
    // TRANSLATORS: colors tab. label.
73

8
    mRedLabel(new Label(this, _("Red:"))),
74

4
    mRedSlider(new Slider(this, 0.0, 255.0, 1.0)),
75

4
    mRedText(new TextField(this)),
76
    // TRANSLATORS: colors tab. label.
77

8
    mGreenLabel(new Label(this, _("Green:"))),
78

4
    mGreenSlider(new Slider(this, 0.0, 255.0, 1.0)),
79

4
    mGreenText(new TextField(this)),
80
    // TRANSLATORS: colors tab. label.
81

8
    mBlueLabel(new Label(this, _("Blue:"))),
82

4
    mBlueSlider(new Slider(this, 0.0, 255.0, 1.0)),
83









292
    mBlueText(new TextField(this))
84
{
85
    // TRANSLATORS: settings colors tab name
86
20
    setName(_("Colors"));
87
4
    mColorBox->addSelectionListener(this);
88
4
    mScroll->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
89
8
    mPreview->setOpaque(Opaque_false);
90
91
    // don't do anything with links
92
4
    mPreview->setLinkHandler(nullptr);
93
94
4
    mPreviewBox->setHeight(20);
95
4
    mPreviewBox->setScrollPolicy(ScrollArea::SHOW_NEVER,
96
        ScrollArea::SHOW_NEVER);
97
98
4
    mGradTypeSlider->setWidth(180);
99
20
    mGradTypeSlider->setActionEventId("slider_grad");
100
4
    mGradTypeSlider->setValue(0);
101
4
    mGradTypeSlider->addActionListener(this);
102
8
    mGradTypeSlider->setEnabled(false);
103
104
    // TRANSLATORS: color type
105
16
    std::string longText = _("Static");
106
107
4
    const Font *const font = getFont();
108
    // TRANSLATORS: color type
109


16
    if (getFont()->getWidth(_("Pulse")) > font->getWidth(longText))
110
    {
111
        // TRANSLATORS: color type
112
        longText = _("Pulse");
113
    }
114
    // TRANSLATORS: color type
115


16
    if (getFont()->getWidth(_("Rainbow")) > font->getWidth(longText))
116
    {
117
        // TRANSLATORS: color type
118
4
        longText = _("Rainbow");
119
    }
120
    // TRANSLATORS: color type
121


16
    if (getFont()->getWidth(_("Spectrum")) > font->getWidth(longText))
122
    {
123
        // TRANSLATORS: color type
124
4
        longText = _("Spectrum");
125
    }
126
127
4
    mGradTypeText->setCaption(longText);
128
129
4
    mGradDelayText->setWidth(40);
130
8
    mGradDelayText->setRange(20, 100);
131
4
    mGradDelayText->setNumeric(true);
132
8
    mGradDelayText->setEnabled(false);
133
134
4
    mGradDelaySlider->setWidth(180);
135

4
    mGradDelaySlider->setValue(mGradDelayText->getValue());
136
20
    mGradDelaySlider->setActionEventId("slider_graddelay");
137
4
    mGradDelaySlider->addActionListener(this);
138
8
    mGradDelaySlider->setEnabled(false);
139
140
4
    mRedText->setWidth(40);
141
8
    mRedText->setRange(0, 255);
142
4
    mRedText->setNumeric(true);
143
8
    mRedText->setEnabled(false);
144
145
4
    mRedSlider->setWidth(180);
146

4
    mRedSlider->setValue(mRedText->getValue());
147
20
    mRedSlider->setActionEventId("slider_red");
148
4
    mRedSlider->addActionListener(this);
149
8
    mRedSlider->setEnabled(false);
150
151
4
    mGreenText->setWidth(40);
152
8
    mGreenText->setRange(0, 255);
153
4
    mGreenText->setNumeric(true);
154
8
    mGreenText->setEnabled(false);
155
156
4
    mGreenSlider->setWidth(180);
157

4
    mGreenSlider->setValue(mGreenText->getValue());
158
20
    mGreenSlider->setActionEventId("slider_green");
159
4
    mGreenSlider->addActionListener(this);
160
8
    mGreenSlider->setEnabled(false);
161
162
4
    mBlueText->setWidth(40);
163
8
    mBlueText->setRange(0, 255);
164
4
    mBlueText->setNumeric(true);
165
8
    mBlueText->setEnabled(false);
166
167
4
    mBlueSlider->setWidth(180);
168

4
    mBlueSlider->setValue(mBlueText->getValue());
169
20
    mBlueSlider->setActionEventId("slider_blue");
170
4
    mBlueSlider->addActionListener(this);
171
8
    mBlueSlider->setEnabled(false);
172
173
8
    setOpaque(Opaque_false);
174
175
    // Do the layout
176
8
    LayoutHelper h(this);
177
4
    ContainerPlacer place = h.getPlacer(0, 0);
178
179
8
    place(0, 0, mScroll, 6, 6).setPadding(2);
180
8
    place(0, 6, mPreviewBox, 6).setPadding(2);
181
4
    place(0, 7, mGradTypeLabel, 3);
182
4
    place(3, 7, mGradTypeSlider);
183
8
    place(4, 7, mGradTypeText, 2).setPadding(1);
184
4
    place(0, 8, mRedLabel, 3);
185
4
    place(3, 8, mRedSlider);
186
8
    place(5, 8, mRedText).setPadding(1);
187
4
    place(0, 9, mGreenLabel, 3);
188
4
    place(3, 9, mGreenSlider);
189
8
    place(5, 9, mGreenText).setPadding(1);
190
4
    place(0, 10, mBlueLabel, 3);
191
4
    place(3, 10, mBlueSlider);
192
8
    place(5, 10, mBlueText).setPadding(1);
193
4
    place(0, 11, mGradDelayLabel, 3);
194
4
    place(3, 11, mGradDelaySlider);
195
8
    place(5, 11, mGradDelayText).setPadding(1);
196
197

16
    mGradTypeText->setCaption("");
198
199
8
    setDimension(Rect(0, 0, 365, 350));
200
4
}
201
202
16
Setup_Colors::~Setup_Colors()
203
{
204

4
    if ((mPreviewBox != nullptr) && mPreviewBox->getContent() == mPreview)
205
8
        delete2(mTextPreview)
206
    else
207
        delete2(mPreview)
208
8
}
209
210
void Setup_Colors::action(const ActionEvent &event)
211
{
212
    const std::string &eventId = event.getId();
213
    if (eventId == "slider_grad")
214
    {
215
        updateGradType();
216
        updateColor();
217
        return;
218
    }
219
220
    if (eventId == "slider_graddelay")
221
    {
222
        mGradDelayText->setText(toString(
223
            std::floor(mGradDelaySlider->getValue())));
224
        updateColor();
225
        return;
226
    }
227
    if (eventId == "slider_red")
228
    {
229
        mRedText->setText(toString(std::floor(mRedSlider->getValue())));
230
        updateColor();
231
        return;
232
    }
233
    if (eventId == "slider_green")
234
    {
235
        mGreenText->setText(toString(std::floor(mGreenSlider->getValue())));
236
        updateColor();
237
        return;
238
    }
239
    if (eventId == "slider_blue")
240
    {
241
        mBlueText->setText(toString(std::floor(mBlueSlider->getValue())));
242
        updateColor();
243
        return;
244
    }
245
}
246
247
void Setup_Colors::valueChanged(const SelectionEvent &event A_UNUSED)
248
{
249
    if (userPalette == nullptr)
250
        return;
251
252
    mSelected = mColorBox->getSelected();
253
    const UserColorIdT type = static_cast<UserColorIdT>(
254
        userPalette->getColorTypeAt(mSelected));
255
    const Color *col = &userPalette->getColor(type);
256
    const GradientTypeT grad = userPalette->getGradientType(type);
257
    const int delay = userPalette->getGradientDelay(type);
258
    const Visible showControls = fromBool(grad != GradientType::LABEL,
259
        Visible);
260
    mPreview->setVisible(showControls);
261
    mPreviewBox->setVisible(showControls);
262
    mTextPreview->setVisible(showControls);
263
    mGradTypeLabel->setVisible(showControls);
264
    mGradTypeSlider->setVisible(showControls);
265
    mGradTypeText->setVisible(showControls);
266
    mGradDelayLabel->setVisible(showControls);
267
    mGradDelaySlider->setVisible(showControls);
268
    mGradDelayText->setVisible(showControls);
269
    mRedLabel->setVisible(showControls);
270
    mRedSlider->setVisible(showControls);
271
    mRedText->setVisible(showControls);
272
    mGreenLabel->setVisible(showControls);
273
    mGreenSlider->setVisible(showControls);
274
    mGreenText->setVisible(showControls);
275
    mBlueLabel->setVisible(showControls);
276
    mBlueSlider->setVisible(showControls);
277
    mBlueText->setVisible(showControls);
278
279
    mPreview->clearRows();
280
    mPreviewBox->setContent(mTextPreview);
281
    mTextPreview->setFont(boldFont);
282
    mTextPreview->setTextColor(col);
283
    mTextPreview->setTextBGColor(nullptr);
284
    mTextPreview->setOpaque(Opaque_false);
285
    mTextPreview->setShadow(true);
286
    mTextPreview->setOutline(true);
287
    mTextPreview->useTextAlpha(false);
288
289
// probably need combite both switches and add all mssing color ids.
290
291
    PRAGMA45(GCC diagnostic push)
292
    PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
293
    switch (type)
294
    {
295
        case UserColorId::AIR_COLLISION_HIGHLIGHT:
296
        case UserColorId::WATER_COLLISION_HIGHLIGHT:
297
        case UserColorId::MONSTER_COLLISION_HIGHLIGHT:
298
        case UserColorId::GROUNDTOP_COLLISION_HIGHLIGHT:
299
        case UserColorId::COLLISION_HIGHLIGHT:
300
        case UserColorId::PORTAL_HIGHLIGHT:
301
        case UserColorId::HOME_PLACE:
302
        case UserColorId::ROAD_POINT:
303
        case UserColorId::NET:
304
            mTextPreview->setBGColor(col);
305
            mTextPreview->setOpaque(Opaque_true);
306
            mTextPreview->setOutline(false);
307
            mTextPreview->setShadow(false);
308
            break;
309
        case UserColorId::ATTACK_RANGE_BORDER:
310
        case UserColorId::HOME_PLACE_BORDER:
311
            if (gui != nullptr)
312
                mTextPreview->setFont(gui->getFont());
313
            mTextPreview->setTextColor(col);
314
            mTextPreview->setOutline(false);
315
            mTextPreview->setShadow(false);
316
            break;
317
        case UserColorId::PARTICLE:
318
        case UserColorId::EXP_INFO:
319
        case UserColorId::PICKUP_INFO:
320
        case UserColorId::HIT_PLAYER_MONSTER:
321
        case UserColorId::HIT_MONSTER_PLAYER:
322
        case UserColorId::HIT_CRITICAL:
323
        case UserColorId::MISS:
324
        case UserColorId::HIT_LOCAL_PLAYER_MONSTER:
325
        case UserColorId::HIT_LOCAL_PLAYER_CRITICAL:
326
        case UserColorId::HIT_LOCAL_PLAYER_MISS:
327
        case UserColorId::ATTACK_RANGE:
328
        case UserColorId::MONSTER_ATTACK_RANGE:
329
        case UserColorId::FLOOR_ITEM_TEXT:
330
        case UserColorId::SKILL_ATTACK_RANGE:
331
            mTextPreview->setShadow(false);
332
            break;
333
        default:
334
            break;
335
    }
336
337
    switch (type)
338
    {
339
        case UserColorId::PORTAL_HIGHLIGHT:
340
        case UserColorId::ATTACK_RANGE:
341
        case UserColorId::ATTACK_RANGE_BORDER:
342
        case UserColorId::MONSTER_ATTACK_RANGE:
343
        case UserColorId::SKILL_ATTACK_RANGE:
344
        case UserColorId::HOME_PLACE:
345
        case UserColorId::HOME_PLACE_BORDER:
346
        case UserColorId::AIR_COLLISION_HIGHLIGHT:
347
        case UserColorId::WATER_COLLISION_HIGHLIGHT:
348
        case UserColorId::MONSTER_COLLISION_HIGHLIGHT:
349
        case UserColorId::GROUNDTOP_COLLISION_HIGHLIGHT:
350
        case UserColorId::COLLISION_HIGHLIGHT:
351
        case UserColorId::WALKABLE_HIGHLIGHT:
352
        case UserColorId::ROAD_POINT:
353
        case UserColorId::MONSTER_HP:
354
        case UserColorId::MONSTER_HP2:
355
        case UserColorId::HOMUN_HP:
356
        case UserColorId::HOMUN_HP2:
357
        case UserColorId::MERC_HP:
358
        case UserColorId::MERC_HP2:
359
        case UserColorId::ELEMENTAL_HP:
360
        case UserColorId::ELEMENTAL_HP2:
361
        case UserColorId::PLAYER_HP:
362
        case UserColorId::PLAYER_HP2:
363
        case UserColorId::FLOOR_ITEM_TEXT:
364
        case UserColorId::NET:
365
            // TRANSLATORS: colors tab. label.
366
            mGradDelayLabel->setCaption(_("Alpha:"));
367
            mGradDelayText->setRange(0, 255);
368
            mGradDelaySlider->setScale(0, 255);
369
            break;
370
        default:
371
            // TRANSLATORS: colors tab. label.
372
            mGradDelayLabel->setCaption(_("Delay:"));
373
            mGradDelayText->setRange(20, 100);
374
            mGradDelaySlider->setScale(20, 100);
375
            break;
376
    }
377
    PRAGMA45(GCC diagnostic pop)
378
    if (grad != GradientType::STATIC && grad != GradientType::PULSE)
379
    { // If nonstatic color, don't display the current, but the committed
380
      // color at the sliders
381
        col = &userPalette->getCommittedColor(type);
382
    }
383
    else if (grad == GradientType::PULSE)
384
    {
385
        col = &userPalette->getTestColor(type);
386
    }
387
388
    setEntry(mGradDelaySlider, mGradDelayText, delay);
389
    setEntry(mRedSlider, mRedText, col->r);
390
    setEntry(mGreenSlider, mGreenText, col->g);
391
    setEntry(mBlueSlider, mBlueText, col->b);
392
393
    mGradTypeSlider->setValue(CAST_S32(grad));
394
    updateGradType();
395
    mGradTypeSlider->setEnabled(true);
396
}
397
398
void Setup_Colors::setEntry(Slider *const s, TextField *const t,
399
                            const int value)
400
{
401
    if (s != nullptr)
402
        s->setValue(value);
403
    if (t != nullptr)
404
        t->setText(toString(value));
405
}
406
407
void Setup_Colors::apply()
408
{
409
    if (userPalette != nullptr)
410
        userPalette->commit();
411
}
412
413
void Setup_Colors::cancel()
414
{
415
    if (userPalette == nullptr)
416
        return;
417
418
    userPalette->rollback();
419
    const UserColorIdT type = static_cast<UserColorIdT>(
420
        userPalette->getColorTypeAt(mSelected));
421
    const Color *const col = &userPalette->getColor(type);
422
    mGradTypeSlider->setValue(CAST_S32(
423
        userPalette->getGradientType(type)));
424
    const int delay = userPalette->getGradientDelay(type);
425
    setEntry(mGradDelaySlider, mGradDelayText, delay);
426
    setEntry(mRedSlider, mRedText, col->r);
427
    setEntry(mGreenSlider, mGreenText, col->g);
428
    setEntry(mBlueSlider, mBlueText, col->b);
429
}
430
431
void Setup_Colors::updateGradType()
432
{
433
    if (mSelected == -1 || (userPalette == nullptr))
434
        return;
435
436
    mSelected = mColorBox->getSelected();
437
    const UserColorIdT type = static_cast<UserColorIdT>(
438
        userPalette->getColorTypeAt(mSelected));
439
    const GradientTypeT grad = userPalette->getGradientType(type);
440
441
    mGradTypeText->setCaption(
442
        // TRANSLATORS: color type
443
        (grad == GradientType::STATIC) ? _("Static") :
444
        // TRANSLATORS: color type
445
        (grad == GradientType::PULSE) ? _("Pulse") :
446
        // TRANSLATORS: color type
447
        (grad == GradientType::RAINBOW) ? _("Rainbow") : _("Spectrum"));
448
449
    const bool enable = (grad == GradientType::STATIC ||
450
        grad == GradientType::PULSE);
451
    const bool delayEnable = true;
452
453
    mGradDelayText->setEnabled(delayEnable);
454
    mGradDelaySlider->setEnabled(delayEnable);
455
456
    mRedText->setEnabled(enable);
457
    mRedSlider->setEnabled(enable);
458
    mGreenText->setEnabled(enable);
459
    mGreenSlider->setEnabled(enable);
460
    mBlueText->setEnabled(enable);
461
    mBlueSlider->setEnabled(enable);
462
}
463
464
void Setup_Colors::updateColor() const
465
{
466
    if (mSelected == -1 || (userPalette == nullptr))
467
        return;
468
469
    const UserColorIdT type = static_cast<UserColorIdT>(
470
        userPalette->getColorTypeAt(mSelected));
471
    const GradientTypeT grad = static_cast<GradientTypeT>(
472
        CAST_S32(mGradTypeSlider->getValue()));
473
    const int delay = CAST_S32(mGradDelaySlider->getValue());
474
    userPalette->setGradient(type, grad);
475
    userPalette->setGradientDelay(type, delay);
476
477
    if (grad == GradientType::STATIC)
478
    {
479
        userPalette->setColor(type,
480
                CAST_S32(mRedSlider->getValue()),
481
                CAST_S32(mGreenSlider->getValue()),
482
                CAST_S32(mBlueSlider->getValue()));
483
    }
484
    else if (grad == GradientType::PULSE)
485
    {
486
        userPalette->setTestColor(type, Color(
487
                CAST_S32(mRedSlider->getValue()),
488
                CAST_S32(mGreenSlider->getValue()),
489
                CAST_S32(mBlueSlider->getValue())));
490
    }
491
}