From 53e85dbcd0a04df2966192307f3fcf4508f160ef Mon Sep 17 00:00:00 2001 From: Lillian Vixe Date: Sun, 9 Jun 2024 13:16:14 -0700 Subject: [PATCH] fixed reshape and rebuild to update on modifications. --- src/vvedit/s_editor_ui.rs | 47 +++++++++----- src/vvlib/s_structure_asset.rs | 111 ++++++++++++++++++--------------- 2 files changed, 92 insertions(+), 66 deletions(-) diff --git a/src/vvedit/s_editor_ui.rs b/src/vvedit/s_editor_ui.rs index c7d849a..78d48ca 100644 --- a/src/vvedit/s_editor_ui.rs +++ b/src/vvedit/s_editor_ui.rs @@ -37,6 +37,7 @@ pub struct GridEditData { pub struct StructureEditData { pub path: String, pub id: Handle, + pub edit_entity: Entity, //which instance of this structure is representing the one we can edit in the structure editor. other instances will exist which will be tricky (seperate store?) pub has_changed_since_last_save: bool, } @@ -209,12 +210,6 @@ pub fn startup_system_edit_ui( spawn_camera(&mut commands); let str = "test.vvs"; let handle = asset_server.load::(str); - let data = StructureEditData { - path: str.into(), - id: handle.clone(), - has_changed_since_last_save: false, - }; - shared_ui_state.structures.push(data); //let id = OctTreeAsset::load(&mut pairs, asset_server.into(), mesh_assets, "test.vvg".into()); let parent = commands @@ -223,6 +218,17 @@ pub fn startup_system_edit_ui( ..Default::default() }) .id(); + let id = commands.spawn((SpatialBundle::from_transform(Transform::IDENTITY), StructureComponent { + asset: handle.clone(), + nodes: Default::default(), + })).id(); + let data = StructureEditData { + path: str.into(), + id: handle.clone(), + has_changed_since_last_save: false, + edit_entity: id.clone(), + }; + shared_ui_state.structures.push(data); /*let child = commands .spawn(( PbrBundle { @@ -237,16 +243,12 @@ pub fn startup_system_edit_ui( )) .id(); commands.entity(parent).add_child(child);*/ - let id = commands.spawn((SpatialBundle::INHERITED_IDENTITY, StructureComponent { - asset: handle, - nodes: Default::default(), - })).id(); commands.entity(parent).add_child(id.clone()); event_writer.send(StructureUpdateMessage { - update_type: StructureUpdateType::Build, + update_type: StructureUpdateType::Rebuild, entity: id, }); - let light_transform = Transform::from_xyz(7f32, 7f32, 7f32).looking_at(Vec3::ZERO, Vec3::Y); + let light_transform = Transform::from_xyz(4f32, 4f32, 4f32).looking_at(Vec3::ZERO, Vec3::Y); commands.spawn(PointLightBundle { point_light: PointLight { @@ -417,6 +419,7 @@ pub fn edit_window_ui( windows: Query>, window: Query<&Window>, mut asset_server: ResMut, + mut event_writer: EventWriter, ) { shared_ui_state.egui.windows.clear(); let title = String::from("VVEdit"); @@ -622,6 +625,7 @@ pub fn edit_window_ui( if structure.path == structure_name { let asset = structure_assets.get_mut(structure.id.clone()); if asset.is_some() { + let mut should_reshape = false; let selection = asset.unwrap(); ui.collapsing("Node Data", |ui| { let mut trans = { @@ -632,14 +636,22 @@ pub fn edit_window_ui( } }.clone(); ui.label("Position"); - editable_vec3(ui, &mut trans.translation); + editable_vec3(ui, &mut trans.translation, &mut should_reshape); ui.label("Scale"); - editable_vec3(ui, &mut trans.scale); + editable_vec3(ui, &mut trans.scale, &mut should_reshape); ui.label("Rotation"); - editable_quat(ui, &mut trans.rotation); + editable_quat(ui, &mut trans.rotation, &mut should_reshape); + if should_reshape { + + println!("code reached 2."); + event_writer.send(StructureUpdateMessage { + update_type: StructureUpdateType::Reshape, + entity: structure.edit_entity, + }); - selection.default_pose_positions.insert(selection_name, trans); - structure.has_changed_since_last_save = true; + selection.default_pose_positions.insert(selection_name, trans); + structure.has_changed_since_last_save = true; + } }); } @@ -723,5 +735,6 @@ pub fn edit_window_ui( &mut contexts, &mut asset_server, &mut structure_assets, + &mut event_writer, ); } diff --git a/src/vvlib/s_structure_asset.rs b/src/vvlib/s_structure_asset.rs index ef412bb..09579ec 100644 --- a/src/vvlib/s_structure_asset.rs +++ b/src/vvlib/s_structure_asset.rs @@ -10,7 +10,7 @@ use bevy::{ system::{Commands, Query, ResMut}, world::Mut, }, - hierarchy::{BuildChildren, DespawnRecursiveExt}, + hierarchy::BuildChildren, pbr::{PbrBundle, StandardMaterial}, prelude::{default, SpatialBundle}, reflect::TypePath, @@ -25,7 +25,7 @@ use thiserror::Error; use super::s_oct_asset::{MeshingOctTreePairs, OctAssetMarker, OctTreeAsset}; -#[derive(Asset, TypePath)] +#[derive(Asset, TypePath, Debug)] pub struct StructureAsset { pub layout: StringTree, pub default_pose_positions: HashMap, @@ -52,7 +52,7 @@ pub enum StructureUpdateType { pub fn build_tree( root_entity: &Entity, asset: &StructureAsset, - structure_component: Mut, + mut structure_component: Mut, materials: &mut ResMut>, meshes: &mut ResMut>, pairs: &mut ResMut, @@ -68,45 +68,50 @@ pub fn build_tree( return Ordering::Equal; }); for each in sorted_construction_order { - if let Some(element_data) = asset.element_data.get(&each) { - let trans = { - if let Some(transform) = asset.default_pose_positions.get(&each) { - transform + let element_data = if let Some(element_data) = asset.element_data.get(&each) { + element_data.clone() + } else { + ElementData::None + }; + let trans = { + if let Some(transform) = asset.default_pose_positions.get(&each) { + transform + } else { + &Transform::IDENTITY + } + }; + let p_entity = { + if let Some(parent) = asset.layout.parent_of(&each) { + if let Some(result) = structure_component.nodes.get(parent) { + result.clone() } else { - &Transform::IDENTITY + root_entity.clone() } - }; - let p_entity = { - if let Some(parent) = asset.layout.parent_of(&each) { - if let Some(result) = structure_component.nodes.get(parent) { - result - } else { - &root_entity - } - } else { - &root_entity + } else { + root_entity.clone() + } + }; + let child = { + match element_data { + ElementData::None => { + let mut spat_bundle = SpatialBundle::from_transform(trans.clone()); + spat_bundle.visibility = Visibility::Inherited; + commands.spawn(spat_bundle).id() } - }; - let child = { - match element_data { - ElementData::None => { - let mut spat_bundle = SpatialBundle::from_transform(trans.clone()); - spat_bundle.visibility = Visibility::Inherited; - commands.spawn(spat_bundle).id() - } - ElementData::Unprocessed(_) => { - println!("somehow not processed in build"); - let mut spat_bundle = SpatialBundle::from_transform(trans.clone()); - spat_bundle.visibility = Visibility::Inherited; - commands.spawn(spat_bundle).id() - } - ElementData::MeshAsset(handle, _) => { - let mut spat_bundle = SpatialBundle::from_transform(trans.clone()); - spat_bundle.visibility = Visibility::Inherited; - commands - .spawn(spat_bundle) - .with_children(|parent| { - parent.spawn(( + ElementData::Unprocessed(_) => { + println!("somehow not processed in build"); + let mut spat_bundle = SpatialBundle::from_transform(trans.clone()); + spat_bundle.visibility = Visibility::Inherited; + commands.spawn(spat_bundle).id() + } + ElementData::MeshAsset(handle, _) => { + let mut spat_bundle = SpatialBundle::from_transform(trans.clone()); + spat_bundle.visibility = Visibility::Inherited; + commands + .spawn(spat_bundle) + .with_children(|parent| { + let id = parent + .spawn(( PbrBundle { mesh: pairs.mesh_handle_for(handle.clone(), meshes), material: materials.add(Color::rgb(1., 1., 1.)), @@ -115,16 +120,20 @@ pub fn build_tree( OctAssetMarker { oct_handle: handle.clone(), }, - )); - }) - .id() - } + )) + .id(); + structure_component.nodes.insert( + id.to_bits().to_string() + "||||" + each.clone().as_str(), + id, + ); + }) + .id() } - }; + } + }; + structure_component.nodes.insert(each, child.clone()); - commands.entity(p_entity.clone()).add_child(child.clone()); - println!("{}", child.clone().to_bits()); - } + commands.entity(p_entity.clone()).add_child(child.clone()); } } @@ -165,8 +174,9 @@ pub fn structure_update( &mut commands, ), StructureUpdateType::Rebuild => { + dbg!(&structure_component.nodes); for (_, each) in &structure_component.nodes { - commands.entity(each.clone()).despawn_recursive(); + commands.entity(each.clone()).despawn(); } structure_component.nodes.clear(); build_tree( @@ -184,7 +194,10 @@ pub fn structure_update( if let Ok((_, mut trans)) = shaper.get_mut(ent.clone()) { if let Some(value) = asset.default_pose_positions.get(name) { trans.clone_from(value); + println!("code reached 3."); } + } else { + println!("code failed step 3."); } } } @@ -194,7 +207,7 @@ pub fn structure_update( } } -#[derive(Clone)] +#[derive(Clone, Debug)] pub enum ElementData { None, Unprocessed(String),