GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/skin.cpp Lines: 59 60 98.3 %
Date: 2018-09-20 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-2018  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
838
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
838
           StringIntMap *restrict const options) :
44
    instances(1),
45
    mFilePath(filePath),
46
    mName(name),
47
    mBorder(skin),
48
838
    mCloseImage(images != nullptr ? images->grid[0] : nullptr),
49
838
    mCloseImageHighlighted(images != nullptr ? images->grid[1] : nullptr),
50
838
    mStickyImageUp(images != nullptr ? images->grid[2] : nullptr),
51
838
    mStickyImageDown(images != nullptr ? images->grid[3] : nullptr),
52
    mPadding(padding),
53
    mTitlePadding(titlePadding),
54
5866
    mOptions(options)
55
{
56
838
    if (mCloseImageHighlighted == nullptr)
57
    {
58
781
        mCloseImageHighlighted = mCloseImage;
59
781
        if (mCloseImageHighlighted != nullptr)
60
            mCloseImageHighlighted->incRef();
61
    }
62
838
}
63
64
3352
Skin::~Skin()
65
{
66
8380
    for (int i = 0; i < 9; i++)
67
    {
68

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

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

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