Skip to content

Commit

Permalink
node primitive removal
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Oct 11, 2024
1 parent c846ab8 commit a6f0962
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 44 deletions.
83 changes: 47 additions & 36 deletions crates/unavi-scripting/src/api/wired/scene/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,15 @@ impl HostMesh for ScriptData {

self.command_send
.try_send(Box::new(move |world: &mut World| {
// Remove node primitives.
for node_primitive in primitive.read().node_primitives.iter() {
world.entity_mut(node_primitive.entity).despawn();
let mut primitive_write = primitive.write();

for node_primitive in primitive_write.node_primitives.iter() {
let mut ent = world.entity_mut(node_primitive.entity);
ent.remove_parent();
ent.despawn();
}

primitive_write.node_primitives.clear();
}))
.unwrap();

Expand Down Expand Up @@ -202,19 +207,19 @@ mod tests {
}

fn test_create_node_primitives(
mut app: App,
app: &mut App,
data: &mut ScriptData,
node: Resource<NodeRes>,
primitive: Resource<PrimitiveRes>,
node: &Resource<NodeRes>,
primitive: &Resource<PrimitiveRes>,
) {
let world = app.world_mut();
data.push_commands(&mut world.commands());
world.flush_commands();

let inner = data.table.get(&primitive).unwrap().read();
let inner = data.table.get(primitive).unwrap().read();
assert_eq!(inner.node_primitives.len(), 1);

let node_data = data.table.get(&node).unwrap().read();
let node_data = data.table.get(node).unwrap().read();
let node_id = node_data.id;
let node_entity = *node_data.entity.get().unwrap();
assert_eq!(
Expand All @@ -227,30 +232,45 @@ mod tests {
.id,
node_id
);

assert_eq!(
world
.get::<Parent>(inner.node_primitives[0].entity)
.unwrap()
.get(),
node_entity
);
assert_eq!(world.get::<Children>(node_entity).unwrap().len(), 1);
}

fn test_remove_node_primitives(
app: &mut App,
data: &mut ScriptData,
node: &Resource<NodeRes>,
primitive: &PrimitiveRes,
) {
let ent = primitive.read().node_primitives[0].entity;

drop(inner);
drop(node_data);
let world = app.world_mut();
data.push_commands(&mut world.commands());
world.flush_commands();

HostNode::drop(data, node).unwrap();
HostPrimitive::drop(data, primitive).unwrap();
assert_eq!(primitive.read().node_primitives.len(), 0);

let node_data = data.table.get(node).unwrap().read();
let node_entity = *node_data.entity.get().unwrap();

assert!(world.get::<Parent>(ent).is_none());
assert!(world.get::<Children>(node_entity).is_none());
}

#[test]
fn test_set_mesh() {
let (app, mut data) = init_test_data();
let (mut app, mut data) = init_test_data();

let mesh = HostMesh::new(&mut data).unwrap();

let primitive =
HostMesh::create_primitive(&mut data, Resource::new_own(mesh.rep())).unwrap();
let primitive_data = data.table.get(&primitive).unwrap().clone();

let node = HostNode::new(&mut data).unwrap();
HostNode::set_mesh(
Expand All @@ -260,12 +280,16 @@ mod tests {
)
.unwrap();

test_create_node_primitives(app, &mut data, node, primitive);
test_create_node_primitives(&mut app, &mut data, &node, &primitive);

HostNode::set_mesh(&mut data, Resource::new_own(node.rep()), None).unwrap();

test_remove_node_primitives(&mut app, &mut data, &node, &primitive_data);
}

#[test]
fn test_create_primitive() {
let (app, mut data) = init_test_data();
fn test_create_remove_primitive() {
let (mut app, mut data) = init_test_data();

let node = HostNode::new(&mut data).unwrap();

Expand All @@ -279,25 +303,12 @@ mod tests {

let primitive =
HostMesh::create_primitive(&mut data, Resource::new_own(mesh.rep())).unwrap();
let primitive_data = data.table.get(&primitive).unwrap().clone();

test_create_node_primitives(app, &mut data, node, primitive);
}
test_create_node_primitives(&mut app, &mut data, &node, &primitive);

#[test]
fn test_remove_primitive() {
// TODO
// let (app, mut data) = init_test_data();
//
// let mesh = HostMesh::new(&mut data).unwrap();
// let primitive =
// HostMesh::create_primitive(&mut data, Resource::new_own(mesh.rep())).unwrap();
//
// let node = HostNode::new(&mut data).unwrap();
// HostNode::set_mesh(
// &mut data,
// Resource::new_own(node.rep()),
// Some(Resource::new_own(mesh.rep())),
// )
// .unwrap();
HostMesh::remove_primitive(&mut data, mesh, primitive).unwrap();

test_remove_node_primitives(&mut app, &mut data, &node, &primitive_data);
}
}
5 changes: 3 additions & 2 deletions crates/unavi-scripting/src/api/wired/scene/nodes/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ impl Drop for NodeRes {
if let Err(e) = self
.command_send
.try_send(Box::new(move |world: &mut World| {
let entity = *data.read().unwrap().entity.get().unwrap();
world.entity_mut(entity).despawn();
let mut ent = world.entity_mut(*data.read().unwrap().entity.get().unwrap());
ent.remove_parent();
ent.despawn();
}))
{
// Should only error when the entire script environment is being
Expand Down
16 changes: 10 additions & 6 deletions crates/unavi-scripting/src/api/wired/scene/nodes/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ impl HostNode for ScriptData {

// Remove previous mesh.
if let Some(prev_mesh) = data.read().mesh.clone() {
let mut prev_write = prev_mesh.write();
prev_write
let data_read = data.read();
let mut prev_mesh_write = prev_mesh.write();

prev_mesh_write
.nodes
.iter()
.position(|n| n.upgrade().unwrap().read().unwrap().id == prev_write.id)
.map(|index| prev_write.nodes.remove(index));
drop(prev_write);
.position(|n| n.upgrade().unwrap().read().unwrap().id == data_read.id)
.map(|index| prev_mesh_write.nodes.remove(index));
drop(prev_mesh_write);

let data = data.clone();
self.command_send
Expand All @@ -89,7 +91,9 @@ impl HostNode for ScriptData {
})
{
primitive_data.node_primitives.remove(i);
world.entity_mut(e).despawn();
let mut ent = world.entity_mut(e);
ent.remove_parent();
ent.despawn();
}
}
}))
Expand Down

0 comments on commit a6f0962

Please sign in to comment.