-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
[Merged by Bors] - Add an extension trait to EntityCommands to update hierarchy while preserving GlobalTransform
#7024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d149629
Add hierarchy update commands keeping transform
nicopap 5e6311e
Fix doc warning
nicopap 0941d20
Rename 'keep_global_transform' to 'in_place'
nicopap 58fe794
Implement feedback
nicopap c8e8d8c
Second feedback impl
nicopap 3ee0b99
Update crates/bevy_transform/src/commands.rs
nicopap 3e55235
Update crates/bevy_transform/src/commands.rs
nicopap a7f128d
Remove accidentally added example
nicopap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| //! 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 { | ||
| fn write(self, world: &mut World) { | ||
| let hierarchy_command = AddChild { | ||
| child: self.child, | ||
| parent: self.parent, | ||
| }; | ||
| hierarchy_command.write(world); | ||
| // 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 { | ||
| /// `Entity` whose parent must be removed. | ||
| pub child: Entity, | ||
| } | ||
| impl Command for RemoveParentInPlace { | ||
| fn write(self, world: &mut World) { | ||
| let hierarchy_command = RemoveParent { child: self.child }; | ||
| hierarchy_command.write(world); | ||
| // FIXME: Replace this closure with a `try` block. See: https://github.com/rust-lang/rust/issues/31436. | ||
| let mut update_transform = || { | ||
nicopap marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 | ||
| /// at the end of the current stage. | ||
| 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 | ||
| /// at the end of the current stage. | ||
| 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 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.