Peoplemon  0.1.0
Peoplemon 3 game source documentation
Layers.cpp
Go to the documentation of this file.
2 
3 #include <BLIB/Util/Random.hpp>
4 
5 namespace editor
6 {
7 namespace page
8 {
9 using namespace bl::gui;
10 
11 Layers::Layers(const AppendCb& bottomAddCb, const AppendCb& ysortAddCb, const AppendCb& topAddCb,
12  const DeleteCb& delCb, const ShiftCb& shiftCb, const RenderFilterCb& filterCb)
13 : bottomAdd(bottomAddCb)
14 , ysortAdd(ysortAddCb)
15 , topAdd(topAddCb)
16 , delCb(delCb)
17 , shiftCb(shiftCb)
18 , filterCb(filterCb) {
19  contentWrapper = Box::create(LinePacker::create(LinePacker::Vertical, 4));
20  contentWrapper->setOutlineThickness(0.f);
21  content = Notebook::create();
22  content->setRequisition({1.f, 200.f});
23 }
24 
25 void Layers::sync(const std::vector<core::map::LayerSet>& levels,
26  const std::vector<std::vector<bool>>& filter) {
27  for (unsigned int i = 0; i < content->pageCount(); ++i) { content->removePageByIndex(i); }
28 
29  pages.clear();
30  pages.reserve(levels.size());
31  for (unsigned int i = 0; i < levels.size(); ++i) {
32  const auto& level = levels[i];
33  pages.emplace_back(
34  i, level, filter[i], filterCb, bottomAdd, ysortAdd, topAdd, delCb, shiftCb);
35  const std::string title = "Level " + std::to_string(pages.back().index);
36  content->addPage(title, title, pages.back().page);
37  }
38 }
39 
40 Box::Ptr Layers::getContent() { return contentWrapper; }
41 
42 void Layers::pack() { contentWrapper->pack(content, true, true); }
43 
44 void Layers::unpack() { content->remove(); }
45 
46 Layers::LevelTab::LevelTab(unsigned int i, const core::map::LayerSet& level,
47  const std::vector<bool>& filter, const RenderFilterCb& vcb,
48  const AppendCb& bottomAddCb, const AppendCb& ysortAddCb,
49  const AppendCb& topAddCb, const DeleteCb& delCb, const ShiftCb& shiftCb)
50 : index(i) {
51  page = ScrollArea::create(LinePacker::create(LinePacker::Vertical, 8));
52  page->setMaxSize({300, 175});
53 
54  Box::Ptr box = Box::create(LinePacker::create(LinePacker::Vertical, 8));
55  const auto visibleCb = [this, vcb](unsigned int layer, bool v) { vcb(index, layer, v); };
56  const auto deleteCb = [this, delCb](unsigned int layer) { delCb(index, layer); };
57  const auto onShift = [this, shiftCb](unsigned int layer, bool up) {
58  shiftCb(index, layer, up);
59  };
60 
61  Box::Ptr row = Box::create(LinePacker::create(LinePacker::Horizontal, 0, LinePacker::Uniform));
62  Button::Ptr addBut = Button::create("Add");
63  addBut->getSignal(Event::LeftClicked)
64  .willAlwaysCall(
65  [this, bottomAddCb, visibleCb](const Event&, Element*) { bottomAddCb(index); });
66  bottomBox = Box::create(LinePacker::create(LinePacker::Vertical, 4));
67  row->pack(Label::create("Bottom Layers"), true, true);
68  row->pack(addBut, false, false);
69  bottomBox->pack(row, true, false);
70 
71  row = Box::create(LinePacker::create(LinePacker::Horizontal, 0, LinePacker::Uniform));
72  addBut = Button::create("Add");
73  addBut->getSignal(Event::LeftClicked)
74  .willAlwaysCall([this, ysortAddCb](const Event&, Element*) { ysortAddCb(index); });
75  ysortBox = Box::create(LinePacker::create(LinePacker::Vertical, 4));
76  row->pack(Label::create("Y-Sort Layers"), true, true);
77  row->pack(addBut, false, false);
78  ysortBox->pack(row, true, false);
79 
80  row = Box::create(LinePacker::create(LinePacker::Horizontal, 0, LinePacker::Uniform));
81  addBut = Button::create("Add");
82  addBut->getSignal(Event::LeftClicked).willAlwaysCall([this, topAddCb](const Event&, Element*) {
83  topAddCb(index);
84  });
85  topBox = Box::create(LinePacker::create(LinePacker::Vertical, 4));
86  row->pack(Label::create("Top Layers"), true, true);
87  row->pack(addBut, false, false);
88  topBox->pack(row, true, false);
89 
90  box->pack(bottomBox, true, false);
91  box->pack(ysortBox, true, false);
92  box->pack(topBox, true, false);
93 
94  items.reserve(level.layerCount());
95  for (unsigned int i = 0; i < level.bottomLayers().size(); ++i) {
96  items.emplace_back(i, i != 0, true, filter[i], visibleCb, deleteCb, onShift);
97  bottomBox->pack(items.back().row, true, false);
98  }
99  for (unsigned int i = level.bottomLayers().size();
100  i < level.bottomLayers().size() + level.ysortLayers().size();
101  ++i) {
102  items.emplace_back(i, true, true, filter[i], visibleCb, deleteCb, onShift);
103  ysortBox->pack(items.back().row, true, false);
104  }
105  for (unsigned int i = level.bottomLayers().size() + level.ysortLayers().size();
106  i < level.layerCount();
107  ++i) {
108  items.emplace_back(
109  i, true, i != level.layerCount() - 1, filter[i], visibleCb, deleteCb, onShift);
110  topBox->pack(items.back().row, true, false);
111  }
112 
113  page->pack(box, true, true);
114 }
115 
116 Layers::LayerRow::LayerRow(unsigned int i, bool canUp, bool canDown, bool visible,
117  const VisibleCb& visibleCb, const DeleteCb& delCb,
118  const ShiftCb& shiftCb)
119 : index(i) {
120  row = Box::create(LinePacker::create(
121  LinePacker::Horizontal, 2.f, LinePacker::Compact, LinePacker::LeftAlign));
122  row->setOutlineThickness(0.f);
123 
124  name = Label::create("Layer " + std::to_string(i));
125  name->setColor(sf::Color(bl::util::Random::get<std::uint8_t>(80, 255),
126  bl::util::Random::get<std::uint8_t>(80, 255),
127  bl::util::Random::get<std::uint8_t>(80, 255)),
128  sf::Color::Transparent);
129  row->pack(name, true, false);
130 
131  visibleToggle = CheckButton::create("Visible");
132  visibleToggle->setValue(visible);
133  visibleToggle->getSignal(Event::ValueChanged)
134  .willAlwaysCall([this, visibleCb](const Event&, Element*) {
135  visibleCb(index, visibleToggle->getValue());
136  });
137  row->pack(visibleToggle, false, true);
138 
139  upBut = Button::create("Up");
140  upBut->setTooltip("Move this layer up the list. Higher layers are below lower layers");
141  upBut->setActive(canUp);
142  if (canUp) {
143  upBut->getSignal(Event::LeftClicked)
144  .willAlwaysCall([this, shiftCb](const Event&, Element*) { shiftCb(index, true); });
145  }
146  row->pack(upBut, false, true);
147 
148  downBut = Button::create("Down");
149  downBut->setTooltip("Move this layer down the list. Lower layers are on top of higher layers");
150  downBut->setActive(canDown);
151  if (canDown) {
152  downBut->getSignal(Event::LeftClicked)
153  .willAlwaysCall([this, shiftCb](const Event&, Element*) { shiftCb(index, false); });
154  }
155  row->pack(downBut, false, true);
156 
157  delBut = Button::create("Delete");
158  delBut->setColor(sf::Color(180, 15, 15), sf::Color(60, 0, 0));
159  delBut->getSignal(Event::LeftClicked).willAlwaysCall([this, delCb](const Event&, Element*) {
160  delCb(index);
161  });
162  row->pack(delBut, false, true);
163 }
164 
165 } // namespace page
166 } // namespace editor
All classes and functionality used for implementing the game editor.
Definition: Tile.hpp:11
Encapsulates a set of layers for a given map height. Maps have one LayerSet per height level,...
Definition: LayerSet.hpp:51
std::vector< TileLayer > & bottomLayers()
Returns a reference to the bottom tiles in this set.
Definition: LayerSet.cpp:33
std::vector< TileLayer > & ysortLayers()
Returns a reference to the sorted tiles in this set.
Definition: LayerSet.cpp:35
unsigned int layerCount() const
Returns the total number of layers contained.
Definition: LayerSet.cpp:39
std::function< void(unsigned int level, unsigned int layer)> DeleteCb
Definition: Layers.hpp:20
void unpack()
Hides the GUI content. Good for saving space when not active.
Definition: Layers.cpp:44
std::function< void(unsigned int level)> AppendCb
Definition: Layers.hpp:19
void pack()
Packs the GUI content.
Definition: Layers.cpp:42
void sync(const std::vector< core::map::LayerSet > &levels, const std::vector< std::vector< bool >> &filter)
Syncs the GUI elements with the map layers that exist.
Definition: Layers.cpp:25
std::function< void(unsigned int level, unsigned int layer, bool visible)> RenderFilterCb
Definition: Layers.hpp:23
bl::gui::Box::Ptr getContent()
Returns the GUI content to pack.
Definition: Layers.cpp:40
std::function< void(unsigned int level, unsigned int layer, bool up)> ShiftCb
Definition: Layers.hpp:21
Layers(const AppendCb &bottomAddCb, const AppendCb &ysortAddCb, const AppendCb &topAddCb, const DeleteCb &delCb, const ShiftCb &shiftCb, const RenderFilterCb &filterCb)
Construct a new Layers subpage.
Definition: Layers.cpp:11