Peoplemon  0.1.0
Peoplemon 3 game source documentation
StatBox.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Util/Random.hpp>
5 
6 namespace editor
7 {
8 namespace component
9 {
10 namespace
11 {
12 constexpr float InputWidth = 60.f;
13 
14 core::pplmn::Stats genRandomEVs(unsigned int level) {
15  using namespace core::pplmn;
16 
17  if (level < 7) return {};
18 
19  Stats stats;
20  const unsigned int l = level - 7;
21  const unsigned int iterCount = l * l / 12;
22  const std::vector<Id>& ids = Peoplemon::validIds();
23  for (unsigned int i = 0; i < iterCount; ++i) {
24  const unsigned int j = bl::util::Random::get<unsigned int>(0, ids.size());
25  stats.addEVs(Peoplemon::evAward(ids[j]));
26  if (stats.sum() >= Stats::MaxEVSum) break;
27  }
28 
29  return stats;
30 }
31 
32 } // namespace
33 using namespace bl::gui;
34 
36 : mode(m)
37 , showRandom(sr)
38 , level(50) {
39  const auto f = std::bind(&StatBox::onChange, this, std::placeholders::_2);
40 
41  hpEntry = TextEntry::create();
42  hpEntry->setMode(TextEntry::Mode::Integer | TextEntry::Mode::Unsigned);
43  hpEntry->setRequisition({InputWidth, 1.f});
44  hpEntry->getSignal(Event::ValueChanged).willAlwaysCall(f);
45 
46  atkEntry = TextEntry::create();
47  atkEntry->setMode(TextEntry::Mode::Integer | TextEntry::Mode::Unsigned);
48  atkEntry->setRequisition({InputWidth, 1.f});
49  atkEntry->getSignal(Event::ValueChanged).willAlwaysCall(f);
50 
51  defEntry = TextEntry::create();
52  defEntry->setMode(TextEntry::Mode::Integer | TextEntry::Mode::Unsigned);
53  defEntry->setRequisition({InputWidth, 1.f});
54  defEntry->getSignal(Event::ValueChanged).willAlwaysCall(f);
55 
56  spAtkEntry = TextEntry::create();
57  spAtkEntry->setMode(TextEntry::Mode::Integer | TextEntry::Mode::Unsigned);
58  spAtkEntry->setRequisition({InputWidth, 1.f});
59  spAtkEntry->getSignal(Event::ValueChanged).willAlwaysCall(f);
60 
61  spDefEntry = TextEntry::create();
62  spDefEntry->setMode(TextEntry::Mode::Integer | TextEntry::Mode::Unsigned);
63  spDefEntry->setRequisition({InputWidth, 1.f});
64  spDefEntry->getSignal(Event::ValueChanged).willAlwaysCall(f);
65 
66  spdEntry = TextEntry::create();
67  spdEntry->setMode(TextEntry::Mode::Integer | TextEntry::Mode::Unsigned);
68  spdEntry->setRequisition({InputWidth, 1.f});
69  spdEntry->getSignal(Event::ValueChanged).willAlwaysCall(f);
70 
71  reset();
72 }
73 
75  hpEntry->setInput("0");
76  atkEntry->setInput("0");
77  defEntry->setInput("0");
78  spAtkEntry->setInput("0");
79  spDefEntry->setInput("0");
80  spdEntry->setInput("0");
81 }
82 
85  ret.hp = std::atoi(hpEntry->getInput().c_str());
86  ret.atk = std::atoi(atkEntry->getInput().c_str());
87  ret.def = std::atoi(defEntry->getInput().c_str());
88  ret.spatk = std::atoi(spAtkEntry->getInput().c_str());
89  ret.spdef = std::atoi(spDefEntry->getInput().c_str());
90  ret.spd = std::atoi(spdEntry->getInput().c_str());
91  return ret;
92 }
93 
95  hpEntry->setInput(std::to_string(val.hp));
96  atkEntry->setInput(std::to_string(val.atk));
97  defEntry->setInput(std::to_string(val.def));
98  spAtkEntry->setInput(std::to_string(val.spatk));
99  spDefEntry->setInput(std::to_string(val.spdef));
100  spdEntry->setInput(std::to_string(val.spd));
101  syncSum();
102 }
103 
104 void StatBox::syncSum() {
105  const core::pplmn::Stats stats = currentValue();
106  sumLabel->setText("Sum: " + std::to_string(stats.sum()));
107 }
108 
109 void StatBox::notifyLevel(unsigned int l) { level = l; }
110 
111 void StatBox::pack(Box& content) {
112  LinePacker::Ptr rowPacker = LinePacker::create(LinePacker::Horizontal, 4.f);
113 
114  Box::Ptr row = Box::create(rowPacker);
115  Label::Ptr lbl = Label::create("HP:");
116  lbl->setHorizontalAlignment(RenderSettings::Left);
117  row->pack(lbl, true, true);
118  row->pack(hpEntry, false, true);
119  content.pack(row, true, false);
120 
121  row = Box::create(rowPacker);
122  lbl = Label::create("Atk:");
123  lbl->setHorizontalAlignment(RenderSettings::Left);
124  row->pack(lbl, true, true);
125  row->pack(atkEntry, false, true);
126  content.pack(row, true, false);
127 
128  row = Box::create(rowPacker);
129  lbl = Label::create("Def:");
130  lbl->setHorizontalAlignment(RenderSettings::Left);
131  row->pack(lbl, true, true);
132  row->pack(defEntry, false, true);
133  content.pack(row, true, false);
134 
135  row = Box::create(rowPacker);
136  lbl = Label::create("SpAtk:");
137  lbl->setHorizontalAlignment(RenderSettings::Left);
138  row->pack(lbl, true, true);
139  row->pack(spAtkEntry, false, true);
140  content.pack(row, true, false);
141 
142  row = Box::create(rowPacker);
143  lbl = Label::create("SpDef:");
144  lbl->setHorizontalAlignment(RenderSettings::Left);
145  row->pack(lbl, true, true);
146  row->pack(spDefEntry, false, true);
147  content.pack(row, true, false);
148 
149  row = Box::create(rowPacker);
150  lbl = Label::create("Spd:");
151  lbl->setHorizontalAlignment(RenderSettings::Left);
152  row->pack(lbl, true, true);
153  row->pack(spdEntry, false, true);
154  content.pack(row, true, false);
155 
156  sumLabel = Label::create("");
157  content.pack(sumLabel, true, false);
158 
159  if (showRandom) {
160  Button::Ptr but = Button::create("Randomize");
161  but->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
162  if (mode == Mode::IV) {
163  constexpr unsigned int max = core::pplmn::Stats::MaxIVStat;
164  hpEntry->setInput(std::to_string(bl::util::Random::get<unsigned int>(0, max)));
165  atkEntry->setInput(std::to_string(bl::util::Random::get<unsigned int>(0, max)));
166  defEntry->setInput(std::to_string(bl::util::Random::get<unsigned int>(0, max)));
167  spAtkEntry->setInput(std::to_string(bl::util::Random::get<unsigned int>(0, max)));
168  spDefEntry->setInput(std::to_string(bl::util::Random::get<unsigned int>(0, max)));
169  spdEntry->setInput(std::to_string(bl::util::Random::get<unsigned int>(0, max)));
170  }
171  else {
172  update(genRandomEVs(level));
173  }
174  syncSum();
175  });
176  content.pack(but);
177  }
178 }
179 
180 void StatBox::onChange(Element* e) {
181  using core::pplmn::Stats;
182 
183  TextEntry* te = dynamic_cast<TextEntry*>(e);
184  if (te) {
185  const Stats val = currentValue();
186  if (mode == EV) {
187  if (val.sum() > Stats::MaxEVSum) {
188  std::stringstream ss;
189  ss << "EV values cannot sum to more than " << Stats::MaxEVSum;
190  bl::dialog::tinyfd_messageBox("Warning", ss.str().c_str(), "ok", "warning", 1);
191  }
192  for (const core::pplmn::Stat stat : Stats::IterableStats) {
193  if (val.get(stat) > Stats::MaxEVStat) {
194  std::stringstream ss;
195  ss << Stats::statToString(stat) << " EV stat cannot be above "
196  << Stats::MaxEVStat;
197  bl::dialog::tinyfd_messageBox("Warning", ss.str().c_str(), "ok", "warning", 1);
198  }
199  }
200  }
201  else {
202  for (const core::pplmn::Stat stat : Stats::IterableStats) {
203  if (val.get(stat) > Stats::MaxIVStat) {
204  std::stringstream ss;
205  ss << Stats::statToString(stat) << " IV stat cannot be above "
206  << Stats::MaxIVStat;
207  bl::dialog::tinyfd_messageBox("Warning", ss.str().c_str(), "ok", "warning", 1);
208  }
209  }
210  }
211 
212  syncSum();
213  }
214 }
215 
216 } // namespace component
217 } // namespace editor
Stat
Represents a single stat. Used as an offset to access Stats as an array.
Definition: Stat.hpp:16
All classes and functionality used for implementing the game editor.
Definition: Tile.hpp:11
Collection of classes and enums pertaining to Peoplemon themselves.
Definition: Ailment.hpp:9
static const std::vector< Id > & validIds()
Returtns the list of all valid ids.
Definition: Peoplemon.cpp:118
static const Stats & evAward(Id id)
Returns the ev points awarded for defeating the given peoplemon.
Definition: Peoplemon.cpp:176
Stats for Peoplemon. This struct is used for base stats, EVs, IVs, battle increases/decreases,...
Definition: Stats.hpp:19
static constexpr int MaxEVStat
The maximum amount that a single EV can be.
Definition: Stats.hpp:110
static constexpr int MaxIVStat
The maximum amount that a single IV can be.
Definition: Stats.hpp:113
int & get(Stat stat)
Returns a reference to the given stat.
Definition: Stats.cpp:39
static const std::array< Stat, 6 > IterableStats
Helper array to iterate over stats in loop.
Definition: Stats.hpp:104
void addEVs(const Stats &evs)
Adds the given EV points to this set of stats while obeying the constraints on EV values and sums.
Definition: Stats.cpp:31
static const char * statToString(Stat stat)
Converts the given stat to its string representation.
Definition: Stats.cpp:74
static constexpr int MaxEVSum
The maximum amount that EVs or IVs can sum to.
Definition: Stats.hpp:107
int sum() const
Returns the sum of the 6 stats that are used for EV calculations.
Definition: Stats.cpp:25
void pack(bl::gui::Box &container)
Packs the GUI elements into the given container.
Definition: StatBox.cpp:111
void reset()
Resets all stats to 0.
Definition: StatBox.cpp:74
core::pplmn::Stats currentValue() const
Returns the current Stats value entered.
Definition: StatBox.cpp:83
Mode
Mode the entry is in.
Definition: StatBox.hpp:20
StatBox(Mode mode, bool showRandomBut=true)
Construct a new Stat Box component.
Definition: StatBox.cpp:35
void notifyLevel(unsigned int level)
Make the statbox aware of the level of the peoplemon it is editing. Used for random EV generation.
Definition: StatBox.cpp:109
void update(const core::pplmn::Stats &value)
Updates the GUI elements with the given value.
Definition: StatBox.cpp:94