GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/ministatuswindow.cpp Lines: 114 283 40.3 %
Date: 2021-03-17 Branches: 121 396 30.6 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
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/windows/ministatuswindow.h"
25
26
#include "configuration.h"
27
28
#include "being/localplayer.h"
29
#include "being/playerinfo.h"
30
31
#include "gui/popupmanager.h"
32
#include "gui/skin.h"
33
34
#include "gui/popups/popupmenu.h"
35
#include "gui/popups/statuspopup.h"
36
#include "gui/popups/textpopup.h"
37
38
#include "gui/windows/statuswindow.h"
39
40
#include "gui/widgets/createwidget.h"
41
#include "gui/widgets/progressbar.h"
42
43
#include "net/playerhandler.h"
44
45
#include "resources/inventory/inventory.h"
46
47
#include "resources/sprite/animatedsprite.h"
48
49
#include "utils/delete2.h"
50
#include "utils/dtor.h"
51
#include "utils/foreach.h"
52
#include "utils/gettext.h"
53
54
#include "debug.h"
55
56
MiniStatusWindow *miniStatusWindow = nullptr;
57
extern volatile int tick_time;
58
59
typedef STD_VECTOR <ProgressBar*>::const_iterator ProgressBarVectorCIter;
60
61
1
MiniStatusWindow::MiniStatusWindow() :
62
    Window("MiniStatus", Modal_false, nullptr, "ministatus.xml"),
63
    InventoryListener(),
64
    AttributeListener(),
65
    StatListener(),
66
    ArrowsListener(),
67
    UpdateStatusListener(),
68
    mBars(),
69
    mBarNames(),
70
    mIcons(),
71


13
    mHpBar(createBar(0, 100, 0, ThemeColorId::HP_BAR, ProgressColorId::PROG_HP,
72
        "hpprogressbar.xml", "hpprogressbar_fill.xml",
73
        // TRANSLATORS: status bar name
74
1
        "hp bar", _("health bar"))),
75





14
    mMpBar(createBar(0, 100, 0, playerHandler->canUseMagic()
76
        ? ThemeColorId::MP_BAR : ThemeColorId::NO_MP_BAR,
77
1
        playerHandler->canUseMagic()
78
        ? ProgressColorId::PROG_MP : ProgressColorId::PROG_NO_MP,
79
1
        playerHandler->canUseMagic()
80
        ? "mpprogressbar.xml" : "nompprogressbar.xml",
81
1
        playerHandler->canUseMagic()
82
        ? "mpprogressbar_fill.xml" : "nompprogressbar_fill.xml",
83
        // TRANSLATORS: status bar name
84
1
        "mp bar", _("mana bar"))),
85


13
    mXpBar(createBar(0, 100, 0,
86
        ThemeColorId::XP_BAR, ProgressColorId::PROG_EXP,
87
        "xpprogressbar.xml", "xpprogressbar_fill.xml",
88
        // TRANSLATORS: status bar name
89
1
        "xp bar", _("experience bar"))),
90
    mJobBar(nullptr),
91


13
    mWeightBar(createBar(0, 140, 0, ThemeColorId::WEIGHT_BAR,
92
        ProgressColorId::PROG_WEIGHT,
93
        "weightprogressbar.xml", "weightprogressbar_fill.xml",
94
        // TRANSLATORS: status bar name
95
1
        "weight bar", _("weight bar"))),
96


13
    mInvSlotsBar(createBar(0, 45, 0,
97
        ThemeColorId::SLOTS_BAR, ProgressColorId::PROG_INVY_SLOTS,
98
        "slotsprogressbar.xml", "slotsprogressbar_fill.xml",
99
        "inventory slots bar",
100
        // TRANSLATORS: status bar name
101
1
        _("inventory slots bar"))),
102


