mirror of
https://github.com/bevyengine/bevy
synced 2025-02-16 14:08:32 +00:00
Allow non-static trigger targets (#14327)
# Objective `TriggerTargets` can not be borrowed for use in `World::trigger_targets` ## Solution Drop `'static` bound on `TriggerEvent`, keep it for `Command` impl. ## Testing n/a
This commit is contained in:
parent
cf1b7fa4cc
commit
d276525350
4 changed files with 26 additions and 8 deletions
|
@ -313,12 +313,12 @@ impl World {
|
|||
|
||||
/// Triggers the given `event`, which will run any observers watching for it.
|
||||
pub fn trigger(&mut self, event: impl Event) {
|
||||
TriggerEvent { event, targets: () }.apply(self);
|
||||
TriggerEvent { event, targets: () }.trigger(self);
|
||||
}
|
||||
|
||||
/// Triggers the given `event` for the given `targets`, which will run any observers watching for it.
|
||||
pub fn trigger_targets(&mut self, event: impl Event, targets: impl TriggerTargets) {
|
||||
TriggerEvent { event, targets }.apply(self);
|
||||
TriggerEvent { event, targets }.trigger(self);
|
||||
}
|
||||
|
||||
/// Register an observer to the cache, called when an observer is created
|
||||
|
|
|
@ -14,13 +14,21 @@ pub struct TriggerEvent<E, Targets: TriggerTargets = ()> {
|
|||
pub targets: Targets,
|
||||
}
|
||||
|
||||
impl<E: Event, Targets: TriggerTargets> Command for TriggerEvent<E, Targets> {
|
||||
fn apply(mut self, world: &mut World) {
|
||||
impl<E: Event, Targets: TriggerTargets> TriggerEvent<E, Targets> {
|
||||
pub(super) fn trigger(mut self, world: &mut World) {
|
||||
let event_type = world.init_component::<E>();
|
||||
trigger_event(world, event_type, &mut self.event, self.targets);
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Event, Targets: TriggerTargets + Send + Sync + 'static> Command
|
||||
for TriggerEvent<E, Targets>
|
||||
{
|
||||
fn apply(self, world: &mut World) {
|
||||
self.trigger(world);
|
||||
}
|
||||
}
|
||||
|
||||
/// Emit a trigger for a dynamic component id. This is unsafe and must be verified manually.
|
||||
pub struct EmitDynamicTrigger<T, Targets: TriggerTargets = ()> {
|
||||
event_type: ComponentId,
|
||||
|
@ -41,7 +49,9 @@ impl<E, Targets: TriggerTargets> EmitDynamicTrigger<E, Targets> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<E: Event, Targets: TriggerTargets> Command for EmitDynamicTrigger<E, Targets> {
|
||||
impl<E: Event, Targets: TriggerTargets + Send + Sync + 'static> Command
|
||||
for EmitDynamicTrigger<E, Targets>
|
||||
{
|
||||
fn apply(mut self, world: &mut World) {
|
||||
trigger_event(world, self.event_type, &mut self.event_data, self.targets);
|
||||
}
|
||||
|
@ -88,7 +98,7 @@ fn trigger_event<E: Event, Targets: TriggerTargets>(
|
|||
///
|
||||
/// [`Trigger`]: crate::observer::Trigger
|
||||
/// [`Observer`]: crate::observer::Observer
|
||||
pub trait TriggerTargets: Send + Sync + 'static {
|
||||
pub trait TriggerTargets {
|
||||
/// The components the trigger should target.
|
||||
fn components(&self) -> &[ComponentId];
|
||||
|
||||
|
|
|
@ -759,7 +759,11 @@ impl<'w, 's> Commands<'w, 's> {
|
|||
/// watches those targets.
|
||||
///
|
||||
/// [`Trigger`]: crate::observer::Trigger
|
||||
pub fn trigger_targets(&mut self, event: impl Event, targets: impl TriggerTargets) {
|
||||
pub fn trigger_targets(
|
||||
&mut self,
|
||||
event: impl Event,
|
||||
targets: impl TriggerTargets + Send + Sync + 'static,
|
||||
) {
|
||||
self.add(TriggerEvent { event, targets });
|
||||
}
|
||||
|
||||
|
|
|
@ -423,7 +423,11 @@ impl<'w> DeferredWorld<'w> {
|
|||
}
|
||||
|
||||
/// Sends a [`Trigger`](crate::observer::Trigger) with the given `targets`.
|
||||
pub fn trigger_targets(&mut self, trigger: impl Event, targets: impl TriggerTargets) {
|
||||
pub fn trigger_targets(
|
||||
&mut self,
|
||||
trigger: impl Event,
|
||||
targets: impl TriggerTargets + Send + Sync + 'static,
|
||||
) {
|
||||
self.commands().trigger_targets(trigger, targets);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue