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:
parent
ca25a67d0d
commit
80b059c815
@ -39,24 +39,30 @@ pub mod prelude {
|
|||||||
pub use crate::input::PointerInputPlugin;
|
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)]
|
#[derive(Copy, Clone, Resource, Debug, Reflect)]
|
||||||
#[reflect(Resource, Default, Clone)]
|
#[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?
|
/// Should touch inputs be updated?
|
||||||
pub is_touch_enabled: bool,
|
pub is_touch_enabled: bool,
|
||||||
/// Should mouse inputs be updated?
|
/// Should mouse inputs be updated?
|
||||||
pub is_mouse_enabled: bool,
|
pub is_mouse_enabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PointerInputPlugin {
|
impl PointerInputSettings {
|
||||||
fn is_mouse_enabled(state: Res<Self>) -> bool {
|
fn is_mouse_enabled(state: Res<Self>) -> bool {
|
||||||
state.is_mouse_enabled
|
state.is_mouse_enabled
|
||||||
}
|
}
|
||||||
@ -66,7 +72,7 @@ impl PointerInputPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for PointerInputPlugin {
|
impl Default for PointerInputSettings {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
is_touch_enabled: true,
|
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 {
|
impl Plugin for PointerInputPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
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(Startup, spawn_mouse_pointer)
|
||||||
.add_systems(
|
.add_systems(
|
||||||
First,
|
First,
|
||||||
(
|
(
|
||||||
mouse_pick_events.run_if(PointerInputPlugin::is_mouse_enabled),
|
mouse_pick_events.run_if(PointerInputSettings::is_mouse_enabled),
|
||||||
touch_pick_events.run_if(PointerInputPlugin::is_touch_enabled),
|
touch_pick_events.run_if(PointerInputSettings::is_touch_enabled),
|
||||||
)
|
)
|
||||||
.chain()
|
.chain()
|
||||||
.in_set(PickingSystems::Input),
|
.in_set(PickingSystems::Input),
|
||||||
)
|
)
|
||||||
.add_systems(
|
.add_systems(
|
||||||
Last,
|
Last,
|
||||||
deactivate_touch_pointers.run_if(PointerInputPlugin::is_touch_enabled),
|
deactivate_touch_pointers.run_if(PointerInputSettings::is_touch_enabled),
|
||||||
)
|
);
|
||||||
.register_type::<Self>()
|
|
||||||
.register_type::<PointerInputPlugin>();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,20 +293,31 @@ pub struct DefaultPickingPlugins;
|
|||||||
impl PluginGroup for DefaultPickingPlugins {
|
impl PluginGroup for DefaultPickingPlugins {
|
||||||
fn build(self) -> PluginGroupBuilder {
|
fn build(self) -> PluginGroupBuilder {
|
||||||
PluginGroupBuilder::start::<Self>()
|
PluginGroupBuilder::start::<Self>()
|
||||||
.add(input::PointerInputPlugin::default())
|
.add(input::PointerInputPlugin)
|
||||||
.add(PickingPlugin::default())
|
.add(PickingPlugin)
|
||||||
.add(InteractionPlugin)
|
.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)]
|
#[derive(Copy, Clone, Debug, Resource, Reflect)]
|
||||||
#[reflect(Resource, Default, Debug, Clone)]
|
#[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.
|
/// Enables and disables all picking features.
|
||||||
pub is_enabled: bool,
|
pub is_enabled: bool,
|
||||||
/// Enables and disables input collection.
|
/// Enables and disables input collection.
|
||||||
@ -317,7 +328,7 @@ pub struct PickingPlugin {
|
|||||||
pub is_window_picking_enabled: bool,
|
pub is_window_picking_enabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PickingPlugin {
|
impl PickingSettings {
|
||||||
/// Whether or not input collection systems should be running.
|
/// Whether or not input collection systems should be running.
|
||||||
pub fn input_should_run(state: Res<Self>) -> bool {
|
pub fn input_should_run(state: Res<Self>) -> bool {
|
||||||
state.is_input_enabled && state.is_enabled
|
state.is_input_enabled && state.is_enabled
|
||||||
@ -335,7 +346,7 @@ impl PickingPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for PickingPlugin {
|
impl Default for PickingSettings {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
is_enabled: true,
|
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 {
|
impl Plugin for PickingPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.insert_resource(*self)
|
app.init_resource::<PickingSettings>()
|
||||||
.init_resource::<pointer::PointerMap>()
|
.init_resource::<pointer::PointerMap>()
|
||||||
.init_resource::<backend::ray::RayMap>()
|
.init_resource::<backend::ray::RayMap>()
|
||||||
.add_event::<pointer::PointerInput>()
|
.add_event::<pointer::PointerInput>()
|
||||||
@ -369,7 +389,7 @@ impl Plugin for PickingPlugin {
|
|||||||
.add_systems(
|
.add_systems(
|
||||||
PreUpdate,
|
PreUpdate,
|
||||||
window::update_window_hits
|
window::update_window_hits
|
||||||
.run_if(Self::window_picking_should_run)
|
.run_if(PickingSettings::window_picking_should_run)
|
||||||
.in_set(PickingSystems::Backend),
|
.in_set(PickingSystems::Backend),
|
||||||
)
|
)
|
||||||
.configure_sets(
|
.configure_sets(
|
||||||
@ -382,15 +402,15 @@ impl Plugin for PickingPlugin {
|
|||||||
.configure_sets(
|
.configure_sets(
|
||||||
PreUpdate,
|
PreUpdate,
|
||||||
(
|
(
|
||||||
PickingSystems::ProcessInput.run_if(Self::input_should_run),
|
PickingSystems::ProcessInput.run_if(PickingSettings::input_should_run),
|
||||||
PickingSystems::Backend,
|
PickingSystems::Backend,
|
||||||
PickingSystems::Hover.run_if(Self::hover_should_run),
|
PickingSystems::Hover.run_if(PickingSettings::hover_should_run),
|
||||||
PickingSystems::PostHover,
|
PickingSystems::PostHover,
|
||||||
PickingSystems::Last,
|
PickingSystems::Last,
|
||||||
)
|
)
|
||||||
.chain(),
|
.chain(),
|
||||||
)
|
)
|
||||||
.register_type::<Self>()
|
.register_type::<PickingSettings>()
|
||||||
.register_type::<Pickable>()
|
.register_type::<Pickable>()
|
||||||
.register_type::<hover::PickingInteraction>()
|
.register_type::<hover::PickingInteraction>()
|
||||||
.register_type::<hover::Hovered>()
|
.register_type::<hover::Hovered>()
|
||||||
|
@ -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.
|
@ -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.
|
Loading…
Reference in New Issue
Block a user