GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/emoteshortcutcontainer.cpp Lines: 18 118 15.3 %
Date: 2018-11-12 Branches: 7 110 6.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  Aethyra Development Team
4
 *  Copyright (C) 2011-2018  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
1
EmoteShortcutContainer::EmoteShortcutContainer(Widget2 *restrict const
54
1
                                               widget) :
55
    ShortcutContainer(widget),
56
    mEmoteImg(),
57
    mEmoteClicked(false),
58
2
    mEmoteMoved(0)
59
{
60
1
    if (mBackgroundImg != nullptr)
61
1
        mBackgroundImg->setAlpha(settings.guiAlpha);
62
63
    // Setup emote sprites
64

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

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