From 54aa45e62ff10cf03ad21986805ca4faa1e5a512 Mon Sep 17 00:00:00 2001 From: BigWingBeat Date: Thu, 5 Sep 2024 20:16:57 +0100 Subject: [PATCH] Fix `bevy_ui` compile error when `bevy_picking` feature is disabled (#15053) # Objective #14957 added the `pick_rounded_rect` function to `bevy_ui` in the `picking_backend` module, which is gated behind the `bevy_picking` feature. This function is used in that module, as well as in the `focus` module. The latter usage is not gated behind the `bevy_picking` feature, causing a compile error when the feature is disabled. ## Solution Move the `pick_rounded_rect` function out of the `picking_backend` module, as it does not depend on anything defined in that module. I put it in `lib.rs` but it could reasonably be moved somewhere else instead. ## Testing Encountered this compile error in a project and confirmed that this patch fixes it. --- crates/bevy_ui/src/focus.rs | 26 ++++++++++++++++++++++++-- crates/bevy_ui/src/picking_backend.rs | 25 +------------------------ 2 files changed, 25 insertions(+), 26 deletions(-) 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. -}