GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/userpalette.h Lines: 7 20 35.0 %
Date: 2017-11-29 Branches: 1 2 50.0 %

Line Branch Exec Source
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
#ifndef GUI_USERPALETTE_H
25
#define GUI_USERPALETTE_H
26
27
#include "logger.h"
28
29
#include "enums/gui/usercolorid.h"
30
31
#include "gui/palette.h"
32
33
#include "gui/models/listmodel.h"
34
35
/**
36
 * Class controlling the game's color palette.
37
 */
38
class UserPalette final : public Palette, public ListModel
39
{
40
    public:
41
        /**
42
         * Constructor
43
         */
44
        UserPalette();
45
46
        A_DELETE_COPY(UserPalette)
47
48
        /**
49
         * Destructor
50
         */
51
        ~UserPalette();
52
53
        /**
54
         * Gets the committed color associated with the specified type.
55
         *
56
         * @param type the color type requested
57
         *
58
         * @return the requested committed color
59
         */
60
        inline const Color &getCommittedColor(const UserColorIdT type)
61
                                              const A_WARN_UNUSED
62
        {
63
            return mColors[CAST_SIZE(type)].committedColor;
64
        }
65
66
        /**
67
         * Gets the test color associated with the specified type.
68
         *
69
         * @param type the color type requested
70
         *
71
         * @return the requested test color
72
         */
73
        inline const Color &getTestColor(const UserColorIdT type)
74
                                         const A_WARN_UNUSED
75
        { return mColors[CAST_SIZE(type)].testColor; }
76
77
        /**
78
         * Sets the test color associated with the specified type.
79
         *
80
         * @param type the color type requested
81
         * @param color the color that should be tested
82
         */
83
        inline void setTestColor(const UserColorIdT type,
84
                                 const Color &color)
85
        { mColors[CAST_SIZE(type)].testColor = color; }
86
87
        /**
88
         * Sets the color for the specified type.
89
         *
90
         * @param type color to be set
91
         * @param r red component
92
         * @param g green component
93
         * @param b blue component
94
         */
95
        void setColor(const UserColorIdT type,
96
                      const int r,
97
                      const int g,
98
                      const int b);
99
100
        /**
101
         * Sets the gradient type for the specified color.
102
         *
103
         * @param grad gradient type to set
104
         */
105
        void setGradient(const UserColorIdT type,
106
                         const GradientTypeT grad);
107
108
        void setGradientDelay(const UserColorIdT type,
109
                              const int delay)
110
        { mColors[CAST_SIZE(type)].delay = delay; }
111
112
        /**
113
         * Returns the number of colors known.
114
         *
115
         * @return the number of colors known
116
         */
117
4
        inline int getNumberOfElements() override final A_WARN_UNUSED
118
8
        { return CAST_S32(mColors.size()); }
119
120
        /**
121
         * Returns the name of the ith color.
122
         *
123
         * @param i index of color interested in
124
         *
125
         * @return the name of the color
126
         */
127
        std::string getElementAt(int i) override final A_WARN_UNUSED;
128
129
        /**
130
         * Commit the colors
131
         */
132
        inline void commit()
133
        { commit(false); }
134
135
        /**
136
         * Rollback the colors
137
         */
138
        void rollback();
139
140
        /**
141
         * Gets the ColorType used by the color for the element at index i in
142
         * the current color model.
143
         *
144
         * @param i the index of the color
145
         *
146
         * @return the color type of the color with the given index
147
         */
148
        int getColorTypeAt(const int i) A_WARN_UNUSED;
149
150
        /**
151
         * Gets the color associated with the type. Sets the alpha channel
152
         * before returning.
153
         *
154
         * @param type the color type requested
155
         * @param alpha alpha channel to use
156
         *
157
         * @return the requested color
158
         */
159
276
        inline const Color &getColor(UserColorIdT type,
160
                                     const unsigned int alpha = 255U)
161
                                     A_WARN_UNUSED
162
        {
163
552
            if (CAST_SIZE(type) >= mColors.size())
164
            {
165
                logger->log("incorrect color request type: %d from %u",
166
                    CAST_S32(type),
167
                    CAST_U32(mColors.size()));
168
                type = UserColorId::BEING;
169
            }
170
552
            Color* col = &mColors[CAST_SIZE(type)].color;
171
276
            col->a = alpha;
172
276
            return *col;
173
        }
174
175
        int getIdByChar(const signed char c, bool &valid) const A_WARN_UNUSED;
176
177
        /**
178
         * Gets the GradientType associated with the specified type.
179
         *
180
         * @param type the color type of the color
181
         *
182
         * @return the gradient type of the color with the given index
183
         */
184
        inline GradientTypeT getGradientType(const UserColorIdT type)
185
                                             const A_WARN_UNUSED
186
        { return mColors[CAST_SIZE(type)].grad; }
187
188
        /**
189
         * Gets the gradient delay for the specified type.
190
         *
191
         * @param type the color type of the color
192
         *
193
         * @return the gradient delay of the color with the given index
194
         */
195
        inline int getGradientDelay(const UserColorIdT type)
196
                                    const A_WARN_UNUSED
197
        { return mColors[CAST_SIZE(type)].delay; }
198
199
        inline const Color &getColorWithAlpha(const UserColorIdT type)
200
                                              A_WARN_UNUSED
201
        {
202
            Color *const col = &mColors[CAST_SIZE(type)].color;
203
            col->a = CAST_U32(
204
                mColors[CAST_SIZE(type)].delay);
205
            return *col;
206
        }
207
208
    private:
209
        /**
210
         * Define a color replacement.
211
         *
212
         * @param i the index of the color to replace
213
         * @param r red component
214
         * @param g green component
215
         * @param b blue component
216
         */
217
        void setColorAt(int i, int r, int g, int b);
218
219
        /**
220
         * Commit the colors. Commit the non-static color values, if
221
         * commitNonStatic is true. Only needed in the constructor.
222
         */
223
        void commit(const bool commitNonStatic);
224
225
        /**
226
         * Prefixes the given string with "Color", lowercases all letters but
227
         * the first and all following a '_'. All '_'s will be removed.
228
         *
229
         * E.g.: HIT_PLAYER_MONSTER -> HitPlayerMonster
230
         *
231
         * @param typeName string to transform
232
         *
233
         * @return the transformed string
234
         */
235
        static std::string getConfigName(const std::string &typeName)
236
                                         A_WARN_UNUSED;
237
238
        void addColor(const UserColorIdT type,
239
                      const unsigned rgb,
240
                      GradientTypeT grad,
241
                      const std::string &text,
242
                      int delay = GRADIENT_DELAY);
243
244
        void addLabel(const UserColorIdT type,
245
                      const std::string &text);
246
};
247
248
extern UserPalette *userPalette;
249
250
#endif  // GUI_USERPALETTE_H