GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/attrs/changedisplay.cpp Lines: 1 39 2.6 %
Date: 2021-03-17 Branches: 2 52 3.8 %

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/widgets/attrs/changedisplay.h"
25
26
#include "configuration.h"
27
28
#include "being/playerinfo.h"
29
30
#include "gui/widgets/button.h"
31
#include "gui/widgets/containerplacer.h"
32
#include "gui/widgets/layouthelper.h"
33
34
#include "net/net.h"
35
#include "net/playerhandler.h"
36
37
#include "utils/gettext.h"
38
39
#include "debug.h"
40
41
ChangeDisplay::ChangeDisplay(const Widget2 *const widget,
42
                             const AttributesT id,
43
                             const std::string &restrict name,
44
                             const std::string &restrict shortName) :
45
    AttrDisplay(widget, id, name, shortName),
46
    ActionListener(),
47
    mNeeded(1),
48
    // TRANSLATORS: status window label
49
    mPoints(new Label(this, _("Max"))),
50
    // TRANSLATORS: status window label (plus sign)
51
    mInc(new Button(this, _("+"), "inc", BUTTON_SKIN, this))
52
{
53
    // Do the layout
54
    ContainerPlacer place = mLayout->getPlacer(0, 0);
55
56
    place(0, 0, mLabel, 3, 1);
57
    place(4, 0, mValue, 2, 1);
58
    place(6, 0, mInc, 1, 1);
59
    place(7, 0, mPoints, 1, 1);
60
}
61
62
std::string ChangeDisplay::update()
63
{
64
    if (mNeeded > 0)
65
    {
66
        mPoints->setCaption(toString(mNeeded));
67
    }
68
    else
69
    {
70
        // TRANSLATORS: status bar label
71
        mPoints->setCaption(_("Max"));
72
    }
73
74
    mInc->setEnabled(PlayerInfo::getAttribute(Attributes::PLAYER_CHAR_POINTS)
75
        >= mNeeded && mNeeded > 0);
76
77
    return AttrDisplay::update();
78
}
79
80
void ChangeDisplay::setPointsNeeded(const int needed)
81
{
82
    mNeeded = needed;
83
    update();
84
}
85
86
void ChangeDisplay::action(const ActionEvent &event)
87
{
88
    if (event.getSource() == mInc)
89
    {
90
        int cnt = 1;
91
        if (config.getBoolValue("quickStats"))
92
        {
93
            cnt = mInc->getClickCount();
94
            if (cnt > 10)
95
                cnt = 10;
96
        }
97
98
        const int newpoints = PlayerInfo::getAttribute(
99
            Attributes::PLAYER_CHAR_POINTS) - cnt;
100
        PlayerInfo::setAttribute(Attributes::PLAYER_CHAR_POINTS,
101
            newpoints,
102
            Notify_true);
103
104
        const int newbase = PlayerInfo::getStatBase(mId) + cnt;
105
        PlayerInfo::setStatBase(mId, newbase, Notify_true);
106
107
        if (Net::getNetworkType() != ServerType::TMWATHENA)
108
        {
109
            playerHandler->increaseAttribute(mId, cnt);
110
        }
111
        else
112
        {
113
            for (int f = 0; f < cnt; f ++)
114
            {
115
                playerHandler->increaseAttribute(mId, 1);
116
                if (cnt != 1)
117
                    SDL_Delay(100);
118
            }
119
        }
120
    }
121

3
}