Skip to main content

sequoia_view/
hit_shape.rs

1use sequoia_core::{BrushStroke, Color, Fill, Point, Rect};
2
3use super::painter::Painter;
4
5#[derive(Clone, Copy, Debug, PartialEq)]
6pub struct HitCircle {
7    pub center: Point,
8    pub radius: f32,
9}
10
11impl HitCircle {
12    pub fn new(center: Point, radius: f32) -> Self {
13        Self { center, radius }
14    }
15
16    pub fn paint(&self, g: &mut Painter, fill: Fill) -> Rect {
17        g.circle(self.center, self.radius, fill)
18    }
19
20    pub fn outline(&self, g: &mut Painter, stroke: BrushStroke, color: Color) -> Rect {
21        g.ring(self.center, self.radius, stroke.width.max(0.0), color)
22    }
23
24    pub fn contains(&self, p: Point, tol: f32) -> bool {
25        if !(self.radius > 0.0) {
26            return false;
27        }
28        self.center.distance(p) <= self.radius + tol.max(0.0)
29    }
30}
31
32#[derive(Clone, Copy, Debug, PartialEq)]
33pub struct HitRect {
34    pub rect: Rect,
35    pub radius: f32,
36}
37
38impl HitRect {
39    pub fn new(rect: Rect, radius: f32) -> Self {
40        Self { rect, radius }
41    }
42
43    pub fn paint(&self, g: &mut Painter, fill: Fill) -> Rect {
44        g.rounded(self.rect, self.radius, fill)
45    }
46
47    pub fn outline(&self, g: &mut Painter, stroke: BrushStroke, color: Color) -> Rect {
48        g.rect_outline(self.rect, self.radius, stroke, color)
49    }
50
51    pub fn contains(&self, p: Point, tol: f32) -> bool {
52        if self.rect.is_empty() {
53            return false;
54        }
55        let t = tol.max(0.0);
56        self.rect.inflate(t, t).contains(p)
57    }
58}