From 80b059c815d3c4c2d2f3755fcbb7056d5ec24cb0 Mon Sep 17 00:00:00 2001 From: Lucas Franca Date: Mon, 7 Jul 2025 16:40:25 -0300 Subject: [PATCH] Extract members of `PickingPlugin` and `PointerInputPlugin` into new types (#19078) # Objective `PickingPlugin` and `PointerInputPlugin` were kinda weird being both a plugin and a resource. ## Solution Extract the resource functionality of `PickingPlugin` and `PointerInputPlugin` into new resources ## Testing `mesh_picking` and `sprite_picking` --------- Co-authored-by: Alice Cecile Co-authored-by: Jan Hohenheim --- crates/bevy_picking/src/input.rs | 52 ++++++++++++------- crates/bevy_picking/src/lib.rs | 50 ++++++++++++------ .../extract-picking-plugin-members.md | 10 ++++ .../extract-pointer-input-plugin-members.md | 10 ++++ 4 files changed, 89 insertions(+), 33 deletions(-) create mode 100644 release-content/migration-guides/extract-picking-plugin-members.md create mode 100644 release-content/migration-guides/extract-pointer-input-plugin-members.md diff --git a/crates/bevy_picking/src/input.rs b/crates/bevy_picking/src/input.rs index 2995174d6d..3050522ab5 100644 --- a/crates/bevy_picking/src/input.rs +++ b/crates/bevy_picking/src/input.rs @@ -39,24 +39,30 @@ pub mod prelude { pub use crate::input::PointerInputPlugin; } -/// Adds mouse and touch inputs for picking pointers to your app. This is a default input plugin, -/// that you can replace with your own plugin as needed. -/// -/// [`crate::PickingPlugin::is_input_enabled`] can be used to toggle whether -/// the core picking plugin processes the inputs sent by this, or other input plugins, in one place. -/// -/// This plugin contains several settings, and is added to the world as a resource after initialization. -/// You can configure pointer input settings at runtime by accessing the resource. #[derive(Copy, Clone, Resource, Debug, Reflect)] #[reflect(Resource, Default, Clone)] -pub struct PointerInputPlugin { +/// Settings for enabling and disabling updating mouse and touch inputs for picking +/// +/// ## Custom initialization +/// ``` +/// # use bevy_app::App; +/// # use bevy_picking::input::{PointerInputSettings,PointerInputPlugin}; +/// App::new() +/// .insert_resource(PointerInputSettings { +/// is_touch_enabled: false, +/// is_mouse_enabled: true, +/// }) +/// // or DefaultPlugins +/// .add_plugins(PointerInputPlugin); +/// ``` +pub struct PointerInputSettings { /// Should touch inputs be updated? pub is_touch_enabled: bool, /// Should mouse inputs be updated? pub is_mouse_enabled: bool, } -impl PointerInputPlugin { +impl PointerInputSettings { fn is_mouse_enabled(state: Res) -> bool { state.is_mouse_enabled } @@ -66,7 +72,7 @@ impl PointerInputPlugin { } } -impl Default for PointerInputPlugin { +impl Default for PointerInputSettings { fn default() -> Self { Self { is_touch_enabled: true, @@ -75,25 +81,35 @@ impl Default for PointerInputPlugin { } } +/// Adds mouse and touch inputs for picking pointers to your app. This is a default input plugin, +/// that you can replace with your own plugin as needed. +/// +/// Toggling mouse input or touch input can be done at runtime by modifying +/// [`PointerInputSettings`] resource. +/// +/// [`PointerInputSettings`] can be initialized with custom values, but will be +/// initialized with default values if it is not present at the moment this is +/// added to the app. +pub struct PointerInputPlugin; + impl Plugin for PointerInputPlugin { fn build(&self, app: &mut App) { - app.insert_resource(*self) + app.init_resource::() + .register_type::() .add_systems(Startup, spawn_mouse_pointer) .add_systems( First, ( - mouse_pick_events.run_if(PointerInputPlugin::is_mouse_enabled), - touch_pick_events.run_if(PointerInputPlugin::is_touch_enabled), + mouse_pick_events.run_if(PointerInputSettings::is_mouse_enabled), + touch_pick_events.run_if(PointerInputSettings::is_touch_enabled), ) .chain() .in_set(PickingSystems::Input), ) .add_systems( Last, - deactivate_touch_pointers.run_if(PointerInputPlugin::is_touch_enabled), - ) - .register_type::() - .register_type::(); + deactivate_touch_pointers.run_if(PointerInputSettings::is_touch_enabled), + ); } } diff --git a/crates/bevy_picking/src/lib.rs b/crates/bevy_picking/src/lib.rs index 74a765fbcd..026d2a1953 100644 --- a/crates/bevy_picking/src/lib.rs +++ b/crates/bevy_picking/src/lib.rs @@ -293,20 +293,31 @@ pub struct DefaultPickingPlugins; impl PluginGroup for DefaultPickingPlugins { fn build(self) -> PluginGroupBuilder { PluginGroupBuilder::start::() - .add(input::PointerInputPlugin::default()) - .add(PickingPlugin::default()) + .add(input::PointerInputPlugin) + .add(PickingPlugin) .add(InteractionPlugin) } } -/// This plugin sets up the core picking infrastructure. It receives input events, and provides the shared -/// types used by other picking plugins. -/// -/// This plugin contains several settings, and is added to the world as a resource after initialization. You -/// can configure picking settings at runtime through the resource. #[derive(Copy, Clone, Debug, Resource, Reflect)] #[reflect(Resource, Default, Debug, Clone)] -pub struct PickingPlugin { +/// Controls the behavior of picking +/// +/// ## Custom initialization +/// ``` +/// # use bevy_app::App; +/// # use bevy_picking::{PickingSettings, PickingPlugin}; +/// App::new() +/// .insert_resource(PickingSettings { +/// is_enabled: true, +/// is_input_enabled: false, +/// is_hover_enabled: true, +/// is_window_picking_enabled: false, +/// }) +/// // or DefaultPlugins +/// .add_plugins(PickingPlugin); +/// ``` +pub struct PickingSettings { /// Enables and disables all picking features. pub is_enabled: bool, /// Enables and disables input collection. @@ -317,7 +328,7 @@ pub struct PickingPlugin { pub is_window_picking_enabled: bool, } -impl PickingPlugin { +impl PickingSettings { /// Whether or not input collection systems should be running. pub fn input_should_run(state: Res) -> bool { state.is_input_enabled && state.is_enabled @@ -335,7 +346,7 @@ impl PickingPlugin { } } -impl Default for PickingPlugin { +impl Default for PickingSettings { fn default() -> Self { Self { is_enabled: true, @@ -346,9 +357,18 @@ impl Default for PickingPlugin { } } +/// This plugin sets up the core picking infrastructure. It receives input events, and provides the shared +/// types used by other picking plugins. +/// +/// Behavior of picking can be controlled by modifying [`PickingSettings`]. +/// +/// [`PickingSettings`] will be initialized with default values if it +/// is not present at the moment this is added to the app. +pub struct PickingPlugin; + impl Plugin for PickingPlugin { fn build(&self, app: &mut App) { - app.insert_resource(*self) + app.init_resource::() .init_resource::() .init_resource::() .add_event::() @@ -369,7 +389,7 @@ impl Plugin for PickingPlugin { .add_systems( PreUpdate, window::update_window_hits - .run_if(Self::window_picking_should_run) + .run_if(PickingSettings::window_picking_should_run) .in_set(PickingSystems::Backend), ) .configure_sets( @@ -382,15 +402,15 @@ impl Plugin for PickingPlugin { .configure_sets( PreUpdate, ( - PickingSystems::ProcessInput.run_if(Self::input_should_run), + PickingSystems::ProcessInput.run_if(PickingSettings::input_should_run), PickingSystems::Backend, - PickingSystems::Hover.run_if(Self::hover_should_run), + PickingSystems::Hover.run_if(PickingSettings::hover_should_run), PickingSystems::PostHover, PickingSystems::Last, ) .chain(), ) - .register_type::() + .register_type::() .register_type::() .register_type::() .register_type::() diff --git a/release-content/migration-guides/extract-picking-plugin-members.md b/release-content/migration-guides/extract-picking-plugin-members.md new file mode 100644 index 0000000000..7778cc871d --- /dev/null +++ b/release-content/migration-guides/extract-picking-plugin-members.md @@ -0,0 +1,10 @@ +--- +title: Extract `PickingPlugin` members into `PickingSettings` +pull_requests: [19078] +--- + +Controlling the behavior of picking should be done through +the `PickingSettings` resource instead of `PickingPlugin`. + +To initialize `PickingSettings` with non-default values, simply add +the resource to the app using `insert_resource` with the desired value. diff --git a/release-content/migration-guides/extract-pointer-input-plugin-members.md b/release-content/migration-guides/extract-pointer-input-plugin-members.md new file mode 100644 index 0000000000..0ff4c00b93 --- /dev/null +++ b/release-content/migration-guides/extract-pointer-input-plugin-members.md @@ -0,0 +1,10 @@ +--- +title: Extract `PointerInputPlugin` members into `PointerInputSettings` +pull_requests: [19078] +--- + +Toggling mouse and touch input update for picking should be done through +the `PointerInputSettings` resource instead of `PointerInputPlugin`. + +To initialize `PointerInputSettings` with non-default values, simply add +the resource to the app using `insert_resource` with the desired value.