diff --git a/src/editor/assets.rs b/src/editor/assets.rs index 88c7a49..a001fbe 100644 --- a/src/editor/assets.rs +++ b/src/editor/assets.rs @@ -142,14 +142,10 @@ pub fn sync_asset_buttons( buttons .iter() .find_map(|(entity, ui::TargetAsset { handle })| { - if handle == this_handle { - Some(entity) - } else { - None - } + (handle == this_handle).then_some(entity) }) - .iter() - .for_each(|&entity| { + .into_iter() + .for_each(|entity| { commands.entity(entity).insert(ui::Active); }); }); @@ -166,20 +162,16 @@ pub fn sync_remove_asset_buttons( events .iter() .find_map(|this_asset_entity| asset_entities.get(this_asset_entity).ok()) - .iter() + .into_iter() .for_each(|this_handle| { info!("Syncing removal of {:?}", this_handle); buttons .iter() .find_map(|(entity, ui::TargetAsset { handle })| { - if handle == *this_handle { - Some(entity) - } else { - None - } + (handle == this_handle).then_some(entity) }) - .iter() - .for_each(|&entity| { + .into_iter() + .for_each(|entity| { commands.entity(entity).remove::(); }); }); diff --git a/src/editor/audio.rs b/src/editor/audio.rs index 74e70a4..5b6b884 100644 --- a/src/editor/audio.rs +++ b/src/editor/audio.rs @@ -6,14 +6,14 @@ pub struct EditorAudioPlugin; impl Plugin for EditorAudioPlugin { fn build(&self, app: &mut App) { - app.add_systems(Update, audio_ui) + app.register_type::() + .add_event::() + .add_systems(Update, audio_ui) .add_systems(Update, ui_control_audio) .add_systems(Update, ui_active::) .add_systems(Update, ui_inactive::) .add_systems(Update, control_audio) .add_systems(Update, sync_asset_buttons::) - .register_type::() - .add_event::() .add_systems(Update, sync_remove_asset_buttons::); } } diff --git a/src/editor/gltf.rs b/src/editor/gltf.rs index cc8e038..139597f 100644 --- a/src/editor/gltf.rs +++ b/src/editor/gltf.rs @@ -1,79 +1,2 @@ -use crate::editor::prelude::*; -#[derive(Debug, Default)] -pub struct EditorGltfPlugin; -impl Plugin for EditorGltfPlugin { - fn build(&self, app: &mut App) { - app.add_systems(Update, sync_asset_buttons::) - .add_systems(Update, sync_remove_asset_buttons::) - .add_systems(Update, gltf_ui); - } -} - -#[derive(Debug, Component, Default)] -pub struct GltfWidget; - -pub fn gltf_ui( - mut events: EventReader>, - mut commands: Commands, - widget: Query>, - current: Query<(Entity, &ui::TargetAsset)>, - server: Res, -) { - events.iter().for_each(|event| match event { - AssetEvent::Created { handle } => { - info!("Asset created! {:?}", event); - create_asset_button( - &widget, - &mut commands, - ui::TargetAsset { - handle: handle.clone(), - }, - get_asset_name(&server, handle.clone()), - None, - ); - } - AssetEvent::Removed { handle } => { - info!("Asset removed! {:?}", event); - destroy_asset_button( - ¤t, - &mut commands, - &ui::TargetAsset { - handle: handle.clone(), - }, - ); - } - AssetEvent::Modified { handle } => { - info!("Asset modified! {:?}", event); - destroy_asset_button( - ¤t, - &mut commands, - &ui::TargetAsset { - handle: handle.clone(), - }, - ); - create_asset_button( - &widget, - &mut commands, - ui::TargetAsset { - handle: handle.clone(), - }, - get_asset_name(&server, handle.clone()), - None, - ); - } - }); -} - -pub fn control_active_gltf( - events: Query>, Added)>, - root: Query>, - mut commands: Commands, -) { - events.iter().for_each(|_| { - root.iter().for_each(|entity| { - commands.entity(entity).despawn_descendants(); - }); - }); -} diff --git a/src/editor/mod.rs b/src/editor/mod.rs index 943891a..32d79f3 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -3,7 +3,6 @@ pub mod assets; pub mod audio; pub mod camera; pub mod font; -pub mod gltf; pub mod level; pub mod lighting; pub mod monologue; @@ -26,15 +25,11 @@ pub fn ui_active( buttons .iter() .find_map(|(entity, ui::TargetAsset { handle })| { - if handle == this_handle { - Some(entity) - } else { - None - } + (handle == this_handle).then_some(entity) }) - .and_then(|entity| { + .into_iter() + .for_each(|entity| { commands.entity(entity).insert(ui::Active); - Some(()) }); }); } @@ -51,9 +46,9 @@ pub fn ui_inactive( .find_map(|(entity, ui::TargetAsset { handle })| { (!sources.iter().any(|this_handle| this_handle == handle)).then_some(entity) }) - .and_then(|entity| { + .into_iter() + .for_each(|entity| { commands.entity(entity).remove::(); - Some(()) }); }); } @@ -156,8 +151,6 @@ fn initialize_ui(mut commands: Commands) { .push(spawn_tab_container::("Audio", parent)); content_containers .push(spawn_tab_container::("Level", parent)); - content_containers - .push(spawn_tab_container::("Gltf", parent)); content_containers .push(spawn_tab_container::("Scene", parent)); content_containers diff --git a/src/editor/monologue.rs b/src/editor/monologue.rs index 85e1322..a59adf0 100644 --- a/src/editor/monologue.rs +++ b/src/editor/monologue.rs @@ -17,7 +17,6 @@ impl Plugin for EditorMonologuePlugin { .add_event::() .add_systems(Update, sync_asset_buttons::) .add_systems(Update, sync_remove_asset_buttons::) - .add_systems(Update, control_active_gltf) .add_systems(Update, control_monologue) .add_systems(Update, ui_control_monologue) .add_systems(Update, ui_active::) @@ -62,7 +61,6 @@ impl AssetLoader for MonologueLoader { let asset = Monologue { text: String::from_utf8(bytes.to_vec())?, }; - info!("!!! Loading Monologue !!!"); load_context.set_default_asset(LoadedAsset::new(asset)); Ok(()) }) diff --git a/src/editor/plugin.rs b/src/editor/plugin.rs index 45f2a5b..02cc71f 100644 --- a/src/editor/plugin.rs +++ b/src/editor/plugin.rs @@ -10,7 +10,6 @@ impl Plugin for EditorPlugin { .add_plugins(EditorAudioPlugin::default()) .add_plugins(EditorCameraPlugin::default()) .add_plugins(EditorFontPlugin::default()) - .add_plugins(EditorGltfPlugin::default()) .add_plugins(EditorLevelPlugin::default()) .add_plugins(EditorLightingPlugin::default()) .add_plugins(EditorMonologuePlugin::default()) diff --git a/src/editor/prelude.rs b/src/editor/prelude.rs index 866245d..97006d9 100644 --- a/src/editor/prelude.rs +++ b/src/editor/prelude.rs @@ -1,7 +1,7 @@ pub use crate::{ editor::{ - animation::*, assets::*, audio::*, camera::*, font::*, gltf::*, level::*, lighting::*, - monologue::*, quit::*, scene::*, timeline::*, *, + animation::*, assets::*, audio::*, camera::*, font::*, level::*, lighting::*, monologue::*, + quit::*, scene::*, timeline::*, *, }, ui, }; diff --git a/src/editor/scene.rs b/src/editor/scene.rs index 6a6099d..e197e07 100644 --- a/src/editor/scene.rs +++ b/src/editor/scene.rs @@ -5,40 +5,56 @@ pub struct EditorScenePlugin; impl Plugin for EditorScenePlugin { fn build(&self, app: &mut App) { - app.add_systems(Update, remove_scenes_ui) - .add_systems(Update, add_scenes_ui) - .add_systems(Update, control_active_scenes) + app.add_event::() .add_systems(Update, sync_asset_buttons::) - .add_systems(Update, sync_remove_asset_buttons::); + .add_systems(Update, sync_remove_asset_buttons::) + .add_systems(Update, add_scenes_ui) + .add_systems(Update, remove_scenes_ui) + .add_systems(Update, sync_asset_buttons::) + .add_systems(Update, sync_remove_asset_buttons::) + .add_systems(Update, control_scene) + .add_systems(Update, ui_control_scene); } } +#[derive(Debug, Event)] +pub enum ControlScene { + Spawn(Handle), + Despawn(Handle), +} + #[derive(Debug, Component, Default)] pub struct SceneWidget; pub fn add_scenes_ui( - gltf_selected: Query<&ui::TargetAsset, Added>>, + mut events: EventReader>, mut commands: Commands, gltfs: Res>, widget: Query>, server: Res, ) { - gltf_selected.iter().for_each(|ui::TargetAsset { handle }| { - let gltf_name = get_asset_name(&server, handle.clone()); - if let Some(gltf) = gltfs.get(&handle.clone()) { - gltf.named_scenes.iter().for_each(|(name, handle)| { - create_asset_button( - &widget, - &mut commands, - ui::TargetAsset { - handle: handle.clone(), - }, - format!("{}/{}", gltf_name, name), - None, - ); - }) - } - }); + events + .iter() + .filter_map(|event| match event { + AssetEvent::Created { handle } => Some(handle), + _ => None, + }) + .for_each(|handle| { + let gltf_name = get_asset_name(&server, handle.clone()); + if let Some(gltf) = gltfs.get(&handle.clone()) { + gltf.named_scenes.iter().for_each(|(name, handle)| { + create_asset_button( + &widget, + &mut commands, + ui::TargetAsset { + handle: handle.clone(), + }, + format!("{}/{}", gltf_name, name), + None, + ); + }) + } + }); } pub fn remove_scenes_ui( @@ -65,44 +81,48 @@ pub fn remove_scenes_ui( }); } -pub fn control_active_scenes( - added: Query, Added)>, - mut removed: RemovedComponents, - scene_refs: Query<&ui::TargetAsset>, +pub fn control_scene( + mut events: EventReader, + root: Query>, scenes: Query<(Entity, &Handle)>, - level_root: Query>, mut commands: Commands, ) { - // A scene button was marked inactive - removed.iter().for_each(|entity| { - // Get the handle associated with that button - scene_refs - .get(entity) - .iter() - .for_each(|ui::TargetAsset { handle }| { - scenes - .iter() - .find_map(|(entity, this_handle)| (this_handle == handle).then_some(entity)) - .iter() - .for_each(|&entity| { - commands.entity(entity).despawn_recursive(); - }); - }); - }); - added.iter().for_each(|entity| { - scene_refs - .get(entity) - .iter() - .for_each(|ui::TargetAsset { handle }| { - info!("Spawning Scene {:?}", handle); - commands - .entity(level_root.single()) - .with_children(|parent| { - parent.spawn(SceneBundle { - scene: handle.clone(), - ..default() - }); - }); + events.iter().for_each(|event| match event { + ControlScene::Spawn(handle) => { + commands.entity(root.single()).with_children(|parent| { + parent.spawn(SceneBundle { + scene: handle.clone(), + ..default() + }); }); + } + ControlScene::Despawn(handle) => { + scenes + .iter() + .find_map(|(entity, this_handle)| (handle == this_handle).then_some(entity)) + .into_iter() + .for_each(|entity| commands.entity(entity).despawn_recursive()); + } }); } + +pub fn ui_control_scene( + events: Query< + (&Interaction, &ui::TargetAsset, Option<&ui::Active>), + (With