GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/emoteshortcutcontainer.cpp Lines: 18 114 15.8 %
Date: 2017-11-29 Branches: 7 106 6.6 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  Aethyra Development Team
4
 *  Copyright (C) 2011-2017  The ManaPlus Developers
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "gui/widgets/emoteshortcutcontainer.h"
23
24
#include "settings.h"
25
26
#include "input/inputmanager.h"
27
28
#include "gui/viewport.h"
29
30
#include "gui/fonts/font.h"
31
32
#include "gui/shortcut/emoteshortcut.h"
33
34
#include "gui/popups/popupmenu.h"
35
#include "gui/popups/textpopup.h"
36
37
#include "input/inputactionoperators.h"
38
39
#include "resources/emotesprite.h"
40
41
#include "resources/db/emotedb.h"
42
43
#include "resources/image/image.h"
44
45
#include "resources/sprite/animatedsprite.h"
46
47
#include "utils/stringutils.h"
48
49
#include "debug.h"
50
51
static const int MAX_ITEMS = 48;
52
53
2
EmoteShortcutContainer::EmoteShortcutContainer(Widget2 *restrict const
54
2
                                               widget) :
55
    ShortcutContainer(widget),
56
    mEmoteImg(),
57
    mEmoteClicked(false),
58
4
    mEmoteMoved(0)
59
{
60
2
    if (mBackgroundImg != nullptr)
61
2
        mBackgroundImg->setAlpha(settings.guiAlpha);
62
63
    // Setup emote sprites
64

6
    for (int i = 0; i <= EmoteDB::getLast(); i++)
65
    {
66
2
        const EmoteSprite *const sprite = EmoteDB::getSprite(i, true);
67

2
        if ((sprite != nullptr) && (sprite->sprite != nullptr))
68
            mEmoteImg.push_back(sprite);
69
    }
70
71
2
    mMaxItems = MAX_ITEMS;
72
2
}
73
74
6
EmoteShortcutContainer::~EmoteShortcutContainer()
75
{
76
4
}
77
78
2
void EmoteShortcutContainer::setWidget2(const Widget2 *restrict const widget)
79
                                        restrict2
80
{
81
4
    Widget2::setWidget2(widget);
82
4
    mForegroundColor = getThemeColor(ThemeColorId::TEXT);
83
4
    mForegroundColor2 = getThemeColor(ThemeColorId::TEXT_OUTLINE);
84
2
}
85
86
void EmoteShortcutContainer::draw(Graphics *restrict graphics) restrict2
87
{
88
    if (emoteShortcut == nullptr)
89
        return;
90
91
    BLOCK_START("EmoteShortcutContainer::draw")
92
    if (settings.guiAlpha != mAlpha)
93
    {
94
        if (mBackgroundImg != nullptr)
95
            mBackgroundImg->setAlpha(mAlpha);
96
        mAlpha = settings.guiAlpha;
97
    }
98
99
    Font *const font = getFont();
100
    drawBackground(graphics);
101
102
    unsigned sz = CAST_U32(mEmoteImg.size());
103
    if (sz > mMaxItems)
104
        sz = mMaxItems;
105
    for (unsigned i = 0; i < sz; i++)
106
    {
107
        const EmoteSprite *restrict const emoteImg = mEmoteImg[i];
108
        if (emoteImg != nullptr)
109
        {
110
            const AnimatedSprite *restrict const sprite = emoteImg->sprite;
111
            if (sprite != nullptr)
112
            {
113
                sprite->draw(graphics,
114
                    (i % mGridWidth) * mBoxWidth + 2,
115
                    (i / mGridWidth) * mBoxHeight + 10);
116
            }
117
        }
118
    }
119
    for (unsigned i = 0; i < mMaxItems; i++)
120
    {
121
        const int emoteX = (i % mGridWidth) * mBoxWidth;
122
        const int emoteY = (i / mGridWidth) * mBoxHeight;
123
124
        // Draw emote keyboard shortcut.
125
        const std::string key = inputManager.getKeyValueString(
126
            InputAction::EMOTE_1 + i);
127
128
        font->drawString(graphics,
129
            mForegroundColor,
130
            mForegroundColor2,
131
            key,
132
            emoteX + 2, emoteY + 2);
133
    }
134
135
    BLOCK_END("EmoteShortcutContainer::draw")
136
}
137
138
void EmoteShortcutContainer::safeDraw(Graphics *restrict graphics) restrict2
139
{
140
    if (emoteShortcut == nullptr)
141
        return;
142
143
    BLOCK_START("EmoteShortcutContainer::draw")
144
    if (settings.guiAlpha != mAlpha)
145
    {
146
        if (mBackgroundImg != nullptr)
147
            mBackgroundImg->setAlpha(mAlpha);
148
        mAlpha = settings.guiAlpha;
149
    }
150
151
    Font *const font = getFont();
152
    safeDrawBackground(graphics);
153
154
    unsigned sz = CAST_U32(mEmoteImg.size());
155
    if (sz > mMaxItems)
156
        sz = mMaxItems;
157
    for (unsigned i = 0; i < sz; i++)
158
    {
159
        const EmoteSprite *restrict const emoteImg = mEmoteImg[i];
160
        if (emoteImg != nullptr)
161
        {
162
            const AnimatedSprite *restrict const sprite = emoteImg->sprite;
163
            if (sprite != nullptr)
164
            {
165
                sprite->draw(graphics,
166
                    (i % mGridWidth) * mBoxWidth + 2,
167
                    (i / mGridWidth) * mBoxHeight + 10);
168
            }
169
        }
170
    }
171
    for (unsigned i = 0; i < mMaxItems; i++)
172
    {
173
        const int emoteX = (i % mGridWidth) * mBoxWidth;
174
        const int emoteY = (i / mGridWidth) * mBoxHeight;
175
176
        // Draw emote keyboard shortcut.
177
        const std::string key = inputManager.getKeyValueString(
178
            InputAction::EMOTE_1 + i);
179
180
        font->drawString(graphics,
181
            mForegroundColor,
182
            mForegroundColor2,
183
            key,
184
            emoteX + 2, emoteY + 2);
185
    }
186
187
    BLOCK_END("EmoteShortcutContainer::draw")
188
}
189
190
void EmoteShortcutContainer::mouseDragged(MouseEvent &restrict event A_UNUSED)
191
                                          restrict2
