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

6
}