ManaPlus
attrdisplay.cpp
Go to the documentation of this file.
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 
25 
26 #include "being/playerinfo.h"
27 
29 
30 #include "utils/delete2.h"
31 #include "utils/stringutils.h"
32 
33 #include "debug.h"
34 
35 AttrDisplay::AttrDisplay(const Widget2 *const widget,
36  const AttributesT id,
37  const std::string &restrict name,
38  const std::string &restrict shortName) :
39  Container(widget),
40  mId(id),
41  mName(name),
42  mShortName(shortName),
43  mLayout(new LayoutHelper(this)),
44  mLabel(new Label(this, name)),
45  mValue(new Label(this, "1 "))
46 {
47  setSize(100, 32);
48 
49  setSelectable(false);
52 }
53 
55 {
57 }
58 
59 std::string AttrDisplay::update()
60 {
61  const int base = PlayerInfo::getStatBase(mId);
62  const int bonus = PlayerInfo::getStatMod(mId);
63  std::string value = toString(base + bonus);
64  if (bonus != 0)
65  value.append(strprintf("=%d%+d", base, bonus));
66  mValue->setCaption(value);
67  return mName;
68 }
Attributes ::T AttributesT
Definition: attributes.h:118
virtual std::string update()
Definition: attrdisplay.cpp:59
Label * mLabel
Definition: attrdisplay.h:77
LayoutHelper * mLayout
Definition: attrdisplay.h:76
const std::string mName
Definition: attrdisplay.h:73
Label * mValue
Definition: attrdisplay.h:78
AttrDisplay(const Widget2 *const widget, const AttributesT id, const std::string &name, const std::string &shortName)
Definition: attrdisplay.cpp:35
const AttributesT mId
Definition: attrdisplay.h:72
@ CENTER
Definition: graphics.h:132
Definition: label.h:91
void setAlignment(Graphics::Alignment alignment)
Definition: label.h:153
void setCaption(const std::string &caption)
Definition: label.cpp:264
void setSize(const int width, const int height)
Definition: widget.cpp:367
void setSelectable(const bool selectable)
Definition: widget.h:948
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
#define restrict
Definition: localconsts.h:165
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
int getStatBase(const AttributesT id)
Definition: playerinfo.cpp:135
int getStatMod(const AttributesT id)
Definition: playerinfo.cpp:151
std::string strprintf(const char *const format,...)