added more mutations and more elements to the demo to test the mutations

This commit is contained in:
dylanblokhuis 2023-12-17 20:58:29 +01:00
parent e10a0fabd3
commit 0a4b750eb6
2 changed files with 90 additions and 7 deletions

View File

@ -61,7 +61,13 @@ fn SceneTree<'a>(cx: Scope, selected_entity: &'a UseState<Option<Entity>>) -> El
rsx! {
for (entity, name) in entities {
div {
onclick: move |_| selected_entity.set(Some(entity)),
onclick: move |_| {
if Some(entity) == ***selected_entity {
selected_entity.set(None);
return;
}
selected_entity.set(Some(entity));
},
padding: "8",
background_color: if Some(entity) == ***selected_entity { INDIGO_600 } else { NEUTRAL_800 },
match name.name {
@ -84,11 +90,45 @@ fn SceneTree<'a>(cx: Scope, selected_entity: &'a UseState<Option<Entity>>) -> El
#[component]
fn EntityInspector<'a>(cx: Scope, selected_entity: &'a UseState<Option<Entity>>) -> Element {
let world = use_world(cx);
let components = if let Some(selected_entity) = selected_entity.get() {
let entity_ref = world.get_entity(*selected_entity).unwrap();
let archetype = entity_ref.archetype();
let mut components = archetype.components().map(|component_id| {
let info = world.components().get_info(component_id).unwrap();
let name = info.name();
(name, component_id, info.type_id(), info.layout().size())
}).collect::<Vec<_>>();
components.sort_by(|(name_a, ..), (name_b, ..)| name_a.cmp(name_b));
components
} else {
vec![]
};
render! {
if selected_entity.is_none() {
"Select an entity to view its components"
rsx! {
"Select an entity to view its components!!!"
}
} else {
"TODO: Component widgets"
rsx! {
div {
flex_direction: "column",
for (name, component_id, type_id, size) in components {
div {
padding: "8",
background_color: NEUTRAL_800,
div {
"Component: {name}"
}
}
}
}
}
}
}
}

View File

@ -85,7 +85,27 @@ pub fn apply_mutations(
bevy_ui_entity_to_element_id.insert(entity, id);
stack.push(entity);
}
Mutation::ReplaceWith { id, m } => todo!(),
Mutation::ReplaceWith { id, m } => {
let new_nodes = stack.split_off(stack.len() - m);
let existing = element_id_to_bevy_ui_entity[&id];
// here we insert before the old entity that's going to be removed after
let parent = world.entity(existing).get::<Parent>().unwrap().get();
let mut parent = world.entity_mut(parent);
let index = parent
.get::<Children>()
.unwrap()
.iter()
.position(|child| *child == existing)
.unwrap();
parent.insert_children(index, &new_nodes);
DespawnRecursive { entity: existing }.apply(world);
// TODO: We're not removing child entities from the element maps
if let Some(existing_element_id) = bevy_ui_entity_to_element_id.remove(&existing) {
element_id_to_bevy_ui_entity.remove(&existing_element_id);
}
},
Mutation::ReplacePlaceholder { path, m } => {
let mut existing = stack[stack.len() - m - 1];
for index in path {
@ -107,7 +127,7 @@ pub fn apply_mutations(
// TODO: We're not removing child entities from the element maps
if let Some(existing_element_id) = bevy_ui_entity_to_element_id.remove(&existing) {
element_id_to_bevy_ui_entity.remove(&existing_element_id);
}
}
}
Mutation::InsertAfter { id, m } => {
let entity = element_id_to_bevy_ui_entity[&id];
@ -123,7 +143,20 @@ pub fn apply_mutations(
let new = stack.drain((stack.len() - m)..).collect::<Vec<Entity>>();
parent.insert_children(index + 1, &new);
}
Mutation::InsertBefore { id, m } => todo!(),
Mutation::InsertBefore { id, m } => {
let new_nodes = stack.split_off(stack.len() - m);
let existing = element_id_to_bevy_ui_entity[&id];
let parent = world.entity(existing).get::<Parent>().unwrap().get();
let mut parent = world.entity_mut(parent);
let index = parent
.get::<Children>()
.unwrap()
.iter()
.position(|child| *child == existing)
.unwrap();
parent.insert_children(index, &new_nodes);
}
Mutation::SetAttribute {
name,
value,
@ -154,7 +187,17 @@ pub fn apply_mutations(
}
}
Mutation::RemoveEventListener { .. } => {}
Mutation::Remove { id } => todo!(),
Mutation::Remove { id } => {
let existing = element_id_to_bevy_ui_entity[&id];
DespawnRecursive {
entity: existing,
}
.apply(world);
// TODO: We're not removing child entities from the element maps
if let Some(existing_element_id) = bevy_ui_entity_to_element_id.remove(&existing) {
element_id_to_bevy_ui_entity.remove(&existing_element_id);
}
}
Mutation::PushRoot { id } => stack.push(element_id_to_bevy_ui_entity[&id]),
}
}