From 7f78bfa51d0dd84369e80f814685065dd2d3bd53 Mon Sep 17 00:00:00 2001 From: David Wendt Date: Sat, 16 Jan 2021 15:53:35 -0500 Subject: [PATCH] core: Add a utility method to check if a display object is rooted to the current stage --- core/src/display_object.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/core/src/display_object.rs b/core/src/display_object.rs index 15228b607..5d17386f0 100644 --- a/core/src/display_object.rs +++ b/core/src/display_object.rs @@ -1134,6 +1134,24 @@ pub trait TDisplayObject<'gc>: }) } + /// Determine if this display object is currently on the stage. + fn is_on_stage(self, context: &mut UpdateContext<'_, 'gc, '_>) -> bool { + let mut ancestor = self.parent(); + while let Some(parent) = ancestor { + if parent.parent().is_some() { + ancestor = parent.parent(); + } else { + break; + } + } + + let ancestor = ancestor.unwrap_or_else(|| self.into()); + context + .levels + .values() + .any(|o| DisplayObject::ptr_eq(*o, ancestor)) + } + /// Obtain the top-most parent of the display tree hierarchy, or some kind /// of an error. ///