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.
This commit is contained in:
BigWingBeat 2024-09-05 20:16:57 +01:00 committed by GitHub
parent cb221d8852
commit 54aa45e62f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 26 deletions

View file

@ -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.
}

View file

@ -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.
}