13
    mMoneyBar(createBar(0, 130, 0, ThemeColorId::MONEY_BAR,
103
        ProgressColorId::PROG_MONEY,
104
        "moneyprogressbar.xml", "moneyprogressbar_fill.xml",
105
        // TRANSLATORS: status bar name
106
1
        "money bar", _("money bar"))),
107


13
    mArrowsBar(createBar(0, 50, 0, ThemeColorId::ARROWS_BAR,
108
        ProgressColorId::PROG_ARROWS,
109
        "arrowsprogressbar.xml", "arrowsprogressbar_fill.xml",
110
        // TRANSLATORS: status bar name
111
1
        "arrows bar", _("arrows bar"))),
112




16
    mStatusBar(createBar(100, (config.getIntValue("fontSize") > 16
113
        ? 270 : 180), 0, ThemeColorId::STATUS_BAR,
114
        ProgressColorId::PROG_STATUS,
115
        "statusprogressbar.xml", "statusprogressbar_fill.xml",
116
        // TRANSLATORS: status bar name
117
1
        "status bar", _("status bar"))),
118

1
    mStatusPopup(CREATEWIDGETR0(StatusPopup)),
119


4
    mSpacing(mSkin != nullptr ? mSkin->getOption("spacing", 3) : 3),
120


4
    mIconPadding(mSkin != nullptr ? mSkin->getOption("iconPadding", 3) : 3),
121


4
    mIconSpacing(mSkin != nullptr ? mSkin->getOption("iconSpacing", 2) : 2),
122



25
    mMaxX(0)
123
{
124
1
    StatusWindow::updateHPBar(mHpBar,
125
1
        false);
126
127
1
    if (statusWindow != nullptr)
128
    {
129
        statusWindow->updateMPBar(mMpBar,
130
            false);
131
    }
132
133

4
    const bool job = serverConfig.getValueBool("showJob", true);
134
135
1
    StatusWindow::updateXPBar(mXpBar,
136
1
        true);
137
138
1
    if (job)
139
    {
140


13
        mJobBar = createBar(0, 100, 0,
141
            ThemeColorId::JOB_BAR,
142
            ProgressColorId::PROG_JOB,
143
            "jobprogressbar.xml", "jobprogressbar_fill.xml",
144
            // TRANSLATORS: status bar name
145
1
            "job bar", _("job bar"));
146
1
        StatusWindow::updateJobBar(mJobBar,
147
1
            true);
148
    }
149
150
1
    loadBars();
151
1
    updateBars();
152
153
1
    setVisible(Visible_true);
154
1
    addMouseListener(this);
155
1
    Inventory *const inv = PlayerInfo::getInventory();
156
1
    if (inv != nullptr)
157
        inv->addInventoyListener(this);
158
159
1
    StatusWindow::updateMoneyBar(mMoneyBar);
160
1
    StatusWindow::updateArrowsBar(mArrowsBar);
161
1
    updateStatus();
162
1
}
163
164
7
MiniStatusWindow::~MiniStatusWindow()
165
{
166
1
    delete2(mStatusPopup)
167
2
    delete_all(mIcons);
168
2
    mIcons.clear();
169
170
1
    Inventory *const inv = PlayerInfo::getInventory();
171
1
    if (inv != nullptr)
172
        inv->removeInventoyListener(this);
173
174
15
    FOR_EACH (ProgressBarVectorCIter, it, mBars)
175
    {
176
9
        ProgressBar *bar = *it;
177
9
        if (bar == nullptr)
178
            continue;
179
9
        if (bar->mVisible == Visible_false)
180
5
            delete bar;
181
    }
182
2
    mBars.clear();
183
2
}
184
185
9
ProgressBar *MiniStatusWindow::createBar(const float progress,
186
                                         const int width,
187
                                         const int height,
188
                                         const ThemeColorIdT textColor,
189
                                         const ProgressColorIdT backColor,
190
                                         const std::string &restrict skin,
191
                                         const std::string &restrict skinFill,
192
                                         const std::string &restrict name,
193
                                         const std::string &restrict
194
                                         description)
