@@ -261,7 +261,7 @@ impl Command for RemoveParent {
261
261
/// ```
262
262
/// # use bevy_ecs::bundle::Bundle;
263
263
/// # use bevy_ecs::system::Commands;
264
- /// # use bevy_hierarchy::BuildChildren;
264
+ /// # use bevy_hierarchy::{ChildBuild, BuildChildren} ;
265
265
/// # #[derive(Bundle)]
266
266
/// # struct MyBundle {}
267
267
/// # #[derive(Bundle)]
@@ -279,39 +279,68 @@ pub struct ChildBuilder<'a> {
279
279
push_children : PushChildren ,
280
280
}
281
281
282
- impl ChildBuilder < ' _ > {
282
+ /// Trait for building children entities and adding them to a parent entity. This is used in
283
+ /// implementations of [`BuildChildren`] as a bound on the [`Builder`](BuildChildren::Builder)
284
+ /// associated type. The closure passed to [`BuildChildren::with_children`] accepts an
285
+ /// implementation of `ChildBuild` so that children can be spawned via [`ChildBuild::spawn`].
286
+ pub trait ChildBuild {
287
+ /// Spawn output type. Both [`spawn`](Self::spawn) and [`spawn_empty`](Self::spawn_empty) return
288
+ /// an implementation of this type so that children can be operated on via method-chaining.
289
+ /// Implementations of `ChildBuild` reborrow `self` when spawning entities (see
290
+ /// [`Commands::spawn_empty`] and [`World::get_entity_mut`]). Lifetime `'a` corresponds to this
291
+ /// reborrowed self, and `Self` outlives it.
292
+ type SpawnOutput < ' a > : BuildChildren
293
+ where
294
+ Self : ' a ;
295
+
283
296
/// Spawns an entity with the given bundle and inserts it into the parent entity's [`Children`].
284
297
/// Also adds [`Parent`] component to the created entity.
285
- pub fn spawn ( & mut self , bundle : impl Bundle ) -> EntityCommands {
298
+ fn spawn ( & mut self , bundle : impl Bundle ) -> Self :: SpawnOutput < ' _ > ;
299
+
300
+ /// Spawns an [`Entity`] with no components and inserts it into the parent entity's [`Children`].
301
+ /// Also adds [`Parent`] component to the created entity.
302
+ fn spawn_empty ( & mut self ) -> Self :: SpawnOutput < ' _ > ;
303
+
304
+ /// Returns the parent entity.
305
+ fn parent_entity ( & self ) -> Entity ;
306
+
307
+ /// Adds a command to be executed, like [`Commands::add`].
308
+ fn add_command < C : Command > ( & mut self , command : C ) -> & mut Self ;
309
+ }
310
+
311
+ impl ChildBuild for ChildBuilder < ' _ > {
312
+ type SpawnOutput < ' a > = EntityCommands < ' a > where Self : ' a ;
313
+
314
+ fn spawn ( & mut self , bundle : impl Bundle ) -> EntityCommands {
286
315
let e = self . commands . spawn ( bundle) ;
287
316
self . push_children . children . push ( e. id ( ) ) ;
288
317
e
289
318
}
290
319
291
- /// Spawns an [`Entity`] with no components and inserts it into the parent entity's [`Children`].
292
- /// Also adds [`Parent`] component to the created entity.
293
- pub fn spawn_empty ( & mut self ) -> EntityCommands {
320
+ fn spawn_empty ( & mut self ) -> EntityCommands {
294
321
let e = self . commands . spawn_empty ( ) ;
295
322
self . push_children . children . push ( e. id ( ) ) ;
296
323
e
297
324
}
298
325
299
- /// Returns the parent entity of this [`ChildBuilder`].
300
- pub fn parent_entity ( & self ) -> Entity {
326
+ fn parent_entity ( & self ) -> Entity {
301
327
self . push_children . parent
302
328
}
303
329
304
- /// Adds a command to be executed, like [`Commands::add`].
305
- pub fn add_command < C : Command > ( & mut self , command : C ) -> & mut Self {
330
+ fn add_command < C : Command > ( & mut self , command : C ) -> & mut Self {
306
331
self . commands . add ( command) ;
307
332
self
308
333
}
309
334
}
310
335
311
336
/// Trait for removing, adding and replacing children and parents of an entity.
312
337
pub trait BuildChildren {
313
- /// Takes a closure which builds children for this entity using [`ChildBuilder`].
314
- fn with_children ( & mut self , f : impl FnOnce ( & mut ChildBuilder ) ) -> & mut Self ;
338
+ /// Child builder type.
339
+ type Builder < ' a > : ChildBuild ;
340
+
341
+ /// Takes a closure which builds children for this entity using [`ChildBuild`].
342
+ fn with_children ( & mut self , f : impl FnOnce ( & mut Self :: Builder < ' _ > ) ) -> & mut Self ;
343
+
315
344
/// Pushes children to the back of the builder's children. For any entities that are
316
345
/// already a child of this one, this method does nothing.
317
346
///
@@ -323,6 +352,7 @@ pub trait BuildChildren {
323
352
///
324
353
/// Panics if any of the children are the same as the parent.
325
354
fn push_children ( & mut self , children : & [ Entity ] ) -> & mut Self ;
355
+
326
356
/// Inserts children at the given index.
327
357
///
328
358
/// If the children were previously children of another parent, that parent's [`Children`] component
@@ -333,10 +363,12 @@ pub trait BuildChildren {
333
363
///
334
364
/// Panics if any of the children are the same as the parent.
335
365
fn insert_children ( & mut self , index : usize , children : & [ Entity ] ) -> & mut Self ;
366
+
336
367
/// Removes the given children
337
368
///
338
369
/// Removing all children from a parent causes its [`Children`] component to be removed from the entity.
339
370
fn remove_children ( & mut self , children : & [ Entity ] ) -> & mut Self ;
371
+
340
372
/// Adds a single child.
341
373
///
342
374
/// If the children were previously children of another parent, that parent's [`Children`] component
@@ -347,8 +379,10 @@ pub trait BuildChildren {
347
379
///
348
380
/// Panics if the child is the same as the parent.
349
381
fn add_child ( & mut self , child : Entity ) -> & mut Self ;
382
+
350
383
/// Removes all children from this entity. The [`Children`] component will be removed if it exists, otherwise this does nothing.
351
384
fn clear_children ( & mut self ) -> & mut Self ;
385
+
352
386
/// Removes all current children from this entity, replacing them with the specified list of entities.
353
387
///
354
388
/// The removed children will have their [`Parent`] component removed.
@@ -357,6 +391,7 @@ pub trait BuildChildren {
357
391
///
358
392
/// Panics if any of the children are the same as the parent.
359
393
fn replace_children ( & mut self , children : & [ Entity ] ) -> & mut Self ;
394
+
360
395
/// Sets the parent of this entity.
361
396
///
362
397
/// If this entity already had a parent, the parent's [`Children`] component will have this
@@ -367,6 +402,7 @@ pub trait BuildChildren {
367
402
///
368
403
/// Panics if the parent is the same as the child.
369
404
fn set_parent ( & mut self , parent : Entity ) -> & mut Self ;
405
+
370
406
/// Removes the [`Parent`] of this entity.
371
407
///
372
408
/// Also removes this entity from its parent's [`Children`] component. Removing all children from a parent causes
@@ -375,7 +411,9 @@ pub trait BuildChildren {
375
411
}
376
412
377
413
impl BuildChildren for EntityCommands < ' _ > {
378
- fn with_children ( & mut self , spawn_children : impl FnOnce ( & mut ChildBuilder ) ) -> & mut Self {
414
+ type Builder < ' a > = ChildBuilder < ' a > ;
415
+
416
+ fn with_children ( & mut self , spawn_children : impl FnOnce ( & mut Self :: Builder < ' _ > ) ) -> & mut Self {
379
417
let parent = self . id ( ) ;
380
418
let mut builder = ChildBuilder {
381
419
commands : self . commands ( ) ,
@@ -478,10 +516,10 @@ pub struct WorldChildBuilder<'w> {
478
516
parent : Entity ,
479
517
}
480
518
481
- impl < ' w > WorldChildBuilder < ' w > {
482
- /// Spawns an entity with the given bundle and inserts it into the parent entity's [`Children`].
483
- /// Also adds [`Parent`] component to the created entity.
484
- pub fn spawn ( & mut self , bundle : impl Bundle ) -> EntityWorldMut < ' _ > {
519
+ impl ChildBuild for WorldChildBuilder < ' _ > {
520
+ type SpawnOutput < ' a > = EntityWorldMut < ' a > where Self : ' a ;
521
+
522
+ fn spawn ( & mut self , bundle : impl Bundle ) -> EntityWorldMut {
485
523
let entity = self . world . spawn ( ( bundle, Parent ( self . parent ) ) ) . id ( ) ;
486
524
push_child_unchecked ( self . world , self . parent , entity) ;
487
525
push_events (
@@ -494,9 +532,7 @@ impl<'w> WorldChildBuilder<'w> {
494
532
self . world . entity_mut ( entity)
495
533
}
496
534
497
- /// Spawns an [`Entity`] with no components and inserts it into the parent entity's [`Children`].
498
- /// Also adds [`Parent`] component to the created entity.
499
- pub fn spawn_empty ( & mut self ) -> EntityWorldMut < ' _ > {
535
+ fn spawn_empty ( & mut self ) -> EntityWorldMut {
500
536
let entity = self . world . spawn ( Parent ( self . parent ) ) . id ( ) ;
501
537
push_child_unchecked ( self . world , self . parent , entity) ;
502
538
push_events (
@@ -509,83 +545,19 @@ impl<'w> WorldChildBuilder<'w> {
509
545
self . world . entity_mut ( entity)
510
546
}
511
547
512
- /// Returns the parent entity of this [`WorldChildBuilder`].
513
- pub fn parent_entity ( & self ) -> Entity {
548
+ fn parent_entity ( & self ) -> Entity {
514
549
self . parent
515
550
}
516
- }
517
-
518
- /// Trait that defines adding, changing and children and parents of an entity directly through the [`World`].
519
- pub trait BuildWorldChildren {
520
- /// Takes a closure which builds children for this entity using [`WorldChildBuilder`].
521
- fn with_children ( & mut self , spawn_children : impl FnOnce ( & mut WorldChildBuilder ) ) -> & mut Self ;
522
-
523
- /// Adds a single child.
524
- ///
525
- /// If the children were previously children of another parent, that parent's [`Children`] component
526
- /// will have those children removed from its list. Removing all children from a parent causes its
527
- /// [`Children`] component to be removed from the entity.
528
- ///
529
- /// # Panics
530
- ///
531
- /// Panics if the child is the same as the parent.
532
- fn add_child ( & mut self , child : Entity ) -> & mut Self ;
533
-
534
- /// Pushes children to the back of the builder's children. For any entities that are
535
- /// already a child of this one, this method does nothing.
536
- ///
537
- /// If the children were previously children of another parent, that parent's [`Children`] component
538
- /// will have those children removed from its list. Removing all children from a parent causes its
539
- /// [`Children`] component to be removed from the entity.
540
- ///
541
- /// # Panics
542
- ///
543
- /// Panics if any of the children are the same as the parent.
544
- fn push_children ( & mut self , children : & [ Entity ] ) -> & mut Self ;
545
- /// Inserts children at the given index.
546
- ///
547
- /// If the children were previously children of another parent, that parent's [`Children`] component
548
- /// will have those children removed from its list. Removing all children from a parent causes its
549
- /// [`Children`] component to be removed from the entity.
550
- ///
551
- /// # Panics
552
- ///
553
- /// Panics if any of the children are the same as the parent.
554
- fn insert_children ( & mut self , index : usize , children : & [ Entity ] ) -> & mut Self ;
555
- /// Removes the given children
556
- ///
557
- /// Removing all children from a parent causes its [`Children`] component to be removed from the entity.
558
- fn remove_children ( & mut self , children : & [ Entity ] ) -> & mut Self ;
559
-
560
- /// Sets the parent of this entity.
561
- ///
562
- /// If this entity already had a parent, the parent's [`Children`] component will have this
563
- /// child removed from its list. Removing all children from a parent causes its [`Children`]
564
- /// component to be removed from the entity.
565
- ///
566
- /// # Panics
567
- ///
568
- /// Panics if the parent is the same as the child.
569
- fn set_parent ( & mut self , parent : Entity ) -> & mut Self ;
570
551
571
- /// Removes the [`Parent`] of this entity.
572
- ///
573
- /// Also removes this entity from its parent's [`Children`] component. Removing all children from a parent causes
574
- /// its [`Children`] component to be removed from the entity.
575
- fn remove_parent ( & mut self ) -> & mut Self ;
576
- /// Removes all children from this entity. The [`Children`] component will be removed if it exists, otherwise this does nothing.
577
- fn clear_children ( & mut self ) -> & mut Self ;
578
- /// Removes all current children from this entity, replacing them with the specified list of entities.
579
- ///
580
- /// The removed children will have their [`Parent`] component removed.
581
- ///
582
- /// # Panics
583
- ///
584
- /// Panics if any of the children are the same as the parent.
585
- fn replace_children ( & mut self , children : & [ Entity ] ) -> & mut Self ;
552
+ fn add_command < C : Command > ( & mut self , command : C ) -> & mut Self {
553
+ command. apply ( self . world ) ;
554
+ self
555
+ }
586
556
}
587
557
588
- impl < ' w > BuildWorldChildren for EntityWorldMut < ' w > {
558
+ impl BuildChildren for EntityWorldMut < ' _ > {
559
+ type Builder < ' a > = WorldChildBuilder < ' a > ;
560
+
589
561
fn with_children ( & mut self , spawn_children : impl FnOnce ( & mut WorldChildBuilder ) ) -> & mut Self {
590
562
let parent = self . id ( ) ;
591
563
self . world_scope ( |world| {
@@ -691,7 +663,7 @@ impl<'w> BuildWorldChildren for EntityWorldMut<'w> {
691
663
692
664
#[ cfg( test) ]
693
665
mod tests {
694
- use super :: { BuildChildren , BuildWorldChildren } ;
666
+ use super :: { BuildChildren , ChildBuild } ;
695
667
use crate :: {
696
668
components:: { Children , Parent } ,
697
669
HierarchyEvent :: { self , ChildAdded , ChildMoved , ChildRemoved } ,
0 commit comments