mirror of
https://github.com/bevyengine/bevy
synced 2024-11-21 20:23:28 +00:00
better parenting ergonomics in world builder
This commit is contained in:
parent
91a6c0d9db
commit
a790332505
3 changed files with 115 additions and 127 deletions
|
@ -38,16 +38,17 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
..Default::default()
|
||||
})
|
||||
.add(Rotator)
|
||||
// cube
|
||||
.add_archetype(MeshEntity {
|
||||
mesh: cube_handle,
|
||||
material: StandardMaterial {
|
||||
albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(),
|
||||
},
|
||||
translation: Translation::new(0.0, 0.0, 3.0),
|
||||
..Default::default()
|
||||
.add_children(|child_builder| {
|
||||
// cube
|
||||
child_builder.add_archetype(MeshEntity {
|
||||
mesh: cube_handle,
|
||||
material: StandardMaterial {
|
||||
albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(),
|
||||
},
|
||||
translation: Translation::new(0.0, 0.0, 3.0),
|
||||
..Default::default()
|
||||
})
|
||||
})
|
||||
.set_last_entity_as_parent()
|
||||
// light
|
||||
.add_archetype(LightEntity {
|
||||
translation: Translation::new(4.0, -4.0, 5.0),
|
||||
|
|
172
examples/ui.rs
172
examples/ui.rs
|
@ -51,106 +51,84 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||
}),
|
||||
active_camera_2d: ActiveCamera2d,
|
||||
})
|
||||
.build();
|
||||
|
||||
// bottom left anchor with vertical fill
|
||||
world.insert(
|
||||
(),
|
||||
vec![(Node::new(
|
||||
math::vec2(0.0, 0.0),
|
||||
Anchors::new(0.0, 0.0, 0.0, 1.0),
|
||||
Margins::new(10.0, 200.0, 10.0, 10.0),
|
||||
math::vec4(0.1, 0.1, 0.1, 1.0),
|
||||
),)],
|
||||
);
|
||||
|
||||
// top right anchor with vertical fill
|
||||
world.insert(
|
||||
(),
|
||||
vec![(Node::new(
|
||||
math::vec2(0.0, 0.0),
|
||||
Anchors::new(1.0, 1.0, 0.0, 1.0),
|
||||
Margins::new(10.0, 100.0, 50.0, 100.0),
|
||||
math::vec4(0.1, 0.1, 0.1, 1.0),
|
||||
),)],
|
||||
);
|
||||
|
||||
// render order test: reddest in the back, whitest in the front
|
||||
world.insert(
|
||||
(),
|
||||
vec![(Node::new(
|
||||
math::vec2(75.0, 75.0),
|
||||
Anchors::new(0.5, 0.5, 0.5, 0.5),
|
||||
Margins::new(0.0, 100.0, 0.0, 100.0),
|
||||
math::vec4(1.0, 0.1, 0.1, 1.0),
|
||||
),)],
|
||||
);
|
||||
|
||||
world.insert(
|
||||
(),
|
||||
vec![(Node::new(
|
||||
math::vec2(50.0, 50.0),
|
||||
Anchors::new(0.5, 0.5, 0.5, 0.5),
|
||||
Margins::new(0.0, 100.0, 0.0, 100.0),
|
||||
math::vec4(1.0, 0.3, 0.3, 1.0),
|
||||
),)],
|
||||
);
|
||||
|
||||
world.insert(
|
||||
(),
|
||||
vec![(Node::new(
|
||||
math::vec2(100.0, 100.0),
|
||||
Anchors::new(0.5, 0.5, 0.5, 0.5),
|
||||
Margins::new(0.0, 100.0, 0.0, 100.0),
|
||||
math::vec4(1.0, 0.5, 0.5, 1.0),
|
||||
),)],
|
||||
);
|
||||
|
||||
world.insert(
|
||||
(),
|
||||
vec![(Node::new(
|
||||
math::vec2(150.0, 150.0),
|
||||
Anchors::new(0.5, 0.5, 0.5, 0.5),
|
||||
Margins::new(0.0, 100.0, 0.0, 100.0),
|
||||
math::vec4(1.0, 0.7, 0.7, 1.0),
|
||||
),)],
|
||||
);
|
||||
|
||||
// parenting
|
||||
let parent = *world
|
||||
.insert(
|
||||
(),
|
||||
vec![(Node::new(
|
||||
// bottom left anchor with vertical fill
|
||||
.add_archetype(UiEntity {
|
||||
node: Node::new(
|
||||
math::vec2(0.0, 0.0),
|
||||
Anchors::new(0.0, 0.0, 0.0, 1.0),
|
||||
Margins::new(10.0, 200.0, 10.0, 10.0),
|
||||
math::vec4(0.1, 0.1, 0.1, 1.0),
|
||||
),
|
||||
})
|
||||
// top right anchor with vertical fill
|
||||
.add_archetype(UiEntity {
|
||||
node: Node::new(
|
||||
math::vec2(0.0, 0.0),
|
||||
Anchors::new(1.0, 1.0, 0.0, 1.0),
|
||||
Margins::new(10.0, 100.0, 50.0, 100.0),
|
||||
math::vec4(0.1, 0.1, 0.1, 1.0),
|
||||
),
|
||||
})
|
||||
// render order test: reddest in the back, whitest in the front
|
||||
.add_archetype(UiEntity {
|
||||
node: Node::new(
|
||||
math::vec2(75.0, 75.0),
|
||||
Anchors::new(0.5, 0.5, 0.5, 0.5),
|
||||
Margins::new(0.0, 100.0, 0.0, 100.0),
|
||||
math::vec4(1.0, 0.1, 0.1, 1.0),
|
||||
),
|
||||
})
|
||||
.add_archetype(UiEntity {
|
||||
node: Node::new(
|
||||
math::vec2(50.0, 50.0),
|
||||
Anchors::new(0.5, 0.5, 0.5, 0.5),
|
||||
Margins::new(0.0, 100.0, 0.0, 100.0),
|
||||
math::vec4(1.0, 0.3, 0.3, 1.0),
|
||||
),
|
||||
})
|
||||
.add_archetype(UiEntity {
|
||||
node: Node::new(
|
||||
math::vec2(100.0, 100.0),
|
||||
Anchors::new(0.5, 0.5, 0.5, 0.5),
|
||||
Margins::new(0.0, 100.0, 0.0, 100.0),
|
||||
math::vec4(1.0, 0.5, 0.5, 1.0),
|
||||
),
|
||||
})
|
||||
.add_archetype(UiEntity {
|
||||
node: Node::new(
|
||||
math::vec2(150.0, 150.0),
|
||||
Anchors::new(0.5, 0.5, 0.5, 0.5),
|
||||
Margins::new(0.0, 100.0, 0.0, 100.0),
|
||||
math::vec4(1.0, 0.7, 0.7, 1.0),
|
||||
),
|
||||
})
|
||||
// parenting
|
||||
.add_archetype(UiEntity {
|
||||
node: Node::new(
|
||||
math::vec2(300.0, 300.0),
|
||||
Anchors::new(0.0, 0.0, 0.0, 0.0),
|
||||
Margins::new(0.0, 200.0, 0.0, 200.0),
|
||||
math::vec4(0.1, 0.1, 1.0, 1.0),
|
||||
),)],
|
||||
)
|
||||
.first()
|
||||
.unwrap();
|
||||
|
||||
world.insert(
|
||||
(),
|
||||
vec![(
|
||||
Node::new(
|
||||
math::vec2(0.0, 0.0),
|
||||
Anchors::new(0.0, 1.0, 0.0, 1.0),
|
||||
Margins::new(20.0, 20.0, 20.0, 20.0),
|
||||
math::vec4(0.6, 0.6, 1.0, 1.0),
|
||||
),
|
||||
Parent(parent),
|
||||
)],
|
||||
);
|
||||
|
||||
// alpha test
|
||||
world.insert(
|
||||
(),
|
||||
vec![(Node::new(
|
||||
math::vec2(200.0, 200.0),
|
||||
Anchors::new(0.5, 0.5, 0.5, 0.5),
|
||||
Margins::new(0.0, 100.0, 0.0, 100.0),
|
||||
math::vec4(1.0, 0.9, 0.9, 0.4),
|
||||
),)],
|
||||
);
|
||||
})
|
||||
.add_children(|child_builder| {
|
||||
child_builder.add_archetype(UiEntity {
|
||||
node: Node::new(
|
||||
math::vec2(0.0, 0.0),
|
||||
Anchors::new(0.0, 1.0, 0.0, 1.0),
|
||||
Margins::new(20.0, 20.0, 20.0, 20.0),
|
||||
math::vec4(0.6, 0.6, 1.0, 1.0),
|
||||
),
|
||||
})
|
||||
})
|
||||
// alpha test
|
||||
.add_archetype(UiEntity {
|
||||
node: Node::new(
|
||||
math::vec2(200.0, 200.0),
|
||||
Anchors::new(0.5, 0.5, 0.5, 0.5),
|
||||
Margins::new(0.0, 100.0, 0.0, 100.0),
|
||||
math::vec4(1.0, 0.9, 0.9, 0.4),
|
||||
),
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ impl WorldBuilderSource for World {
|
|||
WorldBuilder {
|
||||
world: self,
|
||||
current_entity: None,
|
||||
last_entity: None,
|
||||
parent_entity: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,17 +23,14 @@ impl WorldBuilderSource for World {
|
|||
pub struct WorldBuilder<'a> {
|
||||
world: &'a mut World,
|
||||
current_entity: Option<Entity>,
|
||||
last_entity: Option<Entity>,
|
||||
parent_entity: Option<Entity>,
|
||||
}
|
||||
|
||||
impl<'a> WorldBuilder<'a> {
|
||||
pub fn build_entity(mut self) -> Self {
|
||||
let entity = *self.world.insert((), vec![()]).first().unwrap();
|
||||
if let Some(last_entity) = self.current_entity.take() {
|
||||
self.last_entity = Some(last_entity);
|
||||
}
|
||||
|
||||
self.current_entity = Some(entity);
|
||||
self.add_parent_to_current_entity();
|
||||
self
|
||||
}
|
||||
pub fn build(self) {}
|
||||
|
@ -69,24 +66,36 @@ impl<'a> WorldBuilder<'a> {
|
|||
}
|
||||
|
||||
pub fn add_archetype(mut self, entity_archetype: impl EntityArchetype) -> Self {
|
||||
if let Some(last_entity) = self.current_entity.take() {
|
||||
self.last_entity = Some(last_entity);
|
||||
}
|
||||
|
||||
self.current_entity = Some(entity_archetype.insert(self.world));
|
||||
let current_entity = entity_archetype.insert(self.world);
|
||||
self.current_entity = Some(current_entity);
|
||||
self.add_parent_to_current_entity();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_last_entity_as_parent(self) -> Self {
|
||||
let current_entity = self.current_entity.unwrap();
|
||||
let _ = self.world.add_component(
|
||||
current_entity,
|
||||
Parent(self.last_entity.unwrap()),
|
||||
);
|
||||
let _ = self
|
||||
.world
|
||||
.add_component(current_entity, LocalToParent::identity());
|
||||
pub fn add_children(self, build_children: impl Fn(WorldBuilder) -> WorldBuilder) -> Self {
|
||||
let mut child_builder = WorldBuilder {
|
||||
world: self.world,
|
||||
parent_entity: self.current_entity,
|
||||
current_entity: None,
|
||||
};
|
||||
|
||||
self
|
||||
child_builder = build_children(child_builder);
|
||||
|
||||
child_builder.current_entity = child_builder.parent_entity;
|
||||
child_builder.parent_entity = None;
|
||||
child_builder
|
||||
}
|
||||
|
||||
fn add_parent_to_current_entity(&mut self) {
|
||||
let current_entity = self.current_entity.unwrap();
|
||||
if let Some(parent_entity) = self.parent_entity {
|
||||
let _ = self.world.add_component(
|
||||
current_entity,
|
||||
Parent(parent_entity),
|
||||
);
|
||||
let _ = self
|
||||
.world
|
||||
.add_component(current_entity, LocalToParent::identity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue