use super::*;
use bevy::ecs::system::EntityCommands;
use std::fmt::Debug;
#[derive(Clone, Debug)]
pub enum Dest {
Root,
Replace(Entity),
ReplaceChildren(Entity),
Append(Entity),
}
impl From<Entity> for Dest {
fn from(id: Entity) -> Dest {
Dest::Replace(id)
}
}
impl Dest {
pub fn entity<'a>(&self, commands: &'a mut Commands) -> EntityCommands<'a> {
use Dest::*;
match self {
Append(id) => {
let mut child = None;
commands.entity(*id).with_children(|parent| {
child = Some(parent.spawn_empty().id());
});
commands.entity(child.unwrap())
}
Replace(id) => commands.entity(*id),
ReplaceChildren(id) => {
commands.entity(*id).despawn_related::<Children>();
let mut child = None;
commands.entity(*id).with_children(|parent| {
child = Some(parent.spawn_empty().id());
});
commands.entity(child.unwrap())
}
Root => commands.spawn_empty(),
}
}
pub fn get_entity<'a>(&self, commands: &'a mut Commands) -> Option<EntityCommands<'a>> {
use Dest::*;
match self {
Append(id) => {
let mut child = None;
if let Ok(mut ecommands) = commands.get_entity(*id) {
ecommands.with_children(|parent| {
child = Some(parent.spawn_empty().id());
});
}
child.and_then(|id| commands.get_entity(id).ok())
}
Replace(id) => commands.get_entity(*id).ok(),
ReplaceChildren(id) => {
if let Ok(mut ecommands) = commands.get_entity(*id) {
ecommands.despawn_related::<Children>();
}
let mut child = None;
if let Ok(mut ecommands) = commands.get_entity(*id) {
ecommands.with_children(|parent| {
child = Some(parent.spawn_empty().id());
});
}
child.and_then(|id| commands.get_entity(id).ok())
}
Root => Some(commands.spawn_empty()),
}
}
}