195
{
196
    ProgressBar *const bar = new ProgressBar(this,
197
9
        progress, width, height, backColor, skin, skinFill);
198
18
    bar->setActionEventId(name);
199
18
    bar->setId(description);
200
36
    bar->setColor(getThemeColor(textColor, 255U),
201
9
        getThemeColor(textColor + 1, 255U));
202
9
    mBars.push_back(bar);
203
9
    mBarNames[name] = bar;
204
9
    return bar;
205
}
206
207
1
void MiniStatusWindow::updateBars()
208
{
209
1
    int x = 0;
210
1
    const ProgressBar *lastBar = nullptr;
211
15
    FOR_EACH (ProgressBarVectorCIter, it, mBars)
212
9
        remove(*it);
213
214
15
    FOR_EACH (ProgressBarVectorCIter, it, mBars)
215
    {
216
9
        ProgressBar *const bar = *it;
217
9
        if (bar == nullptr)
218
            continue;
219
9
        if (bar->mVisible == Visible_true)
220
        {
221
4
            bar->setPosition(x, 0);
222
4
            add(bar);
223
8
            x += bar->getWidth() + mSpacing;
224
4
            lastBar = bar;
225
        }
226
    }
227
228
1
    if (lastBar != nullptr)
229
    {
230
3
        setContentSize(lastBar->getX() + lastBar->getWidth(),
231
4
            lastBar->getY() + lastBar->getHeight());
232
    }
233
1
    mMaxX = x;
234
1
}
235
236
void MiniStatusWindow::setIcon(const int index, AnimatedSprite *const sprite)
237
{
238
    if (index >= CAST_S32(mIcons.size()))
239
        mIcons.resize(index + 1, nullptr);
240
241
    delete mIcons[index];
242
    mIcons[index] = sprite;
243
}
244
245
void MiniStatusWindow::eraseIcon(const int index)
246
{
247
    if (index < CAST_S32(mIcons.size()))
248
    {
249
        delete mIcons[index];
250
        mIcons.erase(mIcons.begin() + index);
251
    }
252
}
253
254
void MiniStatusWindow::drawIcons(Graphics *const graphics)
255
{
256
    // Draw icons
257
    unsigned int icon_x = mMaxX + mIconPadding;
258
    for (size_t i = 0, sz = mIcons.size(); i < sz; i ++)
259
    {
260
        const AnimatedSprite *const icon = mIcons[i];
261
        if (icon != nullptr)
262
        {
263
            icon->draw(graphics, CAST_S32(icon_x), mIconPadding);
264
            icon_x += CAST_U32(mIconSpacing + icon->getWidth());
265
        }
266
    }
267
}
268
269
void MiniStatusWindow::statChanged(const AttributesT id A_UNUSED,
270
                                   const int oldVal1 A_UNUSED,
271
                                   const int oldVal2 A_UNUSED)
272
{
273
    if (statusWindow != nullptr)
274
    {
275
        statusWindow->updateMPBar(mMpBar,
276
            false);
277
    }
278
    StatusWindow::updateJobBar(mJobBar,
279
        true);
280
}
281
282
void MiniStatusWindow::attributeChanged(const AttributesT id,
283
                                        const int64_t oldVal A_UNUSED,
284
                                        const int64_t newVal A_UNUSED)
285
{
286
    PRAGMA45(GCC diagnostic push)
287
    PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
288
    switch (id)
289
    {
290
        case Attributes::PLAYER_HP:
291
        case Attributes::PLAYER_MAX_HP:
292
            StatusWindow::updateHPBar(mHpBar,
293
                false);
294
            break;
295
        case Attributes::PLAYER_MP:
296
        case Attributes::PLAYER_MAX_MP:
297
            statusWindow->updateMPBar(mMpBar,
298
                false);
299
            break;
300
        case Attributes::PLAYER_EXP:
301
        case Attributes::PLAYER_EXP_NEEDED:
302
            StatusWindow::updateXPBar(mXpBar,
303
                true);
304
            break;
305
        case Attributes::TOTAL_WEIGHT:
306
        case Attributes::MAX_WEIGHT:
307
            StatusWindow::updateWeightBar(mWeightBar);
308
            break;
309
        case Attributes::MONEY:
310
            StatusWindow::updateMoneyBar(mMoneyBar);
311
            break;
312
        default:
313
            break;
314
    }
315
    PRAGMA45(GCC diagnostic pop)
316
}
317
318
1
void MiniStatusWindow::updateStatus()
319
{
320
1
    if (statusWindow != nullptr)
321
    {
322
        statusWindow->updateStatusBar(mStatusBar,
323
            true);
324
    }
325

1
    if ((mStatusPopup != nullptr) && mStatusPopup->isPopupVisible())
326
        mStatusPopup->update();
327
1
}
328
329
void MiniStatusWindow::logic()
330
{
331
    BLOCK_START("MiniStatusWindow::logic")
332
    Window::logic();
333
334
    for (size_t i = 0, sz = mIcons.size(); i < sz; i++)
335
    {
336
        AnimatedSprite *const icon = mIcons[i];
337
        if (icon != nullptr)
338
            icon->update(tick_time * 10);
339
    }
340
    BLOCK_END("MiniStatusWindow::logic")
341
}
342
343
1
void MiniStatusWindow::draw(Graphics *const graphics)
344
{
345
    BLOCK_START("MiniStatusWindow::draw")
346
1
    drawChildren(graphics);
347
    BLOCK_END("MiniStatusWindow::draw")
348
1
}
349
350
void MiniStatusWindow::safeDraw(Graphics *const graphics)
351
{
352
    BLOCK_START("MiniStatusWindow::draw")
353
    safeDrawChildren(graphics);
354
    BLOCK_END("MiniStatusWindow::draw")
355
}
356
357
void MiniStatusWindow::mouseMoved(MouseEvent &event)
358
{
359
    Window::mouseMoved(event);
360
361
    const int x = event.getX();
362
    const int y = event.getY();
363
364
    const Rect &rect = mDimension;
365
    if (event.getSource() == mStatusBar)
366
    {
367
        mStatusPopup->view(x + rect.x, y + rect.y);
368
        textPopup->hide();
369
    }
370
    else if (event.getSource() == mXpBar)
371
    {
372
        std::string level;
373
        if ((localPlayer != nullptr) && localPlayer->isGM())
374
        {
375
            // TRANSLATORS: status bar label
376
            level = strprintf(_("Level: %d (GM %d)"),
377
                PlayerInfo::getAttribute(Attributes::PLAYER_BASE_LEVEL),
378
                localPlayer->getGroupId());
379
        }
380
        else
381
        {
382
            // TRANSLATORS: status bar label
383
            level = strprintf(_("Level: %d"),
384
                PlayerInfo::getAttribute(Attributes::PLAYER_BASE_LEVEL));
385
        }
386
387
        const int64_t exp = PlayerInfo::getAttribute64(Attributes::PLAYER_EXP);
388
        const int64_t expNeed = PlayerInfo::getAttribute64(
389
            Attributes::PLAYER_EXP_NEEDED);
390
        const std::string str = toString(CAST_U64(exp)) +
391
            "/" +
392
            toString(CAST_U64(expNeed));
393
        if (exp > expNeed)
394
        {
395
            textPopup->show(x + rect.x,
396
                y + rect.y,
397
                level,
398
                str);
399
        }
400
        else
401
        {
402
            const std::string str2 = toString(CAST_U64(expNeed - exp));
403
            textPopup->show(x + rect.x,
404
                y + rect.y,
405
                level,
406
                str,
407
                // TRANSLATORS: status bar label
408
                strprintf("%s: %s", _("Need"), str2.c_str()));
409
        }
410
        mStatusPopup->hide();
411
    }
412
    else if (event.getSource() == mHpBar)
413
    {
414
        textPopup->show(x + rect.x, y + rect.y, event.getSource()->getId(),
415
            strprintf("%d/%d", PlayerInfo::getAttribute(Attributes::PLAYER_HP),
416
            PlayerInfo::getAttribute(Attributes::PLAYER_MAX_HP)));
417
        mStatusPopup->hide();
418
    }
419
    else if (event.getSource() == mMpBar)
420
    {
421
        textPopup->show(x + rect.x, y + rect.y, event.getSource()->getId(),
422
            strprintf("%d/%d", PlayerInfo::getAttribute(Attributes::PLAYER_MP),
423
            PlayerInfo::getAttribute(Attributes::PLAYER_MAX_MP)));
424
        mStatusPopup->hide();
425
    }
426
    else if (event.getSource() == mJobBar)
427
    {
428
        const int64_t exp =  PlayerInfo::getAttribute64(
429
            Attributes::PLAYER_JOB_EXP);
430
        const int64_t expNeed =  PlayerInfo::getAttribute64(
431
            Attributes::PLAYER_JOB_EXP_NEEDED);
432
        const int32_t jobLevel = PlayerInfo::getAttribute(
433
            Attributes::PLAYER_JOB_LEVEL);
434
        const std::string expStr = toString(CAST_U64(exp));
435
        const std::string expNeedStr = toString(CAST_U64(expNeed));
436
437
        // TRANSLATORS: job bar label
438
        const std::string level = strprintf(_("Job level: %d"),
439
            jobLevel);
440
441
        if (exp > expNeed)
442
        {
443
            textPopup->show(x + rect.x,
444
                y + rect.y,
445
                level,
446
                strprintf("%s/%s", expStr.c_str(), expNeedStr.c_str()));
447
        }
448
        else
449
        {
450
            const std::string expLeftStr = toString(CAST_U64(expNeed - exp));
451
            textPopup->show(x + rect.x,
452
                y + rect.y,
453
                level,
454
                strprintf("%s/%s", expStr.c_str(), expNeedStr.c_str()),
455
                // TRANSLATORS: status bar label
456
                strprintf("%s: %s", _("Need"), expLeftStr.c_str()));
457
        }
458
        mStatusPopup->hide();
459
    }
460
    else if (event.getSource() == mWeightBar)
461
    {
462
        textPopup->show(x + rect.x, y + rect.y, event.getSource()->getId(),
463
            strprintf("%d/%d", PlayerInfo::getAttribute(
464
            Attributes::TOTAL_WEIGHT),
465
            PlayerInfo::getAttribute(Attributes::MAX_WEIGHT)));
466
        mStatusPopup->hide();
467
    }
468
    else if (event.getSource() == mInvSlotsBar)
469
    {
470
        const Inventory *const inv = PlayerInfo::getInventory();
471
        if (inv != nullptr)
472
        {
473
            const int usedSlots = inv->getNumberOfSlotsUsed();
474
            const int maxSlots = inv->getSize();
475
            textPopup->show(x + rect.x, y + rect.y,
476
                event.getSource()->getId(),
477
                strprintf("%d/%d", usedSlots, maxSlots));
478
        }
479
        mStatusPopup->hide();
480
    }
481
    else if (event.getSource() == mMoneyBar)
482
    {
483
        textPopup->show(x + rect.x, y + rect.y,
484
            event.getSource()->getId(),
485
            toString(PlayerInfo::getAttribute(Attributes::MONEY)));
486
    }
487
    else
488
    {
489
        textPopup->hide();
490
        mStatusPopup->hide();
491
    }
492
}
493
494
void MiniStatusWindow::mousePressed(MouseEvent &event)
495
{
496
    if (event.getButton() == MouseButton::RIGHT)
497
    {
498
        const ProgressBar *const bar = dynamic_cast<ProgressBar*>(
499
            event.getSource());
500
        if (bar == nullptr)
501
            return;
502
        event.consume();
503
        if (popupManager != nullptr)
504
        {
505
            popupMenu->showPopup(getX() + event.getX(),
506
                getY() + event.getY(), bar);
507
        }
508
    }
509
}
510
511
void MiniStatusWindow::mouseExited(MouseEvent &event)
512
{
513
    Window::mouseExited(event);
514
515
    textPopup->hide();
516
    mStatusPopup->hide();
517
}
518
519
void MiniStatusWindow::showBar(const std::string &name,
520
                               const Visible visible)
