diff --git a/src/editor/timeline.rs b/src/editor/timeline.rs index 5a8aa01..0246fad 100644 --- a/src/editor/timeline.rs +++ b/src/editor/timeline.rs @@ -6,8 +6,7 @@ pub struct EditorTimelinePlugin; impl Plugin for EditorTimelinePlugin { fn build(&self, app: &mut App) { app.add_systems(Update, add_timeline_epoch.run_if(ui::activated::)) - .add_systems(Update, set_epoch_gltf) - .add_systems(Update, load_epoch_gltf) + .add_systems(Update, control_active_epoch) .add_systems(Update, set_epoch_scene) .add_systems(Update, load_epoch_scene) .add_systems(Update, set_epoch_camera) @@ -29,6 +28,12 @@ impl Plugin for EditorTimelinePlugin { #[derive(Debug, Component)] pub struct TimelineWidget; +#[derive(Debug, Resource)] +struct ActiveEpoch { + id: EpochId, + entity: Entity, +} + /// Add Epoch component, used on a button to trigger a new epoch addition #[derive(Debug, Component)] pub struct AddEpoch; @@ -39,12 +44,6 @@ pub struct EpochId { id: usize, } -/// Epoch GLTF Component -#[derive(Debug, Reflect, Component, Clone)] -pub struct EpochGltf { - gltf: Handle, -} - /// Epoch Scene Component #[derive(Debug, Reflect, Component, Clone)] pub struct EpochScene { @@ -87,6 +86,18 @@ pub struct EpochAnimations { animations: Vec>, } +fn control_active_epoch( + events: Query<(Entity, &EpochId), Added>, + mut commands: Commands, +) { + events.iter().for_each(|(entity, &ref id)| { + commands.insert_resource(ActiveEpoch { + id: id.clone(), + entity, + }); + }); +} + /// System for adding an epoch to the level's timeline /// Triggered when a button with the AddEpoch marker is Active fn add_timeline_epoch( @@ -120,53 +131,33 @@ fn add_timeline_epoch( }); } -/// Set the GLTF for the current epoch -fn set_epoch_gltf( - events: Query<&ui::TargetAsset, Added>, - active_epoch: Query, With)>, - mut commands: Commands, -) { - // Each time a GLTF is selected in the editor - events.iter().for_each(|ui::TargetAsset { handle }| { - // Iterate over all (0 or 1) active epochs - active_epoch.iter().for_each(|entity| { - // Set the GLTF (overwrite existing GLTF selections) - commands.entity(entity).insert(EpochGltf { - gltf: handle.clone(), - }); - - // TODO: Unset Scene, Camera, Animations - }); - }); -} - -fn load_epoch_gltf(events: Query, (Added, With)>) { - events.iter().for_each(|epoch_gltf| { - warn!("TODO: Load epoch GLTF!"); - }) -} - fn set_epoch_scene( - events: Query<&ui::TargetAsset, Added>, - active_epoch: Query, With)>, + mut events: EventReader, + active_epoch: Option>, mut commands: Commands, ) { // Each time a Scene is selected in the editor - events.iter().for_each(|ui::TargetAsset { handle }| { - // Iterate over all (0 or 1) active epochs - active_epoch.iter().for_each(|entity| { - // Set the Scene (overwrite existing Scene selections) - commands.entity(entity).insert(EpochScene { - scene: handle.clone(), - }); + events.iter().for_each(|event| { + active_epoch.iter().for_each(|active| { + match event { + ControlScene::Spawn(handle) => { + // Set the Scene (overwrite existing Scene selections) + commands.entity(active.entity).insert(EpochScene { + scene: handle.clone(), + }); + } + ControlScene::Despawn(_) => (), + } }); }); } -fn load_epoch_scene(events: Query, (Added, With)>) { - events.iter().for_each(|epoch_scene| { - warn!("TODO: Load epoch Scene!"); - }) +fn load_epoch_scene(active_epoch: Option>) { + if let Some(active) = active_epoch { + if active.is_added() || active.is_changed() { + warn!("TODO: Load epoch Scene!"); + } + } } fn set_epoch_camera( @@ -186,10 +177,12 @@ fn set_epoch_camera( }); } -fn load_epoch_camera(events: Query, (Added, With)>) { - events.iter().for_each(|epoch_camera| { - warn!("TODO: Load epoch Camera"); - }) +fn load_epoch_camera(active_epoch: Option>) { + if let Some(active) = active_epoch { + if active.is_added() || active.is_changed() { + warn!("TODO: Load epoch Camera"); + } + } } fn set_epoch_music( @@ -209,10 +202,12 @@ fn set_epoch_music( }); } -fn load_epoch_music(events: Query, (Added, With)>) { - events.iter().for_each(|epoch_music| { - warn!("TODO: Load epoch music!"); - }) +fn load_epoch_music(active_epoch: Option>) { + if let Some(active) = active_epoch { + if active.is_added() || active.is_changed() { + warn!("TODO: Load epoch music!"); + } + } } fn set_epoch_monologue( @@ -272,22 +267,32 @@ fn load_epoch_font( } fn set_epoch_sfx( - events: Query<&ui::TargetAsset, Added>, + mut events: EventReader, mut active_epoch: Query<(Entity, Option<&mut EpochSfx>), (With, With)>, mut commands: Commands, ) { // Each time a Scene is selected in the editor - events.iter().for_each(|ui::TargetAsset { handle }| { + events.iter().for_each(|event| { // Iterate over all (0 or 1) active epochs active_epoch.iter_mut().for_each(|(entity, maybe_sfx)| { - info!("Adding sfx {:?} to epoch {:?}", maybe_sfx, entity); - if let Some(mut epoch_sfx) = maybe_sfx { - epoch_sfx.sfx.push(handle.clone()); - } else { - // Set the Scene (overwrite existing Scene selections) - commands.entity(entity).insert(EpochSfx { - sfx: vec![handle.clone()], - }); + match event { + ControlAudio::Loop(handle) => { + debug!("Adding sfx {:?} to epoch {:?}", handle, entity); + if let Some(mut epoch_sfx) = maybe_sfx { + epoch_sfx.sfx.push(handle.clone()); + } else { + // Set the Scene (overwrite existing Scene selections) + commands.entity(entity).insert(EpochSfx { + sfx: vec![handle.clone()], + }); + } + } + ControlAudio::Stop(handle) => { + if let Some(mut epoch_sfx) = maybe_sfx { + debug!("Removing sfx {:?} from epoch {:?}", handle, entity); + epoch_sfx.sfx.retain(|element| element != handle); + } + } } }); });