camera UI fixed

main
Elijah Voigt 2 years ago
parent 822c2de042
commit fab810c007

@ -5,7 +5,8 @@ pub struct EditorAnimationPlugin;
impl Plugin for EditorAnimationPlugin { impl Plugin for EditorAnimationPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Update, sync_asset_buttons::<AnimationClip>) app.add_event::<ControlAnimation>()
.add_systems(Update, sync_asset_buttons::<AnimationClip>)
.add_systems(Update, sync_remove_asset_buttons::<AnimationClip>) .add_systems(Update, sync_remove_asset_buttons::<AnimationClip>)
.add_systems(Update, set_epoch_animations) .add_systems(Update, set_epoch_animations)
.add_systems(Update, load_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, remove_animations_ui)
.add_systems(Update, add_animations_ui) .add_systems(Update, add_animations_ui)
.add_systems(Update, play_all_animations) .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( #[derive(Debug, Event)]
start: Query<Entity, (With<Button>, Added<ui::Active>)>, pub enum ControlAnimation {
mut stop: RemovedComponents<ui::Active>, Play(Handle<AnimationClip>),
play_all_btn: Query<Entity, With<AnimationPlayAll>>, Pause(Handle<AnimationClip>),
clip_btns: Query<Entity, With<ui::TargetAsset<AnimationClip>>>,
mut commands: Commands,
) {
stop.iter()
.filter(|&entity| play_all_btn.contains(entity))
.for_each(|_| {
clip_btns.iter().for_each(|entity| {
commands.entity(entity).remove::<ui::Active>();
})
});
start
.iter()
.filter(|&entity| play_all_btn.contains(entity))
.for_each(|_| {
clip_btns.iter().for_each(|entity| {
commands.entity(entity).insert(ui::Active);
})
});
} }
pub fn play_animation( pub fn play_animation(
start: Query<Entity, (With<Button>, Added<ui::Active>)>, mut events: EventReader<ControlAnimation>,
mut stop: RemovedComponents<ui::Active>,
clip_refs: Query<&ui::TargetAsset<AnimationClip>>,
mut targets: Query<(&mut AnimationPlayer, &Name), With<Transform>>, mut targets: Query<(&mut AnimationPlayer, &Name), With<Transform>>,
clips: Res<Assets<AnimationClip>>, clips: Res<Assets<AnimationClip>>,
) { ) {
stop.iter().for_each(|entity| { events.iter().for_each(|event| match event {
if let Ok(ui::TargetAsset { handle }) = clip_refs.get(entity) { ControlAnimation::Play(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();
})
}
});
start.iter().for_each(|entity| {
if let Ok(ui::TargetAsset { handle }) = clip_refs.get(entity) {
let clip = clips.get(&handle).expect("Load animation clip"); let clip = clips.get(&handle).expect("Load animation clip");
targets targets
.iter_mut() .iter_mut()
@ -166,7 +138,102 @@ pub fn play_animation(
} else { } else {
player.play(handle.clone()).repeat(); 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<AnimationPlayAll>, Changed<Interaction>),
>,
clip_btns: Query<&ui::TargetAsset<AnimationClip>>,
mut writer: EventWriter<ControlAnimation>,
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::<ui::Active>();
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<AnimationClip>,
Option<&ui::Active>,
),
(With<Button>, Changed<Interaction>),
>,
mut writer: EventWriter<ControlAnimation>,
) {
events
.iter()
.filter_map(
|(interaction, ui::TargetAsset { handle }, active)| match interaction {
Interaction::Pressed => Some((handle, active)),
_ => None,
},
)
.for_each(|(handle, active)| match active {
Some(_) => writer.send(ControlAnimation::Pause(handle.clone())),
None => writer.send(ControlAnimation::Play(handle.clone())),
});
}
pub fn ui_active_animation(
mut events: EventReader<ControlAnimation>,
targets: Query<(Entity, &ui::TargetAsset<AnimationClip>)>,
mut commands: Commands,
) {
events.iter().for_each(|event| match event {
ControlAnimation::Play(this_handle) => {
targets
.iter()
.find_map(|(entity, ui::TargetAsset { handle })| {
(handle == this_handle).then_some(entity)
})
.into_iter()
.for_each(|entity| {
commands.entity(entity).insert(ui::Active);
});
}
ControlAnimation::Pause(this_handle) => {
targets
.iter()
.find_map(|(entity, ui::TargetAsset { handle })| {
(handle == this_handle).then_some(entity)
}) })
.into_iter()
.for_each(|entity| {
commands.entity(entity).remove::<ui::Active>();
});
} }
}); });
} }

@ -1,3 +1,5 @@
// TODO: Stop all button
use crate::editor::prelude::*; use crate::editor::prelude::*;
use bevy::audio::PlaybackMode; use bevy::audio::PlaybackMode;

@ -5,7 +5,8 @@ pub struct EditorCameraPlugin;
impl Plugin for EditorCameraPlugin { impl Plugin for EditorCameraPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Update, cameras_ui) app.insert_resource(Msaa::Sample4)
.add_systems(Update, cameras_ui)
.add_systems(Update, control_active_camera) .add_systems(Update, control_active_camera)
.add_systems(Update, ui_control_active_camera) .add_systems(Update, ui_control_active_camera)
.add_systems(Update, fallback_camera); .add_systems(Update, fallback_camera);
@ -71,7 +72,7 @@ pub fn ui_control_active_camera(
(&Interaction, &ui::TargetEntity, Option<&ui::Active>), (&Interaction, &ui::TargetEntity, Option<&ui::Active>),
(With<Button>, Changed<Interaction>), (With<Button>, Changed<Interaction>),
>, >,
mut cameras: Query<(Entity, &mut Camera)>, mut cameras: Query<(Entity, &mut Camera, Option<&EditorCamera>)>,
) { ) {
events events
.iter() .iter()
@ -79,13 +80,19 @@ pub fn ui_control_active_camera(
(interaction == Interaction::Pressed).then_some((entity, active)) (interaction == Interaction::Pressed).then_some((entity, active))
}) })
.for_each(|(&entity, active)| { .for_each(|(&entity, active)| {
cameras.iter_mut().for_each(|(this, mut camera)| { cameras
if this == entity { .iter_mut()
camera.is_active = !active.is_some(); .for_each(|(this, mut camera, editor_cam)| {
} else { if this == entity {
camera.is_active = false; if editor_cam.is_some() {
} camera.is_active = true;
}); } else {
camera.is_active = !active.is_some();
}
} else {
camera.is_active = false;
}
});
}); });
} }

@ -49,7 +49,7 @@ pub fn add_scenes_ui(
ui::TargetAsset { ui::TargetAsset {
handle: handle.clone(), handle: handle.clone(),
}, },
format!("{}/{}", gltf_name, name), format!("({}) {}", gltf_name, name),
None, None,
); );
}) })

Loading…
Cancel
Save