521
{
522
    ProgressBar *const bar = mBarNames[name];
523
    if (bar == nullptr)
524
        return;
525
    bar->setVisible(visible);
526
    updateBars();
527
    saveBars();
528
}
529
530
1
void MiniStatusWindow::loadBars()
531
{
532

4
    if (config.getIntValue("ministatussaved") == 0)
533
    {
534
1
        if (mWeightBar != nullptr)
535
1
            mWeightBar->setVisible(Visible_false);
536
1
        if (mInvSlotsBar != nullptr)
537
1
            mInvSlotsBar->setVisible(Visible_false);
538
1
        if (mMoneyBar != nullptr)
539
1
            mMoneyBar->setVisible(Visible_false);
540
1
        if (mArrowsBar != nullptr)
541
1
            mArrowsBar->setVisible(Visible_false);
542
1
        if (mStatusBar != nullptr)
543
1
            mStatusBar->setVisible(Visible_false);
544
1
        if (mJobBar != nullptr)
545
1
            mJobBar->setVisible(Visible_true);
546
        return;
547
    }
548
549
    for (int f = 0; f < 10; f ++)
550
    {
551
        const std::string str = config.getValue(
552
            "ministatus" + toString(f), "");
553
        if (str.empty())
554
            continue;
555
        ProgressBar *const bar = mBarNames[str];
556
        if (bar == nullptr)
557
            continue;
558
        bar->setVisible(Visible_false);
559
    }
560
}
561
562
void MiniStatusWindow::saveBars() const
563
{
564
    int i = 0;
565
    FOR_EACH (ProgressBarVectorCIter, it, mBars)
566
    {
567
        const ProgressBar *const bar = *it;
568
        if (bar->mVisible == Visible_false)
569
        {
570
            config.setValue("ministatus" + toString(i),
571
                bar->getActionEventId());
572
            i ++;
573
        }
574
    }
575
    for (int f = i; f < 10; f ++)
576
        config.deleteKey("ministatus" + toString(f));
577
578
    config.setValue("ministatussaved", true);
579
}
580
581
void MiniStatusWindow::slotsChanged(const Inventory *const inventory)
582
{
583
    if (inventory == nullptr)
584
        return;
585
586
    if (inventory->getType() == InventoryType::Inventory)
587
        StatusWindow::updateInvSlotsBar(mInvSlotsBar);
588
}
589
590
1
Rect MiniStatusWindow::getChildrenArea()
591
{
592
1
    const int padding = mPadding;
593
1
    const int padding2 = padding * 2;
594
1
    const Rect &rect = mDimension;
595
    return Rect(padding, padding,
596
1
        rect.width - padding2,
597
3
        rect.height - padding2);
598
}
599
600
void MiniStatusWindow::arrowsChanged()
601
{
602
    StatusWindow::updateArrowsBar(mArrowsBar);
603
2
}
604
605
#ifdef USE_PROFILER
606
void MiniStatusWindow::logicChildren()
607
{
608
    BLOCK_START("MiniStatusWindow::logicChildren")
609
    BasicContainer::logicChildren();
610
    BLOCK_END("MiniStatusWindow::logicChildren")
611
}
612
#endif  // USE_PROFILER