Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trigger_map to support triggering mapped events #14664

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
70 changes: 67 additions & 3 deletions crates/bevy_ecs/src/observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ mod trigger_event;
pub use runner::*;
pub use trigger_event::*;

use crate::observer::entity_observer::ObservedBy;
use crate::{archetype::ArchetypeFlags, system::IntoObserverSystem, world::*};
use crate::{component::ComponentId, prelude::*, world::DeferredWorld};
use crate::{
archetype::ArchetypeFlags,
component::ComponentId,
observer::entity_observer::ObservedBy,
prelude::*,
system::{IntoObserverSystem, ObserverSystem},
world::DeferredWorld,
};
use bevy_ptr::Ptr;
use bevy_utils::{EntityHashMap, HashMap};
use std::marker::PhantomData;
Expand Down Expand Up @@ -427,6 +432,65 @@ impl World {
}
}

/// Returns an [`Observer`] that watches for an input event, maps it to a new event type, and triggers
/// that event.
///
/// # Examples
///
/// ```
/// # use bevy_ecs::prelude::*;
/// # use bevy_ecs::observer::trigger_map;
/// # let mut world = World::new();
///
/// #[derive(Event, Clone, Debug)]
/// struct Foo(usize);
///
/// #[derive(Event, Clone, Debug)]
/// struct Bar(bool);
///
/// #[derive(Event, Debug)]
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
/// enum Combined {
/// Foo(Foo),
/// Bar(Bar),
/// }
///
/// world.observe(trigger_map(Combined::Foo));
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
/// world.observe(trigger_map(Combined::Bar));
/// world.observe(|trigger: Trigger<Combined>| {
/// println!("Combined Event: {:?}", trigger.event());
/// });
/// ```
///
/// ```
/// # use bevy_ecs::prelude::*;
/// # use bevy_ecs::observer::trigger_map;
/// # let mut world = World::new();
///
/// #[derive(Event, Clone, Debug)]
/// struct Foo(usize);
///
/// #[derive(Event, Clone, Debug)]
/// struct Bar(bool);
///
/// // If you don't care about capturing the original event context,
/// // you can just use a unit struct.
/// #[derive(Event, Debug)]
/// struct Combined;
///
/// world.observe(trigger_map(|event: Foo| Combined));
/// world.observe(trigger_map(|event: Bar| Combined));
/// world.observe(|trigger: Trigger<Combined>| {
/// println!("Combined Event");
/// });
/// ```
pub fn trigger_map<T: Event + Clone, U: Event, F: Fn(T) -> U + Send + Sync + 'static>(
map: F,
) -> impl ObserverSystem<T, ()> {
IntoObserverSystem::into_system(move |trigger: Trigger<T>, mut commands: Commands| {
commands.trigger(map(trigger.event().clone()));
})
}

#[cfg(test)]
mod tests {
use bevy_ptr::OwningPtr;
Expand Down
Loading