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 <alice.i.cecile@gmail.com>
Co-authored-by: Jan Hohenheim <jan@hohenheim.ch>
This commit is contained in:
Lucas Franca 2025-07-07 16:40:25 -03:00 committed by GitHub
parent ca25a67d0d
commit 80b059c815
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 89 additions and 33 deletions

View File

@ -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<Self>) -> 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::<PointerInputSettings>()
.register_type::<PointerInputSettings>()
.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::<Self>()
.register_type::<PointerInputPlugin>();
deactivate_touch_pointers.run_if(PointerInputSettings::is_touch_enabled),
);
}
}

View File

@ -293,20 +293,31 @@ pub struct DefaultPickingPlugins;
impl PluginGroup for DefaultPickingPlugins {
fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::start::<Self>()
.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<Self>) -> 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::<PickingSettings>()
.init_resource::<pointer::PointerMap>()
.init_resource::<backend::ray::RayMap>()
.add_event::<pointer::PointerInput>()
@ -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::<Self>()
.register_type::<PickingSettings>()
.register_type::<Pickable>()
.register_type::<hover::PickingInteraction>()
.register_type::<hover::Hovered>()

View File

@ -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.

View File

@ -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.