use types::{ Color, Radius, Rectangle, Resolution };
use { triangulation, DrawState, Graphics };
use math::Matrix2d;
pub use rectangle::centered;
pub use rectangle::centered_square as circle;
#[derive(Copy, Clone)]
pub struct Border {
pub color: Color,
pub radius: Radius,
}
#[derive(Copy, Clone)]
pub struct Ellipse {
pub color: Color,
pub border: Option<Border>,
pub resolution: Resolution,
}
impl Ellipse {
pub fn new(color: Color) -> Ellipse {
Ellipse {
color: color,
border: None,
resolution: 128,
}
}
pub fn new_border(
color: Color,
radius: Radius
) -> Ellipse {
Ellipse {
color: [0.0; 4],
border: Some(Border {
color: color,
radius: radius,
}),
resolution: 128,
}
}
pub fn color(mut self, value: Color) -> Self {
self.color = value;
self
}
pub fn border(mut self, value: Border) -> Self {
self.border = Some(value);
self
}
pub fn maybe_border(mut self, value: Option<Border>) -> Self {
self.border = value;
self
}
pub fn resolution(mut self, value: Resolution) -> Self {
self.resolution = value;
self
}
#[inline(always)]
pub fn draw<R: Into<Rectangle>, G>(
&self,
rectangle: R,
draw_state: &DrawState,
transform: Matrix2d,
g: &mut G
)
where G: Graphics
{
g.ellipse(self, rectangle, draw_state, transform);
}
pub fn draw_tri<R: Into<Rectangle>, G>(
&self,
rectangle: R,
draw_state: &DrawState,
transform: Matrix2d,
g: &mut G
)
where G: Graphics
{
let rectangle = rectangle.into();
g.tri_list(
draw_state,
&self.color,
|f|
triangulation::with_ellipse_tri_list(
self.resolution,
transform,
rectangle,
|vertices| f(vertices)
));
if let Some(Border { color, radius: border_radius }) = self.border {
g.tri_list(
&draw_state,
&color,
|f|
triangulation::with_ellipse_border_tri_list(
self.resolution,
transform,
rectangle,
border_radius,
|vertices| f(vertices)
));
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_ellipse() {
let _ellipse = Ellipse::new([1.0; 4])
.color([0.0; 4])
.border(Border { color: [1.0; 4], radius: 3.0 });
}
}