mirror of
https://github.com/bevyengine/bevy
synced 2024-12-22 11:03:06 +00:00
82128d778a
# Objective Correctly order picking events. Resolves https://github.com/bevyengine/bevy/issues/5984. ## Solution Event ordering [very long standing problem](https://github.com/aevyrie/bevy_mod_picking/issues/294) with mod picking, stemming from two related issues. The first problem was that `Pointer<T>` events of different types couldn't be ordered, but we have already gotten around that in the upstream by switching to observers. Since observers run in the order they are triggered, this isn't an issue. The second problem was that the underlying event streams that picking uses to create it's pointer interaction events *also* lacked ordering, and the systems that generated the points couldn't interleave events. This PR fixes that by unifying the event streams and integrating the various interaction systems. The concrete changes are as follows: + `bevy_winit::WinitEvent` has been moved to `bevy_window::WindowEvent`. This provides a unified (and more importantly, *ordered*) input stream for both `bevy_window` and `bevy_input` events. + Replaces `InputMove` and `InputPress` with `PointerInput`, a new unified input event which drives picking and interaction. This event is built to have drop-in forward compatibility with [winit's upcoming pointer abstraction](https://github.com/rust-windowing/winit/pull/3876). I have added code to emulate it using the current winit input abstractions, but this entire thing will be much more robust when it lands. + Rolls `pointer_events` `send_click_and_drag_events` and `send_drag_over_events` into a single system, which operates directly on `PointerEvent` and triggers observers as output. The PR also improves docs and takes the opportunity to refactor/streamline the pointer event dispatch logic. ## Status & Testing This PR is now feature complete and documented. While it is theoretically possible to add unit tests for the ordering, building the picking mocking for that will take a little while. Feedback on the chosen ordering of events is within-scope. ## Migration Guide For users switching from `bevy_mod_picking` to `bevy_picking`: + Instead of adding an `On<T>` component, use `.observe(|trigger: Trigger<T>|)`. You may now apply multiple handlers to the same entity using this command. + Pointer interaction events now have semi-deterministic ordering which (more or less) aligns with the order of the raw input stream. Consult the docs on `bevy_picking::event::pointer_events` for current information. You may need to adjust your event handling logic accordingly. + `PointerCancel` has been replaced with `Pointer<Cancled>`, which now has the semantics of an OS touch pointer cancel event. + `InputMove` and `InputPress` have been merged into `PointerInput`. The use remains exactly the same. + Picking interaction events are now only accessible through observers, and no `EventReader`. This functionality may be re-implemented later. For users of `bevy_winit`: + The event `bevy_winit::WinitEvent` has moved to `bevy_window::WindowEvent`. If this was the only thing you depended on `bevy_winit` for, you should switch your dependency to `bevy_window`. + `bevy_window` now depends on `bevy_input`. The dependencies of `bevy_input` are a subset of the existing dependencies for `bevy_window` so this should be non-breaking.
193 lines
7 KiB
Rust
193 lines
7 KiB
Rust
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
|
#![forbid(unsafe_code)]
|
|
#![doc(
|
|
html_logo_url = "https://bevyengine.org/assets/icon.png",
|
|
html_favicon_url = "https://bevyengine.org/assets/icon.png"
|
|
)]
|
|
|
|
//! `bevy_winit` provides utilities to handle window creation and the eventloop through [`winit`]
|
|
//!
|
|
//! Most commonly, the [`WinitPlugin`] is used as part of
|
|
//! [`DefaultPlugins`](https://docs.rs/bevy/latest/bevy/struct.DefaultPlugins.html).
|
|
//! The app's [runner](bevy_app::App::runner) is set by `WinitPlugin` and handles the `winit` [`EventLoop`].
|
|
//! See `winit_runner` for details.
|
|
|
|
use bevy_derive::Deref;
|
|
use bevy_window::{RawHandleWrapperHolder, WindowEvent};
|
|
use std::marker::PhantomData;
|
|
use winit::event_loop::EventLoop;
|
|
#[cfg(target_os = "android")]
|
|
pub use winit::platform::android::activity as android_activity;
|
|
|
|
use bevy_a11y::AccessibilityRequested;
|
|
use bevy_app::{App, Last, Plugin};
|
|
use bevy_ecs::prelude::*;
|
|
#[allow(deprecated)]
|
|
use bevy_window::{exit_on_all_closed, Window, WindowCreated};
|
|
pub use converters::convert_system_cursor_icon;
|
|
pub use state::{CursorSource, CustomCursorCache, CustomCursorCacheKey, PendingCursor};
|
|
use system::{changed_windows, despawn_windows};
|
|
pub use system::{create_monitors, create_windows};
|
|
pub use winit::event_loop::EventLoopProxy;
|
|
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
|
|
pub use winit::platform::web::CustomCursorExtWebSys;
|
|
pub use winit::window::{CustomCursor as WinitCustomCursor, CustomCursorSource};
|
|
pub use winit_config::*;
|
|
pub use winit_windows::*;
|
|
|
|
use crate::accessibility::{AccessKitAdapters, AccessKitPlugin, WinitActionRequestHandlers};
|
|
use crate::state::winit_runner;
|
|
use crate::winit_monitors::WinitMonitors;
|
|
|
|
pub mod accessibility;
|
|
mod converters;
|
|
mod state;
|
|
mod system;
|
|
mod winit_config;
|
|
mod winit_monitors;
|
|
mod winit_windows;
|
|
|
|
/// [`AndroidApp`] provides an interface to query the application state as well as monitor events
|
|
/// (for example lifecycle and input events).
|
|
#[cfg(target_os = "android")]
|
|
pub static ANDROID_APP: std::sync::OnceLock<android_activity::AndroidApp> =
|
|
std::sync::OnceLock::new();
|
|
|
|
/// A [`Plugin`] that uses `winit` to create and manage windows, and receive window and input
|
|
/// events.
|
|
///
|
|
/// This plugin will add systems and resources that sync with the `winit` backend and also
|
|
/// replace the existing [`App`] runner with one that constructs an [event loop](EventLoop) to
|
|
/// receive window and input events from the OS.
|
|
///
|
|
/// The `T` event type can be used to pass custom events to the `winit`'s loop, and handled as events
|
|
/// in systems.
|
|
#[derive(Default)]
|
|
pub struct WinitPlugin<T: Event = WakeUp> {
|
|
/// Allows the window (and the event loop) to be created on any thread
|
|
/// instead of only the main thread.
|
|
///
|
|
/// See [`EventLoopBuilder::build`](winit::event_loop::EventLoopBuilder::build) for more information on this.
|
|
///
|
|
/// # Supported platforms
|
|
///
|
|
/// Only works on Linux (X11/Wayland) and Windows.
|
|
/// This field is ignored on other platforms.
|
|
pub run_on_any_thread: bool,
|
|
marker: PhantomData<T>,
|
|
}
|
|
|
|
impl<T: Event> Plugin for WinitPlugin<T> {
|
|
fn name(&self) -> &str {
|
|
"bevy_winit::WinitPlugin"
|
|
}
|
|
|
|
fn build(&self, app: &mut App) {
|
|
let mut event_loop_builder = EventLoop::<T>::with_user_event();
|
|
|
|
// linux check is needed because x11 might be enabled on other platforms.
|
|
#[cfg(all(target_os = "linux", feature = "x11"))]
|
|
{
|
|
use winit::platform::x11::EventLoopBuilderExtX11;
|
|
|
|
// This allows a Bevy app to be started and ran outside the main thread.
|
|
// A use case for this is to allow external applications to spawn a thread
|
|
// which runs a Bevy app without requiring the Bevy app to need to reside on
|
|
// the main thread, which can be problematic.
|
|
event_loop_builder.with_any_thread(self.run_on_any_thread);
|
|
}
|
|
|
|
// linux check is needed because wayland might be enabled on other platforms.
|
|
#[cfg(all(target_os = "linux", feature = "wayland"))]
|
|
{
|
|
use winit::platform::wayland::EventLoopBuilderExtWayland;
|
|
event_loop_builder.with_any_thread(self.run_on_any_thread);
|
|
}
|
|
|
|
#[cfg(target_os = "windows")]
|
|
{
|
|
use winit::platform::windows::EventLoopBuilderExtWindows;
|
|
event_loop_builder.with_any_thread(self.run_on_any_thread);
|
|
}
|
|
|
|
#[cfg(target_os = "android")]
|
|
{
|
|
use winit::platform::android::EventLoopBuilderExtAndroid;
|
|
let msg = "Bevy must be setup with the #[bevy_main] macro on Android";
|
|
event_loop_builder.with_android_app(ANDROID_APP.get().expect(msg).clone());
|
|
}
|
|
|
|
app.init_non_send_resource::<WinitWindows>()
|
|
.init_resource::<WinitMonitors>()
|
|
.init_resource::<WinitSettings>()
|
|
.set_runner(winit_runner::<T>)
|
|
.add_systems(
|
|
Last,
|
|
(
|
|
// `exit_on_all_closed` only checks if windows exist but doesn't access data,
|
|
// so we don't need to care about its ordering relative to `changed_windows`
|
|
changed_windows.ambiguous_with(exit_on_all_closed),
|
|
despawn_windows,
|
|
)
|
|
.chain(),
|
|
);
|
|
|
|
app.add_plugins(AccessKitPlugin);
|
|
|
|
let event_loop = event_loop_builder
|
|
.build()
|
|
.expect("Failed to build event loop");
|
|
|
|
// `winit`'s windows are bound to the event loop that created them, so the event loop must
|
|
// be inserted as a resource here to pass it onto the runner.
|
|
app.insert_non_send_resource(event_loop);
|
|
}
|
|
}
|
|
|
|
/// The default event that can be used to wake the window loop
|
|
/// Wakes up the loop if in wait state
|
|
#[derive(Debug, Default, Clone, Copy, Event)]
|
|
pub struct WakeUp;
|
|
|
|
/// A wrapper type around [`winit::event_loop::EventLoopProxy`] with the specific
|
|
/// [`winit::event::Event::UserEvent`] used in the [`WinitPlugin`].
|
|
///
|
|
/// The `EventLoopProxy` can be used to request a redraw from outside bevy.
|
|
///
|
|
/// Use `Res<EventLoopProxy>` to receive this resource.
|
|
#[derive(Resource, Deref)]
|
|
pub struct EventLoopProxyWrapper<T: 'static>(EventLoopProxy<T>);
|
|
|
|
trait AppSendEvent {
|
|
fn send(&mut self, event: impl Into<WindowEvent>);
|
|
}
|
|
|
|
impl AppSendEvent for Vec<WindowEvent> {
|
|
fn send(&mut self, event: impl Into<WindowEvent>) {
|
|
self.push(Into::<WindowEvent>::into(event));
|
|
}
|
|
}
|
|
|
|
/// The parameters of the [`create_windows`] system.
|
|
pub type CreateWindowParams<'w, 's, F = ()> = (
|
|
Commands<'w, 's>,
|
|
Query<
|
|
'w,
|
|
's,
|
|
(
|
|
Entity,
|
|
&'static mut Window,
|
|
Option<&'static RawHandleWrapperHolder>,
|
|
),
|
|
F,
|
|
>,
|
|
EventWriter<'w, WindowCreated>,
|
|
NonSendMut<'w, WinitWindows>,
|
|
NonSendMut<'w, AccessKitAdapters>,
|
|
ResMut<'w, WinitActionRequestHandlers>,
|
|
Res<'w, AccessibilityRequested>,
|
|
Res<'w, WinitMonitors>,
|
|
);
|
|
|
|
/// The parameters of the [`create_monitors`] system.
|
|
pub type CreateMonitorParams<'w, 's> = (Commands<'w, 's>, ResMut<'w, WinitMonitors>);
|