Add Reflect derive to Events and contained types (#13149)

# Objective

The `Events` containerr should be reflectable, in order to make dev
tools that examine its state more useful.

Fixes #13148.

## Solution

- Add a `Reflect` derive to `Events`, gated behind the `bevy_reflect`
feature
- Add `Reflect` to the contained types to make everything compile.

---------

Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com>
This commit is contained in:
Alice Cecile 2024-05-01 14:47:11 -04:00 committed by GitHub
parent dac66e83b2
commit b3ed0dd002
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -11,6 +11,8 @@ use crate::{
}; };
pub use bevy_ecs_macros::Event; pub use bevy_ecs_macros::Event;
use bevy_ecs_macros::SystemSet; use bevy_ecs_macros::SystemSet;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
use bevy_utils::detailed_trace; use bevy_utils::detailed_trace;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use std::{ use std::{
@ -34,10 +36,12 @@ pub trait Event: Send + Sync + 'static {}
/// sent to the point it was processed. `EventId`s increase montonically by send order. /// sent to the point it was processed. `EventId`s increase montonically by send order.
/// ///
/// [`World`]: crate::world::World /// [`World`]: crate::world::World
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct EventId<E: Event> { pub struct EventId<E: Event> {
/// Uniquely identifies the event associated with this ID. /// Uniquely identifies the event associated with this ID.
// This value corresponds to the order in which each event was added to the world. // This value corresponds to the order in which each event was added to the world.
pub id: usize, pub id: usize,
#[cfg_attr(feature = "bevy_reflect", reflect(ignore))]
_marker: PhantomData<E>, _marker: PhantomData<E>,
} }
@ -93,6 +97,7 @@ impl<E: Event> Hash for EventId<E> {
} }
#[derive(Debug)] #[derive(Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
struct EventInstance<E: Event> { struct EventInstance<E: Event> {
pub event_id: EventId<E>, pub event_id: EventId<E>,
pub event: E, pub event: E,
@ -171,6 +176,7 @@ struct EventInstance<E: Event> {
/// [Example usage standalone.](https://github.com/bevyengine/bevy/blob/latest/crates/bevy_ecs/examples/events.rs) /// [Example usage standalone.](https://github.com/bevyengine/bevy/blob/latest/crates/bevy_ecs/examples/events.rs)
/// ///
#[derive(Debug, Resource)] #[derive(Debug, Resource)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct Events<E: Event> { pub struct Events<E: Event> {
/// Holds the oldest still active events. /// Holds the oldest still active events.
/// Note that `a.start_event_count + a.len()` should always be equal to `events_b.start_event_count`. /// Note that `a.start_event_count + a.len()` should always be equal to `events_b.start_event_count`.
@ -393,6 +399,7 @@ impl<E: Event> Extend<E> for Events<E> {
} }
#[derive(Debug)] #[derive(Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
struct EventSequence<E: Event> { struct EventSequence<E: Event> {
events: Vec<EventInstance<E>>, events: Vec<EventInstance<E>>,
start_event_count: usize, start_event_count: usize,