ManaPlus
statspagebasic.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
23 
24 #include "being/playerinfo.h"
25 
26 #include "gui/widgets/scrollarea.h"
28 
30 
31 #include "utils/gettext.h"
32 #include "utils/foreach.h"
33 #include "utils/stringutils.h"
34 
35 #include "resources/db/statdb.h"
36 
37 #include "debug.h"
38 
40  Container(widget),
43  StatListener(),
44  mAttrs(),
45  mAttrCont(new VertContainer(this, 32, true, 0)),
46  mAttrScroll(new ScrollArea(this, mAttrCont, Opaque_false, std::string())),
47  mCharacterPointsLabel(new Label(this, "C"))
48 {
49  addWidgetListener(this);
50  setSelectable(false);
51 
54  mAttrScroll->setSelectable(false);
55  mAttrCont->setSelectable(false);
56 
58  const STD_VECTOR<BasicStat> &basicStats = StatDb::getBasicStats();
59  FOR_EACH (STD_VECTOR<BasicStat>::const_iterator, it, basicStats)
60  {
61  const BasicStat &stat = *it;
62  AttrDisplay *disp = new ChangeDisplay(this,
63  stat.attr,
64  stat.name,
65  stat.tag);
66  disp->update();
67  mAttrCont->add2(disp, true, -1);
68  mAttrs[stat.attr] = disp;
69  }
70 
71  // TRANSLATORS: status window label
72  mCharacterPointsLabel->setCaption(strprintf(_("Character points: %d"),
76 }
77 
79 {
81 }
82 
84  const int oldVal1 A_UNUSED,
85  const int oldVal2 A_UNUSED)
86 {
87  const Attrs::const_iterator it = mAttrs.find(id);
88  if (it != mAttrs.end() && (it->second != nullptr))
89  it->second->update();
90 }
91 
93  const int64_t oldVal A_UNUSED,
94  const int64_t newVal)
95 {
96  PRAGMA45(GCC diagnostic push)
97  PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
98  switch (id)
99  {
102  // TRANSLATORS: status window label
103  _("Character points: %d"), CAST_S32(newVal)));
104 
106  // Update all attributes
107  for (Attrs::const_iterator it = mAttrs.begin();
108  it != mAttrs.end(); ++it)
109  {
110  if (it->second != nullptr)
111  it->second->update();
112  }
113  return;
114 
116  // Update all attributes
117  for (Attrs::const_iterator it = mAttrs.begin();
118  it != mAttrs.end(); ++it)
119  {
120  if (it->second != nullptr)
121  it->second->update();
122  }
123  return;
124 
125  default:
126  break;
127  }
128  PRAGMA45(GCC diagnostic pop)
129  const Attrs::const_iterator it = mAttrs.find(id);
130  if (it != mAttrs.end() && (it->second != nullptr))
131  it->second->update();
132 }
133 
135  const int needed)
136 {
137  const Attrs::const_iterator it = mAttrs.find(id);
138 
139  if (it != mAttrs.end())
140  {
141  AttrDisplay *const disp = it->second;
142  if ((disp != nullptr) && disp->getType() == AttrDisplay::CHANGEABLE)
143  static_cast<ChangeDisplay*>(disp)->setPointsNeeded(needed);
144  }
145 }
146 
147 std::string StatsPageBasic::getStatsStr() const
148 {
149  Attrs::const_iterator it = mAttrs.begin();
150  const Attrs::const_iterator it_end = mAttrs.end();
151  std::string str;
152  while (it != it_end)
153  {
154  const ChangeDisplay *const attr = dynamic_cast<ChangeDisplay*>(
155  (*it).second);
156  if (attr != nullptr)
157  {
158  str.append(strprintf("%s:%s ", attr->getShortName().c_str(),
159  attr->getValue().c_str()));
160  }
161  ++ it;
162  }
163  return str;
164 }
Attributes ::T AttributesT
Definition: attributes.h:118
#define CAST_S32
Definition: cast.h:30
virtual Type getType() const
Definition: attrdisplay.h:53
virtual std::string update()
Definition: attrdisplay.cpp:59
const std::string & getShortName() const
Definition: attrdisplay.h:63
std::string getValue() const
Definition: attrdisplay.h:56
virtual void add(Widget *const widget)
Definition: event.h:79
Definition: label.h:91
void adjustSize()
Definition: label.cpp:200
void setCaption(const std::string &caption)
Definition: label.cpp:264
void setVerticalScrollPolicy(const ScrollPolicy vPolicy)
void setHorizontalScrollPolicy(const ScrollPolicy hPolicy)
VertContainer * mAttrCont
Label * mCharacterPointsLabel
void setPointsNeeded(const AttributesT id, const int needed)
StatsPageBasic(const Widget2 *const widget)
std::string getStatsStr() const
void widgetResized(const Event &event)
void attributeChanged(const AttributesT id, const int64_t oldVal, const int64_t newVal)
void statChanged(const AttributesT id, const int oldVal1, const int oldVal2)
ScrollArea * mAttrScroll
void add2(Widget *const widget, const bool resizable, const int spacing)
void add1(Widget *const widget, const int spacing)
void setSize(const int width, const int height)
Definition: widget.cpp:367
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:302
void setSelectable(const bool selectable)
Definition: widget.h:948
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
#define new
Definition: debug_new.h:147
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define _(s)
Definition: gettext.h:35
PRAGMA45(GCC diagnostic push) PRAGMA45(GCC diagnostic ignored "-Wredundant-decls") PRAGMA45(GCC diagnostic pop) class TestMain
#define A_UNUSED
Definition: localconsts.h:160
@ PLAYER_CHAR_POINTS
Definition: attributes.h:49
@ PLAYER_CORR_POINTS
Definition: attributes.h:50
int32_t getAttribute(const AttributesT id)
Definition: playerinfo.cpp:102
const std::vector< BasicStat > & getBasicStats()
Definition: statdb.cpp:72
const bool Opaque_false
Definition: opaque.h:30
std::string strprintf(const char *const format,...)
AttributesT attr
Definition: basicstat.h:47
std::string name
Definition: basicstat.h:46
std::string tag
Definition: basicstat.h:45