|
|
|
|
@ -5,7 +5,8 @@ pub struct EditorAnimationPlugin;
|
|
|
|
|
|
|
|
|
|
impl Plugin for EditorAnimationPlugin {
|
|
|
|
|
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, 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,61 +115,125 @@ pub fn remove_animations_ui(
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn play_all_animations(
|
|
|
|
|
start: Query<Entity, (With<Button>, Added<ui::Active>)>,
|
|
|
|
|
mut stop: RemovedComponents<ui::Active>,
|
|
|
|
|
play_all_btn: Query<Entity, With<AnimationPlayAll>>,
|
|
|
|
|
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);
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
#[derive(Debug, Event)]
|
|
|
|
|
pub enum ControlAnimation {
|
|
|
|
|
Play(Handle<AnimationClip>),
|
|
|
|
|
Pause(Handle<AnimationClip>),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn play_animation(
|
|
|
|
|
start: Query<Entity, (With<Button>, Added<ui::Active>)>,
|
|
|
|
|
mut stop: RemovedComponents<ui::Active>,
|
|
|
|
|
clip_refs: Query<&ui::TargetAsset<AnimationClip>>,
|
|
|
|
|
mut events: EventReader<ControlAnimation>,
|
|
|
|
|
mut targets: Query<(&mut AnimationPlayer, &Name), With<Transform>>,
|
|
|
|
|
clips: Res<Assets<AnimationClip>>,
|
|
|
|
|
) {
|
|
|
|
|
stop.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()
|
|
|
|
|
.filter(|(_, name)| clip.compatible_with(name))
|
|
|
|
|
.for_each(|(mut player, _)| {
|
|
|
|
|
player.pause();
|
|
|
|
|
})
|
|
|
|
|
if player.is_paused() {
|
|
|
|
|
player.resume();
|
|
|
|
|
} else {
|
|
|
|
|
player.play(handle.clone()).repeat();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
start.iter().for_each(|entity| {
|
|
|
|
|
if let Ok(ui::TargetAsset { handle }) = clip_refs.get(entity) {
|
|
|
|
|
let clip = clips.get(&handle).expect("Load animation clip");
|
|
|
|
|
}
|
|
|
|
|
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, _)| {
|
|
|
|
|
if player.is_paused() {
|
|
|
|
|
player.resume();
|
|
|
|
|
} else {
|
|
|
|
|
player.play(handle.clone()).repeat();
|
|
|
|
|
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>();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|