Skip to content

Commit 80b059c

Browse files
hukasualice-i-cecilejanhohenheim
authored
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>
1 parent ca25a67 commit 80b059c

File tree

4 files changed

+89
-33
lines changed

4 files changed

+89
-33
lines changed

crates/bevy_picking/src/input.rs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,30 @@ pub mod prelude {
3939
pub use crate::input::PointerInputPlugin;
4040
}
4141

42-
/// Adds mouse and touch inputs for picking pointers to your app. This is a default input plugin,
43-
/// that you can replace with your own plugin as needed.
44-
///
45-
/// [`crate::PickingPlugin::is_input_enabled`] can be used to toggle whether
46-
/// the core picking plugin processes the inputs sent by this, or other input plugins, in one place.
47-
///
48-
/// This plugin contains several settings, and is added to the world as a resource after initialization.
49-
/// You can configure pointer input settings at runtime by accessing the resource.
5042
#[derive(Copy, Clone, Resource, Debug, Reflect)]
5143
#[reflect(Resource, Default, Clone)]
52-
pub struct PointerInputPlugin {
44+
/// Settings for enabling and disabling updating mouse and touch inputs for picking
45+
///
46+
/// ## Custom initialization
47+
/// ```
48+
/// # use bevy_app::App;
49+
/// # use bevy_picking::input::{PointerInputSettings,PointerInputPlugin};
50+
/// App::new()
51+
/// .insert_resource(PointerInputSettings {
52+
/// is_touch_enabled: false,
53+
/// is_mouse_enabled: true,
54+
/// })
55+
/// // or DefaultPlugins
56+
/// .add_plugins(PointerInputPlugin);
57+
/// ```
58+
pub struct PointerInputSettings {
5359
/// Should touch inputs be updated?
5460
pub is_touch_enabled: bool,
5561
/// Should mouse inputs be updated?
5662
pub is_mouse_enabled: bool,
5763
}
5864

59-
impl PointerInputPlugin {
65+
impl PointerInputSettings {
6066
fn is_mouse_enabled(state: Res<Self>) -> bool {
6167
state.is_mouse_enabled
6268
}
@@ -66,7 +72,7 @@ impl PointerInputPlugin {
6672
}
6773
}
6874

69-
impl Default for PointerInputPlugin {
75+
impl Default for PointerInputSettings {
7076
fn default() -> Self {
7177
Self {
7278
is_touch_enabled: true,
@@ -75,25 +81,35 @@ impl Default for PointerInputPlugin {
7581
}
7682
}
7783

84+
/// Adds mouse and touch inputs for picking pointers to your app. This is a default input plugin,
85+
/// that you can replace with your own plugin as needed.
86+
///
87+
/// Toggling mouse input or touch input can be done at runtime by modifying
88+
/// [`PointerInputSettings`] resource.
89+
///
90+
/// [`PointerInputSettings`] can be initialized with custom values, but will be
91+
/// initialized with default values if it is not present at the moment this is
92+
/// added to the app.
93+
pub struct PointerInputPlugin;
94+
7895
impl Plugin for PointerInputPlugin {
7996
fn build(&self, app: &mut App) {
80-
app.insert_resource(*self)
97+
app.init_resource::<PointerInputSettings>()
98+
.register_type::<PointerInputSettings>()
8199
.add_systems(Startup, spawn_mouse_pointer)
82100
.add_systems(
83101
First,
84102
(
85-
mouse_pick_events.run_if(PointerInputPlugin::is_mouse_enabled),
86-
touch_pick_events.run_if(PointerInputPlugin::is_touch_enabled),
103+
mouse_pick_events.run_if(PointerInputSettings::is_mouse_enabled),
104+
touch_pick_events.run_if(PointerInputSettings::is_touch_enabled),
87105
)
88106
.chain()
89107
.in_set(PickingSystems::Input),
90108
)
91109
.add_systems(
92110
Last,
93-
deactivate_touch_pointers.run_if(PointerInputPlugin::is_touch_enabled),
94-
)
95-
.register_type::<Self>()
96-
.register_type::<PointerInputPlugin>();
111+
deactivate_touch_pointers.run_if(PointerInputSettings::is_touch_enabled),
112+
);
97113
}
98114
}
99115

crates/bevy_picking/src/lib.rs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -293,20 +293,31 @@ pub struct DefaultPickingPlugins;
293293
impl PluginGroup for DefaultPickingPlugins {
294294
fn build(self) -> PluginGroupBuilder {
295295
PluginGroupBuilder::start::<Self>()
296-
.add(input::PointerInputPlugin::default())
297-
.add(PickingPlugin::default())
296+
.add(input::PointerInputPlugin)
297+
.add(PickingPlugin)
298298
.add(InteractionPlugin)
299299
}
300300
}
301301

302-
/// This plugin sets up the core picking infrastructure. It receives input events, and provides the shared
303-
/// types used by other picking plugins.
304-
///
305-
/// This plugin contains several settings, and is added to the world as a resource after initialization. You
306-
/// can configure picking settings at runtime through the resource.
307302
#[derive(Copy, Clone, Debug, Resource, Reflect)]
308303
#[reflect(Resource, Default, Debug, Clone)]
309-
pub struct PickingPlugin {
304+
/// Controls the behavior of picking
305+
///
306+
/// ## Custom initialization
307+
/// ```
308+
/// # use bevy_app::App;
309+
/// # use bevy_picking::{PickingSettings, PickingPlugin};
310+
/// App::new()
311+
/// .insert_resource(PickingSettings {
312+
/// is_enabled: true,
313+
/// is_input_enabled: false,
314+
/// is_hover_enabled: true,
315+
/// is_window_picking_enabled: false,
316+
/// })
317+
/// // or DefaultPlugins
318+
/// .add_plugins(PickingPlugin);
319+
/// ```
320+
pub struct PickingSettings {
310321
/// Enables and disables all picking features.
311322
pub is_enabled: bool,
312323
/// Enables and disables input collection.
@@ -317,7 +328,7 @@ pub struct PickingPlugin {
317328
pub is_window_picking_enabled: bool,
318329
}
319330

320-
impl PickingPlugin {
331+
impl PickingSettings {
321332
/// Whether or not input collection systems should be running.
322333
pub fn input_should_run(state: Res<Self>) -> bool {
323334
state.is_input_enabled && state.is_enabled
@@ -335,7 +346,7 @@ impl PickingPlugin {
335346
}
336347
}
337348

338-
impl Default for PickingPlugin {
349+
impl Default for PickingSettings {
339350
fn default() -> Self {
340351
Self {
341352
is_enabled: true,
@@ -346,9 +357,18 @@ impl Default for PickingPlugin {
346357
}
347358
}
348359

360+
/// This plugin sets up the core picking infrastructure. It receives input events, and provides the shared
361+
/// types used by other picking plugins.
362+
///
363+
/// Behavior of picking can be controlled by modifying [`PickingSettings`].
364+
///
365+
/// [`PickingSettings`] will be initialized with default values if it
366+
/// is not present at the moment this is added to the app.
367+
pub struct PickingPlugin;
368+
349369
impl Plugin for PickingPlugin {
350370
fn build(&self, app: &mut App) {
351-
app.insert_resource(*self)
371+
app.init_resource::<PickingSettings>()
352372
.init_resource::<pointer::PointerMap>()
353373
.init_resource::<backend::ray::RayMap>()
354374
.add_event::<pointer::PointerInput>()
@@ -369,7 +389,7 @@ impl Plugin for PickingPlugin {
369389
.add_systems(
370390
PreUpdate,
371391
window::update_window_hits
372-
.run_if(Self::window_picking_should_run)
392+
.run_if(PickingSettings::window_picking_should_run)
373393
.in_set(PickingSystems::Backend),
374394
)
375395
.configure_sets(
@@ -382,15 +402,15 @@ impl Plugin for PickingPlugin {
382402
.configure_sets(
383403
PreUpdate,
384404
(
385-
PickingSystems::ProcessInput.run_if(Self::input_should_run),
405+
PickingSystems::ProcessInput.run_if(PickingSettings::input_should_run),
386406
PickingSystems::Backend,
387-
PickingSystems::Hover.run_if(Self::hover_should_run),
407+
PickingSystems::Hover.run_if(PickingSettings::hover_should_run),
388408
PickingSystems::PostHover,
389409
PickingSystems::Last,
390410
)
391411
.chain(),
392412
)
393-
.register_type::<Self>()
413+
.register_type::<PickingSettings>()
394414
.register_type::<Pickable>()
395415
.register_type::<hover::PickingInteraction>()
396416
.register_type::<hover::Hovered>()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Extract `PickingPlugin` members into `PickingSettings`
3+
pull_requests: [19078]
4+
---
5+
6+
Controlling the behavior of picking should be done through
7+
the `PickingSettings` resource instead of `PickingPlugin`.
8+
9+
To initialize `PickingSettings` with non-default values, simply add
10+
the resource to the app using `insert_resource` with the desired value.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Extract `PointerInputPlugin` members into `PointerInputSettings`
3+
pull_requests: [19078]
4+
---
5+
6+
Toggling mouse and touch input update for picking should be done through
7+
the `PointerInputSettings` resource instead of `PointerInputPlugin`.
8+
9+
To initialize `PointerInputSettings` with non-default values, simply add
10+
the resource to the app using `insert_resource` with the desired value.

0 commit comments

Comments
 (0)