Zen 0.3.0
Loading...
Searching...
No Matches
ZEN_LayerList.cpp
1#include <zen/layer/ZEN_LayerList.h>
2
3namespace Zen {
4 LayerList::~LayerList() {
5 for (auto &l : m_layers) {
6 if (l) {
7 l->onDetach();
8 }
9 }
10 m_layers.clear();
11 }
12
13 void LayerList::pushLayer(Layer *layer) { pushLayer(std::unique_ptr<Layer>(layer)); }
14
15 void LayerList::pushOverlay(Layer *overlay) { pushOverlay(std::unique_ptr<Layer>(overlay)); }
16
17 void LayerList::pushLayer(std::unique_ptr<Layer> layer) {
18 const int priority = layer->getPriority();
19
20 auto start = m_layers.begin();
21 auto end = m_layers.begin() + m_layerInsertIndex;
22
23 auto position =
24 std::upper_bound(start, end, priority, [](int i, const std::unique_ptr<Layer> &l) {
25 return i < l->getPriority();
26 });
27 m_layers.insert(position, std::move(layer));
28 m_layerInsertIndex++;
29 }
30
31 void LayerList::pushOverlay(std::unique_ptr<Layer> overlay) {
32 m_layers.emplace_back(std::move(overlay));
33 }
34
35 void LayerList::popLayer(Layer *layer) {
36 if (!layer) {
37 return;
38 }
39
40 auto start = m_layers.begin();
41 auto end = m_layers.begin() + m_layerInsertIndex;
42
43 auto it = std::find_if(start, end, [layer](const std::unique_ptr<Layer> &l) {
44 return l.get() == layer;
45 });
46
47 if (it != m_layers.begin() + m_layerInsertIndex) {
48 m_layers.erase(it);
49 m_layerInsertIndex--;
50 }
51 }
52
53 void LayerList::popOverlay(Layer *overlay) {
54 if (!overlay)
55 return;
56
57 auto start = m_layers.begin() + m_layerInsertIndex;
58 auto end = m_layers.end();
59
60 auto it = std::find_if(start, end, [overlay](const std::unique_ptr<Layer> &l) {
61 return l.get() == overlay;
62 });
63
64 if (it != m_layers.end()) {
65 m_layers.erase(it);
66 }
67 }
68} // namespace Zen