forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevent.rs
More file actions
160 lines (149 loc) · 5.98 KB
/
event.rs
File metadata and controls
160 lines (149 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! Definitions for [`Event`] reflection.
//! This allows triggering events whose type is only known at runtime.
//!
//! This module exports two types: [`ReflectEventFns`] and [`ReflectEvent`].
//!
//! Same as [`component`](`super::component`), but for events.
use alloc::boxed::Box;
use crate::{
entity::Entity,
event::Event,
observer::{Observer, On},
reflect::from_reflect_with_fallback,
world::{DeferredWorld, World},
};
use bevy_reflect::{FromReflect, FromType, PartialReflect, Reflect, TypePath, TypeRegistry};
/// A struct used to operate on reflected [`Event`] trait of a type.
///
/// A [`ReflectEvent`] for type `T` can be obtained via
/// [`bevy_reflect::TypeRegistration::data`].
#[derive(Clone)]
pub struct ReflectEvent(ReflectEventFns);
/// The raw function pointers needed to make up a [`ReflectEvent`].
///
/// This is used when creating custom implementations of [`ReflectEvent`] with
/// [`ReflectEvent::new()`].
///
/// > **Note:**
/// > Creating custom implementations of [`ReflectEvent`] is an advanced feature
/// > that most users will not need. Usually a [`ReflectEvent`] is created for a
/// > type by deriving [`Reflect`] and adding the `#[reflect(Event)]` attribute.
/// > After adding the event to the [`TypeRegistry`], its [`ReflectEvent`] can
/// > then be retrieved when needed.
///
/// Creating a custom [`ReflectEvent`] may be useful if you need to create new
/// event types at runtime, for example, for scripting implementations.
///
/// By creating a custom [`ReflectEvent`] and inserting it into a type's
/// [`TypeRegistration`][bevy_reflect::TypeRegistration], you can modify the way
/// that reflected event of that type will be triggered in the Bevy world.
#[derive(Clone)]
pub struct ReflectEventFns {
/// Function pointer implementing [`ReflectEvent::trigger`].
pub trigger: fn(&mut World, &dyn PartialReflect, &TypeRegistry),
/// Function pointer implementing [`ReflectEvent::observe`].
pub observe:
fn(&mut World, Box<dyn Fn(&dyn PartialReflect, DeferredWorld) + Send + Sync>) -> Entity,
/// Function pointer implementing [`ReflectEvent::observe_entity`].
pub observe_entity: fn(
&mut World,
Entity,
Box<dyn Fn(&dyn PartialReflect, DeferredWorld) + Send + Sync>,
) -> Entity,
}
impl ReflectEventFns {
/// Get the default set of [`ReflectEventFns`] for a specific event type
/// using its [`FromType`] implementation.
///
/// This is useful if you want to start with the default implementation
/// before overriding some of the functions to create a custom implementation.
pub fn new<'a, T: Event + FromReflect + TypePath>() -> Self
where
T::Trigger<'a>: Default,
{
<ReflectEvent as FromType<T>>::from_type().0
}
}
impl ReflectEvent {
/// Triggers a reflected [`Event`] like [`trigger()`](World::trigger).
pub fn trigger(&self, world: &mut World, event: &dyn PartialReflect, registry: &TypeRegistry) {
(self.0.trigger)(world, event, registry);
}
/// Registers a global [`Observer`] for this event type.
pub fn observe(
&self,
world: &mut World,
callback: Box<dyn Fn(&dyn PartialReflect, DeferredWorld) + Send + Sync>,
) -> Entity {
(self.0.observe)(world, callback)
}
/// Registers an entity-scoped [`Observer`] for this event type.
pub fn observe_entity(
&self,
world: &mut World,
entity: Entity,
callback: Box<dyn Fn(&dyn PartialReflect, DeferredWorld) + Send + Sync>,
) -> Entity {
(self.0.observe_entity)(world, entity, callback)
}
/// Create a custom implementation of [`ReflectEvent`].
///
/// This is an advanced feature,
/// useful for scripting implementations,
/// that should not be used by most users
/// unless you know what you are doing.
///
/// Usually you should derive [`Reflect`] and add the `#[reflect(Event)]`
/// attribute to generate a [`ReflectEvent`] implementation automatically.
///
/// See [`ReflectEventFns`] for more information.
pub fn new(fns: ReflectEventFns) -> Self {
ReflectEvent(fns)
}
/// The underlying function pointers implementing methods on [`ReflectEvent`].
///
/// This is useful when you want to keep track locally of an individual
/// function pointer.
///
/// Calling [`TypeRegistry::get`] followed by
/// [`TypeRegistration::data::<ReflectEvent>`] can be costly if done several
/// times per frame. Consider cloning [`ReflectEvent`] and keeping it
/// between frames, cloning a `ReflectEvent` is very cheap.
///
/// If you only need a subset of the methods on `ReflectEvent`,
/// use `fn_pointers` to get the underlying [`ReflectEventFns`]
/// and copy the subset of function pointers you care about.
///
/// [`TypeRegistration::data::<ReflectEvent>`]: bevy_reflect::TypeRegistration::data
/// [`TypeRegistry::get`]: bevy_reflect::TypeRegistry::get
pub fn fn_pointers(&self) -> &ReflectEventFns {
&self.0
}
}
impl<'a, E: Event + Reflect + TypePath> FromType<E> for ReflectEvent
where
<E as Event>::Trigger<'a>: Default,
{
fn from_type() -> Self {
ReflectEvent(ReflectEventFns {
trigger: |world, reflected_event, registry| {
let event = from_reflect_with_fallback::<E>(reflected_event, world, registry);
world.trigger(event);
},
observe: |world, callback| {
world
.add_observer(move |event: On<E>, world: DeferredWorld| {
callback((*event).as_partial_reflect(), world);
})
.id()
},
observe_entity: |world, entity, callback| {
let observer = Observer::new(move |event: On<E>, world: DeferredWorld| {
callback((*event).as_partial_reflect(), world);
})
.with_entity(entity);
world.spawn(observer).id()
},
})
}
}