better parenting ergonomics in world builder

This commit is contained in:
Carter Anderson 2020-03-09 02:00:59 -07:00
parent 91a6c0d9db
commit a790332505
3 changed files with 115 additions and 127 deletions

View file

@ -38,8 +38,9 @@ fn setup(world: &mut World, resources: &mut Resources) {
..Default::default()
})
.add(Rotator)
.add_children(|child_builder| {
// cube
.add_archetype(MeshEntity {
child_builder.add_archetype(MeshEntity {
mesh: cube_handle,
material: StandardMaterial {
albedo: math::vec4(0.5, 0.4, 0.3, 1.0).into(),
@ -47,7 +48,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
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),

View file

@ -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(
.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
world.insert(
(),
vec![(Node::new(
.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
world.insert(
(),
vec![(Node::new(
.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),
),)],
);
world.insert(
(),
vec![(Node::new(
),
})
.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),
),)],
);
world.insert(
(),
vec![(Node::new(
),
})
.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),
),)],
);
world.insert(
(),
vec![(Node::new(
),
})
.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
let parent = *world
.insert(
(),
vec![(Node::new(
.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(
),
})
.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),
),
Parent(parent),
)],
);
})
})
// alpha test
world.insert(
(),
vec![(Node::new(
.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();
}

View file

@ -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 {
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,
};
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(self.last_entity.unwrap()),
Parent(parent_entity),
);
let _ = self
.world
.add_component(current_entity, LocalToParent::identity());
self
}
}
}