GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/shortcutcontainer.cpp Lines: 30 66 45.5 %
Date: 2018-11-12 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-2018  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/shortcutcontainer.h"
24
25
#include "settings.h"
26
27
#include "gui/gui.h"
28
#include "gui/skin.h"
29
30
#include "utils/delete2.h"
31
32
#include "resources/image/image.h"
33
34
#include "render/graphics.h"
35
36
#include "render/vertexes/imagecollection.h"
37
38
#include "debug.h"
39
40
float ShortcutContainer::mAlpha = 1.0;
41
42
1
ShortcutContainer::ShortcutContainer(Widget2 *const widget) :
43
    Widget(widget),
44
    WidgetListener(),
45
    MouseListener(),
46
    mBackgroundImg(nullptr),
47
    mSkin(nullptr),
48
    mMaxItems(0),
49
    mBoxWidth(1),
50
    mBoxHeight(1),
51
    mGridWidth(1),
52
    mGridHeight(1),
53
    mImageOffsetX(2),
54
    mImageOffsetY(2),
55
    mTextOffsetX(2),
56
    mTextOffsetY(2),
57

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

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