Change event to event_key where it refers to an EventKey (#20060)
# Objective The ECS code to do with Observers was recently refactored to use an `EventKey` newtype. On reading through the PR, I was a bit confused that sometimes a variable called `event` refers to an `Event` and sometimes it refers to an `EventKey`. I think the code is clearer when `event` refers to an `Event` and `event_key` refers to an `EventKey`. ## Solution This PR renames some uses of `event` to `event_key`. ## Testing - Did you test these changes? If so, how? I ran `cargo run -p ci -- tests` - Are there any parts that need more testing? No - How can other people (reviewers) test your changes? Is there anything specific they need to know? No. - If relevant, what platforms did you test these changes on, and are there any important ones you can't test? Probably not relevant, but MacOS. --------- Co-authored-by: Steve Alexander <steve.alexander@priva.com>
This commit is contained in:
parent
2bcd85421e
commit
713acc2e6d
@ -12,7 +12,7 @@ struct RegisteredEvent {
|
|||||||
event_key: EventKey,
|
event_key: EventKey,
|
||||||
// Required to flush the secondary buffer and drop events even if left unchanged.
|
// Required to flush the secondary buffer and drop events even if left unchanged.
|
||||||
previously_updated: bool,
|
previously_updated: bool,
|
||||||
// SAFETY: The component ID and the function must be used to fetch the Events<T> resource
|
// SAFETY: The `EventKey`'s component ID and the function must be used to fetch the Events<T> resource
|
||||||
// of the same type initialized in `register_event`, or improper type casts will occur.
|
// of the same type initialized in `register_event`, or improper type casts will occur.
|
||||||
update: unsafe fn(MutUntyped),
|
update: unsafe fn(MutUntyped),
|
||||||
}
|
}
|
||||||
|
|||||||
@ -248,7 +248,7 @@ impl Observer {
|
|||||||
world.commands().queue(move |world: &mut World| {
|
world.commands().queue(move |world: &mut World| {
|
||||||
let entity = hook_context.entity;
|
let entity = hook_context.entity;
|
||||||
if let Some(mut observe) = world.get_mut::<Observer>(entity) {
|
if let Some(mut observe) = world.get_mut::<Observer>(entity) {
|
||||||
if observe.descriptor.events.is_empty() {
|
if observe.descriptor.event_keys.is_empty() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if observe.error_handler.is_none() {
|
if observe.error_handler.is_none() {
|
||||||
@ -286,13 +286,13 @@ impl Observer {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Observe the given `event`. This will cause the [`Observer`] to run whenever an event with the given [`ComponentId`]
|
/// Observe the given `event_key`. This will cause the [`Observer`] to run whenever an event with the given [`EventKey`]
|
||||||
/// is triggered.
|
/// is triggered.
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// The type of the `event` [`EventKey`] _must_ match the actual value
|
/// The type of the `event_key` [`EventKey`] _must_ match the actual value
|
||||||
/// of the event passed into the observer system.
|
/// of the event passed into the observer system.
|
||||||
pub unsafe fn with_event(mut self, event: EventKey) -> Self {
|
pub unsafe fn with_event_key(mut self, event_key: EventKey) -> Self {
|
||||||
self.descriptor.events.push(event);
|
self.descriptor.event_keys.push(event_key);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -349,8 +349,8 @@ impl Component for Observer {
|
|||||||
/// This information is stored inside of the [`Observer`] component,
|
/// This information is stored inside of the [`Observer`] component,
|
||||||
#[derive(Default, Clone)]
|
#[derive(Default, Clone)]
|
||||||
pub struct ObserverDescriptor {
|
pub struct ObserverDescriptor {
|
||||||
/// The events the observer is watching.
|
/// The event keys the observer is watching.
|
||||||
pub(super) events: Vec<EventKey>,
|
pub(super) event_keys: Vec<EventKey>,
|
||||||
|
|
||||||
/// The components the observer is watching.
|
/// The components the observer is watching.
|
||||||
pub(super) components: Vec<ComponentId>,
|
pub(super) components: Vec<ComponentId>,
|
||||||
@ -360,12 +360,12 @@ pub struct ObserverDescriptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ObserverDescriptor {
|
impl ObserverDescriptor {
|
||||||
/// Add the given `events` to the descriptor.
|
/// Add the given `event_keys` to the descriptor.
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// The type of each [`EventKey`] in `events` _must_ match the actual value
|
/// The type of each [`EventKey`] in `event_keys` _must_ match the actual value
|
||||||
/// of the event passed into the observer.
|
/// of the event passed into the observer.
|
||||||
pub unsafe fn with_events(mut self, events: Vec<EventKey>) -> Self {
|
pub unsafe fn with_event_keys(mut self, event_keys: Vec<EventKey>) -> Self {
|
||||||
self.events = events;
|
self.event_keys = event_keys;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -381,9 +381,9 @@ impl ObserverDescriptor {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the `events` that the observer is watching.
|
/// Returns the `event_keys` that the observer is watching.
|
||||||
pub fn events(&self) -> &[EventKey] {
|
pub fn event_keys(&self) -> &[EventKey] {
|
||||||
&self.events
|
&self.event_keys
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the `components` that the observer is watching.
|
/// Returns the `components` that the observer is watching.
|
||||||
@ -416,7 +416,7 @@ fn hook_on_add<E: Event, B: Bundle, S: ObserverSystem<E, B>>(
|
|||||||
components.push(id);
|
components.push(id);
|
||||||
});
|
});
|
||||||
if let Some(mut observer) = world.get_mut::<Observer>(entity) {
|
if let Some(mut observer) = world.get_mut::<Observer>(entity) {
|
||||||
observer.descriptor.events.push(event_key);
|
observer.descriptor.event_keys.push(event_key);
|
||||||
observer.descriptor.components.extend(components);
|
observer.descriptor.components.extend(components);
|
||||||
|
|
||||||
let system: &mut dyn Any = observer.system.as_mut();
|
let system: &mut dyn Any = observer.system.as_mut();
|
||||||
|
|||||||
@ -43,7 +43,7 @@ fn component_clone_observed_by(_source: &SourceComponent, ctx: &mut ComponentClo
|
|||||||
.get_mut::<Observer>(observer_entity)
|
.get_mut::<Observer>(observer_entity)
|
||||||
.expect("Source observer entity must have Observer");
|
.expect("Source observer entity must have Observer");
|
||||||
observer_state.descriptor.entities.push(target);
|
observer_state.descriptor.entities.push(target);
|
||||||
let event_keys = observer_state.descriptor.events.clone();
|
let event_keys = observer_state.descriptor.event_keys.clone();
|
||||||
let components = observer_state.descriptor.components.clone();
|
let components = observer_state.descriptor.components.clone();
|
||||||
for event_key in event_keys {
|
for event_key in event_keys {
|
||||||
let observers = world.observers.get_observers_mut(event_key);
|
let observers = world.observers.get_observers_mut(event_key);
|
||||||
|
|||||||
@ -379,7 +379,7 @@ impl World {
|
|||||||
};
|
};
|
||||||
let descriptor = &observer_state.descriptor;
|
let descriptor = &observer_state.descriptor;
|
||||||
|
|
||||||
for &event_key in &descriptor.events {
|
for &event_key in &descriptor.event_keys {
|
||||||
let cache = observers.get_observers_mut(event_key);
|
let cache = observers.get_observers_mut(event_key);
|
||||||
|
|
||||||
if descriptor.components.is_empty() && descriptor.entities.is_empty() {
|
if descriptor.components.is_empty() && descriptor.entities.is_empty() {
|
||||||
@ -430,7 +430,7 @@ impl World {
|
|||||||
let archetypes = &mut self.archetypes;
|
let archetypes = &mut self.archetypes;
|
||||||
let observers = &mut self.observers;
|
let observers = &mut self.observers;
|
||||||
|
|
||||||
for &event_key in &descriptor.events {
|
for &event_key in &descriptor.event_keys {
|
||||||
let cache = observers.get_observers_mut(event_key);
|
let cache = observers.get_observers_mut(event_key);
|
||||||
if descriptor.components.is_empty() && descriptor.entities.is_empty() {
|
if descriptor.components.is_empty() && descriptor.entities.is_empty() {
|
||||||
cache.global_observers.remove(&entity);
|
cache.global_observers.remove(&entity);
|
||||||
@ -741,7 +741,7 @@ mod tests {
|
|||||||
Observer::new(|_: On<Add, A>, mut res: ResMut<Order>| {
|
Observer::new(|_: On<Add, A>, mut res: ResMut<Order>| {
|
||||||
res.observed("add/remove");
|
res.observed("add/remove");
|
||||||
})
|
})
|
||||||
.with_event(on_remove)
|
.with_event_key(on_remove)
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -1015,7 +1015,7 @@ mod tests {
|
|||||||
Observer::with_dynamic_runner(|mut world, _trigger, _ptr, _propagate| {
|
Observer::with_dynamic_runner(|mut world, _trigger, _ptr, _propagate| {
|
||||||
world.resource_mut::<Order>().observed("event_a");
|
world.resource_mut::<Order>().observed("event_a");
|
||||||
})
|
})
|
||||||
.with_event(event_a)
|
.with_event_key(event_a)
|
||||||
};
|
};
|
||||||
world.spawn(observe);
|
world.spawn(observe);
|
||||||
|
|
||||||
|
|||||||
@ -181,7 +181,7 @@ impl<'w, E, B: Bundle> DerefMut for On<'w, E, B> {
|
|||||||
pub struct ObserverTrigger {
|
pub struct ObserverTrigger {
|
||||||
/// The [`Entity`] of the observer handling the trigger.
|
/// The [`Entity`] of the observer handling the trigger.
|
||||||
pub observer: Entity,
|
pub observer: Entity,
|
||||||
/// The [`Event`] the trigger targeted.
|
/// The [`EventKey`] the trigger targeted.
|
||||||
pub event_key: EventKey,
|
pub event_key: EventKey,
|
||||||
/// The [`ComponentId`]s the trigger targeted.
|
/// The [`ComponentId`]s the trigger targeted.
|
||||||
pub components: SmallVec<[ComponentId; 2]>,
|
pub components: SmallVec<[ComponentId; 2]>,
|
||||||
|
|||||||
@ -781,18 +781,18 @@ impl<'w> DeferredWorld<'w> {
|
|||||||
/// Triggers all event observers for [`ComponentId`] in target.
|
/// Triggers all event observers for [`ComponentId`] in target.
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// Caller must ensure observers listening for `event` can accept ZST pointers
|
/// Caller must ensure observers listening for `event_key` can accept ZST pointers
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) unsafe fn trigger_observers(
|
pub(crate) unsafe fn trigger_observers(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: EventKey,
|
event_key: EventKey,
|
||||||
target: Option<Entity>,
|
target: Option<Entity>,
|
||||||
components: impl Iterator<Item = ComponentId> + Clone,
|
components: impl Iterator<Item = ComponentId> + Clone,
|
||||||
caller: MaybeLocation,
|
caller: MaybeLocation,
|
||||||
) {
|
) {
|
||||||
Observers::invoke::<_>(
|
Observers::invoke::<_>(
|
||||||
self.reborrow(),
|
self.reborrow(),
|
||||||
event,
|
event_key,
|
||||||
target,
|
target,
|
||||||
target,
|
target,
|
||||||
components,
|
components,
|
||||||
@ -805,11 +805,11 @@ impl<'w> DeferredWorld<'w> {
|
|||||||
/// Triggers all event observers for [`ComponentId`] in target.
|
/// Triggers all event observers for [`ComponentId`] in target.
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// Caller must ensure `E` is accessible as the type represented by `event`
|
/// Caller must ensure `E` is accessible as the type represented by `event_key`
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) unsafe fn trigger_observers_with_data<E, T>(
|
pub(crate) unsafe fn trigger_observers_with_data<E, T>(
|
||||||
&mut self,
|
&mut self,
|
||||||
event: EventKey,
|
event_key: EventKey,
|
||||||
current_target: Option<Entity>,
|
current_target: Option<Entity>,
|
||||||
original_target: Option<Entity>,
|
original_target: Option<Entity>,
|
||||||
components: impl Iterator<Item = ComponentId> + Clone,
|
components: impl Iterator<Item = ComponentId> + Clone,
|
||||||
@ -821,7 +821,7 @@ impl<'w> DeferredWorld<'w> {
|
|||||||
{
|
{
|
||||||
Observers::invoke::<_>(
|
Observers::invoke::<_>(
|
||||||
self.reborrow(),
|
self.reborrow(),
|
||||||
event,
|
event_key,
|
||||||
current_target,
|
current_target,
|
||||||
original_target,
|
original_target,
|
||||||
components.clone(),
|
components.clone(),
|
||||||
@ -849,7 +849,7 @@ impl<'w> DeferredWorld<'w> {
|
|||||||
}
|
}
|
||||||
Observers::invoke::<_>(
|
Observers::invoke::<_>(
|
||||||
self.reborrow(),
|
self.reborrow(),
|
||||||
event,
|
event_key,
|
||||||
Some(current_target),
|
Some(current_target),
|
||||||
original_target,
|
original_target,
|
||||||
components.clone(),
|
components.clone(),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user