Peoplemon  0.1.0
Peoplemon 3 game source documentation
Moves.cpp
Go to the documentation of this file.
1 #include <Editor/Pages/Moves.hpp>
2 
4 
5 namespace editor
6 {
7 namespace page
8 {
9 namespace
10 {
11 std::vector<core::pplmn::MoveId> getSorted(const core::file::MoveDB& db) {
12  std::vector<core::pplmn::MoveId> ids;
13  ids.reserve(db.names.size());
14  for (const auto& p : db.names) { ids.emplace_back(p.first); }
15  std::sort(ids.begin(), ids.end());
16  return ids;
17 }
18 } // namespace
19 
20 using namespace bl::gui;
21 
23 : Page(s)
24 , moveWindow(moveDb, std::bind(&Moves::onChange, this)) {
25  content = Box::create(LinePacker::create(LinePacker::Vertical, 8.f));
26 
27  Box::Ptr row = Box::create(LinePacker::create(LinePacker::Horizontal, 8.f));
28  Button::Ptr but = Button::create("New Move");
29  but->getSignal(Event::LeftClicked).willAlwaysCall(std::bind(&Moves::newMove, this));
30  row->pack(but, false, true);
31  saveBut = Button::create("Save");
32  saveBut->setColor(sf::Color::Green, sf::Color::Black);
33  saveBut->getSignal(Event::LeftClicked).willAlwaysCall(std::bind(&Moves::save, this));
34  row->pack(saveBut, false, true);
35  but = Button::create("Reset");
36  but->getSignal(Event::LeftClicked).willAlwaysCall(std::bind(&Moves::reset, this));
37  but->setColor(sf::Color::Red, sf::Color::Black);
38  row->pack(but, false, true);
39  content->pack(row, true, false);
40 
41  rowArea = ScrollArea::create(LinePacker::create(LinePacker::Vertical, 4.f));
42  content->pack(rowArea, true, true);
43 
44  if (!moveDb.load()) BL_LOG_ERROR << "Failed to load move database";
46  sync();
47 }
48 
49 void Moves::sync() {
50  rowArea->clearChildren(true);
51  const std::vector<core::pplmn::MoveId> allIds = getSorted(moveDb);
52 
53  int i = 0;
54  LinePacker::Ptr rowPack = LinePacker::create(LinePacker::Horizontal, 4.f, LinePacker::Uniform);
55  for (core::pplmn::MoveId id : allIds) {
56  Box::Ptr row = Box::create(rowPack);
57  row->setColor(i % 2 == 0 ? sf::Color(185, 185, 185) : sf::Color(70, 70, 70),
58  sf::Color(20, 85, 230));
59  Label::Ptr lbl = Label::create("Id: " + std::to_string(static_cast<int>(id)));
60  lbl->setColor(sf::Color(30, 85, 255), sf::Color::Transparent);
61  row->pack(lbl, false, true);
62  lbl = Label::create(core::pplmn::Move::name(id));
63  lbl->setColor(sf::Color(255, 60, 90), sf::Color::Transparent);
64  row->pack(lbl, false, true);
65  Button::Ptr but = Button::create("Edit");
66  but->setColor(sf::Color(230, 230, 30), sf::Color::Black);
67  but->getSignal(Event::LeftClicked).willAlwaysCall(std::bind(&Moves::editMove, this, id));
68  row->pack(but, false, true);
69  but = Button::create("Delete");
70  but->setColor(sf::Color(230, 30, 30), sf::Color::Black);
71  but->getSignal(Event::LeftClicked).willAlwaysCall(std::bind(&Moves::deleteMove, this, id));
72  row->pack(but, false, true);
73  rowArea->pack(row, true, false);
74  ++i;
75  }
76 }
77 
78 void Moves::makeDirty() { saveBut->setColor(sf::Color::Yellow, sf::Color::Black); }
79 
80 void Moves::save() {
81  if (!moveDb.save()) { BL_LOG_ERROR << "Failed to save move database"; }
82  else {
83  saveBut->setColor(sf::Color::Green, sf::Color::Black);
84  }
85 }
86 
87 void Moves::newMove() { moveWindow.open(parent, core::pplmn::MoveId::Unknown); }
88 
89 void Moves::editMove(core::pplmn::MoveId move) { moveWindow.open(parent, move); }
90 
91 void Moves::reset() {
92  if (!moveDb.load()) { BL_LOG_ERROR << "Failed to load move database"; }
93  else {
94  sync();
95  saveBut->setColor(sf::Color::Green, sf::Color::Black);
96  }
97 }
98 
99 void Moves::deleteMove(core::pplmn::MoveId move) {
100  makeDirty();
101 
102  moveDb.names.erase(move);
103  moveDb.descriptions.erase(move);
104  moveDb.types.erase(move);
105  moveDb.damages.erase(move);
106  moveDb.accuracies.erase(move);
107  moveDb.priorities.erase(move);
108  moveDb.pps.erase(move);
109  moveDb.contactors.erase(move);
110  moveDb.specials.erase(move);
111  moveDb.effects.erase(move);
112  moveDb.effectChances.erase(move);
113  moveDb.effectIntensities.erase(move);
114  moveDb.effectSelves.erase(move);
115 
116  sync();
117 }
118 
119 void Moves::onChange() {
120  sync();
121  makeDirty();
122 }
123 
124 void Moves::update(float) {}
125 
126 } // namespace page
127 } // namespace editor
MoveId
The id of a move.
Definition: MoveId.hpp:16
All classes and functionality used for implementing the game editor.
Definition: Tile.hpp:11
Stores the metadata of all peoplemon moves.
Definition: MoveDB.hpp:22
std::unordered_map< pplmn::MoveId, bool > specials
Definition: MoveDB.hpp:71
std::unordered_map< pplmn::MoveId, bool > contactors
Definition: MoveDB.hpp:70
std::unordered_map< pplmn::MoveId, std::int32_t > damages
Definition: MoveDB.hpp:66
std::unordered_map< pplmn::MoveId, std::uint32_t > pps
Definition: MoveDB.hpp:69
std::unordered_map< pplmn::MoveId, std::int32_t > accuracies
Definition: MoveDB.hpp:67
std::unordered_map< pplmn::MoveId, std::string > descriptions
Definition: MoveDB.hpp:64
std::unordered_map< pplmn::MoveId, std::int32_t > effectChances
Definition: MoveDB.hpp:73
std::unordered_map< pplmn::MoveId, std::int32_t > priorities
Definition: MoveDB.hpp:68
std::unordered_map< pplmn::MoveId, pplmn::MoveEffect > effects
Definition: MoveDB.hpp:72
bool load()
Loads the moves from the data file.
Definition: MoveDB.cpp:12
std::unordered_map< pplmn::MoveId, std::int32_t > effectIntensities
Definition: MoveDB.hpp:74
bool save() const
Saves the moves to the data file.
Definition: MoveDB.cpp:22
std::unordered_map< pplmn::MoveId, std::string > names
Definition: MoveDB.hpp:63
std::unordered_map< pplmn::MoveId, pplmn::Type > types
Definition: MoveDB.hpp:65
std::unordered_map< pplmn::MoveId, bool > effectSelves
Definition: MoveDB.hpp:75
static void setDataSource(file::MoveDB &source)
Set the data source for each method.
Definition: Move.cpp:46
static const std::string & name(MoveId move)
Returns the name of the given move.
Definition: Move.cpp:71
Owns all primary systems and a reference to the engine.
Definition: Systems.hpp:47
void open(bl::gui::GUI *parent, core::pplmn::MoveId move)
Opens the window and populates for the given move. Pass MoveId::Unknown for a new move.
Page for editing move metadata.
Definition: Moves.hpp:20
virtual void update(float dt) override
Does nothing.
Definition: Moves.cpp:124
Moves(core::system::Systems &systems)
Construct a new Moves page.
Definition: Moves.cpp:22
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