GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/radiobutton.cpp Lines: 105 142 73.9 %
Date: 2021-03-17 Branches: 56 110 50.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
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
/*      _______   __   __   __   ______   __   __   _______   __   __
25
 *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
26
 *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
27
 *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
28
 *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
29
 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
30
 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
31
 *
32
 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
33
 *
34
 *
35
 * Per Larsson a.k.a finalman
36
 * Olof Naessén a.k.a jansem/yakslem
37
 *
38
 * Visit: http://guichan.sourceforge.net
39
 *
40
 * License: (BSD)
41
 * Redistribution and use in source and binary forms, with or without
42
 * modification, are permitted provided that the following conditions
43
 * are met:
44
 * 1. Redistributions of source code must retain the above copyright
45
 *    notice, this list of conditions and the following disclaimer.
46
 * 2. Redistributions in binary form must reproduce the above copyright
47
 *    notice, this list of conditions and the following disclaimer in
48
 *    the documentation and/or other materials provided with the
49
 *    distribution.
50
 * 3. Neither the name of Guichan nor the names of its contributors may
51
 *    be used to endorse or promote products derived from this software
52
 *    without specific prior written permission.
53
 *
54
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
60
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65
 */
66
67
#include "gui/widgets/radiobutton.h"
68
69
#include "settings.h"
70
71
#include "gui/gui.h"
72
#include "gui/skin.h"
73
74
#include "gui/fonts/font.h"
75
76
#include "render/graphics.h"
77
78
#include "resources/imagerect.h"
79
80
#include "resources/image/image.h"
81
82
#include "debug.h"
83
84
int RadioButton::instances = 0;
85
Skin *RadioButton::mSkin = nullptr;
86
float RadioButton::mAlpha = 1.0;
87
88
1
RadioButton::GroupMap RadioButton::mGroupMap;
89
90
8
RadioButton::RadioButton(const Widget2 *const widget,
91
                         const std::string &restrict caption,
92
                         const std::string &restrict group,
93
8
                         const bool marked) :
94
    Widget(widget),
95
    MouseListener(),
96
    KeyListener(),
97
    WidgetListener(),
98
    mSelected(false),
99
    mCaption(),
100
    mGroup(),
101
    mTextChunk(),
102
    mPadding(0),
103
    mImagePadding(0),
104
    mImageSize(9),
105
    mSpacing(2),
106
    mTextX(0 + 9 + 2),
107
    mHasMouse(false),
108
48
    mTextChanged(true)
