7
7
#include " growl/core/input/event.h"
8
8
#include " growl/core/scripting/script.h"
9
9
#include " growl/scene/scene.h"
10
+ #include < algorithm>
10
11
#include < string>
11
12
#ifdef GROWL_IMGUI
12
13
#include " glm/ext/scalar_constants.hpp"
@@ -40,15 +41,22 @@ Node* Node::addChild(std::unique_ptr<Node> node) {
40
41
children.emplace_back (std::move (node));
41
42
Node* n = children.back ().get ();
42
43
n->setDepth (depth + 1 );
44
+ children_z_order.emplace_back (n);
43
45
return n;
44
46
}
45
47
46
48
void Node::removeChild (int i) {
47
49
children.erase (std::next (children.begin (), i));
50
+ children_z_order.clear ();
51
+ for (auto & child : children) {
52
+ children_z_order.emplace_back (child.get ());
53
+ }
54
+ reorderZ ();
48
55
}
49
56
50
57
void Node::clear () {
51
58
children.clear ();
59
+ children_z_order.clear ();
52
60
}
53
61
54
62
void Node::setDepth (int depth) {
@@ -167,7 +175,7 @@ void Node::draw(Batch& batch, float parent_alpha) {
167
175
}
168
176
169
177
void Node::onDraw (Batch& batch, float parent_alpha, glm::mat4x4 transform) {
170
- for (auto & child : children ) {
178
+ for (auto child : children_z_order ) {
171
179
child->draw (batch, parent_alpha);
172
180
}
173
181
}
@@ -176,13 +184,31 @@ void Node::setClickListener(std::function<bool(float, float)> listener) {
176
184
this ->click_listener = std::move (listener);
177
185
}
178
186
187
+ void Node::setZPriority (float z) {
188
+ this ->z = z;
189
+ if (this ->parent ) {
190
+ this ->parent ->reorderZ ();
191
+ }
192
+ }
193
+
194
+ void Node::cancelEvent () {
195
+ event_cancelled = true ;
196
+ if (parent) {
197
+ parent->cancelEvent ();
198
+ }
199
+ }
200
+
179
201
bool Node::onEvent (const InputEvent& event) {
180
202
if (InputProcessor::onEvent (event)) {
181
203
return true ;
182
204
}
183
205
bool handled = false ;
206
+ this ->event_cancelled = false ;
184
207
for (auto & child : children) {
185
208
handled |= child->onEvent (event);
209
+ if (this ->event_cancelled ) {
210
+ break ;
211
+ }
186
212
}
187
213
return onPostEvent (event, handled);
188
214
}
@@ -240,6 +266,12 @@ void Node::computeLocalTransform() {
240
266
}
241
267
}
242
268
269
+ void Node::reorderZ () {
270
+ std::stable_sort (
271
+ children_z_order.begin (), children_z_order.end (),
272
+ [](const Node* a, const Node* b) -> bool { return a->z < b->z ; });
273
+ }
274
+
243
275
bool Node::onMouseEvent (const InputMouseEvent& event) {
244
276
if (!bound_script_obj) {
245
277
return onMouseEventRaw (event);
@@ -410,7 +442,11 @@ bool Node::processClick(float x, float y, PointerEventType type) {
410
442
default :
411
443
return false ;
412
444
}
445
+ } else if (type == PointerEventType::Move) {
446
+ // Move outside, cancel click
447
+ click_listener_down = false ;
413
448
}
449
+
414
450
if (type == PointerEventType::Up) {
415
451
click_listener_down = false ;
416
452
}
0 commit comments