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