bevy_input_sequence/
action.rs

1//! Common actions to do on key sequence matches
2use bevy::ecs::{
3    event::{Event, EventWriter},
4    observer::TriggerTargets,
5    prelude::Commands,
6    system::In,
7};
8
9/// Send this event.
10///
11/// ```rust
12/// use bevy::prelude::*;
13/// use bevy_input_sequence::prelude::*;
14///
15/// #[derive(Debug, Clone, Eq, PartialEq, Hash, States)]
16/// enum AppState { Menu, Game }
17/// #[derive(Event, Clone, Debug)]
18/// struct MyEvent;
19///
20/// KeySequence::new(
21///    action::send_event(MyEvent),
22///    keyseq! { Space });
23/// ```
24pub fn send_event<E: Event + Clone>(event: E) -> impl FnMut(EventWriter<E>) {
25    move |mut writer: EventWriter<E>| {
26        writer.write(event.clone());
27    }
28}
29
30/// Trigger an event.
31pub fn trigger<E: Event + Clone>(event: E) -> impl FnMut(Commands) {
32    move |mut commands: Commands| {
33        commands.trigger(event.clone());
34    }
35}
36
37/// Trigger an event with targets.
38pub fn trigger_targets<E: Event + Clone, T: TriggerTargets + Clone + Send + Sync + 'static>(
39    event: E,
40    targets: T,
41) -> impl FnMut(Commands) {
42    move |mut commands: Commands| {
43        commands.trigger_targets(event.clone(), targets.clone());
44    }
45}
46
47/// Sends an event with input, .e.g, [ButtonSequence](crate::input_sequence::ButtonSequence) provides a [Gamepad](bevy::input::gamepad::Gamepad) identifier.
48pub fn send_event_with_input<E: Event, Input: 'static, F: FnMut(Input) -> E>(
49    mut f: F,
50) -> impl FnMut(In<Input>, EventWriter<E>) {
51    move |In(x), mut writer: EventWriter<E>| {
52        writer.write(f(x));
53    }
54}