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