GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/itempopup.cpp Lines: 45 228 19.7 %
Date: 2017-11-29 Branches: 33 278 11.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008  The Legend of Mazzeroth Development Team
4
 *  Copyright (C) 2008-2009  The Mana World Development Team
5
 *  Copyright (C) 2009-2010  The Mana Developers
6
 *  Copyright (C) 2011-2017  The ManaPlus Developers
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "gui/popups/itempopup.h"
25
26
#include "actormanager.h"
27
#include "configuration.h"
28
29
#include "gui/gui.h"
30
31
#include "gui/fonts/font.h"
32
33
#include "gui/widgets/icon.h"
34
#include "gui/widgets/label.h"
35
#include "gui/widgets/textbox.h"
36
37
#include "net/beinghandler.h"
38
#ifdef TMWA_SUPPORT
39
#include "net/net.h"
40
#endif  // TMWA_SUPPORT
41
42
#include "utils/foreach.h"
43
#include "utils/gettext.h"
44
#include "utils/stdmove.h"
45
46
#include "utils/translation/podict.h"
47
48
#include "resources/iteminfo.h"
49
50
#include "resources/db/itemoptiondb.h"
51
#include "resources/db/unitsdb.h"
52
53
#include "resources/image/image.h"
54
55
#include "resources/item/item.h"
56
#include "resources/item/itemfieldtype.h"
57
#include "resources/item/itemoptionslist.h"
58
59
#include "resources/loaders/imageloader.h"
60
61
#include "debug.h"
62
63
ItemPopup *itemPopup = nullptr;
64
65
4
ItemPopup::ItemPopup() :
66
    Popup("ItemPopup", "itempopup.xml"),
67

4
    mItemName(new Label(this)),
68

4
    mItemDesc(new TextBox(this)),
69

4
    mItemEffect(new TextBox(this)),
70

4
    mItemWeight(new TextBox(this)),
71

4
    mItemCards(new TextBox(this)),
72

4
    mItemOptions(new TextBox(this)),
73
    mItemType(ItemDbType::UNUSABLE),
74

4
    mIcon(new Icon(this, nullptr)),
75
    mLastName(),
76
    mCardsStr(),
77
    mItemOptionsStr(),
78
    mLastId(0),
79

68
    mLastColor(ItemColor_one)
80
{
81
    // Item name
82
4
    mItemName->setFont(boldFont);
83
4
    mItemName->setPosition(0, 0);
84
85

4
    const int fontHeight = getFont()->getHeight();
86
87
    // Item description
88
8
    mItemDesc->setEditable(false);
89
4
    mItemDesc->setPosition(0, fontHeight);
90
12
    mItemDesc->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
91
        getThemeColor(ThemeColorId::POPUP_OUTLINE));
92
93
    // Item effect
94
8
    mItemEffect->setEditable(false);
95
4
    mItemEffect->setPosition(0, 2 * fontHeight);
96
12
    mItemEffect->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
97
        getThemeColor(ThemeColorId::POPUP_OUTLINE));
98
99
    // Item weight
100
8
    mItemWeight->setEditable(false);
101
4
    mItemWeight->setPosition(0, 3 * fontHeight);
102
12
    mItemWeight->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
103
        getThemeColor(ThemeColorId::POPUP_OUTLINE));
104
105
    // Item cards
106
8
    mItemCards->setEditable(false);
107
4
    mItemCards->setPosition(0, 4 * fontHeight);
108
12
    mItemCards->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
109
        getThemeColor(ThemeColorId::POPUP_OUTLINE));
110
111
    // Item options
112
8
    mItemOptions->setEditable(false);
113
4
    mItemOptions->setPosition(0, 5 * fontHeight);
114
12
    mItemOptions->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
115
        getThemeColor(ThemeColorId::POPUP_OUTLINE));
116
4
}
117
118
4
void ItemPopup::postInit()
119
{
120
8
    Popup::postInit();
121
4
    add(mItemName);
122
4
    add(mItemDesc);
123
4
    add(mItemEffect);
124
4
    add(mItemWeight);
125
4
    add(mItemCards);
126
4
    add(mItemOptions);
127
4
    add(mIcon);
128
129
4
    addMouseListener(this);
130
4
}
131
132
24
ItemPopup::~ItemPopup()
133
{
134
4
    if (mIcon != nullptr)
135
    {
136
8
        Image *const image = mIcon->getImage();
137
4
        if (image != nullptr)
138
            image->decRef();
139
    }
140
8
}
141
142
void ItemPopup::setItem(const Item *const item,
143
                        const bool showImage)
