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

6
    mVertexes(new ImageCollection)
52
{
53
2
    mAllowLogic = false;
54
55
2
    addMouseListener(this);
56
2
    addWidgetListener(this);
57
58
4
    mForegroundColor = getThemeColor(ThemeColorId::TEXT);
59
4
    mForegroundColor2 = getThemeColor(ThemeColorId::TEXT_OUTLINE);
60
61

14
    mBackgroundImg = Theme::getImageFromThemeXml(
62
        "item_shortcut_background.xml", "background.xml");
63
64
2
    if (mBackgroundImg != nullptr)
65
    {
66
2
        mBackgroundImg->setAlpha(settings.guiAlpha);
67
4
        mBoxHeight = mBackgroundImg->getHeight();
68
4
        mBoxWidth = mBackgroundImg->getWidth();
69
    }
70
    else
71
    {
72
        mBoxHeight = 1;
73
        mBoxWidth = 1;
74
    }
75
2
}
76
77
8
ShortcutContainer::~ShortcutContainer()
78
{
79
2
    if (mBackgroundImg != nullptr)
80
    {
81
2
        mBackgroundImg->decRef();
82
2
        mBackgroundImg = nullptr;
83
    }
84
85
2
    if (gui != nullptr)
86
2
        gui->removeDragged(this);
87
88
2
    delete2(mVertexes);
89
2
}
90
91
void ShortcutContainer::widgetResized(const Event &event A_UNUSED)
92
{
93
    mGridWidth = mDimension.width / mBoxWidth;
94
95
    if (mGridWidth < 1)
96
        mGridWidth = 1;
97
98
    mGridHeight = mMaxItems / CAST_U32(mGridWidth);
99
100
    if (mMaxItems % mGridWidth != 0 || mGridHeight < 1)
101
        ++mGridHeight;
102
103
    setHeight(mGridHeight * mBoxHeight);
104
    mRedraw = true;
105
}
106
107
int ShortcutContainer::getIndexFromGrid(const int pointX,
108
                                        const int pointY) const
109
{
110
    const Rect tRect = Rect(0, 0,
111
        mGridWidth * mBoxWidth, mGridHeight * mBoxHeight);
112
113
    int index = ((pointY / mBoxHeight) * mGridWidth) + pointX / mBoxWidth;
114
115
    if (!tRect.isPointInRect(pointX, pointY) ||
116
        index >= CAST_S32(mMaxItems) || index < 0)
117
    {
118
        index = -1;
119
    }
120
121
    return index;
122
}
123
124
void ShortcutContainer::drawBackground(Graphics *const g)
125
{
126
    if (mBackgroundImg != nullptr)
127
    {
128
        if (mRedraw)
129
        {
130
            mRedraw = false;
131
            mVertexes->clear();
132
            for (unsigned i = 0; i < mMaxItems; i ++)
133
            {
134
                g->calcTileCollection(mVertexes, mBackgroundImg,
135
                    (i % mGridWidth) * mBoxWidth,
136
                    (i / mGridWidth) * mBoxHeight);
137
            }
138
            g->finalize(mVertexes);
139
        }
140
        g->drawTileCollection(mVertexes);
141
    }
142
}
143
144
void ShortcutContainer::safeDrawBackground(Graphics *const g)
145
{
146
    if (mBackgroundImg != nullptr)
147
    {
148
        for (unsigned i = 0; i < mMaxItems; i ++)
149
        {
150
            g->drawImage(mBackgroundImg, (i % mGridWidth) * mBoxWidth,
151
                (i / mGridWidth) * mBoxHeight);
152
        }
153
    }
154
}
155
156
void ShortcutContainer::widgetMoved(const Event& event A_UNUSED)
157
{
158
    mRedraw = true;
159
}