-
I'm drawing a lot of lines using a painter and I'd like to detect when the mouse is hovering or clicking them. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The painter doesn't interact with user input, it just paints. You can only detect user input inside a rectangle, using something like Ui::allocate_response or Ui::allocate_painter. You can also get the pointer position with Ui::input and then write your own logic to determine what it is hovering. The shapes themselves don't have a way to determine whether they contain a given point. They only offer a visual_bounding_rect to get the rectangle containing the shape. That is why when you click the circular widgets like radio buttons, you can click on the corners outside the circle. All hitboxes are actually rectangles. |
Beta Was this translation helpful? Give feedback.
-
For lines I managed to copy this off of stackoverflow: fn distance_point_to_segment(p: Pos2, x1: f32, y1: f32, x2: f32, y2: f32) -> f32 {
let a = p.x - x1;
let b = p.y - y1;
let c = x2 - x1;
let d = y2 - y1;
let dot = a * c + b * d;
let len_sq = c * c + d * d;
let mut param = -1f32;
if len_sq != 0f32 {
param = dot / len_sq;
}
let xx;
let yy;
if param < 0f32 {
xx = x1;
yy = y1;
}
else if param > 1f32 {
xx = x2;
yy = y2;
}
else {
xx = x1 + param * c;
yy = y1 + param * d;
}
let dx = p.x - xx;
let dy = p.y - yy;
(dx * dx + dy * dy).sqrt()
} |
Beta Was this translation helpful? Give feedback.
The painter doesn't interact with user input, it just paints. You can only detect user input inside a rectangle, using something like Ui::allocate_response or Ui::allocate_painter. You can also get the pointer position with Ui::input and then write your own logic to determine what it is hovering. The shapes themselves don't have a way to determine whether they contain a given point. They only offer a visual_bounding_rect to get the rectangle containing the shape. That is why when you click the circular widgets like radio buttons, you can click on the corners outside the circle. All hitboxes are actually rectangles.