192
{
193
}
194
195
void EmoteShortcutContainer::mousePressed(MouseEvent &restrict event) restrict2
196
{
197
    if (event.isConsumed())
198
        return;
199
200
    if (event.getButton() == MouseButton::LEFT)
201
    {
202
        if (emoteShortcut == nullptr)
203
            return;
204
205
        const int index = getIndexFromGrid(event.getX(), event.getY());
206
207
        if (index == -1)
208
            return;
209
210
        event.consume();
211
        // Stores the selected emote if there is one.
212
        if (emoteShortcut->isEmoteSelected())
213
        {
214
            emoteShortcut->setEmote(index);
215
            emoteShortcut->setEmoteSelected(0);
216
        }
217
        else if (emoteShortcut->getEmote(index) != 0u)
218
        {
219
            mEmoteClicked = true;
220
        }
221
    }
222
    else if (event.getButton() == MouseButton::RIGHT)
223
    {
224
        if (popupMenu != nullptr)
225
        {
226
            event.consume();
227
            popupMenu->showEmoteType();
228
        }
229
    }
230
}
231
232
void EmoteShortcutContainer::mouseReleased(MouseEvent &restrict event)
233
                                           restrict2
234
{
235
    if (emoteShortcut == nullptr)
236
        return;
237
238
    if (event.getButton() == MouseButton::LEFT)
239
    {
240
        const int index = getIndexFromGrid(event.getX(), event.getY());
241
242
        if (emoteShortcut->isEmoteSelected())
243
            emoteShortcut->setEmoteSelected(0);
244
245
        if (index == -1)
246
        {
247
            mEmoteMoved = 0;
248
            return;
249
        }
250
251
        if (mEmoteMoved != 0u)
252
        {
253
            emoteShortcut->setEmotes(index, mEmoteMoved);
254
            mEmoteMoved = 0;
255
        }
256
        else if ((emoteShortcut->getEmote(index) != 0u) && mEmoteClicked)
257
        {
258
            emoteShortcut->useEmote(index + 1);
259
        }
260
261
        mEmoteClicked = false;
262
    }
263
}
264
265
void EmoteShortcutContainer::mouseMoved(MouseEvent &restrict event) restrict2
266
{
267
    if ((emoteShortcut == nullptr) || (textPopup == nullptr))
268
        return;
269
270
    const int index = getIndexFromGrid(event.getX(), event.getY());
271
272
    if (index == -1)
273
        return;
274
275
    textPopup->setVisible(Visible_false);
276
277
    if (CAST_SIZE(index) < mEmoteImg.size() && (mEmoteImg[index] != nullptr))
278
    {
279
        const EmoteSprite *restrict const sprite = mEmoteImg[index];
280
        textPopup->show(viewport->mMouseX, viewport->mMouseY,
281
            strprintf("%s, %d", sprite->name.c_str(), sprite->id));
282
    }
283
}
284
285
void EmoteShortcutContainer::mouseExited(MouseEvent &restrict event A_UNUSED)
286
                                         restrict2
287
{
288
    if (textPopup != nullptr)
289
        textPopup->setVisible(Visible_false);
290
}
291
292
void EmoteShortcutContainer::widgetHidden(const Event &restrict event A_UNUSED)
293
                                          restrict2
294
{
295
    if (textPopup != nullptr)
296
        textPopup->setVisible(Visible_false);
297
4
}