Add spawn entity button

This commit is contained in:
JMS55 2023-12-16 21:33:13 -08:00
parent 322e0dbcb6
commit e10a0fabd3
2 changed files with 33 additions and 3 deletions

View File

@ -2,7 +2,7 @@ use bevy::{
app::{App, Startup},
core::{DebugName, Name},
core_pipeline::core_2d::Camera2dBundle,
ecs::{entity::Entity, query::Without, system::Commands},
ecs::{entity::Entity, query::Without, system::Commands, world::World},
ui::{node_bundles::NodeBundle, Node},
DefaultPlugins,
};
@ -47,6 +47,10 @@ fn SceneTree<'a>(cx: Scope, selected_entity: &'a UseState<Option<Entity>>) -> El
let mut entities = entities.into_iter().collect::<Vec<_>>();
entities.sort_by_key(|(entity, _)| *entity);
let spawn_entity = use_system(cx, |world: &mut World| {
world.spawn_empty();
});
render! {
div {
onclick: move |_| selected_entity.set(None),
@ -68,6 +72,12 @@ fn SceneTree<'a>(cx: Scope, selected_entity: &'a UseState<Option<Entity>>) -> El
}
}
}
div {
onclick: move |_| spawn_entity(),
padding: "8",
background_color: NEUTRAL_800,
"Spawn Entity"
}
}
}
}

View File

@ -44,7 +44,14 @@ pub fn apply_mutations(
parent.add_child(child);
}
}
Mutation::AssignId { path, id } => todo!(),
Mutation::AssignId { path, id } => {
let mut entity = *stack.last().unwrap();
for index in path {
entity = world.entity(entity).get::<Children>().unwrap()[*index as usize];
}
element_id_to_bevy_ui_entity.insert(id, entity);
bevy_ui_entity_to_element_id.insert(entity, id);
}
Mutation::CreatePlaceholder { id } => {
let entity = world.spawn(NodeBundle::default()).id();
element_id_to_bevy_ui_entity.insert(id, entity);
@ -102,7 +109,20 @@ pub fn apply_mutations(
element_id_to_bevy_ui_entity.remove(&existing_element_id);
}
}
Mutation::InsertAfter { id, m } => todo!(),
Mutation::InsertAfter { id, m } => {
let entity = element_id_to_bevy_ui_entity[&id];
let parent = world.entity(entity).get::<Parent>().unwrap().get();
let mut parent = world.entity_mut(parent);
let index = parent
.get::<Children>()
.unwrap()
.iter()
.position(|child| *child == entity)
.unwrap();
let new = stack.drain((stack.len() - m)..).collect::<Vec<Entity>>();
parent.insert_children(index + 1, &new);
}
Mutation::InsertBefore { id, m } => todo!(),
Mutation::SetAttribute {
name,