aevyrie / bevy_eventlistener

Event listening, bubbling, and callbacks
Apache License 2.0
178 stars 27 forks source link

Make `On::send_event` more flexible #25

Open alice-i-cecile opened 7 months ago

alice-i-cecile commented 7 months ago
    pub fn send_event<F: Event + From<ListenerInput<E>>>() -> Self {

This is the current signature of On::send_event.

However, I want to be able to quickly do things like "send an AppExit event when this button is clicked".

I propose three related methods:

  1. send_event: takes an event of type F and captures it in the closure.
  2. convert_and_send_event: current behavior: converts E to F and sends it.
  3. send_default_event: sends an event with the Default value.
alice-i-cecile commented 7 months ago

Note that in some cases, I simply can't add a From impl between the event types: orphan rules block implementations on e.g. AppExit.

alice-i-cecile commented 7 months ago
    fn send_default_event<E: Event + Default>() -> Self {
        On::run(|mut event_writer: EventWriter<E>| {
            event_writer.send_default();
        })
    }

A working prototype of 3.

aevyrie commented 7 months ago

We could do all three things with a single closure version:

send_event<F: Event>(impl FnMut(E)-> F>)
  1. Capture send_event(move |_| my_other_event.clone())
  2. Convert send_event(MyEvent::from)
  3. Default send_event(|_| MyEvent::default)