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