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