bevy_transform/commands.rs
1//! Extension to [`EntityCommands`] to modify `bevy_hierarchy` hierarchies
2//! while preserving [`GlobalTransform`].
3
4use crate::prelude::{GlobalTransform, Transform};
5use bevy_ecs::{prelude::Entity, system::EntityCommands, world::Command, world::World};
6use bevy_hierarchy::{PushChild, RemoveParent};
7
8/// Command similar to [`PushChild`], but updating the child transform to keep
9/// it at the same [`GlobalTransform`].
10///
11/// You most likely want to use [`BuildChildrenTransformExt::set_parent_in_place`]
12/// method on [`EntityCommands`] instead.
13pub struct PushChildInPlace {
14 /// Parent entity to add the child to.
15 pub parent: Entity,
16 /// Child entity to add.
17 pub child: Entity,
18}
19impl Command for PushChildInPlace {
20 fn apply(self, world: &mut World) {
21 let hierarchy_command = PushChild {
22 child: self.child,
23 parent: self.parent,
24 };
25 hierarchy_command.apply(world);
26 // FIXME: Replace this closure with a `try` block. See: https://github.com/rust-lang/rust/issues/31436.
27 let mut update_transform = || {
28 let parent = *world.get_entity(self.parent)?.get::<GlobalTransform>()?;
29 let child_global = *world.get_entity(self.child)?.get::<GlobalTransform>()?;
30 let mut child_entity = world.get_entity_mut(self.child)?;
31 let mut child = child_entity.get_mut::<Transform>()?;
32 *child = child_global.reparented_to(&parent);
33 Some(())
34 };
35 update_transform();
36 }
37}
38/// Command similar to [`RemoveParent`], but updating the child transform to keep
39/// it at the same [`GlobalTransform`].
40///
41/// You most likely want to use [`BuildChildrenTransformExt::remove_parent_in_place`]
42/// method on [`EntityCommands`] instead.
43pub struct RemoveParentInPlace {
44 /// [`Entity`] whose parent must be removed.
45 pub child: Entity,
46}
47impl Command for RemoveParentInPlace {
48 fn apply(self, world: &mut World) {
49 let hierarchy_command = RemoveParent { child: self.child };
50 hierarchy_command.apply(world);
51 // FIXME: Replace this closure with a `try` block. See: https://github.com/rust-lang/rust/issues/31436.
52 let mut update_transform = || {
53 let child_global = *world.get_entity(self.child)?.get::<GlobalTransform>()?;
54 let mut child_entity = world.get_entity_mut(self.child)?;
55 let mut child = child_entity.get_mut::<Transform>()?;
56 *child = child_global.compute_transform();
57 Some(())
58 };
59 update_transform();
60 }
61}
62/// Collection of methods similar to [`BuildChildren`](bevy_hierarchy::BuildChildren), but preserving each
63/// entity's [`GlobalTransform`].
64pub trait BuildChildrenTransformExt {
65 /// Change this entity's parent while preserving this entity's [`GlobalTransform`]
66 /// by updating its [`Transform`].
67 ///
68 /// See [`BuildChildren::set_parent`](bevy_hierarchy::BuildChildren::set_parent) for a method that doesn't update the
69 /// [`Transform`].
70 ///
71 /// Note that both the hierarchy and transform updates will only execute
72 /// the next time commands are applied
73 /// (during [`apply_deferred`](bevy_ecs::schedule::apply_deferred)).
74 fn set_parent_in_place(&mut self, parent: Entity) -> &mut Self;
75
76 /// Make this entity parentless while preserving this entity's [`GlobalTransform`]
77 /// by updating its [`Transform`] to be equal to its current [`GlobalTransform`].
78 ///
79 /// See [`BuildChildren::remove_parent`](bevy_hierarchy::BuildChildren::remove_parent) for a method that doesn't update the
80 /// [`Transform`].
81 ///
82 /// Note that both the hierarchy and transform updates will only execute
83 /// the next time commands are applied
84 /// (during [`apply_deferred`](bevy_ecs::schedule::apply_deferred)).
85 fn remove_parent_in_place(&mut self) -> &mut Self;
86}
87impl BuildChildrenTransformExt for EntityCommands<'_> {
88 fn set_parent_in_place(&mut self, parent: Entity) -> &mut Self {
89 let child = self.id();
90 self.commands().add(PushChildInPlace { child, parent });
91 self
92 }
93
94 fn remove_parent_in_place(&mut self) -> &mut Self {
95 let child = self.id();
96 self.commands().add(RemoveParentInPlace { child });
97 self
98 }
99}