From eeaa7eed2d55cb701a3463bec46df52cfce45a99 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Sat, 9 Mar 2024 23:15:02 +0000 Subject: [PATCH 1/2] no cursor relative position for zero sized nodes --- crates/bevy_ui/src/focus.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index 08d488de34bb9..7dd4defc1eefb 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -252,9 +252,13 @@ pub fn ui_focus_system( // The mouse position relative to the node // (0., 0.) is the top-left corner, (1., 1.) is the bottom-right corner // Coordinates are relative to the entire node, not just the visible region. - let relative_cursor_position = camera_cursor_positions - .get(&camera_entity) - .map(|cursor_position| (*cursor_position - node_rect.min) / node_rect.size()); + let relative_cursor_position = + camera_cursor_positions + .get(&camera_entity) + .and_then(|cursor_position| { + (node_rect.size().cmpgt(Vec2::ZERO).all()) + .then_some((*cursor_position - node_rect.min) / node_rect.size()) + }); // If the current cursor position is within the bounds of the node's visible area, consider it for // clicking From 7ec2a3b23f50b1cef148f5607fbc66b3db2ee497 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Sat, 9 Mar 2024 23:46:21 +0000 Subject: [PATCH 2/2] comment --- crates/bevy_ui/src/focus.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index 7dd4defc1eefb..60626b040e6f0 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -256,6 +256,9 @@ pub fn ui_focus_system( camera_cursor_positions .get(&camera_entity) .and_then(|cursor_position| { + // ensure node size is non-zero in all dimensions, otherwise relative position will be + // +/-inf. if the node is hidden, the visible rect min/max will also be -inf leading to + // false positives for mouse_over (#12395) (node_rect.size().cmpgt(Vec2::ZERO).all()) .then_some((*cursor_position - node_rect.min) / node_rect.size()) });