|
| 1 | +#[cfg(feature = "bevy_reflect")] |
| 2 | +use bevy_reflect::Reflect; |
| 3 | +use std::{ |
| 4 | + cmp::Ordering, |
| 5 | + fmt, |
| 6 | + hash::{Hash, Hasher}, |
| 7 | + marker::PhantomData, |
| 8 | +}; |
| 9 | + |
| 10 | +/// A type that can be stored in an [`Events<E>`] resource |
| 11 | +/// You can conveniently access events using the [`EventReader`] and [`EventWriter`] system parameter. |
| 12 | +/// |
| 13 | +/// Events must be thread-safe. |
| 14 | +#[diagnostic::on_unimplemented( |
| 15 | + message = "`{Self}` is not an `Event`", |
| 16 | + label = "invalid `Event`", |
| 17 | + note = "consider annotating `{Self}` with `#[derive(Event)]`" |
| 18 | +)] |
| 19 | +pub trait Event: Send + Sync + 'static {} |
| 20 | + |
| 21 | +/// An `EventId` uniquely identifies an event stored in a specific [`World`]. |
| 22 | +/// |
| 23 | +/// An `EventId` can among other things be used to trace the flow of an event from the point it was |
| 24 | +/// sent to the point it was processed. `EventId`s increase monotonically by send order. |
| 25 | +/// |
| 26 | +/// [`World`]: crate::world::World |
| 27 | +#[cfg_attr(feature = "bevy_reflect", derive(Reflect))] |
| 28 | +pub struct EventId<E: Event> { |
| 29 | + /// Uniquely identifies the event associated with this ID. |
| 30 | + // This value corresponds to the order in which each event was added to the world. |
| 31 | + pub id: usize, |
| 32 | + #[cfg_attr(feature = "bevy_reflect", reflect(ignore))] |
| 33 | + pub(super) _marker: PhantomData<E>, |
| 34 | +} |
| 35 | + |
| 36 | +impl<E: Event> Copy for EventId<E> {} |
| 37 | + |
| 38 | +impl<E: Event> Clone for EventId<E> { |
| 39 | + fn clone(&self) -> Self { |
| 40 | + *self |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl<E: Event> fmt::Display for EventId<E> { |
| 45 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 46 | + <Self as fmt::Debug>::fmt(self, f) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl<E: Event> fmt::Debug for EventId<E> { |
| 51 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 52 | + write!( |
| 53 | + f, |
| 54 | + "event<{}>#{}", |
| 55 | + std::any::type_name::<E>().split("::").last().unwrap(), |
| 56 | + self.id, |
| 57 | + ) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl<E: Event> PartialEq for EventId<E> { |
| 62 | + fn eq(&self, other: &Self) -> bool { |
| 63 | + self.id == other.id |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl<E: Event> Eq for EventId<E> {} |
| 68 | + |
| 69 | +impl<E: Event> PartialOrd for EventId<E> { |
| 70 | + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 71 | + Some(self.cmp(other)) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl<E: Event> Ord for EventId<E> { |
| 76 | + fn cmp(&self, other: &Self) -> Ordering { |
| 77 | + self.id.cmp(&other.id) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl<E: Event> Hash for EventId<E> { |
| 82 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 83 | + Hash::hash(&self.id, state); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[derive(Debug)] |
| 88 | +#[cfg_attr(feature = "bevy_reflect", derive(Reflect))] |
| 89 | +pub(crate) struct EventInstance<E: Event> { |
| 90 | + pub event_id: EventId<E>, |
| 91 | + pub event: E, |
| 92 | +} |
0 commit comments