Peoplemon  0.1.0
Peoplemon 3 game source documentation
Peoplemon.cpp
Go to the documentation of this file.
2 
3 #include <Core/Resources.hpp>
4 
5 namespace editor
6 {
7 namespace page
8 {
9 using namespace bl::gui;
10 
12 : Page(s)
13 , window(peoplemonDb, std::bind(&Peoplemon::onChange, this))
14 , firstSync(false) {
15  content = Box::create(LinePacker::create(LinePacker::Vertical));
16 
17  Box::Ptr row = Box::create(LinePacker::create(LinePacker::Horizontal, 8.f));
18  Button::Ptr but = Button::create("New Peoplemon");
19  but->getSignal(Event::LeftClicked).willAlwaysCall(std::bind(&Peoplemon::newPeoplemon, this));
20  row->pack(but, false, true);
21  saveBut = Button::create("Save");
22  saveBut->setColor(sf::Color::Green, sf::Color::Black);
23  saveBut->getSignal(Event::LeftClicked).willAlwaysCall(std::bind(&Peoplemon::save, this));
24  row->pack(saveBut, false, true);
25  but = Button::create("Reset");
26  but->getSignal(Event::LeftClicked).willAlwaysCall(std::bind(&Peoplemon::reset, this));
27  but->setColor(sf::Color::Red, sf::Color::Black);
28  row->pack(but, false, true);
29  content->pack(row, true, false);
30 
31  rowArea = ScrollArea::create(LinePacker::create(LinePacker::Vertical, 4.f));
32  content->pack(rowArea, true, true);
33 }
34 
35 void Peoplemon::update(float) {
36  if (!firstSync) {
37  firstSync = true;
38  if (!peoplemonDb.load()) { BL_LOG_ERROR << "Failed to load Peoplemon database"; }
40  sync();
41  }
42 }
43 
44 void Peoplemon::sync() {
45  rowArea->clearChildren(true);
46  const auto& allIds = core::pplmn::Peoplemon::validIds();
47 
48  int i = 0;
49  LinePacker::Ptr rowPack = LinePacker::create(LinePacker::Horizontal, 4.f, LinePacker::Uniform);
50  for (core::pplmn::Id id : allIds) {
51  Box::Ptr row = Box::create(rowPack);
52  row->setColor(i % 2 == 0 ? sf::Color(185, 185, 185) : sf::Color(70, 70, 70),
53  sf::Color(20, 85, 230));
54  Label::Ptr lbl = Label::create("Id: " + std::to_string(static_cast<int>(id)));
55  lbl->setColor(sf::Color(30, 85, 255), sf::Color::Transparent);
56  row->pack(lbl, false, true);
57 
58  Image::Ptr icon =
59  Image::create(ImageManager::load(core::pplmn::Peoplemon::thumbnailImage(id)));
60  icon->scaleToSize({40.f, 40.f});
61  row->pack(icon);
62 
63  lbl = Label::create(core::pplmn::Peoplemon::name(id));
64  lbl->setColor(sf::Color(255, 60, 90), sf::Color::Transparent);
65  row->pack(lbl, false, true);
66  Button::Ptr but = Button::create("Edit");
67  but->setColor(sf::Color(230, 230, 30), sf::Color::Black);
68  but->getSignal(Event::LeftClicked)
69  .willAlwaysCall(std::bind(&Peoplemon::editPeoplemon, this, id));
70  row->pack(but, false, true);
71  but = Button::create("Delete");
72  but->setColor(sf::Color(230, 30, 30), sf::Color::Black);
73  but->getSignal(Event::LeftClicked)
74  .willAlwaysCall(std::bind(&Peoplemon::deletePeoplemon, this, id));
75  row->pack(but, false, true);
76  rowArea->pack(row, true, false);
77  ++i;
78  }
79 }
80 
81 void Peoplemon::makeDirty() { saveBut->setColor(sf::Color::Yellow, sf::Color::Black); }
82 
83 void Peoplemon::save() {
84  if (!peoplemonDb.save()) { BL_LOG_ERROR << "Failed to save peoplemon database"; }
85  else { saveBut->setColor(sf::Color::Green, sf::Color::Black); }
86 }
87 
88 void Peoplemon::newPeoplemon() { window.open(parent, core::pplmn::Id::Unknown); }
89 
90 void Peoplemon::editPeoplemon(core::pplmn::Id ppl) { window.open(parent, ppl); }
91 
92 void Peoplemon::reset() {
93  if (!peoplemonDb.load()) { BL_LOG_ERROR << "Failed to load peoplemon database"; }
94  else {
95  sync();
96  saveBut->setColor(sf::Color::Green, sf::Color::Black);
97  }
98 }
99 
100 void Peoplemon::deletePeoplemon(core::pplmn::Id id) {
101  makeDirty();
102 
103  peoplemonDb.names.erase(id);
104  peoplemonDb.descriptions.erase(id);
105  peoplemonDb.types.erase(id);
106  peoplemonDb.abilities.erase(id);
107  peoplemonDb.stats.erase(id);
108  peoplemonDb.validMoves.erase(id);
109  peoplemonDb.learnedMoves.erase(id);
110  peoplemonDb.evolveLevels.erase(id);
111  peoplemonDb.evolveIds.erase(id);
112  peoplemonDb.evAwards.erase(id);
113  peoplemonDb.xpGroups.erase(id);
114  peoplemonDb.xpMults.erase(id);
115  peoplemonDb.catchRates.erase(id);
116 
117  sync();
118 }
119 
120 void Peoplemon::onChange() {
121  sync();
122  makeDirty();
123 }
124 
125 } // namespace page
126 } // namespace editor
Id
The id of a peoplemon.
Definition: Id.hpp:16
All classes and functionality used for implementing the game editor.
Definition: Tile.hpp:11
bool load()
Loads the database data from the save file.
Definition: PeoplemonDB.cpp:12
std::unordered_map< pplmn::Id, int > catchRates
Definition: PeoplemonDB.hpp:77
std::unordered_map< pplmn::Id, pplmn::Id > evolveIds
Definition: PeoplemonDB.hpp:73
bool save() const
Saves the database data to the save file.
Definition: PeoplemonDB.cpp:24
std::unordered_map< pplmn::Id, pplmn::SpecialAbility > abilities
Definition: PeoplemonDB.hpp:68
std::unordered_map< pplmn::Id, int > xpMults
Definition: PeoplemonDB.hpp:76
std::unordered_map< pplmn::Id, pplmn::Type > types
Definition: PeoplemonDB.hpp:67
std::unordered_map< pplmn::Id, std::unordered_set< pplmn::MoveId > > validMoves
Definition: PeoplemonDB.hpp:70
std::unordered_map< pplmn::Id, unsigned int > xpGroups
Definition: PeoplemonDB.hpp:75
std::unordered_map< pplmn::Id, pplmn::Stats > stats
Definition: PeoplemonDB.hpp:69
std::unordered_map< pplmn::Id, unsigned int > evolveLevels
Definition: PeoplemonDB.hpp:72
std::unordered_map< pplmn::Id, pplmn::Stats > evAwards
Definition: PeoplemonDB.hpp:74
std::unordered_map< pplmn::Id, std::string > descriptions
Definition: PeoplemonDB.hpp:66
std::unordered_map< pplmn::Id, std::unordered_map< unsigned int, pplmn::MoveId > > learnedMoves
Definition: PeoplemonDB.hpp:71
std::unordered_map< pplmn::Id, std::string > names
Definition: PeoplemonDB.hpp:65
static void setDataSource(file::PeoplemonDB &data)
Sets the data source for all peoplemon. Must remain in scope.
Definition: Peoplemon.cpp:94
static const std::vector< Id > & validIds()
Returtns the list of all valid ids.
Definition: Peoplemon.cpp:118
static const std::string & name(Id id)
Returns the name of the given Peoplemon.
Definition: Peoplemon.cpp:126
static std::string thumbnailImage(Id id)
Returns the path of the image to render as the peoplemon thumbnail.
Definition: Peoplemon.cpp:211
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47
void open(bl::gui::GUI *parent, core::pplmn::Id ppl)
Base class for all editor pages.
Definition: Page.hpp:32
bl::gui::Box::Ptr content
Definition: Page.hpp:69
bl::gui::GUI * parent
Definition: Page.hpp:70
Page for editing Peoplemon metadata.
Definition: Peoplemon.hpp:18
Peoplemon(core::system::Systems &systems)
Construct a new Peoplemon page.
Definition: Peoplemon.cpp:11
virtual void update(float dt) override
Does nothing.
Definition: Peoplemon.cpp:35