Peoplemon  0.1.0
Peoplemon 3 game source documentation
Catchables.cpp
Go to the documentation of this file.
2 
3 #include <Core/Resources.hpp>
6 
7 namespace editor
8 {
9 namespace page
10 {
11 namespace
12 {
13 const std::array<sf::Color, 10> colors = {sf::Color(89, 147, 240, 150),
14  sf::Color(89, 237, 240, 150),
15  sf::Color(89, 240, 164, 150),
16  sf::Color(139, 240, 89, 150),
17  sf::Color(227, 240, 89, 150),
18  sf::Color(240, 200, 89, 150),
19  sf::Color(240, 144, 89, 150),
20  sf::Color(240, 89, 119, 150),
21  sf::Color(240, 89, 237, 150),
22  sf::Color(170, 89, 240, 150)};
23 }
24 
25 using namespace bl::gui;
26 
28 : map(m)
29 , editWindow(std::bind(&Catchables::onEdit, this)) {
30  content = Box::create(LinePacker::create(LinePacker::Vertical, 8.f));
31  scrollRegion = ScrollArea::create(LinePacker::create(LinePacker::Vertical, 8.f));
32 
33  scrollRegion->setColor(sf::Color::Transparent, sf::Color::Black);
34  scrollRegion->setOutlineThickness(1.5f);
35 
36  Box::Ptr row = Box::create(LinePacker::create(LinePacker::Horizontal, 0.f));
37  noCatchBut = RadioButton::create("No wild peoplemon", "none");
38  noCatchBut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
39  active = 0;
40  });
41  Button::Ptr addbut = Button::create("Add Wild Region");
42  addbut->setHorizontalAlignment(RenderSettings::Right);
43  addbut->getSignal(Event::LeftClicked).willAlwaysCall([this](const Event&, Element*) {
44  map.addCatchRegion();
45  });
46  row->pack(noCatchBut, false, true);
47  row->pack(addbut, true, true);
48 
49  content->pack(row, true, false);
50  content->pack(scrollRegion, true, true);
51 }
52 
53 void Catchables::setGUI(GUI* g) { gui = g; }
54 
55 Element::Ptr Catchables::getContent() { return content; }
56 
57 std::uint8_t Catchables::selected() const { return active; }
58 
59 sf::Color Catchables::getColor(std::uint8_t i) {
60  return i > 0 ? colors[(i - 1) % colors.size()] : sf::Color::Transparent;
61 }
62 
64  scrollRegion->clearChildren(true);
65  noCatchBut->setValue(true);
66  active = 0;
67 
68  rows.clear();
69  rows.reserve(map.catchRegions().size());
70  for (const auto& region : map.catchRegions()) {
71  rows.emplace_back(createRow(
72  region,
73  std::bind(&Catchables::rowClicked, this, std::placeholders::_1, std::placeholders::_2),
74  noCatchBut->getRadioGroup()));
75  scrollRegion->pack(rows.back(), true, false);
76  }
77 
78  refreshColors();
79 }
80 
81 void Catchables::refreshColors() {
82  for (unsigned int i = 0; i < rows.size(); ++i) {
83  rows[i]->setColor(getColor(i + 1), sf::Color::Black);
84  }
85 }
86 
87 Box::Ptr Catchables::createRow(const core::map::CatchRegion& region, const RowSelect& oscb,
88  bl::gui::RadioButton::Group* group) {
89  Box::Ptr row = Box::create(LinePacker::create(
90  LinePacker::Horizontal, 4.f, LinePacker::Compact, LinePacker::RightAlign));
91  RadioButton::Ptr select = RadioButton::create(region.name, region.name, group);
92  Button::Ptr editBut = Button::create("Edit");
93  Button::Ptr delBut = Button::create("Delete");
94 
95  select->getSignal(Event::LeftClicked)
96  .willAlwaysCall(std::bind(oscb, row.get(), RowAction::Select));
97  editBut->getSignal(Event::LeftClicked)
98  .willAlwaysCall(std::bind(oscb, row.get(), RowAction::Edit));
99  delBut->getSignal(Event::LeftClicked)
100  .willAlwaysCall(std::bind(oscb, row.get(), RowAction::Remove));
101 
102  delBut->setColor(sf::Color::Red, sf::Color::Black);
103  select->setHorizontalAlignment(RenderSettings::Left);
104 
105  row->pack(delBut, false, true);
106  row->pack(editBut, false, true);
107  row->pack(select, true, true);
108 
109  return row;
110 }
111 
112 void Catchables::rowClicked(const bl::gui::Box* row, RowAction action) {
113  int i = -1;
114  for (unsigned int j = 0; j < rows.size(); ++j) {
115  if (rows[j].get() == row) {
116  i = j;
117  break;
118  }
119  }
120  if (i < 0) {
121  BL_LOG_ERROR << "Invalid row clicked";
122  return;
123  }
124 
125  switch (action) {
126  case Remove:
127  map.removeCatchRegion(i);
128  break;
129 
130  case Edit:
131  editing = i;
132  editWindow.open(gui, map.catchRegions()[i]);
133  break;
134 
135  case Select:
136  default:
137  active = i + 1;
138  break;
139  }
140 }
141 
142 void Catchables::onEdit() { map.editCatchRegion(editing, editWindow.getValue()); }
143 
144 } // namespace page
145 } // namespace editor
std::array< glm::vec4, 6 > colors
Definition: WildIntro.cpp:27
All classes and functionality used for implementing the game editor.
Definition: Tile.hpp:11
Represents a class of catchable peoplemon.
Definition: CatchRegion.hpp:18
const core::map::CatchRegion & getValue()
Returns the current region value as inputed by the user.
void open(bl::gui::GUI *gui, const core::map::CatchRegion &region)
Opens the window with the given region value.
Wrapper over the core::Map class that is directly usable in a bl::gui::GUI.
Definition: EditMap.hpp:28
void editCatchRegion(std::uint8_t index, const core::map::CatchRegion &zone)
Modifies the catch region at the given index.
Definition: EditMap.cpp:855
const std::vector< core::map::CatchRegion > & catchRegions() const
Returns a reference to all catch regions.
Definition: EditMap.cpp:851
void addCatchRegion()
Adds a new catch region to the map.
Definition: EditMap.cpp:849
void removeCatchRegion(std::uint8_t index)
Removes the catch region at the given index.
Definition: EditMap.cpp:859
Editor subpage for selecting catch tiles to place.
Definition: Catchables.hpp:23
void setGUI(bl::gui::GUI *gui)
Sets the parent GUI object.
Definition: Catchables.cpp:53
void refresh()
Refreshes GUI elements.
Definition: Catchables.cpp:63
Catchables(component::EditMap &map)
Construct a new Catchables GUI.
Definition: Catchables.cpp:27
static sf::Color getColor(std::uint8_t index)
Returns the color to use for the given catch region.
Definition: Catchables.cpp:59
std::uint8_t selected() const
Returns the currently selected catch type.
Definition: Catchables.cpp:57
bl::gui::Element::Ptr getContent()
Returns the GUI element to pack.
Definition: Catchables.cpp:55