Skip to content

Add insert_child and remove_child methods #19622

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions crates/bevy_ecs/src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ impl<'w> EntityWorldMut<'w> {
self.insert_related::<ChildOf>(index, children)
}

/// Insert child at specific index.
/// See also [`insert_related`](Self::insert_related).
pub fn insert_child(&mut self, index: usize, child: Entity) -> &mut Self {
self.insert_related::<ChildOf>(index, &[child])
}

/// Adds the given child to this entity
/// See also [`add_related`](Self::add_related).
pub fn add_child(&mut self, child: Entity) -> &mut Self {
Expand All @@ -305,6 +311,11 @@ impl<'w> EntityWorldMut<'w> {
self.remove_related::<ChildOf>(children)
}

/// Removes the relationship between this entity and the given entity.
pub fn remove_child(&mut self, child: Entity) -> &mut Self {
self.remove_related::<ChildOf>(&[child])
}

/// Replaces all the related children with a new set of children.
pub fn replace_children(&mut self, children: &[Entity]) -> &mut Self {
self.replace_related::<ChildOf>(children)
Expand Down Expand Up @@ -374,6 +385,12 @@ impl<'a> EntityCommands<'a> {
self.insert_related::<ChildOf>(index, children)
}

/// Insert children at specific index.
/// See also [`insert_related`](Self::insert_related).
pub fn insert_child(&mut self, index: usize, child: Entity) -> &mut Self {
self.insert_related::<ChildOf>(index, &[child])
}

/// Adds the given child to this entity
pub fn add_child(&mut self, child: Entity) -> &mut Self {
self.add_related::<ChildOf>(&[child])
Expand All @@ -384,6 +401,11 @@ impl<'a> EntityCommands<'a> {
self.remove_related::<ChildOf>(children)
}

/// Removes the relationship between this entity and the given entity.
pub fn remove_child(&mut self, child: Entity) -> &mut Self {
self.remove_related::<ChildOf>(&[child])
}

/// Replaces the children on this entity with a new list of children.
pub fn replace_children(&mut self, children: &[Entity]) -> &mut Self {
self.replace_related::<ChildOf>(children)
Expand Down Expand Up @@ -641,6 +663,29 @@ mod tests {
);
}

#[test]
fn insert_child() {
let mut world = World::new();
let child1 = world.spawn_empty().id();
let child2 = world.spawn_empty().id();
let child3 = world.spawn_empty().id();

let mut entity_world_mut = world.spawn_empty();

let first_children = entity_world_mut.add_children(&[child1, child2]);

let root = first_children.insert_child(1, child3).id();

let hierarchy = get_hierarchy(&world, root);
assert_eq!(
hierarchy,
Node::new_with(
root,
vec![Node::new(child1), Node::new(child3), Node::new(child2)]
)
);
}

// regression test for https://github.com/bevyengine/bevy/pull/19134
#[test]
fn insert_children_index_bound() {
Expand Down Expand Up @@ -698,6 +743,25 @@ mod tests {
);
}

#[test]
fn remove_child() {
let mut world = World::new();
let child1 = world.spawn_empty().id();
let child2 = world.spawn_empty().id();
let child3 = world.spawn_empty().id();

let mut root = world.spawn_empty();
root.add_children(&[child1, child2, child3]);
root.remove_child(child2);
let root = root.id();

let hierarchy = get_hierarchy(&world, root);
assert_eq!(
hierarchy,
Node::new_with(root, vec![Node::new(child1), Node::new(child3)])
);
}

#[test]
fn self_parenting_invalid() {
let mut world = World::new();
Expand Down