GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/skin.cpp Lines: 59 60 98.3 %
Date: 2021-03-17 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-2019  The ManaPlus Developers
8
 *  Copyright (C) 2019-2021  Andrei Karas
9
 *
10
 *  This file is part of The ManaPlus Client.
11
 *
12
 *  This program is free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  any later version.
16
 *
17
 *  This program is distributed in the hope that it will be useful,
18
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 *  GNU General Public License for more details.
21
 *
22
 *  You should have received a copy of the GNU General Public License
23
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
#include "gui/skin.h"
27
28
#include "settings.h"
29
30
#include "enums/resources/imageposition.h"
31
32
#include "resources/imagerect.h"
33
34
#include "resources/image/image.h"
35
36
#include "utils/delete2.h"
37
38
#include "debug.h"
39
40
838
Skin::Skin(ImageRect *const restrict skin,
41
           const ImageRect *const restrict images,
42
           const std::string &filePath, const std::string &name,
43
           const int padding, const int titlePadding,
44
838
           StringIntMap *restrict const options) :
45
    instances(1),
46
    mFilePath(filePath),
47
    mName(name),
48
    mBorder(skin),
49
838
    mCloseImage(images != nullptr ? images->grid[0] : nullptr),
50
838
    mCloseImageHighlighted(images != nullptr ? images->grid[1] : nullptr),
51
838
    mStickyImageUp(images != nullptr ? images->grid[2] : nullptr),
52
838
    mStickyImageDown(images != nullptr ? images->grid[3] : nullptr),
53
    mPadding(padding),
54
    mTitlePadding(titlePadding),
55
5866
    mOptions(options)
56
{
57
838
    if (mCloseImageHighlighted == nullptr)
58
    {
59
781
        mCloseImageHighlighted = mCloseImage;
60
781
        if (mCloseImageHighlighted != nullptr)
61
            mCloseImageHighlighted->incRef();
62
    }
63
838
}
64
65
3352
Skin::~Skin()
66
{
67
8380
    for (int i = 0; i < 9; i++)
68
    {
69

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

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

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