GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/emoteshortcutcontainer.cpp Lines: 18 118 15.3 %
Date: 2021-03-17 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-2019  The ManaPlus Developers
5
 *  Copyright (C) 2019-2021  Andrei Karas
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/emoteshortcutcontainer.h"
24
25
#include "settings.h"
26
27
#include "input/inputmanager.h"
28
29
#include "gui/viewport.h"
30
31
#include "gui/fonts/font.h"
32
33
#include "gui/shortcut/emoteshortcut.h"
34
35
#include "gui/popups/popupmenu.h"
36
#include "gui/popups/textpopup.h"
37
38
#include "input/inputactionoperators.h"
39
40
#include "resources/emotesprite.h"
41
42
#include "resources/db/emotedb.h"
43
44
#include "resources/image/image.h"
45
46
#include "resources/sprite/animatedsprite.h"
47
48
#include "utils/stringutils.h"
49
50
#include "debug.h"
51
52
static const int MAX_ITEMS = 48;
53
54
1
EmoteShortcutContainer::EmoteShortcutContainer(Widget2 *restrict const
55
1
                                               widget) :
56
    ShortcutContainer(widget),
57
    mEmoteImg(),
58
    mEmoteClicked(false),
59
2
    mEmoteMoved(0)
60
{
61
1
    if (mBackgroundImg != nullptr)
62
1
        mBackgroundImg->setAlpha(settings.guiAlpha);
63
64
    // Setup emote sprites
65

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

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