GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/virtshortcutcontainer.cpp Lines: 1 156 0.6 %
Date: 2018-09-20 Branches: 0 136 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  Andrei Karas
5
 *  Copyright (C) 2011-2018  The ManaPlus Developers
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 itemX = (i % mGridWidth) * mBoxWidth;
104
        const int itemY = (i / mGridWidth) * mBoxHeight;
105
106
        if (mShortcut->getItem(i) < 0)
107
            continue;
108
109
        const Item *const item = inv->findItem(mShortcut->getItem(i),
110
            mShortcut->getItemColor(i));
111
112
        if (item != nullptr)
113
        {
114
            // Draw item icon.
115
            Image *const image = item->getImage();
116
117
            if (image != nullptr)
118
            {
119
                std::string caption;
120
                if (item->getQuantity() > 1)
121
                    caption = toString(item->getQuantity());
122
                else if (item->isEquipped() == Equipped_true)
123
                    caption = "Eq.";
124
125
                image->setAlpha(1.0F);
126
                graphics->drawImage(image,
127
                    itemX + mImageOffsetX,
128
                    itemY + mImageOffsetY);
129
                if (item->isEquipped() == Equipped_true)
130
                {
131
                    font->drawString(graphics,
132
                        mEquipedColor,
133
                        mEquipedColor2,
134
                        caption,
135
                        itemX + (mBoxWidth - font->getWidth(caption)) / 2,
136
                        itemY + mBoxHeight - 14);
137
                }
138
                else
139
                {
140
                    font->drawString(graphics,
141
                        mUnEquipedColor,
142
                        mUnEquipedColor2,
143
                        caption,
144
                        itemX + (mBoxWidth - font->getWidth(caption)) / 2,
145
                        itemY + mBoxHeight - 14);
146
                }
147
            }
148
        }
149
    }
150
    BLOCK_END("VirtShortcutContainer::draw")