144
{
145
    if (item == nullptr)
146
        return;
147
148
    const ItemInfo &ii = item->getInfo();
149
    setItem(ii,
150
        item->getColor(),
151
        showImage,
152
        item->getId(),
153
        item->getCards(),
154
        item->getOptions());
155
    if (item->getRefine() > 0)
156
    {
157
        mLastName = ii.getName();
158
        mLastColor = item->getColor();
159
        mLastId = item->getId();
160
#ifdef TMWA_SUPPORT
161
        if (Net::getNetworkType() == ServerType::TMWATHENA)
162
        {
163
            mItemName->setCaption(strprintf("%s (+%u), %d",
164
                ii.getName().c_str(),
165
                CAST_U32(item->getRefine()),
166
                ii.getId()));
167
        }
168
        else
169
#endif  // TMWA_SUPPORT
170
        {
171
            mItemName->setCaption(strprintf("%s (+%u), %d",
172
                ii.getName(item->getColor()).c_str(),
173
                CAST_U32(item->getRefine()),
174
                ii.getId()));
175
        }
176
        mItemName->adjustSize();
177
        const unsigned minWidth = mItemName->getWidth() + 8;
178
        if (CAST_U32(getWidth()) < minWidth)
179
            setWidth(minWidth);
180
    }
181
}
182
183
void ItemPopup::setItem(const ItemInfo &item,
184
                        const ItemColor color,
185
                        const bool showImage,
186
                        int id,
187
                        const int *const cards,
188
                        const ItemOptionsList *const options)
