diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index b0fe6fb2c0..387d47c06c 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -1,6 +1,5 @@ use crate::{ - picking_backend::pick_rounded_rect, CalculatedClip, DefaultUiCamera, Node, TargetCamera, - UiScale, UiStack, + CalculatedClip, DefaultUiCamera, Node, ResolvedBorderRadius, TargetCamera, UiScale, UiStack, }; use bevy_ecs::{ change_detection::DetectChangesMut, @@ -343,3 +342,26 @@ pub fn ui_focus_system( } } } + +// Returns true if `point` (relative to the rectangle's center) is within the bounds of a rounded rectangle with +// the given size and border radius. +// +// Matches the sdf function in `ui.wgsl` that is used by the UI renderer to draw rounded rectangles. +pub(crate) fn pick_rounded_rect( + point: Vec2, + size: Vec2, + border_radius: ResolvedBorderRadius, +) -> bool { + let s = point.signum(); + let r = (border_radius.top_left * (1. - s.x) * (1. - s.y) + + border_radius.top_right * (1. + s.x) * (1. - s.y) + + border_radius.bottom_right * (1. + s.x) * (1. + s.y) + + border_radius.bottom_left * (1. - s.x) * (1. + s.y)) + / 4.; + + let corner_to_point = point.abs() - 0.5 * size; + let q = corner_to_point + r; + let l = q.max(Vec2::ZERO).length(); + let m = q.max_element().min(0.); + l + m - r < 0. +} diff --git a/crates/bevy_ui/src/picking_backend.rs b/crates/bevy_ui/src/picking_backend.rs index 433e293e7f..e88409f27c 100644 --- a/crates/bevy_ui/src/picking_backend.rs +++ b/crates/bevy_ui/src/picking_backend.rs @@ -23,7 +23,7 @@ #![allow(clippy::too_many_arguments)] #![deny(missing_docs)] -use crate::{prelude::*, UiStack}; +use crate::{focus::pick_rounded_rect, prelude::*, UiStack}; use bevy_app::prelude::*; use bevy_ecs::{prelude::*, query::QueryData}; use bevy_math::Vec2; @@ -217,26 +217,3 @@ pub fn ui_picking( output.send(PointerHits::new(*pointer, picks, order)); } } - -// Returns true if `point` (relative to the rectangle's center) is within the bounds of a rounded rectangle with -// the given size and border radius. -// -// Matches the sdf function in `ui.wgsl` that is used by the UI renderer to draw rounded rectangles. -pub(crate) fn pick_rounded_rect( - point: Vec2, - size: Vec2, - border_radius: ResolvedBorderRadius, -) -> bool { - let s = point.signum(); - let r = (border_radius.top_left * (1. - s.x) * (1. - s.y) - + border_radius.top_right * (1. + s.x) * (1. - s.y) - + border_radius.bottom_right * (1. + s.x) * (1. + s.y) - + border_radius.bottom_left * (1. - s.x) * (1. + s.y)) - / 4.; - - let corner_to_point = point.abs() - 0.5 * size; - let q = corner_to_point + r; - let l = q.max(Vec2::ZERO).length(); - let m = q.max_element().min(0.); - l + m - r < 0. -}