151
}
152
153
void VirtShortcutContainer::safeDraw(Graphics *const graphics)
154
{
155
    if (mShortcut == nullptr)
156
        return;
157
158
    BLOCK_START("VirtShortcutContainer::safeDraw")
159
    if (settings.guiAlpha != mAlpha)
160
    {
161
        mAlpha = settings.guiAlpha;
162
        if (mBackgroundImg != nullptr)
163
            mBackgroundImg->setAlpha(mAlpha);
164
    }
165
166
    safeDrawBackground(graphics);
167
168
    const Inventory *const inv = PlayerInfo::getInventory();
169
    if (inv == nullptr)
170
    {
171
        BLOCK_END("VirtShortcutContainer::safeDraw")
172
        return;
173
    }
174
175
    Font *const font = getFont();
176
177
    for (unsigned i = 0; i < mMaxItems; i++)
178
    {
179
        const int itemX = (i % mGridWidth) * mBoxWidth;
180
        const int itemY = (i / mGridWidth) * mBoxHeight;
181
182
        if (mShortcut->getItem(i) < 0)
183
            continue;
184
185
        const Item *const item = inv->findItem(mShortcut->getItem(i),
186
            mShortcut->getItemColor(i));
187
188
        if (item != nullptr)
189
        {
190
            // Draw item icon.
191
            Image *const image = item->getImage();
192
193
            if (image != nullptr)
194
            {
195
                std::string caption;
196
                if (item->getQuantity() > 1)
197
                    caption = toString(item->getQuantity());
198
                else if (item->isEquipped() == Equipped_true)
199
                    caption = "Eq.";
200
201
                image->setAlpha(1.0F);
202
                graphics->drawImage(image,
203
                    itemX + mImageOffsetX,
204
                    itemY + mImageOffsetY);
205
                if (item->isEquipped() == Equipped_true)
206
                {
207
                    font->drawString(graphics,
208
                        mEquipedColor,
209
                        mEquipedColor2,
210
                        caption,
211
                        itemX + (mBoxWidth - font->getWidth(caption)) / 2,
212
                        itemY + mBoxHeight - 14);
213
                }
214
                else
215
                {
216
                    font->drawString(graphics,
217
                        mUnEquipedColor,
218
                        mUnEquipedColor2,
219
                        caption,
220
                        itemX + (mBoxWidth - font->getWidth(caption)) / 2,
221
                        itemY + mBoxHeight - 14);
222
                }
223
            }
224
        }
225
    }
226
    BLOCK_END("VirtShortcutContainer::safeDraw")
227
}
228
229
void VirtShortcutContainer::mouseDragged(MouseEvent &event)
230
{
231
    if (mShortcut == nullptr)
232
        return;
233
234
    if (event.getButton() == MouseButton::LEFT)
235
    {
236
        if (dragDrop.isEmpty() && mItemClicked)
237
        {
238
            const int index = getIndexFromGrid(event.getX(), event.getY());
239
240
            if (index == -1)
241
                return;
242
243
            const int itemId = mShortcut->getItem(index);
244
            const ItemColor itemColor = mShortcut->getItemColor(index);
245
246
            if (itemId < 0)
247
                return;
248
249
            const Inventory *const inv = PlayerInfo::getInventory();
250
            if (inv == nullptr)
251
                return;
252
253
            Item *const item = inv->findItem(itemId, itemColor);
254
255
            if (item != nullptr)
256
            {
257
                dragDrop.dragItem(item, DragDropSource::Drop, 0);
258
                mShortcut->removeItem(index);
259
            }
260
            else
261
            {
262
                dragDrop.clear();
263
            }
264
        }
265
    }
266
}
267
268
void VirtShortcutContainer::mousePressed(MouseEvent &event)
269
{
270
    if ((mShortcut == nullptr) || (inventoryWindow == nullptr))
271
        return;
272
273
    const int index = getIndexFromGrid(event.getX(), event.getY());
274
275
    if (index == -1)
276
        return;
277
278
    event.consume();
279
280
    const MouseButtonT eventButton = event.getButton();
281
    if (eventButton == MouseButton::LEFT)
282
    {
283
        if (mShortcut->getItem(index) > 0)
284
        {
285
            mItemClicked = true;
286
        }
287
        else
288
        {
289
            if (dragDrop.isSelected())
290
            {
291
                mShortcut->setItems(index, dragDrop.getSelected(),
292
                    dragDrop.getSelectedColor());
293
                dragDrop.deselect();
294
            }
295
        }
296
    }
297
    else if (eventButton == MouseButton::RIGHT)
298
    {
299
        const Inventory *const inv = PlayerInfo::getInventory();
300
        if (inv == nullptr)
301
            return;
302
303
        Item *const item = inv->findItem(mShortcut->getItem(index),
304
            mShortcut->getItemColor(index));
305
306
        if (popupMenu != nullptr)
307
        {
308
            popupMenu->showDropPopup(viewport->mMouseX,
309
                viewport->mMouseY,
310
                item);
311
        }
312
    }
313
}
314
315
void VirtShortcutContainer::mouseReleased(MouseEvent &event)
316
{
317
    if (mShortcut == nullptr)
318
        return;
319
320
    if (event.getButton() == MouseButton::LEFT)
321
    {
322
        if (mShortcut->isItemSelected())
323
            mShortcut->setItemSelected(-1);
324
325
        const int index = getIndexFromGrid(event.getX(), event.getY());
326
        if (index == -1)
327
        {
328
            dragDrop.clear();
329
            return;
330
        }
331
        if (!dragDrop.isEmpty())
332
        {
333
            if (dragDrop.isSourceItemContainer())
334
            {
335
                mShortcut->setItems(index, dragDrop.getItem(),
336
                    dragDrop.getItemColor());
337
                dragDrop.clear();
338
                dragDrop.deselect();
339
            }
340
        }
341
342
        mItemClicked = false;
343
    }
344
}
345
346
// Show ItemTooltip
347
void VirtShortcutContainer::mouseMoved(MouseEvent &event)
348
{
349
    if (mShortcut == nullptr)
350
        return;
351
352
    const int index = getIndexFromGrid(event.getX(), event.getY());
353
354
    if (index == -1)
355
        return;
356
357
    const int itemId = mShortcut->getItem(index);
358
    const ItemColor itemColor = mShortcut->getItemColor(index);
359
360
    if (itemId < 0)
361
        return;
362
363
    const Inventory *const inv = PlayerInfo::getInventory();
364
    if (inv == nullptr)
365
        return;
366
367
    const Item *const item = inv->findItem(itemId, itemColor);
368
369
    if ((item != nullptr) && (viewport != nullptr))
370
    {
371
        itemPopup->setItem(item, false);
372
        itemPopup->position(viewport->mMouseX, viewport->mMouseY);
373
    }
374
    else
375
    {
376
        itemPopup->setVisible(Visible_false);
377
    }
378
}
379
380
void VirtShortcutContainer::mouseExited(MouseEvent &event A_UNUSED)
381
{
382
    if (itemPopup != nullptr)
383
        itemPopup->setVisible(Visible_false);
384
}
385
386
void VirtShortcutContainer::widgetHidden(const Event &event A_UNUSED)
387
{
388
    if (itemPopup != nullptr)
389
        itemPopup->setVisible(Visible_false);
390
2
}