109
{
110
8
    mAllowLogic = false;
111
8
    setCaption(caption);
112
8
    setGroup(group);
113
8
    setSelected(marked);
114
115
8
    setFocusable(true);
116
8
    addMouseListener(this);
117
8
    addKeyListener(this);
118
119
16
    mForegroundColor = getThemeColor(ThemeColorId::RADIOBUTTON, 255U);
120
16
    mForegroundColor2 = getThemeColor(ThemeColorId::RADIOBUTTON_OUTLINE, 255U);
121
8
    if (instances == 0)
122
    {
123
3
        if (theme != nullptr)
124
        {
125

21
            mSkin = theme->load("radio.xml",
126
                "",
127
                true,
128
3
                Theme::getThemePath());
129
3
            updateAlpha();
130
        }
131
    }
132
133
8
    instances++;
134
135
8
    if (mSkin != nullptr)
136
    {
137
16
        mPadding = mSkin->getPadding();
138

32
        mImagePadding = mSkin->getOption("imagePadding");
139

24
        mImageSize = mSkin->getOption("imageSize");
140

32
        mSpacing = mSkin->getOption("spacing");
141
8
        mTextX = mPadding + mImageSize + mSpacing;
142
    }
143
144
8
    adjustSize();
145
8
}
146
147
64
RadioButton::~RadioButton()
148
{
149
8
    if (mWindow != nullptr)
150
8
        mWindow->removeWidgetListener(this);
151
152
16
    setGroup(std::string());
153
154
8
    if (gui != nullptr)
155
8
        gui->removeDragged(this);
156
157
8
    instances--;
158
159
8
    if (instances == 0)
160
    {
161
3
        if (theme != nullptr)
162
3
            theme->unload(mSkin);
163
    }
164
16
}
165
166
8
void RadioButton::updateAlpha()
167
{
168
    const float alpha = std::max(settings.guiAlpha,
169
24
        theme->getMinimumOpacity());
170
171
8
    if (mAlpha != alpha)
172
    {
173
        mAlpha = alpha;
174
        if (mSkin != nullptr)
175
        {
176
            const ImageRect &rect = mSkin->getBorder();
177
            for (int a = 0; a < 4; a ++)
178
            {
179
                Image *const image = rect.grid[a];
180
                if (image != nullptr)
181
                    image->setAlpha(mAlpha);
182
            }
183
        }
184
    }
185
8
}
186
187
5
void RadioButton::drawBox(Graphics *const graphics)
188
{
189
5
    if (mSkin == nullptr)
190
        return;
191
192
10
    const ImageRect &rect = mSkin->getBorder();
193
5
    int index = 0;
194
195

5
    if (mEnabled && mVisible == Visible_true)
196
    {
197
5
        if (mSelected)
198
        {
199
3
            if (mHasMouse)
200
                index = 1;
201
            else
202
3
                index = 0;
203
        }
204
        else
205
        {
206
2
            if (mHasMouse)
207
                index = 3;
208
            else
209
2
                index = 2;
210
        }
211
    }
212
    else
213
    {
214
        if (mSelected)
215
            index = 0;
216
        else
217
            index = 2;
218
    }
219
220
5
    const Image *const box = rect.grid[index];
221
222
5
    updateAlpha();
223
224
5
    if (box != nullptr)
225
    {
226
5
        graphics->drawImage(box,
227
            mImagePadding,
228
15
            (getHeight() - mImageSize) / 2);
229
    }
230
}
231
232
5
void RadioButton::draw(Graphics *const graphics)
233
{
234
    BLOCK_START("RadioButton::draw")
235
5
    drawBox(graphics);
236
237
5
    Font *const font = getFont();
238
239
5
    if (mTextChanged)
240
    {
241
5
        mTextChunk.textFont = font;
242
5
        mTextChunk.deleteImage();
243
10
        mTextChunk.text = mCaption;
244
5
        mTextChunk.color = mForegroundColor;
245
5
        mTextChunk.color2 = mForegroundColor2;
246
5
        font->generate(mTextChunk);
247
5
        mTextChanged = false;
248
    }
249
250
5
    const Image *const image = mTextChunk.img;
251
5
    if (image != nullptr)
252
5
        graphics->drawImage(image, mTextX, mPadding);
253
254
    BLOCK_END("RadioButton::draw")
255
5
}
256
257
void RadioButton::safeDraw(Graphics *const graphics)
258
{
259
    RadioButton::draw(graphics);
260
}
261
262
void RadioButton::mouseEntered(MouseEvent& event A_UNUSED)
263
{
264
    mHasMouse = true;
265
}
266
267
void RadioButton::mouseExited(MouseEvent& event A_UNUSED)
268
{
269
    mHasMouse = false;
270
}
271
272
void RadioButton::keyPressed(KeyEvent& event)
273
{
274
    const InputActionT action = event.getActionId();
275
    if (action == InputAction::GUI_SELECT)
276
    {
277
        setSelected(true);
278
        distributeActionEvent();
279
        event.consume();
280
    }
281
}
282
283
8
void RadioButton::adjustSize()
284
{
285
8
    Font *const font = getFont();
286
8
    setHeight(font->getHeight() + 2 * mPadding);
287
16
    setWidth(mImagePadding + mImageSize + mSpacing
288
16
        + font->getWidth(mCaption) + mPadding);
289
8
}
290
291
9
void RadioButton::setSelected(const bool selected)
292
{
293

12
    if (selected && !mGroup.empty())
294
    {
295
16
        for (GroupIterator iter = mGroupMap.lower_bound(mGroup),
296
6
             iterEnd = mGroupMap.upper_bound(mGroup);
297
             iter != iterEnd;
298
             ++ iter)
299
        {
300

14
            if ((iter->second != nullptr) && iter->second->isSelected())
301
                iter->second->setSelected(false);
302
        }
303
    }
304
305
9
    mSelected = selected;
306
9
}
307
308
void RadioButton::mouseClicked(MouseEvent& event)
309
{
310
    if (event.getButton() == MouseButton::LEFT)
311
    {
312
        setSelected(true);
313
        event.consume();
314
        distributeActionEvent();
315
    }
316
}
317
318
void RadioButton::mouseDragged(MouseEvent& event)
319
{
320
    event.consume();
321
}
322
323
16
void RadioButton::setGroup(const std::string &group)
324
{
325
32
    if (!mGroup.empty())
326
    {
327
25
        for (GroupIterator iter = mGroupMap.lower_bound(mGroup),
328
16
             iterEnd = mGroupMap.upper_bound(mGroup);
329
             iter != iterEnd;
330
             ++ iter)
331
        {
332
9
            if (iter->second == this)
333
            {
334
                mGroupMap.erase(iter);
335
                break;
336
            }
337
        }
338
    }
339
340
16
    if (!group.empty())
341
32
        mGroupMap.insert(std::pair<std::string, RadioButton *>(group, this));
342
343
32
    mGroup = group;
344
16
}
345
346
8
void RadioButton::setCaption(const std::string& caption)
347
{
348
16
    if (caption != mCaption)
349
8
        mTextChanged = true;
350
16
    mCaption = caption;
351
8
}
352
353
5
void RadioButton::setParent(Widget *widget)
354
{
355
5
    if (mWindow != nullptr)
356
5
        mWindow->addWidgetListener(this);
357
10
    Widget::setParent(widget);
358
5
}
359
360
void RadioButton::widgetHidden(const Event &event A_UNUSED)
361
{
362
    mTextChanged = true;
363
    mTextChunk.deleteImage();
364
}
365
366
void RadioButton::setWindow(Widget *const widget)
367
{
368
    if ((widget == nullptr) && (mWindow != nullptr))
369
    {
370
        mWindow->removeWidgetListener(this);
371
        mWindow = nullptr;
372
    }
373
    else
374
    {
375
        Widget2::setWindow(widget);
376
    }
377
2
}