#![allow(missing_docs)] use bevy_app::App; use bevy_ecs::prelude::*; use bevy_input::keyboard::KeyboardInput; use bevy_input::touch::TouchInput; use bevy_input::{ mouse::{MouseButtonInput, MouseMotion, MouseWheel}, touchpad::{TouchpadMagnify, TouchpadRotate}, }; use bevy_reflect::Reflect; use bevy_window::{ ApplicationLifetime, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, Ime, ReceivedCharacter, RequestRedraw, WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowDestroyed, WindowFocused, WindowMoved, WindowOccluded, WindowResized, WindowScaleFactorChanged, WindowThemeChanged, }; /// Wraps all `bevy_window` events in a common enum. /// /// Read these events with `EventReader` if you need to /// access window events in the order they were received from `winit`. /// Otherwise, the event types are individually readable with /// `EventReader` (e.g. `EventReader`). #[derive(Event, Debug, Clone, PartialEq, Reflect)] #[reflect(Debug, PartialEq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), reflect(Serialize, Deserialize) )] pub enum WinitEvent { ApplicationLifetime(ApplicationLifetime), CursorEntered(CursorEntered), CursorLeft(CursorLeft), CursorMoved(CursorMoved), FileDragAndDrop(FileDragAndDrop), Ime(Ime), ReceivedCharacter(ReceivedCharacter), RequestRedraw(RequestRedraw), WindowBackendScaleFactorChanged(WindowBackendScaleFactorChanged), WindowCloseRequested(WindowCloseRequested), WindowCreated(WindowCreated), WindowDestroyed(WindowDestroyed), WindowFocused(WindowFocused), WindowMoved(WindowMoved), WindowOccluded(WindowOccluded), WindowResized(WindowResized), WindowScaleFactorChanged(WindowScaleFactorChanged), WindowThemeChanged(WindowThemeChanged), MouseButtonInput(MouseButtonInput), MouseMotion(MouseMotion), MouseWheel(MouseWheel), TouchpadMagnify(TouchpadMagnify), TouchpadRotate(TouchpadRotate), TouchInput(TouchInput), KeyboardInput(KeyboardInput), } impl From for WinitEvent { fn from(e: ApplicationLifetime) -> Self { Self::ApplicationLifetime(e) } } impl From for WinitEvent { fn from(e: CursorEntered) -> Self { Self::CursorEntered(e) } } impl From for WinitEvent { fn from(e: CursorLeft) -> Self { Self::CursorLeft(e) } } impl From for WinitEvent { fn from(e: CursorMoved) -> Self { Self::CursorMoved(e) } } impl From for WinitEvent { fn from(e: FileDragAndDrop) -> Self { Self::FileDragAndDrop(e) } } impl From for WinitEvent { fn from(e: Ime) -> Self { Self::Ime(e) } } impl From for WinitEvent { fn from(e: ReceivedCharacter) -> Self { Self::ReceivedCharacter(e) } } impl From for WinitEvent { fn from(e: RequestRedraw) -> Self { Self::RequestRedraw(e) } } impl From for WinitEvent { fn from(e: WindowBackendScaleFactorChanged) -> Self { Self::WindowBackendScaleFactorChanged(e) } } impl From for WinitEvent { fn from(e: WindowCloseRequested) -> Self { Self::WindowCloseRequested(e) } } impl From for WinitEvent { fn from(e: WindowCreated) -> Self { Self::WindowCreated(e) } } impl From for WinitEvent { fn from(e: WindowDestroyed) -> Self { Self::WindowDestroyed(e) } } impl From for WinitEvent { fn from(e: WindowFocused) -> Self { Self::WindowFocused(e) } } impl From for WinitEvent { fn from(e: WindowMoved) -> Self { Self::WindowMoved(e) } } impl From for WinitEvent { fn from(e: WindowOccluded) -> Self { Self::WindowOccluded(e) } } impl From for WinitEvent { fn from(e: WindowResized) -> Self { Self::WindowResized(e) } } impl From for WinitEvent { fn from(e: WindowScaleFactorChanged) -> Self { Self::WindowScaleFactorChanged(e) } } impl From for WinitEvent { fn from(e: WindowThemeChanged) -> Self { Self::WindowThemeChanged(e) } } impl From for WinitEvent { fn from(e: MouseButtonInput) -> Self { Self::MouseButtonInput(e) } } impl From for WinitEvent { fn from(e: MouseMotion) -> Self { Self::MouseMotion(e) } } impl From for WinitEvent { fn from(e: MouseWheel) -> Self { Self::MouseWheel(e) } } impl From for WinitEvent { fn from(e: TouchpadMagnify) -> Self { Self::TouchpadMagnify(e) } } impl From for WinitEvent { fn from(e: TouchpadRotate) -> Self { Self::TouchpadRotate(e) } } impl From for WinitEvent { fn from(e: TouchInput) -> Self { Self::TouchInput(e) } } impl From for WinitEvent { fn from(e: KeyboardInput) -> Self { Self::KeyboardInput(e) } } /// Forwards buffered [`WinitEvent`] events to the app. pub(crate) fn forward_winit_events(buffered_events: &mut Vec, app: &mut App) { if buffered_events.is_empty() { return; } for winit_event in buffered_events.iter() { match winit_event.clone() { WinitEvent::ApplicationLifetime(e) => { app.world.send_event(e); } WinitEvent::CursorEntered(e) => { app.world.send_event(e); } WinitEvent::CursorLeft(e) => { app.world.send_event(e); } WinitEvent::CursorMoved(e) => { app.world.send_event(e); } WinitEvent::FileDragAndDrop(e) => { app.world.send_event(e); } WinitEvent::Ime(e) => { app.world.send_event(e); } WinitEvent::ReceivedCharacter(e) => { app.world.send_event(e); } WinitEvent::RequestRedraw(e) => { app.world.send_event(e); } WinitEvent::WindowBackendScaleFactorChanged(e) => { app.world.send_event(e); } WinitEvent::WindowCloseRequested(e) => { app.world.send_event(e); } WinitEvent::WindowCreated(e) => { app.world.send_event(e); } WinitEvent::WindowDestroyed(e) => { app.world.send_event(e); } WinitEvent::WindowFocused(e) => { app.world.send_event(e); } WinitEvent::WindowMoved(e) => { app.world.send_event(e); } WinitEvent::WindowOccluded(e) => { app.world.send_event(e); } WinitEvent::WindowResized(e) => { app.world.send_event(e); } WinitEvent::WindowScaleFactorChanged(e) => { app.world.send_event(e); } WinitEvent::WindowThemeChanged(e) => { app.world.send_event(e); } WinitEvent::MouseButtonInput(e) => { app.world.send_event(e); } WinitEvent::MouseMotion(e) => { app.world.send_event(e); } WinitEvent::MouseWheel(e) => { app.world.send_event(e); } WinitEvent::TouchpadMagnify(e) => { app.world.send_event(e); } WinitEvent::TouchpadRotate(e) => { app.world.send_event(e); } WinitEvent::TouchInput(e) => { app.world.send_event(e); } WinitEvent::KeyboardInput(e) => { app.world.send_event(e); } } } app.world .resource_mut::>() .send_batch(buffered_events.drain(..)); }