Skip to content

Commit

Permalink
Make sprite picking opt-in (#17842)
Browse files Browse the repository at this point in the history
# Objective

Fix #17108
See
#17108 (comment)

## Solution

- Make the query match `&Pickable` instead `Option<&Pickable>`

## Testing

- Run the `sprite_picking` example and everything still work


## Migration Guide

- Sprite picking are now opt-in, make sure you insert `Pickable`
component when using sprite picking.
```diff
-commands.spawn(Sprite { .. } );
+commands.spawn((Sprite { .. }, Pickable::default());
```
  • Loading branch information
notmd authored Feb 24, 2025
1 parent cb62851 commit f7b2a02
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
10 changes: 4 additions & 6 deletions crates/bevy_sprite/src/picking_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub enum SpritePickingMode {
#[reflect(Resource, Default)]
pub struct SpritePickingSettings {
/// When set to `true` sprite picking will only consider cameras marked with
/// [`SpritePickingCamera`] and entities marked with [`Pickable`]. `false` by default.
/// [`SpritePickingCamera`].
///
/// This setting is provided to give you fine-grained control over which cameras and entities
/// should be used by the sprite picking backend at runtime.
Expand Down Expand Up @@ -94,16 +94,15 @@ fn sprite_picking(
Entity,
&Sprite,
&GlobalTransform,
Option<&Pickable>,
&Pickable,
&ViewVisibility,
)>,
mut output: EventWriter<PointerHits>,
) {
let mut sorted_sprites: Vec<_> = sprite_query
.iter()
.filter_map(|(entity, sprite, transform, pickable, vis)| {
let marker_requirement = !settings.require_markers || pickable.is_some();
if !transform.affine().is_nan() && vis.get() && marker_requirement {
if !transform.affine().is_nan() && vis.get() {
Some((entity, sprite, transform, pickable))
} else {
None
Expand Down Expand Up @@ -219,8 +218,7 @@ fn sprite_picking(
}
};

blocked = cursor_in_valid_pixels_of_sprite
&& pickable.is_none_or(|p| p.should_block_lower);
blocked = cursor_in_valid_pixels_of_sprite && pickable.should_block_lower;

cursor_in_valid_pixels_of_sprite.then(|| {
let hit_pos_world =
Expand Down
3 changes: 3 additions & 0 deletions examples/picking/sprite_picking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
.spawn((
Sprite::from_color(Color::BLACK, sprite_size),
Transform::from_xyz(i * len - len, j * len - len, -1.0),
Pickable::default(),
))
.observe(recolor_on::<Pointer<Over>>(Color::srgb(0.0, 1.0, 1.0)))
.observe(recolor_on::<Pointer<Out>>(Color::BLACK))
Expand All @@ -79,6 +80,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
Transform::from_xyz(i * len - len, j * len - len, 0.0)
.with_scale(Vec3::splat(1.0 + (i - 1.0) * 0.2))
.with_rotation(Quat::from_rotation_z((j - 1.0) * 0.2)),
Pickable::default(),
))
.observe(recolor_on::<Pointer<Over>>(Color::srgb(0.0, 1.0, 0.0)))
.observe(recolor_on::<Pointer<Out>>(Color::srgb(1.0, 0.0, 0.0)))
Expand Down Expand Up @@ -140,6 +142,7 @@ fn setup_atlas(
Transform::from_xyz(300.0, 0.0, 0.0).with_scale(Vec3::splat(6.0)),
animation_indices,
AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
Pickable::default(),
))
.observe(recolor_on::<Pointer<Over>>(Color::srgb(0.0, 1.0, 1.0)))
.observe(recolor_on::<Pointer<Out>>(Color::srgb(1.0, 1.0, 1.0)))
Expand Down

0 comments on commit f7b2a02

Please sign in to comment.