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