bevy_input_sequence/
action.rs

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