core: Add a utility method to check if a display object is rooted to the current stage

This commit is contained in:
David Wendt 2021-01-16 15:53:35 -05:00 committed by Mike Welsh
parent 5acaf0bd01
commit 7f78bfa51d
1 changed files with 18 additions and 0 deletions

View File

@ -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.
///