From 2c890b20bd6f2015e86426fd1e32cd65630c5a33 Mon Sep 17 00:00:00 2001 From: BigWingBeat Date: Thu, 5 Sep 2024 14:10:16 +0100 Subject: [PATCH 1/2] Fix bevy_ui compile error --- crates/bevy_ui/src/focus.rs | 3 +-- crates/bevy_ui/src/lib.rs | 20 ++++++++++++++++++++ crates/bevy_ui/src/picking_backend.rs | 25 +------------------------ 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index b0fe6fb2c087a..36072a4595a62 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, + pick_rounded_rect, CalculatedClip, DefaultUiCamera, Node, TargetCamera, UiScale, UiStack, }; use bevy_ecs::{ change_detection::DetectChangesMut, diff --git a/crates/bevy_ui/src/lib.rs b/crates/bevy_ui/src/lib.rs index a9dcc9e27089c..d177826b4cf44 100644 --- a/crates/bevy_ui/src/lib.rs +++ b/crates/bevy_ui/src/lib.rs @@ -21,6 +21,7 @@ pub mod widget; pub mod picking_backend; use bevy_derive::{Deref, DerefMut}; +use bevy_math::Vec2; use bevy_reflect::Reflect; #[cfg(feature = "bevy_text")] mod accessibility; @@ -245,3 +246,22 @@ fn build_text_interop(app: &mut App) { AmbiguousWithUpdateText2DLayout.ambiguous_with(bevy_text::update_text2d_layout), ); } + +// 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. +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 433e293e7f4bc..27f89157c620c 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::{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. -} From d448bdae740c3165c690ecb9dfa1b3fcb9e1f17b Mon Sep 17 00:00:00 2001 From: BigWingBeat Date: Thu, 5 Sep 2024 19:16:13 +0100 Subject: [PATCH 2/2] Move pick_rounded_rect to focus module --- crates/bevy_ui/src/focus.rs | 25 ++++++++++++++++++++++++- crates/bevy_ui/src/lib.rs | 20 -------------------- crates/bevy_ui/src/picking_backend.rs | 2 +- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index 36072a4595a62..387d47c06cec4 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -1,5 +1,5 @@ use crate::{ - pick_rounded_rect, CalculatedClip, DefaultUiCamera, Node, TargetCamera, UiScale, UiStack, + CalculatedClip, DefaultUiCamera, Node, ResolvedBorderRadius, TargetCamera, UiScale, UiStack, }; use bevy_ecs::{ change_detection::DetectChangesMut, @@ -342,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/lib.rs b/crates/bevy_ui/src/lib.rs index d177826b4cf44..a9dcc9e27089c 100644 --- a/crates/bevy_ui/src/lib.rs +++ b/crates/bevy_ui/src/lib.rs @@ -21,7 +21,6 @@ pub mod widget; pub mod picking_backend; use bevy_derive::{Deref, DerefMut}; -use bevy_math::Vec2; use bevy_reflect::Reflect; #[cfg(feature = "bevy_text")] mod accessibility; @@ -246,22 +245,3 @@ fn build_text_interop(app: &mut App) { AmbiguousWithUpdateText2DLayout.ambiguous_with(bevy_text::update_text2d_layout), ); } - -// 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. -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 27f89157c620c..e88409f27c8bf 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::{pick_rounded_rect, prelude::*, UiStack}; +use crate::{focus::pick_rounded_rect, prelude::*, UiStack}; use bevy_app::prelude::*; use bevy_ecs::{prelude::*, query::QueryData}; use bevy_math::Vec2;