use bevy::prelude::*;
pub fn ancestors_from_world<'w: 's, 's>(
world: &'w World,
entities: &'w QueryState<(Entity, Option<&'s Parent>, Option<&'s Name>)>,
entity_id: Entity,
) -> impl Iterator<Item = Entity> + 'w {
let mut current_entity_id = entity_id;
std::iter::from_fn(move || {
#[allow(clippy::collapsible_match)]
if let Ok((_, parent, _)) = entities.get_manual(world, current_entity_id) {
if let Some(parent) = parent {
current_entity_id = **parent;
return Some(**parent);
}
}
None
})
}
pub fn compute_entity_path<'w: 's, 's>(
world: &'w World,
entities: &'w QueryState<(Entity, Option<&'s Parent>, Option<&'s Name>)>,
entity_id: Entity,
) -> rerun::EntityPath {
std::iter::once(rerun::EntityPathPart::new("world"))
.chain(
std::iter::once(entity_id)
.chain(ancestors_from_world(world, entities, entity_id))
.map(|entity_id| {
rerun::EntityPathPart::new({
entities
.get_manual(world, entity_id)
.ok()
.and_then(|(_, _, name)| name)
.map_or_else(
|| format!("{entity_id:?}"),
|name| format!("{entity_id:?}_{name}"),
)
})
})
.collect::<Vec<_>>()
.into_iter()
.rev(),
)
.collect()
}