GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/virtshortcutcontainer.cpp Lines: 1 158 0.6 %
Date: 2021-03-17 Branches: 0 140 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  The Mana World Development Team
4
 *  Copyright (C) 2011-2019  The ManaPlus Developers
5
 *  Copyright (C) 2009-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/virtshortcutcontainer.h"
24
25
#include "dragdrop.h"
26
#include "settings.h"
27
28
#include "being/playerinfo.h"
29
30
#include "gui/viewport.h"
31
32
#include "gui/fonts/font.h"
33
34
#include "gui/shortcut/shortcutbase.h"
35
36
#include "gui/popups/itempopup.h"
37
#include "gui/popups/popupmenu.h"
38
39
#include "gui/windows/inventorywindow.h"
40
41
#include "utils/stringutils.h"
42
43
#include "debug.h"
44
45
VirtShortcutContainer::VirtShortcutContainer(Widget2 *const widget,
46
                                             ShortcutBase *const shortcut) :
47
    ShortcutContainer(widget),
48
    mItemClicked(false),
49
    mEquipedColor(getThemeColor(ThemeColorId::ITEM_EQUIPPED, 255U)),
50
    mEquipedColor2(getThemeColor(ThemeColorId::ITEM_EQUIPPED_OUTLINE, 255U)),
51
    mUnEquipedColor(getThemeColor(ThemeColorId::ITEM_NOT_EQUIPPED, 255U)),
52
    mUnEquipedColor2(getThemeColor(ThemeColorId::ITEM_NOT_EQUIPPED_OUTLINE,
53
        255U)),
54
    mShortcut(shortcut)
55
{
56
    if (mShortcut != nullptr)
57
        mMaxItems = mShortcut->getItemCount();
58
    else
59
        mMaxItems = 0;
60
}
61
62
VirtShortcutContainer::~VirtShortcutContainer()
63
{
64
}
65
66
void VirtShortcutContainer::setSkin(const Widget2 *const widget,
67
                                    Skin *const skin)
