GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/skin.cpp Lines: 59 60 98.3 %
Date: 2017-11-29 Branches: 41 56 73.2 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008  The Legend of Mazzeroth Development Team
4
 *  Copyright (C) 2009  Aethyra Development Team
5
 *  Copyright (C) 2009  The Mana World Development Team
6
 *  Copyright (C) 2009-2010  The Mana Developers
7
 *  Copyright (C) 2011-2017  The ManaPlus Developers
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
#include "gui/skin.h"
26
27
#include "settings.h"
28
29
#include "enums/resources/imageposition.h"
30
31
#include "resources/imagerect.h"
32
33
#include "resources/image/image.h"
34
35
#include "utils/delete2.h"
36
37
#include "debug.h"
38
39
1642
Skin::Skin(ImageRect *const restrict skin,
40
           const ImageRect *const restrict images,
41
           const std::string &filePath, const std::string &name,
42
           const int padding, const int titlePadding,
43
1642
           StringIntMap *restrict const options) :
44
    instances(1),
45
    mFilePath(filePath),
46
    mName(name),
47
    mBorder(skin),
48
1642
    mCloseImage(images != nullptr ? images->grid[0] : nullptr),
49
1642
    mCloseImageHighlighted(images != nullptr ? images->grid[1] : nullptr),
50
1642
    mStickyImageUp(images != nullptr ? images->grid[2] : nullptr),
51
1642
    mStickyImageDown(images != nullptr ? images->grid[3] : nullptr),
52
    mPadding(padding),
53
    mTitlePadding(titlePadding),
54
11494
    mOptions(options)
55
{
56
1642
    if (mCloseImageHighlighted == nullptr)
57
    {
58
1528
        mCloseImageHighlighted = mCloseImage;
59
1528
        if (mCloseImageHighlighted != nullptr)
60
            mCloseImageHighlighted->incRef();
61
    }
62
1642
}
63
64
6568
Skin::~Skin()
65
{
66
16420
    for (int i = 0; i < 9; i++)
67
    {
68

14778
        if ((mBorder != nullptr) && (mBorder->grid[i] != nullptr))
69
        {
70
9234
            mBorder->grid[i]->decRef();
71
9234
            mBorder->grid[i] = nullptr;
72
        }
73
    }
74
75
1642
    if (mCloseImage != nullptr)
76
    {
77
114
        mCloseImage->decRef();
78
114
        mCloseImage = nullptr;
79
    }
80
81
1642
    if (mCloseImageHighlighted != nullptr)
82
    {
83
114
        mCloseImageHighlighted->decRef();
84
114
        mCloseImageHighlighted = nullptr;
85
    }
86
87
1642
    if (mStickyImageUp != nullptr)
88
    {
89
88
        mStickyImageUp->decRef();
90
88
        mStickyImageUp = nullptr;
91
    }
92
93
1642
    if (mStickyImageDown != nullptr)
94
    {
95
88
        mStickyImageDown->decRef();
96
88
        mStickyImageDown = nullptr;
97
    }
98
99
3284
    delete2(mOptions);
100
1642
    delete2(mBorder);
101
1642
}
102
103
1642
void Skin::updateAlpha(const float minimumOpacityAllowed)
104
{
105
    const float alpha = static_cast<float>(
106
4926
        std::max(static_cast<double>(minimumOpacityAllowed),
107
6568
        static_cast<double>(settings.guiAlpha)));
108
109
1642
    if (mBorder != nullptr)
110
    {
111
31198
        for (int i = 0; i < 9; i++)
112
        {
113
14778
            if (mBorder->grid[i] != nullptr)
114
9234
                mBorder->grid[i]->setAlpha(alpha);
115
        }
116
    }
117
118
1642
    if (mCloseImage != nullptr)
119
114
        mCloseImage->setAlpha(alpha);
120
1642
    if (mCloseImageHighlighted != nullptr)
121
114
        mCloseImageHighlighted->setAlpha(alpha);
122
1642
    if (mStickyImageUp != nullptr)
123
88
        mStickyImageUp->setAlpha(alpha);
124
1642
    if (mStickyImageDown != nullptr)
125
88
        mStickyImageDown->setAlpha(alpha);
126
1642
}
127
128
80
int Skin::getMinWidth() const
129
{
130

160
    if ((mBorder == nullptr) ||
131
156
        (mBorder->grid[ImagePosition::UPPER_LEFT] == nullptr) ||
132
76
        (mBorder->grid[ImagePosition::UPPER_RIGHT] == nullptr))
133
    {
134
        return 1;
135
    }
136
137
152
    return mBorder->grid[ImagePosition::UPPER_LEFT]->getWidth() +
138
152
           mBorder->grid[ImagePosition::UPPER_RIGHT]->getWidth();
139
}
140
141
94
int Skin::getMinHeight() const
142
{
143

188
    if ((mBorder == nullptr) ||
144
188
        (mBorder->grid[ImagePosition::UPPER_LEFT] == nullptr) ||
145
94
        (mBorder->grid[ImagePosition::LOWER_LEFT] == nullptr))
146
    {
147
        return 1;
148
    }
149
150
188
    return mBorder->grid[ImagePosition::UPPER_LEFT]->getHeight() +
151
188
           mBorder->grid[ImagePosition::LOWER_LEFT]->getHeight();
152
}