GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/icon.cpp Lines: 19 38 50.0 %
Date: 2017-11-29 Branches: 8 22 36.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "gui/widgets/icon.h"
24
25
#include "gui/gui.h"
26
27
#include "render/graphics.h"
28
29
#include "resources/image/image.h"
30
31
#include "resources/loaders/imageloader.h"
32
33
#include "debug.h"
34
35
Icon::Icon(const Widget2 *const widget,
36
           const std::string &file,
37
           const AutoRelease autoRelease) :
38
    Widget(widget),
39
    mImage(Loader::getImage(file)),
40
    mAutoRelease(autoRelease)
41
{
42
    if (mImage != nullptr)
43
    {
44
        const SDL_Rect &bounds = mImage->mBounds;
45
        setSize(bounds.w, bounds.h);
46
    }
47
    mAllowLogic = false;
48
}
49
50
6
Icon::Icon(const Widget2 *const widget,
51
           Image *const image,
52
6
           const AutoRelease autoRelease) :
53
    Widget(widget),
54
    mImage(image),
55
6
    mAutoRelease(autoRelease)
56
{
57
6
    if (mImage != nullptr)
58
    {
59
2
        const SDL_Rect &bounds = mImage->mBounds;
60
2
        setSize(bounds.w, bounds.h);
61
    }
62
6
    mAllowLogic = false;
63
6
}
64
65
18
Icon::~Icon()
66
{
67
6
    if (gui != nullptr)
68
6
        gui->removeDragged(this);
69

6
    if ((mImage != nullptr) && mAutoRelease == AutoRelease_true)
70
        mImage->decRef();
71
12
}
72
73
void Icon::setImage(Image *const image)
74
{
75
    mImage = image;
76
    if (mImage != nullptr)
77
    {
78
        const SDL_Rect &bounds = mImage->mBounds;
79
        setSize(bounds.w, bounds.h);
80
    }
81
}
82
83
2
void Icon::draw(Graphics *const graphics)
84
{
85
    BLOCK_START("Icon::draw")
86
2
    if (mImage != nullptr)
87
    {
88
4
        graphics->drawImage(mImage,
89
2
            (mDimension.width - mImage->mBounds.w) / 2,
90
2
            (mDimension.height - mImage->mBounds.h) / 2);
91
    }
92
    BLOCK_END("Icon::draw")
93
2
}
94
95
void Icon::safeDraw(Graphics *const graphics)
96
{
97
    BLOCK_START("Icon::draw")
98
    if (mImage != nullptr)
99
    {
100
        graphics->drawImage(mImage,
101
            (mDimension.width - mImage->mBounds.w) / 2,
102
            (mDimension.height - mImage->mBounds.h) / 2);
103
    }
104
    BLOCK_END("Icon::draw")
105
}