GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/shortcutcontainer.cpp Lines: 30 66 45.5 %
Date: 2021-03-17 Branches: 17 58 29.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2007-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
#include "gui/widgets/shortcutcontainer.h"
25
26
#include "settings.h"
27
28
#include "gui/gui.h"
29
#include "gui/skin.h"
30
31
#include "utils/delete2.h"
32
33
#include "resources/image/image.h"
34
35
#include "render/graphics.h"
36
37
#include "render/vertexes/imagecollection.h"
38
39
#include "debug.h"
40
41
float ShortcutContainer::mAlpha = 1.0;
42
43
1
ShortcutContainer::ShortcutContainer(Widget2 *const widget) :
44
    Widget(widget),
45
    WidgetListener(),
46
    MouseListener(),
47
    mBackgroundImg(nullptr),
48
    mSkin(nullptr),
49
    mMaxItems(0),
50
    mBoxWidth(1),
51
    mBoxHeight(1),
52
    mGridWidth(1),
53
    mGridHeight(1),
54
    mImageOffsetX(2),
55
    mImageOffsetY(2),
56
    mTextOffsetX(2),
57
    mTextOffsetY(2),
58

3
    mVertexes(new ImageCollection)
59
{
60
1
    mAllowLogic = false;
61
62
1
    addMouseListener(this);
63
1
    addWidgetListener(this);
64
65
2
    mForegroundColor = getThemeColor(ThemeColorId::TEXT, 255U);
66
2
    mForegroundColor2 = getThemeColor(ThemeColorId::TEXT_OUTLINE, 255U);
67
68

7
    mBackgroundImg = Theme::getImageFromThemeXml(
69
        "item_shortcut_background.xml", "background.xml");
70
71
1
    if (mBackgroundImg != nullptr)
72
    {
73
1
        mBackgroundImg->setAlpha(settings.guiAlpha);
74
2
        mBoxHeight = mBackgroundImg->getHeight();
75
2
        mBoxWidth = mBackgroundImg->getWidth();
76
    }
77
    else
78
    {
79
        mBoxHeight = 1;
80
        mBoxWidth = 1;
81
    }
82
1
}
83
84
4
ShortcutContainer::~ShortcutContainer()
85
{
86
1
    if (mBackgroundImg != nullptr)
87
    {
88
1
        mBackgroundImg->decRef();
89
1
        mBackgroundImg = nullptr;
90
    }
91
92
1
    if (gui != nullptr)
93
1
        gui->removeDragged(this);
94
95
1
    delete2(mVertexes)
96
1
}
97
98
void ShortcutContainer::widgetResized(const Event &event A_UNUSED)
99
{
100
    mGridWidth = mDimension.width / mBoxWidth;
101
102
    if (mGridWidth < 1)
103
        mGridWidth = 1;
104
105
    mGridHeight = mMaxItems / CAST_U32(mGridWidth);
106
107
    if (mMaxItems % mGridWidth != 0 || mGridHeight < 1)
108
        ++mGridHeight;
109
110
    setHeight(mGridHeight * mBoxHeight);
111
    mRedraw = true;
112
}
113
114
int ShortcutContainer::getIndexFromGrid(const int pointX,
115
                                        const int pointY) const
116
{
117
    const Rect tRect = Rect(0, 0,
118
        mGridWidth * mBoxWidth, mGridHeight * mBoxHeight);
119
120
    int index = ((pointY / mBoxHeight) * mGridWidth) + pointX / mBoxWidth;
121
122
    if (!tRect.isPointInRect(pointX, pointY) ||
123
        index >= CAST_S32(mMaxItems) || index < 0)
124
    {
125
        index = -1;
126
    }
127
128
    return index;
129
}
130
131
void ShortcutContainer::drawBackground(Graphics *const g)
132
{
133
    if (mBackgroundImg != nullptr)
134
    {
135
        if (mRedraw)
136
        {
137
            mRedraw = false;
138
            mVertexes->clear();
139
            for (unsigned i = 0; i < mMaxItems; i ++)
140
            {
141
                g->calcTileCollection(mVertexes, mBackgroundImg,
142
                    (i % mGridWidth) * mBoxWidth,
143
                    (i / mGridWidth) * mBoxHeight);
144
            }
145
            g->finalize(mVertexes);
146
        }
147
        g->drawTileCollection(mVertexes);
148
    }
149
}
150
151
void ShortcutContainer::safeDrawBackground(Graphics *const g)
152
{
153
    if (mBackgroundImg != nullptr)
154
    {
155
        for (unsigned i = 0; i < mMaxItems; i ++)
156
        {
157
            g->drawImage(mBackgroundImg, (i % mGridWidth) * mBoxWidth,
158
                (i / mGridWidth) * mBoxHeight);
159
        }
160
    }
161
}
162
163
void ShortcutContainer::widgetMoved(const Event& event A_UNUSED)
164
{
165
    mRedraw = true;
166
}
167
168
1
void ShortcutContainer::setSkin(const Widget2 *const widget,
169
                                Skin *const skin)
170
{
171
1
    setWidget2(widget);
172
1
    mSkin = skin;
173
1
    if (mSkin)
174
    {
175
4
        mImageOffsetX = mSkin->getOption("imageOffsetX", 2);
176
4
        mImageOffsetY = mSkin->getOption("imageOffsetY", 2);
177
4
        mTextOffsetX = mSkin->getOption("textOffsetX", 2);
178
4
        mTextOffsetY = mSkin->getOption("textOffsetY", 2);
179
    }
180
1
}