189
{
190
    if (mIcon == nullptr)
191
        return;
192
193
    std::string cardsStr;
194
    std::string optionsStr;
195
196
    if (item.getName() == mLastName &&
197
        color == mLastColor &&
198
        id == mLastId)
199
    {
200
        cardsStr = getCardsString(cards);
201
        optionsStr = getOptionsString(options);
202
        if (mItemOptionsStr == optionsStr &&
203
            mCardsStr == cardsStr)
204
        {
205
            return;
206
        }
207
    }
208
    else
209
    {
210
        cardsStr = getCardsString(cards);
211
        optionsStr = getOptionsString(options);
212
    }
213
    mItemOptionsStr = STD_MOVE(optionsStr);
214
    mCardsStr = STD_MOVE(cardsStr);
215
216
    if (id == -1)
217
        id = item.getId();
218
219
    int space = 0;
220
221
    Image *const oldImage = mIcon->getImage();
222
    if (oldImage != nullptr)
223
        oldImage->decRef();
224
225
    if (showImage)
226
    {
227
        Image *const image = Loader::getImage(combineDye2(
228
            pathJoin(paths.getStringValue("itemIcons"),
229
            item.getDisplay().image),
230
            item.getDyeIconColorsString(color)));
231
232
        mIcon->setImage(image);
233
        if (image != nullptr)
234
        {
235
            mIcon->setPosition(0, 0);
236
            space = mIcon->getWidth();
237
        }
238
    }
239
    else
240
    {
241
        mIcon->setImage(nullptr);
242
    }
243
244
    mItemType = item.getType();
245
246
    mLastName = item.getName();
247
    mLastColor = color;
248
    mLastId = id;
249
250
#ifdef TMWA_SUPPORT
251
    if (Net::getNetworkType() == ServerType::TMWATHENA)
252
    {
253
        mItemName->setCaption(strprintf("%s, %d",
254
            item.getName().c_str(), id));
255
        mItemDesc->setTextWrapped(item.getDescription(), 196);
256
    }
257
    else
258
#endif  // TMWA_SUPPORT
259
    {
260
        mItemName->setCaption(strprintf("%s, %d",
261
            item.getName(color).c_str(), id));
262
        mItemDesc->setTextWrapped(item.getDescription(color), 196);
263
    }
264
265
    mItemName->adjustSize();
266
    setLabelColor(mItemName, mItemType);
267
    mItemName->setPosition(space, 0);
268
269
    mItemEffect->setTextWrapped(item.getEffect(), 196);
270
    // TRANSLATORS: popup label
271
    mItemWeight->setTextWrapped(strprintf(_("Weight: %s"),
272
        UnitsDb::formatWeight(item.getWeight()).c_str()), 196);
273
    mItemCards->setTextWrapped(mCardsStr, 196);
274
    mItemOptions->setTextWrapped(mItemOptionsStr, 196);
275
276
    int minWidth = mItemName->getWidth() + space;
277
278
    if (mItemName->getWidth() + space > minWidth)
279
        minWidth = mItemName->getWidth() + space;
280
    if (mItemDesc->getMinWidth() > minWidth)
281
        minWidth = mItemDesc->getMinWidth();
282
    if (mItemEffect->getMinWidth() > minWidth)
283
        minWidth = mItemEffect->getMinWidth();
284
    if (mItemWeight->getMinWidth() > minWidth)
285
        minWidth = mItemWeight->getMinWidth();
286
    if (mItemCards->getMinWidth() > minWidth)
287
        minWidth = mItemCards->getMinWidth();
288
    if (mItemOptions->getMinWidth() > minWidth)
289
        minWidth = mItemOptions->getMinWidth();
290
291
    const int numRowsDesc = mItemDesc->getNumberOfRows();
292
    const int numRowsEffect = mItemEffect->getNumberOfRows();
293
    const int numRowsWeight = mItemWeight->getNumberOfRows();
294
    const int numRowsCards = mItemCards->getNumberOfRows();
295
    const int numRowsOptions = mItemOptions->getNumberOfRows();
296
    const int height = getFont()->getHeight();
297
298
    if (item.getEffect().empty())
299
    {
300
        setContentSize(minWidth,
301
            (numRowsDesc + 2 + numRowsWeight + numRowsCards + numRowsOptions) *
302
            height);
303
        mItemWeight->setPosition(0, (numRowsDesc + 2) * height);
304
        mItemCards->setPosition(0, (numRowsDesc + numRowsWeight + 2) * height);
305
        mItemOptions->setPosition(0,
306
            (numRowsDesc + numRowsWeight + numRowsCards + 2) * height);
307
    }
308
    else
309
    {
310
        setContentSize(minWidth, (numRowsDesc + numRowsEffect + 2
311
            + numRowsWeight + numRowsCards + numRowsOptions) * height);
312
        mItemEffect->setPosition(0, (numRowsDesc + 2) * height);
313
        mItemWeight->setPosition(0, (numRowsDesc + numRowsEffect + 2)
314
            * height);
315
        mItemCards->setPosition(0, (numRowsDesc + numRowsEffect
316
            + numRowsWeight + 2) * height);
317
        mItemOptions->setPosition(0, (numRowsDesc + numRowsEffect
318
            + numRowsWeight + numRowsCards + 2) * height);
319
    }
320
321
    mItemDesc->setPosition(0, 2 * height);
322
}
323
324
std::string ItemPopup::getCardsString(const int *const cards)
325
{
326
    if (cards == nullptr)
327
        return std::string();
328
329
    std::string label;
330
331
    switch (cards[0])
332
    {
333
        case 0xfe:  // named item
334
        {
335
            const int32_t charId = cards[2] + 65536 * cards[3];
336
            std::string name = actorManager->findCharById(charId);
337
            if (name.empty())
338
            {
339
                name = toString(charId);
340
                beingHandler->requestNameByCharId(charId);
341
                mLastId = 0;  // allow recreate popup with same data
342
            }
343
            // TRANSLATORS: named item description
344
            label.append(strprintf(_("Item named: %s"), name.c_str()));
345
            return label;
346
        }
347
        case 0x00FFU:  // forged item
348
        {
349
            return label;
350
        }
351
        case 0xFF00U:
352
        {
353
            return label;
354
        }
355
        default:
356
        {
357
            for (int f = 0; f < maxCards; f ++)
358
            {
359
                const int id = cards[f];
360
                if (id != 0)
361
                {
362
                    if (!label.empty())
363
                        label.append(" / ");
364
                    const ItemInfo &info = ItemDB::get(id);
365
                    label.append(info.getName());
366
                }
367
            }
368
            if (label.empty())
369
                return label;
370
            // TRANSLATORS: popup label
371
            return _("Cards: ") + label;
372
        }
373
    }
374
}
375
376
std::string ItemPopup::getOptionsString(const ItemOptionsList *const options)
377
{
378
    if (options == nullptr || translator == nullptr)
379
        return std::string();
380
    const size_t sz = options->size();
381
    std::string effect;
382
    for (size_t f = 0; f < sz; f ++)
383
    {
384
        const ItemOption &option = options->get(f);
385
        if (option.index == 0)
386
            continue;
387
        const STD_VECTOR<ItemFieldType*> &fields = ItemOptionDb::getFields(
388
            option.index);
389
        if (fields.empty())
390
            continue;
391
        const std::string valueStr = toString(option.value);
392
        FOR_EACH (STD_VECTOR<ItemFieldType*>::const_iterator, it, fields)
393
        {
394
            const ItemFieldType *const field = *it;
395
            std::string value = valueStr;
396
            if (!effect.empty())
397
                effect.append(" / ");
398
            if (field->sign && value[0] != '-')
399
                value = std::string("+").append(value);
400
            const std::string format = translator->getStr(field->description);
401
            effect.append(strprintf(format.c_str(),
402
                value.c_str()));
403
        }
404
    }
405
    if (effect.empty())
406
        return effect;
407
    // TRANSLATORS: popup label
408
    return _("Options: ") + effect;
409
}
410
411
#define caseSetColor(name1, name2) \
412
    case name1: \
