2023-08-31 19:05:49 +00:00
|
|
|
//! Extension to [`EntityCommands`] to modify `bevy_hierarchy` hierarchies
|
2023-01-12 18:46:11 +00:00
|
|
|
//! while preserving [`GlobalTransform`].
|
|
|
|
|
2024-05-31 16:40:36 +00:00
|
|
|
use crate::prelude::{GlobalTransform, Transform};
|
2024-06-26 14:59:20 +00:00
|
|
|
use bevy_ecs::{
|
|
|
|
prelude::Entity,
|
|
|
|
system::EntityCommands,
|
|
|
|
world::{Command, EntityWorldMut, World},
|
|
|
|
};
|
2024-09-16 23:16:04 +00:00
|
|
|
use bevy_hierarchy::{AddChild, RemoveParent};
|
2023-01-12 18:46:11 +00:00
|
|
|
|
2024-09-16 23:16:04 +00:00
|
|
|
/// Command similar to [`AddChild`], but updating the child transform to keep
|
2023-01-12 18:46:11 +00:00
|
|
|
/// it at the same [`GlobalTransform`].
|
|
|
|
///
|
|
|
|
/// You most likely want to use [`BuildChildrenTransformExt::set_parent_in_place`]
|
|
|
|
/// method on [`EntityCommands`] instead.
|
2024-09-16 23:16:04 +00:00
|
|
|
pub struct AddChildInPlace {
|
2023-01-12 18:46:11 +00:00
|
|
|
/// Parent entity to add the child to.
|
|
|
|
pub parent: Entity,
|
|
|
|
/// Child entity to add.
|
|
|
|
pub child: Entity,
|
|
|
|
}
|
2024-09-16 23:16:04 +00:00
|
|
|
impl Command for AddChildInPlace {
|
2023-06-12 17:53:47 +00:00
|
|
|
fn apply(self, world: &mut World) {
|
2024-09-16 23:16:04 +00:00
|
|
|
let hierarchy_command = AddChild {
|
2023-01-12 18:46:11 +00:00
|
|
|
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 = || {
|
Allow `World::entity` family of functions to take multiple entities and get multiple references back (#15614)
# Objective
Following the pattern established in #15593, we can reduce the API
surface of `World` by providing a single function to grab both a
singular entity reference, or multiple entity references.
## Solution
The following functions can now also take multiple entity IDs and will
return multiple entity references back:
- `World::entity`
- `World::get_entity`
- `World::entity_mut`
- `World::get_entity_mut`
- `DeferredWorld::entity_mut`
- `DeferredWorld::get_entity_mut`
If you pass in X, you receive Y:
- give a single `Entity`, receive a single `EntityRef`/`EntityWorldMut`
(matches current behavior)
- give a `[Entity; N]`/`&[Entity; N]` (array), receive an equally-sized
`[EntityRef; N]`/`[EntityMut; N]`
- give a `&[Entity]` (slice), receive a
`Vec<EntityRef>`/`Vec<EntityMut>`
- give a `&EntityHashSet`, receive a
`EntityHashMap<EntityRef>`/`EntityHashMap<EntityMut>`
Note that `EntityWorldMut` is only returned in the single-entity case,
because having multiple at the same time would lead to UB. Also,
`DeferredWorld` receives an `EntityMut` in the single-entity case
because it does not allow structural access.
## Testing
- Added doc-tests on `World::entity`, `World::entity_mut`, and
`DeferredWorld::entity_mut`
- Added tests for aliased mutability and entity existence
---
## Showcase
<details>
<summary>Click to view showcase</summary>
The APIs for fetching `EntityRef`s and `EntityMut`s from the `World`
have been unified.
```rust
// This code will be referred to by subsequent code blocks.
let world = World::new();
let e1 = world.spawn_empty().id();
let e2 = world.spawn_empty().id();
let e3 = world.spawn_empty().id();
```
Querying for a single entity remains mostly the same:
```rust
// 0.14
let eref: EntityRef = world.entity(e1);
let emut: EntityWorldMut = world.entity_mut(e1);
let eref: Option<EntityRef> = world.get_entity(e1);
let emut: Option<EntityWorldMut> = world.get_entity_mut(e1);
// 0.15
let eref: EntityRef = world.entity(e1);
let emut: EntityWorldMut = world.entity_mut(e1);
let eref: Result<EntityRef, Entity> = world.get_entity(e1);
let emut: Result<EntityWorldMut, Entity> = world.get_entity_mut(e1);
```
Querying for multiple entities with an array has changed:
```rust
// 0.14
let erefs: [EntityRef; 2] = world.many_entities([e1, e2]);
let emuts: [EntityMut; 2] = world.many_entities_mut([e1, e2]);
let erefs: Result<[EntityRef; 2], Entity> = world.get_many_entities([e1, e2]);
let emuts: Result<[EntityMut; 2], QueryEntityError> = world.get_many_entities_mut([e1, e2]);
// 0.15
let erefs: [EntityRef; 2] = world.entity([e1, e2]);
let emuts: [EntityMut; 2] = world.entity_mut([e1, e2]);
let erefs: Result<[EntityRef; 2], Entity> = world.get_entity([e1, e2]);
let emuts: Result<[EntityMut; 2], EntityFetchError> = world.get_entity_mut([e1, e2]);
```
Querying for multiple entities with a slice has changed:
```rust
let ids = vec![e1, e2, e3]);
// 0.14
let erefs: Result<Vec<EntityRef>, Entity> = world.get_many_entities_dynamic(&ids[..]);
let emuts: Result<Vec<EntityMut>, QueryEntityError> = world.get_many_entities_dynamic_mut(&ids[..]);
// 0.15
let erefs: Result<Vec<EntityRef>, Entity> = world.get_entity(&ids[..]);
let emuts: Result<Vec<EntityMut>, EntityFetchError> = world.get_entity_mut(&ids[..]);
let erefs: Vec<EntityRef> = world.entity(&ids[..]); // Newly possible!
let emuts: Vec<EntityMut> = world.entity_mut(&ids[..]); // Newly possible!
```
Querying for multiple entities with an `EntityHashSet` has changed:
```rust
let set = EntityHashSet::from_iter([e1, e2, e3]);
// 0.14
let emuts: Result<Vec<EntityMut>, QueryEntityError> = world.get_many_entities_from_set_mut(&set);
// 0.15
let emuts: Result<EntityHashMap<EntityMut>, EntityFetchError> = world.get_entity_mut(&set);
let erefs: Result<EntityHashMap<EntityRef>, EntityFetchError> = world.get_entity(&set); // Newly possible!
let emuts: EntityHashMap<EntityMut> = world.entity_mut(&set); // Newly possible!
let erefs: EntityHashMap<EntityRef> = world.entity(&set); // Newly possible!
```
</details>
## Migration Guide
- `World::get_entity` now returns `Result<_, Entity>` instead of
`Option<_>`.
- Use `world.get_entity(..).ok()` to return to the previous behavior.
- `World::get_entity_mut` and `DeferredWorld::get_entity_mut` now return
`Result<_, EntityFetchError>` instead of `Option<_>`.
- Use `world.get_entity_mut(..).ok()` to return to the previous
behavior.
- Type inference for `World::entity`, `World::entity_mut`,
`World::get_entity`, `World::get_entity_mut`,
`DeferredWorld::entity_mut`, and `DeferredWorld::get_entity_mut` has
changed, and might now require the input argument's type to be
explicitly written when inside closures.
- The following functions have been deprecated, and should be replaced
as such:
- `World::many_entities` -> `World::entity::<[Entity; N]>`
- `World::many_entities_mut` -> `World::entity_mut::<[Entity; N]>`
- `World::get_many_entities` -> `World::get_entity::<[Entity; N]>`
- `World::get_many_entities_dynamic` -> `World::get_entity::<&[Entity]>`
- `World::get_many_entities_mut` -> `World::get_entity_mut::<[Entity;
N]>`
- The equivalent return type has changed from `Result<_,
QueryEntityError>` to `Result<_, EntityFetchError>`
- `World::get_many_entities_dynamic_mut` ->
`World::get_entity_mut::<&[Entity]>1
- The equivalent return type has changed from `Result<_,
QueryEntityError>` to `Result<_, EntityFetchError>`
- `World::get_many_entities_from_set_mut` ->
`World::get_entity_mut::<&EntityHashSet>`
- The equivalent return type has changed from `Result<Vec<EntityMut>,
QueryEntityError>` to `Result<EntityHashMap<EntityMut>,
EntityFetchError>`. If necessary, you can still convert the
`EntityHashMap` into a `Vec`.
2024-10-07 15:21:40 +00:00
|
|
|
let parent = *world
|
|
|
|
.get_entity(self.parent)
|
|
|
|
.ok()?
|
|
|
|
.get::<GlobalTransform>()?;
|
|
|
|
let child_global = *world
|
|
|
|
.get_entity(self.child)
|
|
|
|
.ok()?
|
|
|
|
.get::<GlobalTransform>()?;
|
|
|
|
let mut child_entity = world.get_entity_mut(self.child).ok()?;
|
2023-01-12 18:46:11 +00:00
|
|
|
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 = || {
|
Allow `World::entity` family of functions to take multiple entities and get multiple references back (#15614)
# Objective
Following the pattern established in #15593, we can reduce the API
surface of `World` by providing a single function to grab both a
singular entity reference, or multiple entity references.
## Solution
The following functions can now also take multiple entity IDs and will
return multiple entity references back:
- `World::entity`
- `World::get_entity`
- `World::entity_mut`
- `World::get_entity_mut`
- `DeferredWorld::entity_mut`
- `DeferredWorld::get_entity_mut`
If you pass in X, you receive Y:
- give a single `Entity`, receive a single `EntityRef`/`EntityWorldMut`
(matches current behavior)
- give a `[Entity; N]`/`&[Entity; N]` (array), receive an equally-sized
`[EntityRef; N]`/`[EntityMut; N]`
- give a `&[Entity]` (slice), receive a
`Vec<EntityRef>`/`Vec<EntityMut>`
- give a `&EntityHashSet`, receive a
`EntityHashMap<EntityRef>`/`EntityHashMap<EntityMut>`
Note that `EntityWorldMut` is only returned in the single-entity case,
because having multiple at the same time would lead to UB. Also,
`DeferredWorld` receives an `EntityMut` in the single-entity case
because it does not allow structural access.
## Testing
- Added doc-tests on `World::entity`, `World::entity_mut`, and
`DeferredWorld::entity_mut`
- Added tests for aliased mutability and entity existence
---
## Showcase
<details>
<summary>Click to view showcase</summary>
The APIs for fetching `EntityRef`s and `EntityMut`s from the `World`
have been unified.
```rust
// This code will be referred to by subsequent code blocks.
let world = World::new();
let e1 = world.spawn_empty().id();
let e2 = world.spawn_empty().id();
let e3 = world.spawn_empty().id();
```
Querying for a single entity remains mostly the same:
```rust
// 0.14
let eref: EntityRef = world.entity(e1);
let emut: EntityWorldMut = world.entity_mut(e1);
let eref: Option<EntityRef> = world.get_entity(e1);
let emut: Option<EntityWorldMut> = world.get_entity_mut(e1);
// 0.15
let eref: EntityRef = world.entity(e1);
let emut: EntityWorldMut = world.entity_mut(e1);
let eref: Result<EntityRef, Entity> = world.get_entity(e1);
let emut: Result<EntityWorldMut, Entity> = world.get_entity_mut(e1);
```
Querying for multiple entities with an array has changed:
```rust
// 0.14
let erefs: [EntityRef; 2] = world.many_entities([e1, e2]);
let emuts: [EntityMut; 2] = world.many_entities_mut([e1, e2]);
let erefs: Result<[EntityRef; 2], Entity> = world.get_many_entities([e1, e2]);
let emuts: Result<[EntityMut; 2], QueryEntityError> = world.get_many_entities_mut([e1, e2]);
// 0.15
let erefs: [EntityRef; 2] = world.entity([e1, e2]);
let emuts: [EntityMut; 2] = world.entity_mut([e1, e2]);
let erefs: Result<[EntityRef; 2], Entity> = world.get_entity([e1, e2]);
let emuts: Result<[EntityMut; 2], EntityFetchError> = world.get_entity_mut([e1, e2]);
```
Querying for multiple entities with a slice has changed:
```rust
let ids = vec![e1, e2, e3]);
// 0.14
let erefs: Result<Vec<EntityRef>, Entity> = world.get_many_entities_dynamic(&ids[..]);
let emuts: Result<Vec<EntityMut>, QueryEntityError> = world.get_many_entities_dynamic_mut(&ids[..]);
// 0.15
let erefs: Result<Vec<EntityRef>, Entity> = world.get_entity(&ids[..]);
let emuts: Result<Vec<EntityMut>, EntityFetchError> = world.get_entity_mut(&ids[..]);
let erefs: Vec<EntityRef> = world.entity(&ids[..]); // Newly possible!
let emuts: Vec<EntityMut> = world.entity_mut(&ids[..]); // Newly possible!
```
Querying for multiple entities with an `EntityHashSet` has changed:
```rust
let set = EntityHashSet::from_iter([e1, e2, e3]);
// 0.14
let emuts: Result<Vec<EntityMut>, QueryEntityError> = world.get_many_entities_from_set_mut(&set);
// 0.15
let emuts: Result<EntityHashMap<EntityMut>, EntityFetchError> = world.get_entity_mut(&set);
let erefs: Result<EntityHashMap<EntityRef>, EntityFetchError> = world.get_entity(&set); // Newly possible!
let emuts: EntityHashMap<EntityMut> = world.entity_mut(&set); // Newly possible!
let erefs: EntityHashMap<EntityRef> = world.entity(&set); // Newly possible!
```
</details>
## Migration Guide
- `World::get_entity` now returns `Result<_, Entity>` instead of
`Option<_>`.
- Use `world.get_entity(..).ok()` to return to the previous behavior.
- `World::get_entity_mut` and `DeferredWorld::get_entity_mut` now return
`Result<_, EntityFetchError>` instead of `Option<_>`.
- Use `world.get_entity_mut(..).ok()` to return to the previous
behavior.
- Type inference for `World::entity`, `World::entity_mut`,
`World::get_entity`, `World::get_entity_mut`,
`DeferredWorld::entity_mut`, and `DeferredWorld::get_entity_mut` has
changed, and might now require the input argument's type to be
explicitly written when inside closures.
- The following functions have been deprecated, and should be replaced
as such:
- `World::many_entities` -> `World::entity::<[Entity; N]>`
- `World::many_entities_mut` -> `World::entity_mut::<[Entity; N]>`
- `World::get_many_entities` -> `World::get_entity::<[Entity; N]>`
- `World::get_many_entities_dynamic` -> `World::get_entity::<&[Entity]>`
- `World::get_many_entities_mut` -> `World::get_entity_mut::<[Entity;
N]>`
- The equivalent return type has changed from `Result<_,
QueryEntityError>` to `Result<_, EntityFetchError>`
- `World::get_many_entities_dynamic_mut` ->
`World::get_entity_mut::<&[Entity]>1
- The equivalent return type has changed from `Result<_,
QueryEntityError>` to `Result<_, EntityFetchError>`
- `World::get_many_entities_from_set_mut` ->
`World::get_entity_mut::<&EntityHashSet>`
- The equivalent return type has changed from `Result<Vec<EntityMut>,
QueryEntityError>` to `Result<EntityHashMap<EntityMut>,
EntityFetchError>`. If necessary, you can still convert the
`EntityHashMap` into a `Vec`.
2024-10-07 15:21:40 +00:00
|
|
|
let child_global = *world
|
|
|
|
.get_entity(self.child)
|
|
|
|
.ok()?
|
|
|
|
.get::<GlobalTransform>()?;
|
|
|
|
let mut child_entity = world.get_entity_mut(self.child).ok()?;
|
2023-01-12 18:46:11 +00:00
|
|
|
let mut child = child_entity.get_mut::<Transform>()?;
|
|
|
|
*child = child_global.compute_transform();
|
|
|
|
Some(())
|
|
|
|
};
|
|
|
|
update_transform();
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 19:05:49 +00:00
|
|
|
/// Collection of methods similar to [`BuildChildren`](bevy_hierarchy::BuildChildren), but preserving each
|
2023-01-12 18:46:11 +00:00
|
|
|
/// entity's [`GlobalTransform`].
|
|
|
|
pub trait BuildChildrenTransformExt {
|
|
|
|
/// Change this entity's parent while preserving this entity's [`GlobalTransform`]
|
|
|
|
/// by updating its [`Transform`].
|
|
|
|
///
|
2023-08-31 19:05:49 +00:00
|
|
|
/// See [`BuildChildren::set_parent`](bevy_hierarchy::BuildChildren::set_parent) for a method that doesn't update the
|
2023-01-12 18:46:11 +00:00
|
|
|
/// [`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`].
|
|
|
|
///
|
2023-08-31 19:05:49 +00:00
|
|
|
/// See [`BuildChildren::remove_parent`](bevy_hierarchy::BuildChildren::remove_parent) for a method that doesn't update the
|
2023-01-12 18:46:11 +00:00
|
|
|
/// [`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;
|
|
|
|
}
|
refactor: Simplify lifetimes for `Commands` and related types (#11445)
# Objective
It would be convenient to be able to call functions with `Commands` as a
parameter without having to move your own instance of `Commands`. Since
this struct is composed entirely of references, we can easily get an
owned instance of `Commands` by shortening the lifetime.
## Solution
Add `Commands::reborrow`, `EntiyCommands::reborrow`, and
`Deferred::reborrow`, which returns an owned version of themselves with
a shorter lifetime.
Remove unnecessary lifetimes from `EntityCommands`. The `'w` and `'s`
lifetimes only have to be separate for `Commands` because it's used as a
`SystemParam` -- this is not the case for `EntityCommands`.
---
## Changelog
Added `Commands::reborrow`. This is useful if you have `&mut Commands`
but need `Commands`. Also added `EntityCommands::reborrow` and
`Deferred:reborrow` which serve the same purpose.
## Migration Guide
The lifetimes for `EntityCommands` have been simplified.
```rust
// Before (Bevy 0.12)
struct MyStruct<'w, 's, 'a> {
commands: EntityCommands<'w, 's, 'a>,
}
// After (Bevy 0.13)
struct MyStruct<'a> {
commands: EntityCommands<'a>,
}
```
The method `EntityCommands::commands` now returns `Commands` rather than
`&mut Commands`.
```rust
// Before (Bevy 0.12)
let commands = entity_commands.commands();
commands.spawn(...);
// After (Bevy 0.13)
let mut commands = entity_commands.commands();
commands.spawn(...);
```
2024-01-22 15:35:42 +00:00
|
|
|
impl BuildChildrenTransformExt for EntityCommands<'_> {
|
2023-12-24 17:43:55 +00:00
|
|
|
fn set_parent_in_place(&mut self, parent: Entity) -> &mut Self {
|
2023-01-12 18:46:11 +00:00
|
|
|
let child = self.id();
|
2024-09-17 00:17:49 +00:00
|
|
|
self.commands().queue(AddChildInPlace { child, parent });
|
2023-01-12 18:46:11 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-12-24 17:43:55 +00:00
|
|
|
fn remove_parent_in_place(&mut self) -> &mut Self {
|
2023-01-12 18:46:11 +00:00
|
|
|
let child = self.id();
|
2024-09-17 00:17:49 +00:00
|
|
|
self.commands().queue(RemoveParentInPlace { child });
|
2023-01-12 18:46:11 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2024-06-26 14:59:20 +00:00
|
|
|
|
|
|
|
impl BuildChildrenTransformExt for EntityWorldMut<'_> {
|
|
|
|
fn set_parent_in_place(&mut self, parent: Entity) -> &mut Self {
|
|
|
|
let child = self.id();
|
2024-09-16 23:16:04 +00:00
|
|
|
self.world_scope(|world| AddChildInPlace { child, parent }.apply(world));
|
2024-06-26 14:59:20 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_parent_in_place(&mut self) -> &mut Self {
|
|
|
|
let child = self.id();
|
|
|
|
self.world_scope(|world| RemoveParentInPlace { child }.apply(world));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|