Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix buggy interaction with widgets outside of clip rect #4675

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/egui/src/hit_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ pub fn hit_test(
.filter(|layer| layer.order.allow_interaction())
.flat_map(|&layer_id| widgets.get_layer(layer_id))
.filter(|&w| {
if w.interact_rect.is_negative() {
return false;
}

let pos_in_layer = pos_in_layers.get(&w.layer_id).copied().unwrap_or(pos);
let dist_sq = w.interact_rect.distance_sq_to_pos(pos_in_layer);

Expand Down Expand Up @@ -311,6 +315,10 @@ fn find_closest(widgets: impl Iterator<Item = WidgetRect>, pos: Pos2) -> Option<
let mut closest = None;
let mut closest_dist_sq = f32::INFINITY;
for widget in widgets {
if widget.interact_rect.is_negative() {
continue;
}

let dist_sq = widget.interact_rect.distance_sq_to_pos(pos);

// In case of a tie, take the last one = the one on top.
Expand Down
14 changes: 14 additions & 0 deletions crates/emath/src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ impl Rect {
/// The distance from the rect to the position.
///
/// The distance is zero when the position is in the interior of the rectangle.
///
/// [Negative rectangles](Self::is_negative) always return [`f32::INFINITY`].
#[inline]
pub fn distance_to_pos(&self, pos: Pos2) -> f32 {
self.distance_sq_to_pos(pos).sqrt()
Expand All @@ -369,8 +371,14 @@ impl Rect {
/// The distance from the rect to the position, squared.
///
/// The distance is zero when the position is in the interior of the rectangle.
///
/// [Negative rectangles](Self::is_negative) always return [`f32::INFINITY`].
#[inline]
pub fn distance_sq_to_pos(&self, pos: Pos2) -> f32 {
if self.is_negative() {
return f32::INFINITY;
}

let dx = if self.min.x > pos.x {
self.min.x - pos.x
} else if pos.x > self.max.x {
Expand All @@ -394,6 +402,8 @@ impl Rect {
///
/// Negative inside the box.
///
/// [Negative rectangles](Self::is_negative) always return [`f32::INFINITY`].
///
/// ```
/// # use emath::{pos2, Rect};
/// let rect = Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0));
Expand All @@ -402,6 +412,10 @@ impl Rect {
/// assert_eq!(rect.signed_distance_to_pos(pos2(1.50, 0.50)), 0.50);
/// ```
pub fn signed_distance_to_pos(&self, pos: Pos2) -> f32 {
if self.is_negative() {
return f32::INFINITY;
}

let edge_distances = (pos - self.center()).abs() - self.size() * 0.5;
let inside_dist = edge_distances.max_elem().min(0.0);
let outside_dist = edge_distances.max(Vec2::ZERO).length();
Expand Down
Loading