Path: blob/main/crates/bevy_animation/src/animation_event.rs
6849 views
pub use bevy_animation_macros::AnimationEvent;12use bevy_ecs::{3entity::Entity,4event::{trigger_entity_internal, Event, Trigger},5observer::{CachedObservers, TriggerContext},6world::DeferredWorld,7};89/// An [`Event`] that an [`AnimationPlayer`](crate::AnimationPlayer) can trigger when playing an [`AnimationClip`](crate::AnimationClip).10/// See [`AnimationClip::add_event`](crate::AnimationClip::add_event).11///12/// This trait can be derived.13pub trait AnimationEvent: Clone + for<'a> Event<Trigger<'a> = AnimationEventTrigger> {}1415/// The [`Trigger`] implementation for [`AnimationEvent`]. This passes in the [`AnimationPlayer`](crate::AnimationPlayer)16/// context, and uses that to run any observers that target that entity.17pub struct AnimationEventTrigger {18/// The [`AnimationPlayer`](crate::AnimationPlayer) where this [`AnimationEvent`] occurred.19pub animation_player: Entity,20}2122#[expect(23unsafe_code,24reason = "We must implement this trait to define a custom Trigger, which is required to be unsafe due to safety considerations within bevy_ecs."25)]26// SAFETY:27// - `E`'s [`Event::Trigger`] is constrained to [`AnimationEventTrigger`]28// - The implementation abides by the other safety constraints defined in [`Trigger`]29unsafe impl<E: AnimationEvent + for<'a> Event<Trigger<'a> = AnimationEventTrigger>> Trigger<E>30for AnimationEventTrigger31{32unsafe fn trigger(33&mut self,34world: DeferredWorld,35observers: &CachedObservers,36trigger_context: &TriggerContext,37event: &mut E,38) {39let animation_player = self.animation_player;40// SAFETY:41// - `observers` come from `world` and match the event type `E`, enforced by the call to `trigger`42// - the passed in event pointer comes from `event`, which is an `Event`43// - `trigger` is a matching trigger type, as it comes from `self`, which is the Trigger for `E`44// - `trigger_context`'s event_key matches `E`, enforced by the call to `trigger`45// - this abides by the nuances defined in the `Trigger` safety docs46unsafe {47trigger_entity_internal(48world,49observers,50event.into(),51self.into(),52animation_player,53trigger_context,54);55}56}57}585960