Skip to content

Commit 617828c

Browse files
authored
Simplify ui_picking's camera search (#22075)
# Objective In `ui_picking` the camera_query iterator is run through multiple filters and maps, which could be replaced by a single filter. Also camera_query retrieves the `Camera` component but then it is thrown away and queried for again. ## Solution * Just collapse all the maps and filters into one `filter`. * Only query for `Camera` once and keep the result, remove the second query.
1 parent 5ffc1d6 commit 617828c

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

crates/bevy_ui/src/picking_backend.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,33 +119,24 @@ pub fn ui_picking(
119119
{
120120
// This pointer is associated with a render target, which could be used by multiple
121121
// cameras. We want to ensure we return all cameras with a matching target.
122-
for camera in camera_query
123-
.iter()
124-
.filter(|(_, _, cam_can_pick)| !settings.require_markers || *cam_can_pick)
125-
.map(|(entity, camera, _)| {
126-
(
127-
entity,
128-
camera.target.normalize(primary_window.single().ok()),
129-
)
130-
})
131-
.filter_map(|(entity, target)| Some(entity).zip(target))
132-
.filter(|(_entity, target)| target == &pointer_location.target)
133-
.map(|(cam_entity, _target)| cam_entity)
134-
{
135-
let Ok((_, camera_data, _)) = camera_query.get(camera) else {
136-
continue;
137-
};
122+
for (entity, camera, _) in camera_query.iter().filter(|(_, camera, cam_can_pick)| {
123+
(!settings.require_markers || *cam_can_pick)
124+
&& camera
125+
.target
126+
.normalize(primary_window.single().ok())
127+
.is_some_and(|target| target == pointer_location.target)
128+
}) {
138129
let mut pointer_pos =
139-
pointer_location.position * camera_data.target_scaling_factor().unwrap_or(1.);
140-
if let Some(viewport) = camera_data.physical_viewport_rect() {
130+
pointer_location.position * camera.target_scaling_factor().unwrap_or(1.);
131+
if let Some(viewport) = camera.physical_viewport_rect() {
141132
if !viewport.as_rect().contains(pointer_pos) {
142133
// The pointer is outside the viewport, skip it
143134
continue;
144135
}
145136
pointer_pos -= viewport.min.as_vec2();
146137
}
147138
pointer_pos_by_camera
148-
.entry(camera)
139+
.entry(entity)
149140
.or_default()
150141
.insert(pointer_id, pointer_pos);
151142
}

0 commit comments

Comments
 (0)