413
    { \
414
        return label->setForegroundColorAll(getThemeColor(name2), \
415
        getThemeColor(name2##_OUTLINE)); \
416
    }
417
void ItemPopup::setLabelColor(Label *label,
418
                              const ItemDbTypeT type) const
419
{
420
    switch (type)
421
    {
422
        caseSetColor(ItemDbType::UNUSABLE, ThemeColorId::GENERIC)
423
        caseSetColor(ItemDbType::USABLE, ThemeColorId::USABLE)
424
        caseSetColor(ItemDbType::EQUIPMENT_ONE_HAND_WEAPON,
425
            ThemeColorId::ONEHAND)
426
        caseSetColor(ItemDbType::EQUIPMENT_TWO_HANDS_WEAPON,
427
            ThemeColorId::TWOHAND)
428
        caseSetColor(ItemDbType::EQUIPMENT_TORSO, ThemeColorId::TORSO)
429
        caseSetColor(ItemDbType::EQUIPMENT_ARMS, ThemeColorId::ARMS)
430
        caseSetColor(ItemDbType::EQUIPMENT_HEAD, ThemeColorId::HEAD)
431
        caseSetColor(ItemDbType::EQUIPMENT_LEGS, ThemeColorId::LEGS)
432
        caseSetColor(ItemDbType::EQUIPMENT_SHIELD, ThemeColorId::SHIELD)
433
        caseSetColor(ItemDbType::EQUIPMENT_RING, ThemeColorId::RING)
434
        caseSetColor(ItemDbType::EQUIPMENT_NECKLACE, ThemeColorId::NECKLACE)
435
        caseSetColor(ItemDbType::EQUIPMENT_FEET, ThemeColorId::FEET)
436
        caseSetColor(ItemDbType::EQUIPMENT_AMMO, ThemeColorId::AMMO)
437
        caseSetColor(ItemDbType::EQUIPMENT_CHARM, ThemeColorId::CHARM)
438
        caseSetColor(ItemDbType::SPRITE_RACE, ThemeColorId::UNKNOWN_ITEM)
439
        caseSetColor(ItemDbType::SPRITE_HAIR, ThemeColorId::UNKNOWN_ITEM)
440
        caseSetColor(ItemDbType::CARD, ThemeColorId::CARD)
441
        default:
442
        {
443
            return label->setForegroundColorAll(getThemeColor(
444
                ThemeColorId::UNKNOWN_ITEM), getThemeColor(
445
                ThemeColorId::UNKNOWN_ITEM_OUTLINE));
446
        }
447
    }
448
}
449
#undef caseSetColor
450
451
void ItemPopup::mouseMoved(MouseEvent &event)
452
{
453
    Popup::mouseMoved(event);
454
455
    // When the mouse moved on top of the popup, hide it
456
    setVisible(Visible_false);
457
    resetPopup();
458
}
459
460
void ItemPopup::resetPopup()
461
{
462
    mLastName.clear();
463
    mLastColor = ItemColor_one;
464
    mLastId = 0;
465
4
}