Path: blob/main/crates/bevy_animation/src/animation_event.rs
9367 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) or an [`AnimationTargetId`](crate::AnimationTargetId) can trigger when playing an [`AnimationClip`](crate::AnimationClip).10///11/// - If you used [`AnimationClip::add_event`](crate::AnimationClip::add_event), this will be triggered by the [`AnimationPlayer`](crate::AnimationPlayer).12/// - If you used [`AnimationClip::add_event_to_target`](crate::AnimationClip::add_event_to_target), this will be triggered by the [`AnimationTargetId`](crate::AnimationTargetId).13///14/// This trait can be derived.15pub trait AnimationEvent: Clone + for<'a> Event<Trigger<'a> = AnimationEventTrigger> {}1617/// The [`Trigger`] implementation for [`AnimationEvent`]. This passes in either the [`AnimationPlayer`](crate::AnimationPlayer) or the [`AnimationTargetId`](crate::AnimationTargetId)18/// context, and uses that to run any observers that target that entity. See [`AnimationEvent`] for when which entity is used.19#[derive(Debug)]20pub struct AnimationEventTrigger {21/// The [`AnimationPlayer`](crate::AnimationPlayer) or the [`AnimationTargetId`](crate::AnimationTargetId) where this [`AnimationEvent`] occurred.22/// See [`AnimationEvent`] for when which entity is used.23pub target: Entity,24}2526#[expect(27unsafe_code,28reason = "We must implement this trait to define a custom Trigger, which is required to be unsafe due to safety considerations within bevy_ecs."29)]30// SAFETY:31// - `E`'s [`Event::Trigger`] is constrained to [`AnimationEventTrigger`]32// - The implementation abides by the other safety constraints defined in [`Trigger`]33unsafe impl<E: AnimationEvent + for<'a> Event<Trigger<'a> = AnimationEventTrigger>> Trigger<E>34for AnimationEventTrigger35{36unsafe fn trigger(37&mut self,38world: DeferredWorld,39observers: &CachedObservers,40trigger_context: &TriggerContext,41event: &mut E,42) {43let target = self.target;44// SAFETY:45// - `observers` come from `world` and match the event type `E`, enforced by the call to `trigger`46// - the passed in event pointer comes from `event`, which is an `Event`47// - `trigger` is a matching trigger type, as it comes from `self`, which is the Trigger for `E`48// - `trigger_context`'s event_key matches `E`, enforced by the call to `trigger`49// - this abides by the nuances defined in the `Trigger` safety docs50unsafe {51trigger_entity_internal(52world,53observers,54event.into(),55self.into(),56target,57trigger_context,58);59}60}61}626364