GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/emotepage.cpp Lines: 17 65 26.2 %
Date: 2017-11-29 Branches: 8 42 19.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "gui/widgets/emotepage.h"
22
23
#include "render/graphics.h"
24
25
#include "render/vertexes/imagecollection.h"
26
27
#include "resources/imageset.h"
28
29
#include "resources/loaders/imagesetloader.h"
30
31
#include "utils/delete2.h"
32
#include "utils/foreach.h"
33
34
#include "debug.h"
35
36
namespace
37
{
38
    const unsigned int emoteWidth = 17;
39
    const unsigned int emoteHeight = 18;
40
}  // namespace
41
42
2
EmotePage::EmotePage(const Widget2 *const widget) :
43
    Widget(widget),
44
    MouseListener(),
45
    WidgetListener(),
46

8
    mEmotes(Loader::getImageSet(
47
        "graphics/sprites/chatemotes.png", emoteWidth, emoteHeight)),
48

2
    mVertexes(new ImageCollection),
49
10
    mSelectedIndex(-1)
50
{
51
2
    addMouseListener(this);
52
2
    addWidgetListener(this);
53
2
    mAllowLogic = false;
54
2
}
55
56
10
EmotePage::~EmotePage()
57
{
58
2
    if (mEmotes != nullptr)
59
    {
60
2
        mEmotes->decRef();
61
2
        mEmotes = nullptr;
62
    }
63
2
    delete2(mVertexes);
64
4
}
65
66
void EmotePage::draw(Graphics *const graphics)
67
{
68
    BLOCK_START("EmotePage::draw")
69
70
    if (mRedraw)
71
    {
72
        if (mEmotes == nullptr)
73
            return;
74
75
        const STD_VECTOR<Image*> &images = mEmotes->getImages();
76
77
        const unsigned int width = mDimension.width;
78
        unsigned int x = 0;
79
        unsigned int y = 0;
80
81
        mRedraw = false;
82
        mVertexes->clear();
83
        FOR_EACH (STD_VECTOR<Image*>::const_iterator, it, images)
84
        {
85
            const Image *const image = *it;
86
            if (image != nullptr)
87
            {
88
                graphics->calcTileCollection(mVertexes, image, x, y);
89
                x += emoteWidth;
90
                if (x + emoteWidth > width)
91
                {
92
                    x = 0;
93
                    y += emoteHeight;
94
                }
95
            }
96
        }
97
        graphics->finalize(mVertexes);
98
    }
99
    graphics->drawTileCollection(mVertexes);
100
101
    BLOCK_END("EmotePage::draw")
102
}
103
104
void EmotePage::safeDraw(Graphics *const graphics)
105
{
106
    BLOCK_START("EmotePage::safeDraw")
107
108
    if (mEmotes == nullptr)
109
        return;
110
111
    const STD_VECTOR<Image*> &images = mEmotes->getImages();
112
113
    const unsigned int width = mDimension.width;
114
    unsigned int x = 0;
115
    unsigned int y = 0;
116
117
    FOR_EACH (STD_VECTOR<Image*>::const_iterator, it, images)
118
    {
119
        const Image *const image = *it;
120
        if (image != nullptr)
121
        {
122
            graphics->drawImage(image, x, y);
123
            x += emoteWidth;
124
            if (x + emoteWidth > width)
125
            {
126
                x = 0;
127
                y += emoteHeight;
128
            }
129
        }
130
    }
131
132
    BLOCK_END("EmotePage::safeDraw")
133
}
134
135
void EmotePage::mousePressed(MouseEvent &event)
136
{
137
    mSelectedIndex = getIndexFromGrid(event.getX(), event.getY());
138
    event.consume();
139
    distributeActionEvent();
140
}
141
142
int EmotePage::getIndexFromGrid(const int x, const int y) const
143
{
144
    const int width = mDimension.width;
145
    if (x < 0 || x > width || y < 0 || y > mDimension.height)
146
        return -1;
147
    const int cols = width / emoteWidth;
148
    const int index = (y / emoteHeight) * cols + (x / emoteWidth);
149
    if (index >= CAST_S32(mEmotes->size()))
150
        return -1;
151
    return index;
152
}
153
154
void EmotePage::resetAction()
155
{
156
    mSelectedIndex = -1;
157
}
158
159
12
void EmotePage::widgetResized(const Event &event A_UNUSED)
160
{
161
12
    mRedraw = true;
162
12
}
163
164
void EmotePage::widgetMoved(const Event &event A_UNUSED)
165
{
166
    mRedraw = true;
167
}