Skip to content

Commit 9abb623

Browse files
authored
single query per node in UI picking backend (#22049)
# Objective The UI picking backend queries twice for the node data. Simplify `ui_picking` by storing the camera entity and picking data in `hit_nodes` instead of the second query. ## Solution Store the camera entity and picking data in `hit_nodes` instead of querying for them again.
1 parent 4eed4d0 commit 9abb623

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

crates/bevy_ui/src/picking_backend.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ pub fn ui_picking(
149149
}
150150

151151
// The list of node entities hovered for each (camera, pointer) combo
152-
let mut hit_nodes = HashMap::<(Entity, PointerId), Vec<(Entity, Entity, Vec2)>>::default();
152+
let mut hit_nodes =
153+
HashMap::<(Entity, PointerId), Vec<(Entity, Entity, Option<Pickable>, Vec2)>>::default();
153154

154155
// prepare an iterator that contains all the nodes that have the cursor in their rect,
155156
// from the top node to the bottom one. this will also reset the interaction to `None`
@@ -222,6 +223,7 @@ pub fn ui_picking(
222223
.push((
223224
text_entity,
224225
camera_entity,
226+
node.pickable.cloned(),
225227
node.transform.inverse().transform_point2(*cursor_position)
226228
/ node.node.size(),
227229
));
@@ -240,6 +242,7 @@ pub fn ui_picking(
240242
.push((
241243
node_entity,
242244
camera_entity,
245+
node.pickable.cloned(),
243246
node.transform.inverse().transform_point2(*cursor_position)
244247
/ node.node.size(),
245248
));
@@ -254,13 +257,13 @@ pub fn ui_picking(
254257
let mut picks = Vec::new();
255258
let mut depth = 0.0;
256259

257-
for (hovered_node, camera_entity, position) in hovered {
260+
for (hovered_node, camera_entity, pickable, position) in hovered {
258261
picks.push((
259262
*hovered_node,
260263
HitData::new(*camera_entity, depth, Some(position.extend(0.0)), None),
261264
));
262265

263-
if let Ok(pickable) = pickable_query.get(*hovered_node) {
266+
if let Some(pickable) = pickable {
264267
// If an entity has a `Pickable` component, we will use that as the source of truth.
265268
if pickable.should_block_lower {
266269
break;

0 commit comments

Comments
 (0)