bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.26k stars 3.58k forks source link

Support for low-level virtual input #15776

Open khassar00 opened 1 month ago

khassar00 commented 1 month ago

What problem does this solve or what need does it fill?

Easy to test the bevy program, and can easily receive global input, such as global shortcuts

What solution would you like?

Send virtual events through EventLoopProxy::send_event function, process events in user_event function, and call window_event function if it is a virtual event. The winit master branch has changed user_event to proxy_wake_up. Here is a simple idea:

#[derive(Debug, Clone, PartialEq)]
enum Event<UserEvent> {
    VirtualEvent((WindowId,winit::event::WindowEvent)),
    UserEvent(UserEvent),
}
impl winit::application::ApplicationHandler for Application {
    fn proxy_wake_up(&mut self, event_loop: &dyn ActiveEventLoop) {
        while let Ok(user_event) = self.receiver.try_recv() {
            match user_event {
                Event::VirtualEvent((window_id,event)) => {
                    self.window_event(
                        event_loop,
                        window_id,
                        event,
                    )
                },
                Event::UserEvent(event) => self.handle_user_event_from_proxy(event_loop, user_event),
            }
        }
    }

    fn window_event(
        &mut self,
        event_loop: &dyn ActiveEventLoop,
        window_id: WindowId,
        event: WindowEvent,
    ) {
      //.......
    }

What alternative(s) have you considered?

Using bevy's event system can't test bevy_input itself.

Additional context

If Bevy officials don't consider this issue for the time being or I can't wait, I will try to implement it on my branch.

alice-i-cecile commented 1 month ago

Over in leafwing_input_manager and leafwing_input_playback I mock these things with ordinary Bevy events. For the most part it works quite well, although I think we could and should upstream some convenience methods for it. I haven't experimented much with the windowing side though.

khassar00 commented 1 month ago

Over in leafwing_input_manager and leafwing_input_playback I mock these things with ordinary Bevy events. For the most part it works quite well, although I think we could and should upstream some convenience methods for it. I haven't experimented much with the windowing side though.

I think this design can be considered when upgrading winit to version 0.31, which can save time and the changes should be small.