2023-01-12 18:46:11 +00:00
|
|
|
//! Extension to [`EntityCommands`] to modify [`bevy_hierarchy`] hierarchies
|
|
|
|
//! while preserving [`GlobalTransform`].
|
|
|
|
|
|
|
|
use bevy_ecs::{prelude::Entity, system::Command, system::EntityCommands, world::World};
|
|
|
|
use bevy_hierarchy::{AddChild, RemoveParent};
|
|
|
|
|
|
|
|
#[cfg(doc)]
|
|
|
|
use bevy_hierarchy::BuildChildren;
|
|
|
|
|
|
|
|
use crate::{GlobalTransform, Transform};
|
|
|
|
|
|
|
|
/// Command similar to [`AddChild`], but updating the child transform to keep
|
|
|
|
/// it at the same [`GlobalTransform`].
|
|
|
|
///
|
|
|
|
/// You most likely want to use [`BuildChildrenTransformExt::set_parent_in_place`]
|
|
|
|
/// method on [`EntityCommands`] instead.
|
|
|
|
pub struct AddChildInPlace {
|
|
|
|
/// Parent entity to add the child to.
|
|
|
|
pub parent: Entity,
|
|
|
|
/// Child entity to add.
|
|
|
|
pub child: Entity,
|
|
|
|
}
|
|
|
|
impl Command for AddChildInPlace {
|
2023-06-12 17:53:47 +00:00
|
|
|
fn apply(self, world: &mut World) {
|
2023-01-12 18:46:11 +00:00
|
|
|
let hierarchy_command = AddChild {
|
|
|
|
child: self.child,
|
|
|
|
parent: self.parent,
|
|
|
|
};
|
2023-06-12 17:53:47 +00:00
|
|
|
hierarchy_command.apply(world);
|
2023-01-12 18:46:11 +00:00
|
|
|
// FIXME: Replace this closure with a `try` block. See: https://github.com/rust-lang/rust/issues/31436.
|
|
|
|
let mut update_transform = || {
|
|
|
|
let parent = *world.get_entity(self.parent)?.get::<GlobalTransform>()?;
|
|
|
|
let child_global = *world.get_entity(self.child)?.get::<GlobalTransform>()?;
|
|
|
|
let mut child_entity = world.get_entity_mut(self.child)?;
|
|
|
|
let mut child = child_entity.get_mut::<Transform>()?;
|
|
|
|
*child = child_global.reparented_to(&parent);
|
|
|
|
Some(())
|
|
|
|
};
|
|
|
|
update_transform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Command similar to [`RemoveParent`], but updating the child transform to keep
|
|
|
|
/// it at the same [`GlobalTransform`].
|
|
|
|
///
|
|
|
|
/// You most likely want to use [`BuildChildrenTransformExt::remove_parent_in_place`]
|
|
|
|
/// method on [`EntityCommands`] instead.
|
|
|
|
pub struct RemoveParentInPlace {
|
2023-04-23 17:28:36 +00:00
|
|
|
/// [`Entity`] whose parent must be removed.
|
2023-01-12 18:46:11 +00:00
|
|
|
pub child: Entity,
|
|
|
|
}
|
|
|
|
impl Command for RemoveParentInPlace {
|
2023-06-12 17:53:47 +00:00
|
|
|
fn apply(self, world: &mut World) {
|
2023-01-12 18:46:11 +00:00
|
|
|
let hierarchy_command = RemoveParent { child: self.child };
|
2023-06-12 17:53:47 +00:00
|
|
|
hierarchy_command.apply(world);
|
2023-01-12 18:46:11 +00:00
|
|
|
// FIXME: Replace this closure with a `try` block. See: https://github.com/rust-lang/rust/issues/31436.
|
|
|
|
let mut update_transform = || {
|
|
|
|
let child_global = *world.get_entity(self.child)?.get::<GlobalTransform>()?;
|
|
|
|
let mut child_entity = world.get_entity_mut(self.child)?;
|
|
|
|
let mut child = child_entity.get_mut::<Transform>()?;
|
|
|
|
*child = child_global.compute_transform();
|
|
|
|
Some(())
|
|
|
|
};
|
|
|
|
update_transform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Collection of methods similar to [`BuildChildren`], but preserving each
|
|
|
|
/// entity's [`GlobalTransform`].
|
|
|
|
pub trait BuildChildrenTransformExt {
|
|
|
|
/// Change this entity's parent while preserving this entity's [`GlobalTransform`]
|
|
|
|
/// by updating its [`Transform`].
|
|
|
|
///
|
|
|
|
/// See [`BuildChildren::set_parent`] for a method that doesn't update the
|
|
|
|
/// [`Transform`].
|
|
|
|
///
|
|
|
|
/// Note that both the hierarchy and transform updates will only execute
|
2023-03-27 21:50:21 +00:00
|
|
|
/// the next time commands are applied
|
2023-06-02 14:04:13 +00:00
|
|
|
/// (during [`apply_deferred`](bevy_ecs::schedule::apply_deferred)).
|
2023-01-12 18:46:11 +00:00
|
|
|
fn set_parent_in_place(&mut self, parent: Entity) -> &mut Self;
|
|
|
|
|
|
|
|
/// Make this entity parentless while preserving this entity's [`GlobalTransform`]
|
|
|
|
/// by updating its [`Transform`] to be equal to its current [`GlobalTransform`].
|
|
|
|
///
|
|
|
|
/// See [`BuildChildren::remove_parent`] for a method that doesn't update the
|
|
|
|
/// [`Transform`].
|
|
|
|
///
|
|
|
|
/// Note that both the hierarchy and transform updates will only execute
|
2023-03-27 21:50:21 +00:00
|
|
|
/// the next time commands are applied
|
2023-06-02 14:04:13 +00:00
|
|
|
/// (during [`apply_deferred`](bevy_ecs::schedule::apply_deferred)).
|
2023-01-12 18:46:11 +00:00
|
|
|
fn remove_parent_in_place(&mut self) -> &mut Self;
|
|
|
|
}
|
|
|
|
impl<'w, 's, 'a> BuildChildrenTransformExt for EntityCommands<'w, 's, 'a> {
|
|
|
|
fn remove_parent_in_place(&mut self) -> &mut Self {
|
|
|
|
let child = self.id();
|
|
|
|
self.commands().add(RemoveParentInPlace { child });
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_parent_in_place(&mut self, parent: Entity) -> &mut Self {
|
|
|
|
let child = self.id();
|
|
|
|
self.commands().add(AddChildInPlace { child, parent });
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|