bevy_transform/
commands.rs

1//! Extension to [`EntityCommands`] to modify [`bevy_ecs::hierarchy`] hierarchies.
2//! while preserving [`GlobalTransform`].
3
4use crate::prelude::{GlobalTransform, Transform};
5use bevy_ecs::{entity::Entity, hierarchy::ChildOf, system::EntityCommands, world::EntityWorldMut};
6
7/// Collection of methods similar to the built-in parenting methods on [`EntityWorldMut`] and [`EntityCommands`], but preserving each
8/// entity's [`GlobalTransform`].
9pub trait BuildChildrenTransformExt {
10    /// Change this entity's parent while preserving this entity's [`GlobalTransform`]
11    /// by updating its [`Transform`].
12    ///
13    /// Insert the [`ChildOf`] component directly if you don't want to also update the [`Transform`].
14    ///
15    /// Note that both the hierarchy and transform updates will only execute
16    /// the next time commands are applied
17    /// (during [`ApplyDeferred`](bevy_ecs::schedule::ApplyDeferred)).
18    fn set_parent_in_place(&mut self, parent: Entity) -> &mut Self;
19
20    /// Make this entity parentless while preserving this entity's [`GlobalTransform`]
21    /// by updating its [`Transform`] to be equal to its current [`GlobalTransform`].
22    ///
23    /// See [`EntityWorldMut::remove_parent`] or [`EntityCommands::remove_parent`] for a method that doesn't update the [`Transform`].
24    ///
25    /// Note that both the hierarchy and transform updates will only execute
26    /// the next time commands are applied
27    /// (during [`ApplyDeferred`](bevy_ecs::schedule::ApplyDeferred)).
28    fn remove_parent_in_place(&mut self) -> &mut Self;
29}
30
31impl BuildChildrenTransformExt for EntityCommands<'_> {
32    fn set_parent_in_place(&mut self, parent: Entity) -> &mut Self {
33        self.queue(move |mut entity: EntityWorldMut| {
34            entity.set_parent_in_place(parent);
35        })
36    }
37
38    fn remove_parent_in_place(&mut self) -> &mut Self {
39        self.queue(move |mut entity: EntityWorldMut| {
40            entity.remove_parent_in_place();
41        })
42    }
43}
44
45impl BuildChildrenTransformExt for EntityWorldMut<'_> {
46    fn set_parent_in_place(&mut self, parent: Entity) -> &mut Self {
47        let child = self.id();
48        self.world_scope(|world| {
49            world.entity_mut(parent).add_child(child);
50            // FIXME: Replace this closure with a `try` block. See: https://github.com/rust-lang/rust/issues/31436.
51            let mut update_transform = || {
52                let parent = *world.get_entity(parent).ok()?.get::<GlobalTransform>()?;
53                let child_global = *world.get_entity(child).ok()?.get::<GlobalTransform>()?;
54                let mut child_entity = world.get_entity_mut(child).ok()?;
55                let mut child = child_entity.get_mut::<Transform>()?;
56                *child = child_global.reparented_to(&parent);
57                Some(())
58            };
59            update_transform();
60        });
61        self
62    }
63
64    fn remove_parent_in_place(&mut self) -> &mut Self {
65        let child = self.id();
66        self.world_scope(|world| {
67            world.entity_mut(child).remove::<ChildOf>();
68            // FIXME: Replace this closure with a `try` block. See: https://github.com/rust-lang/rust/issues/31436.
69            let mut update_transform = || {
70                let child_global = *world.get_entity(child).ok()?.get::<GlobalTransform>()?;
71                let mut child_entity = world.get_entity_mut(child).ok()?;
72                let mut child = child_entity.get_mut::<Transform>()?;
73                *child = child_global.compute_transform();
74                Some(())
75            };
76            update_transform();
77        });
78        self
79    }
80}