68
{
69
    ShortcutContainer::setSkin(widget, skin);
70
    mEquipedColor = getThemeColor(ThemeColorId::ITEM_EQUIPPED, 255U);
71
    mEquipedColor2 = getThemeColor(ThemeColorId::ITEM_EQUIPPED_OUTLINE, 255U);
72
    mUnEquipedColor = getThemeColor(ThemeColorId::ITEM_NOT_EQUIPPED, 255U);
73
    mUnEquipedColor2 = getThemeColor(ThemeColorId::ITEM_NOT_EQUIPPED_OUTLINE,
74
        255U);
75
}
76
77
void VirtShortcutContainer::draw(Graphics *const graphics)
78
{
79
    if (mShortcut == nullptr)
80
        return;
81
82
    BLOCK_START("VirtShortcutContainer::draw")
83
    if (settings.guiAlpha != mAlpha)
84
    {
85
        mAlpha = settings.guiAlpha;
86
        if (mBackgroundImg != nullptr)
87
            mBackgroundImg->setAlpha(mAlpha);
88
    }
89
90
    drawBackground(graphics);
91
92
    const Inventory *const inv = PlayerInfo::getInventory();
93
    if (inv == nullptr)
94
    {
95
        BLOCK_END("VirtShortcutContainer::draw")
96
        return;
97
    }
98
99
    Font *const font = getFont();
100
101
    for (unsigned i = 0; i < mMaxItems; i++)
102
    {
103
        const int itemId = mShortcut->getItem(i);
104
        if (itemId < 0)
105
            continue;
106
107
        const Item *const item = inv->findItem(itemId,
108
            mShortcut->getItemColor(i));
109
110
        if (item != nullptr)
111
        {
112
            const int itemX = (i % mGridWidth) * mBoxWidth;
113
            const int itemY = (i / mGridWidth) * mBoxHeight;
114
115
            // Draw item icon.
116
            Image *const image = item->getImage();
117
118
            if (image != nullptr)
119
            {
120
                std::string caption;
121
                if (item->getQuantity() > 1)
122
                    caption = toString(item->getQuantity());
123
                else if (item->isEquipped() == Equipped_true)
124
                    caption = "Eq.";
125
126
                image->setAlpha(1.0F);
127
                graphics->drawImage(image,
128
                    itemX + mImageOffsetX,
129
                    itemY + mImageOffsetY);
130
                if (item->isEquipped() == Equipped_true)
131
                {
132
                    font->drawString(graphics,
133
                        mEquipedColor,
134
                        mEquipedColor2,
135
                        caption,
136
                        itemX + (mBoxWidth - font->getWidth(caption)) / 2,
137
                        itemY + mBoxHeight - 14);
138
                }
139
                else
140
                {
141
                    font->drawString(graphics,
142
                        mUnEquipedColor,
143
                        mUnEquipedColor2,
144
                        caption,
145
                        itemX + (mBoxWidth - font->getWidth(caption)) / 2,
146
                        itemY + mBoxHeight - 14);
147
                }
148
            }
149
        }
150
    }
151
    BLOCK_END("VirtShortcutContainer::draw")
152
}
153
154
void VirtShortcutContainer::safeDraw(Graphics *const graphics)
155
{
156
    if (mShortcut == nullptr)
157
        return;
158
159
    BLOCK_START("VirtShortcutContainer::safeDraw")
160
    if (settings.guiAlpha != mAlpha)
161
    {
162
        mAlpha = settings.guiAlpha;
163
        if (mBackgroundImg != nullptr)
164
            mBackgroundImg->setAlpha(mAlpha);
165
    }
166
167
    safeDrawBackground(graphics);
168
169
    const Inventory *const inv = PlayerInfo::getInventory();
170
    if (inv == nullptr)
171
    {
172
        BLOCK_END("VirtShortcutContainer::safeDraw")
173
        return;
174
    }
175
176
    Font *const font = getFont();
177
178
    for (unsigned i = 0; i < mMaxItems; i++)
179
    {
180
        const int itemId = mShortcut->getItem(i);
181
        if (itemId < 0)
182
            continue;
183
184
        if (itemId < 0)
185
            continue;
186
187
        const Item *const item = inv->findItem(itemId,
188
            mShortcut->getItemColor(i));
189
190
        if (item != nullptr)
191
        {
192
            // Draw item icon.
193
            Image *const image = item->getImage();
194
195
            if (image != nullptr)
196
            {
197
                const int itemX = (i % mGridWidth) * mBoxWidth;
198
                const int itemY = (i / mGridWidth) * mBoxHeight;
199
200
                std::string caption;
201
                if (item->getQuantity() > 1)
202
                    caption = toString(item->getQuantity());
203
                else if (item->isEquipped() == Equipped_true)
204
                    caption = "Eq.";
205
206
                image->setAlpha(1.0F);
207
                graphics->drawImage(image,
208
                    itemX + mImageOffsetX,
209
                    itemY + mImageOffsetY);
210
                if (item->isEquipped() == Equipped_true)
211
                {
212
                    font->drawString(graphics,
213
                        mEquipedColor,
214
                        mEquipedColor2,
215
                        caption,
216
                        itemX + (mBoxWidth - font->getWidth(caption)) / 2,
217
                        itemY + mBoxHeight - 14);
218
                }
219
                else
220
                {
221
                    font->drawString(graphics,
222
                        mUnEquipedColor,
223
                        mUnEquipedColor2,
224
                        caption,
225
                        itemX + (mBoxWidth - font->getWidth(caption)) / 2,
226
                        itemY + mBoxHeight - 14);
227
                }
228
            }
229
        }
230
    }
231
    BLOCK_END("VirtShortcutContainer::safeDraw")
232
}
233
234
void VirtShortcutContainer::mouseDragged(MouseEvent &event)
235
{
236
    if (mShortcut == nullptr)
237
        return;
238
239
    if (event.getButton() == MouseButton::LEFT)
240
    {
241
        if (dragDrop.isEmpty() && mItemClicked)
242
        {
243
            const int index = getIndexFromGrid(event.getX(), event.getY());
244
245
            if (index == -1)
246
                return;
247
248
            const int itemId = mShortcut->getItem(index);
249
            const ItemColor itemColor = mShortcut->getItemColor(index);
250
251
            if (itemId < 0)
252
                return;
253
254
            const Inventory *const inv = PlayerInfo::getInventory();
255
            if (inv == nullptr)
256
                return;
257
258
            Item *const item = inv->findItem(itemId, itemColor);
259
260
            if (item != nullptr)
261
            {
262
                dragDrop.dragItem(item, DragDropSource::Drop, 0);
263
                mShortcut->removeItem(index);
264
            }
265
            else
266
            {
267
                dragDrop.clear();
268
            }
269
        }
270
    }
271
}
272
273
void VirtShortcutContainer::mousePressed(MouseEvent &event)
274
{
275
    if ((mShortcut == nullptr) || (inventoryWindow == nullptr))
276
        return;
277
278
    const int index = getIndexFromGrid(event.getX(), event.getY());
279
280
    if (index == -1)
281
        return;
282
283
    event.consume();
284
285
    const MouseButtonT eventButton = event.getButton();
286
    if (eventButton == MouseButton::LEFT)
287
    {
288
        if (mShortcut->getItem(index) > 0)
289
        {
290
            mItemClicked = true;
291
        }
292
        else
293
        {
294
            if (dragDrop.isSelected())
295
            {
296
                mShortcut->setItems(index, dragDrop.getSelected(),
297
                    dragDrop.getSelectedColor());
298
                dragDrop.deselect();
299
            }
300
        }
301
    }
302
    else if (eventButton == MouseButton::RIGHT)
303
    {
304
        const Inventory *const inv = PlayerInfo::getInventory();
305
        if (inv == nullptr)
306
            return;
307
308
        Item *const item = inv->findItem(mShortcut->getItem(index),
309
            mShortcut->getItemColor(index));
310
311
        if (popupMenu != nullptr)
312
        {
313
            popupMenu->showDropPopup(viewport->mMouseX,
314
                viewport->mMouseY,
315
                item);
316
        }
317
    }
318
}
319
320
void VirtShortcutContainer::mouseReleased(MouseEvent &event)
321
{
322
    if (mShortcut == nullptr)
323
        return;
324
325
    if (event.getButton() == MouseButton::LEFT)
326
    {
327
        if (mShortcut->isItemSelected())
328
            mShortcut->setItemSelected(-1);
329
330
        const int index = getIndexFromGrid(event.getX(), event.getY());
331
        if (index == -1)
332
        {
333
            dragDrop.clear();
334
            return;
335
        }
336
        if (!dragDrop.isEmpty())
337
        {
338
            if (dragDrop.isSourceItemContainer())
339
            {
340
                mShortcut->setItems(index, dragDrop.getItem(),
341
                    dragDrop.getItemColor());
342
                dragDrop.clear();
343
                dragDrop.deselect();
344
            }
345
        }
346
347
        mItemClicked = false;
348
    }
349
}
350
351
// Show ItemTooltip
352
void VirtShortcutContainer::mouseMoved(MouseEvent &event)
353
{
354
    if (mShortcut == nullptr)
355
        return;
356
357
    const int index = getIndexFromGrid(event.getX(), event.getY());
358
359
    if (index == -1)
360
        return;
361
362
    const int itemId = mShortcut->getItem(index);
363
    const ItemColor itemColor = mShortcut->getItemColor(index);
364
365
    if (itemId < 0)
366
        return;
367
368
    const Inventory *const inv = PlayerInfo::getInventory();
369
    if (inv == nullptr)
370
        return;
371
372
    const Item *const item = inv->findItem(itemId, itemColor);
373
374
    if ((item != nullptr) && (viewport != nullptr))
375
    {
376
        itemPopup->setItem(item, false);
377
        itemPopup->position(viewport->mMouseX, viewport->mMouseY);
378
    }
379
    else
380
    {
381
        itemPopup->setVisible(Visible_false);
382
    }
383
}
384
385
void VirtShortcutContainer::mouseExited(MouseEvent &event A_UNUSED)
386
{
387
    if (itemPopup != nullptr)
388
        itemPopup->setVisible(Visible_false);
389
}
390
391
void VirtShortcutContainer::widgetHidden(const Event &event A_UNUSED)
392
{
393
    if (itemPopup != nullptr)
394
        itemPopup->setVisible(Visible_false);
395
2
}