Skip to content

Commit

Permalink
replace all .single calls with matched .get_single calls (#296)
Browse files Browse the repository at this point in the history
* replace all .single calls with matched .get_single calls

* Update changelog

---------

Co-authored-by: Aevyrie <[email protected]>
  • Loading branch information
Schmarni-Dev and aevyrie authored Feb 24, 2024
1 parent f1a64e2 commit fd08e46
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# UNRELEASED

- Fixed: replace all `.single` calls with matched `.get_single` calls to avoid crashing in
environments where there is no window available
- Fixed: sprite picking depth is now consistent with other picking backends.
- Fixed: entities with identical depth could be dropped due to the use of a BTreeMap to sort
entities by depth. This has been changed to use a sorted Vec, to allow entities with the same
Expand Down
5 changes: 4 additions & 1 deletion backends/bevy_picking_sprite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ pub fn sprite_picking(
.find(|(_, camera, _, _)| {
camera
.target
.normalize(Some(primary_window.single()))
.normalize(Some(match primary_window.get_single() {
Ok(w) => w,
Err(_) => return false,
}))
.unwrap()
== location.target
})
Expand Down
5 changes: 4 additions & 1 deletion backends/bevy_picking_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ pub fn ui_picking(
)
})
}) {
let window_entity = primary_window.single();
let window_entity = match primary_window.get_single() {
Ok(w) => w,
Err(_) => continue,
};

// Find the topmost bevy_ui camera with the same target as this pointer.
//
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_picking_core/src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ impl Location {
) -> bool {
if camera
.target
.normalize(Some(primary_window.single()))
.normalize(Some(match primary_window.get_single() {
Ok(w) => w,
Err(_) => return false,
}))
.as_ref()
!= Some(&self.target)
{
Expand Down
8 changes: 7 additions & 1 deletion crates/bevy_picking_input/src/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ pub fn mouse_pick_events(
PointerId::Mouse,
Location {
target: RenderTarget::Window(WindowRef::Entity(event.window))
.normalize(Some(windows.single().0))
.normalize(Some(
match windows.get_single() {
Ok(w) => w,
Err(_) => continue,
}
.0,
))
.unwrap(),
position: event.position,
},
Expand Down
8 changes: 7 additions & 1 deletion crates/bevy_picking_input/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ pub fn touch_pick_events(
let pointer = PointerId::Touch(touch.id);
let location = Location {
target: RenderTarget::Window(WindowRef::Primary)
.normalize(Some(windows.single().0))
.normalize(Some(
match windows.get_single() {
Ok(w) => w,
Err(_) => continue,
}
.0,
))
.unwrap(),
position: touch.position,
};
Expand Down

0 comments on commit fd08e46

Please sign in to comment.