1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! Shape functions for drawing.

use crate::{prelude::*, renderer::Rendering};

#[macro_use]
pub mod ellipse;
#[macro_use]
pub mod line;
#[macro_use]
pub mod point;
#[macro_use]
pub mod rect;
#[macro_use]
pub mod quad;
#[macro_use]
pub mod sphere;
#[macro_use]
pub mod triangle;

pub use ellipse::*;
pub use line::*;
use num_traits::AsPrimitive;
pub use point::*;
pub use quad::*;
pub use rect::*;
pub use sphere::*;
pub use triangle::*;

/// Trait for shape containing operations.
pub trait Contains<T, const N: usize> {
    /// The shape type. e.g. [Rect<T>].
    type Shape;

    /// Returns whether this shape contains a given [Point].
    fn contains_point<P>(&self, _p: P) -> bool
    where
        P: Into<Point<T, N>>;

    /// Returns whether this shape completely contains another shape of the same type.
    fn contains_shape<O>(&self, _other: O) -> bool
    where
        O: Into<Self::Shape>;
}

/// Trait for shape intersection operations.
pub trait Intersects<T, const N: usize> {
    /// The shape type. e.g. [Rect<T>].
    type Shape;

    /// Returns the closest intersection point with a given line and distance along the line or
    /// `None` if there is no intersection.
    fn intersects_line<L>(&self, _line: L) -> Option<(Point<T, N>, T)>
    where
        L: Into<Line<T, N>>;

    /// Returns whether this shape intersects with another shape of the same type.
    fn intersects_shape<O>(&self, _other: O) -> bool
    where
        O: Into<Self::Shape>;
}

impl PixState {
    /// Draw a [Point] to the current canvas.
    pub fn point<P>(&mut self, p: P) -> PixResult<()>
    where
        P: Into<PointI2>,
    {
        if let Some(stroke) = self.settings.stroke {
            self.renderer.point(p.into(), stroke)?;
        }
        Ok(())
    }

    /// Draw a [Line] to the current canvas.
    pub fn line<L>(&mut self, line: L) -> PixResult<()>
    where
        L: Into<LineI2>,
    {
        if let Some(stroke) = self.settings.stroke {
            self.renderer.line(line.into(), stroke)?;
        }
        Ok(())
    }

    /// Draw a [Triangle][Tri] to the current canvas.
    pub fn triangle<T>(&mut self, tri: T) -> PixResult<()>
    where
        T: Into<TriI2>,
    {
        let s = &self.settings;
        Ok(self.renderer.triangle(tri.into(), s.fill, s.stroke)?)
    }

    /// Draw a square [Rect] to the current canvas.
    pub fn square<R>(&mut self, square: R) -> PixResult<()>
    where
        R: Into<Rect<i32>>,
    {
        self.rect(square)
    }

    /// Draw a rounded [Square](Rect) to the current canvas.
    pub fn rounded_square<R, T>(&mut self, square: R, radius: T) -> PixResult<()>
    where
        R: Into<Rect<i32>>,
        T: AsPrimitive<i32>,
    {
        self.rounded_rect(square, radius)
    }

    /// Draw a [Rectangle](Rect) to the current canvas.
    pub fn rect<R>(&mut self, rect: R) -> PixResult<()>
    where
        R: Into<Rect<i32>>,
    {
        let s = &self.settings;
        let mut rect = rect.into();
        if let RectMode::Center = s.rect_mode {
            rect.center_on(rect.top_left());
        };
        Ok(self.renderer.rect(rect, None, s.fill, s.stroke)?)
    }

    /// Draw a rounded [Rectangle](Rect) to the current canvas.
    pub fn rounded_rect<R, T>(&mut self, rect: R, radius: T) -> PixResult<()>
    where
        R: Into<Rect<i32>>,
        T: AsPrimitive<i32>,
    {
        let s = &self.settings;
        let mut rect = rect.into();
        if let RectMode::Center = s.rect_mode {
            rect.center_on(rect.top_left());
        };
        Ok(self
            .renderer
            .rect(rect, Some(radius.as_()), s.fill, s.stroke)?)
    }

    /// Draw a [Quadrilateral](Quad) to the current canvas.
    pub fn quad<Q>(&mut self, quad: Q) -> PixResult<()>
    where
        Q: Into<QuadI2>,
    {
        let s = &self.settings;
        Ok(self.renderer.quad(quad.into(), s.fill, s.stroke)?)
    }

    /// Draw a polygon to the current canvas.
    pub fn polygon<P: AsRef<[PointI2]>>(&mut self, points: P) -> PixResult<()> {
        let s = &self.settings;
        Ok(self.renderer.polygon(points.as_ref(), s.fill, s.stroke)?)
    }

    /// Draw a circle [Ellipse] to the current canvas.
    pub fn circle<C>(&mut self, circle: C) -> PixResult<()>
    where
        C: Into<Ellipse<i32>>,
    {
        self.ellipse(circle)
    }

    /// Draw a [Ellipse] to the current canvas.
    pub fn ellipse<E>(&mut self, ellipse: E) -> PixResult<()>
    where
        E: Into<Ellipse<i32>>,
    {
        let s = &self.settings;
        let mut ellipse = ellipse.into();
        if let EllipseMode::Center = s.ellipse_mode {
            ellipse.center_on(ellipse.top_left());
        };
        Ok(self.renderer.ellipse(ellipse, s.fill, s.stroke)?)
    }

    /// Draw an arc to the current canvas.
    pub fn arc<P, T>(&mut self, p: P, radius: T, start: T, end: T) -> PixResult<()>
    where
        P: Into<PointI2>,
        T: AsPrimitive<i32>,
    {
        let s = &self.settings;
        let p = p.into();
        Ok(self.renderer.arc(
            p,
            radius.as_(),
            start.as_(),
            end.as_(),
            s.arc_mode,
            s.fill,
            s.stroke,
        )?)
    }
}