Skip to content

Commit 54aa45e

Browse files
authored
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.
1 parent cb221d8 commit 54aa45e

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

crates/bevy_ui/src/focus.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::{
2-
picking_backend::pick_rounded_rect, CalculatedClip, DefaultUiCamera, Node, TargetCamera,
3-
UiScale, UiStack,
2+
CalculatedClip, DefaultUiCamera, Node, ResolvedBorderRadius, TargetCamera, UiScale, UiStack,
43
};
54
use bevy_ecs::{
65
change_detection::DetectChangesMut,
@@ -343,3 +342,26 @@ pub fn ui_focus_system(
343342
}
344343
}
345344
}
345+
346+
// Returns true if `point` (relative to the rectangle's center) is within the bounds of a rounded rectangle with
347+
// the given size and border radius.
348+
//
349+
// Matches the sdf function in `ui.wgsl` that is used by the UI renderer to draw rounded rectangles.
350+
pub(crate) fn pick_rounded_rect(
351+
point: Vec2,
352+
size: Vec2,
353+
border_radius: ResolvedBorderRadius,
354+
) -> bool {
355+
let s = point.signum();
356+
let r = (border_radius.top_left * (1. - s.x) * (1. - s.y)
357+
+ border_radius.top_right * (1. + s.x) * (1. - s.y)
358+
+ border_radius.bottom_right * (1. + s.x) * (1. + s.y)
359+
+ border_radius.bottom_left * (1. - s.x) * (1. + s.y))
360+
/ 4.;
361+
362+
let corner_to_point = point.abs() - 0.5 * size;
363+
let q = corner_to_point + r;
364+
let l = q.max(Vec2::ZERO).length();
365+
let m = q.max_element().min(0.);
366+
l + m - r < 0.
367+
}

crates/bevy_ui/src/picking_backend.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#![allow(clippy::too_many_arguments)]
2424
#![deny(missing_docs)]
2525

26-
use crate::{prelude::*, UiStack};
26+
use crate::{focus::pick_rounded_rect, prelude::*, UiStack};
2727
use bevy_app::prelude::*;
2828
use bevy_ecs::{prelude::*, query::QueryData};
2929
use bevy_math::Vec2;
@@ -217,26 +217,3 @@ pub fn ui_picking(
217217
output.send(PointerHits::new(*pointer, picks, order));
218218
}
219219
}
220-
221-
// Returns true if `point` (relative to the rectangle's center) is within the bounds of a rounded rectangle with
222-
// the given size and border radius.
223-
//
224-
// Matches the sdf function in `ui.wgsl` that is used by the UI renderer to draw rounded rectangles.
225-
pub(crate) fn pick_rounded_rect(
226-
point: Vec2,
227-
size: Vec2,
228-
border_radius: ResolvedBorderRadius,
229-
) -> bool {
230-
let s = point.signum();
231-
let r = (border_radius.top_left * (1. - s.x) * (1. - s.y)
232-
+ border_radius.top_right * (1. + s.x) * (1. - s.y)
233-
+ border_radius.bottom_right * (1. + s.x) * (1. + s.y)
234-
+ border_radius.bottom_left * (1. - s.x) * (1. + s.y))
235-
/ 4.;
236-
237-
let corner_to_point = point.abs() - 0.5 * size;
238-
let q = corner_to_point + r;
239-
let l = q.max(Vec2::ZERO).length();
240-
let m = q.max_element().min(0.);
241-
l + m - r < 0.
242-
}

0 commit comments

Comments
 (0)