From fab810c00762e02279aaeb16bb436259217deac0 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Sun, 24 Sep 2023 16:16:48 -0700 Subject: [PATCH] camera UI fixed --- src/editor/animation.rs | 147 +++++++++++++++++++++++++++++----------- src/editor/audio.rs | 2 + src/editor/camera.rs | 25 ++++--- src/editor/scene.rs | 2 +- 4 files changed, 126 insertions(+), 50 deletions(-) diff --git a/src/editor/animation.rs b/src/editor/animation.rs index 95f1c0b..25995b3 100644 --- a/src/editor/animation.rs +++ b/src/editor/animation.rs @@ -5,7 +5,8 @@ pub struct EditorAnimationPlugin; impl Plugin for EditorAnimationPlugin { fn build(&self, app: &mut App) { - app.add_systems(Update, sync_asset_buttons::) + app.add_event::() + .add_systems(Update, sync_asset_buttons::) .add_systems(Update, sync_remove_asset_buttons::) .add_systems(Update, set_epoch_animations) .add_systems(Update, load_epoch_animations) @@ -13,7 +14,9 @@ impl Plugin for EditorAnimationPlugin { .add_systems(Update, remove_animations_ui) .add_systems(Update, add_animations_ui) .add_systems(Update, play_all_animations) - .add_systems(Update, play_animation); + .add_systems(Update, play_animation) + .add_systems(Update, ui_control_animations) + .add_systems(Update, ui_active_animation); } } @@ -112,50 +115,19 @@ pub fn remove_animations_ui( }); } -pub fn play_all_animations( - start: Query, Added)>, - mut stop: RemovedComponents, - play_all_btn: Query>, - clip_btns: Query>>, - mut commands: Commands, -) { - stop.iter() - .filter(|&entity| play_all_btn.contains(entity)) - .for_each(|_| { - clip_btns.iter().for_each(|entity| { - commands.entity(entity).remove::(); - }) - }); - start - .iter() - .filter(|&entity| play_all_btn.contains(entity)) - .for_each(|_| { - clip_btns.iter().for_each(|entity| { - commands.entity(entity).insert(ui::Active); - }) - }); +#[derive(Debug, Event)] +pub enum ControlAnimation { + Play(Handle), + Pause(Handle), } pub fn play_animation( - start: Query, Added)>, - mut stop: RemovedComponents, - clip_refs: Query<&ui::TargetAsset>, + mut events: EventReader, mut targets: Query<(&mut AnimationPlayer, &Name), With>, clips: Res>, ) { - stop.iter().for_each(|entity| { - if let Ok(ui::TargetAsset { handle }) = clip_refs.get(entity) { - let clip = clips.get(&handle).expect("Load animation clip"); - targets - .iter_mut() - .filter(|(_, name)| clip.compatible_with(name)) - .for_each(|(mut player, _)| { - player.pause(); - }) - } - }); - start.iter().for_each(|entity| { - if let Ok(ui::TargetAsset { handle }) = clip_refs.get(entity) { + events.iter().for_each(|event| match event { + ControlAnimation::Play(handle) => { let clip = clips.get(&handle).expect("Load animation clip"); targets .iter_mut() @@ -166,7 +138,102 @@ pub fn play_animation( } else { player.play(handle.clone()).repeat(); } + }); + } + ControlAnimation::Pause(handle) => { + let clip = clips.get(handle).expect("Load animation clip"); + targets + .iter_mut() + .filter(|(_, name)| clip.compatible_with(name)) + .for_each(|(mut player, _)| { + player.pause(); + }); + } + }); +} + +pub fn play_all_animations( + events: Query< + (Entity, &Interaction, Option<&ui::Active>), + (With, Changed), + >, + clip_btns: Query<&ui::TargetAsset>, + mut writer: EventWriter, + mut commands: Commands, +) { + events + .iter() + .filter_map(|(entity, &interaction, active)| { + (interaction == Interaction::Pressed).then_some((entity, active)) + }) + .for_each(|(entity, active)| match active { + Some(_) => { + commands.entity(entity).remove::(); + clip_btns.iter().for_each(|ui::TargetAsset { handle }| { + writer.send(ControlAnimation::Pause(handle.clone())); + }); + } + None => { + commands.entity(entity).insert(ui::Active); + clip_btns.iter().for_each(|ui::TargetAsset { handle }| { + writer.send(ControlAnimation::Play(handle.clone())); + }); + } + }) +} + +fn ui_control_animations( + events: Query< + ( + &Interaction, + &ui::TargetAsset, + Option<&ui::Active>, + ), + (With