Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion crates/bevy_ecs/src/reflect/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
//!
//! Same as [`component`](`super::component`), but for events.

use crate::{event::Event, reflect::from_reflect_with_fallback, world::World};
use alloc::boxed::Box;

use crate::{
entity::Entity,
event::Event,
observer::{Observer, On},
reflect::from_reflect_with_fallback,
world::World,
};
use bevy_reflect::{FromReflect, FromType, PartialReflect, Reflect, TypePath, TypeRegistry};

/// A struct used to operate on reflected [`Event`] trait of a type.
Expand Down Expand Up @@ -37,6 +45,11 @@ pub struct ReflectEvent(ReflectEventFns);
pub struct ReflectEventFns {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR, but will we also want a fn register_event_key(&self, &mut World) -> EventKey, similar to ReflectComponent::register_component?

I think that would make it just possible to construct a completely dynamic observer using Observer::with_dynamic_runner, Observer::with_event_key, and ReflectFromPtr::from_ptr. And the fact that everything would be possible might discourage certain reviewers from trying to overgeneralize the convenience methods :).

/// 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) + Send + Sync>) -> Entity,
/// Function pointer implementing [`ReflectEvent::observe_entity`].
pub observe_entity:
fn(&mut World, Entity, Box<dyn Fn(&dyn PartialReflect) + Send + Sync>) -> Entity,
}

impl ReflectEventFns {
Expand All @@ -59,6 +72,25 @@ impl ReflectEvent {
(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) + Send + Sync>,
Comment thread
alice-i-cecile marked this conversation as resolved.
Outdated
) -> 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) + Send + Sync>,
) -> Entity {
(self.0.observe_entity)(world, entity, callback)
}

/// Create a custom implementation of [`ReflectEvent`].
///
/// This is an advanced feature,
Expand Down Expand Up @@ -105,6 +137,20 @@ where
let event = from_reflect_with_fallback::<E>(reflected_event, world, registry);
world.trigger(event);
},
observe: |world, callback| {
world
.add_observer(move |event: On<E>| {
callback((*event).as_partial_reflect());
})
.id()
},
observe_entity: |world, entity, callback| {
let observer = Observer::new(move |event: On<E>| {
callback((*event).as_partial_reflect());
})
.with_entity(entity);
Comment thread
mockersf marked this conversation as resolved.
Outdated
world.spawn(observer).id()
},
})
}
}