use crate::path::into_bezpath::IntoBezPathTolerance;
use crate::{Point, Polyline};
use kurbo::Vec2;
pub trait Draw {
fn add_path<T: IntoBezPathTolerance>(&mut self, path: T) -> &mut Self;
#[allow(clippy::too_many_arguments)]
#[inline]
fn cubic_bezier(
&mut self,
x1: impl Into<f64>,
y1: impl Into<f64>,
x2: impl Into<f64>,
y2: impl Into<f64>,
x3: impl Into<f64>,
y3: impl Into<f64>,
x4: impl Into<f64>,
y4: impl Into<f64>,
) -> &mut Self {
self.add_path(kurbo::CubicBez::new(
(x1.into(), y1.into()),
(x2.into(), y2.into()),
(x3.into(), y3.into()),
(x4.into(), y4.into()),
))
}
#[inline]
fn quadratic_bezier(
&mut self,
x1: impl Into<f64>,
y1: impl Into<f64>,
x2: impl Into<f64>,
y2: impl Into<f64>,
x3: impl Into<f64>,
y3: impl Into<f64>,
) -> &mut Self {
self.add_path(kurbo::QuadBez::new(
(x1.into(), y1.into()),
(x2.into(), y2.into()),
(x3.into(), y3.into()),
))
}
#[allow(clippy::too_many_arguments)]
#[inline]
fn arc(
&mut self,
x: impl Into<f64>,
y: impl Into<f64>,
rx: impl Into<f64>,
ry: impl Into<f64>,
start: f64,
sweep: f64,
x_rot: f64,
) -> &mut Self {
self.add_path(kurbo::Arc {
center: kurbo::Point {
x: x.into(),
y: y.into(),
},
radii: Vec2 {
x: rx.into(),
y: ry.into(),
},
start_angle: start,
sweep_angle: sweep,
x_rotation: x_rot,
})
}
#[inline]
fn circle(&mut self, x: impl Into<f64>, y: impl Into<f64>, r: impl Into<f64>) -> &mut Self {
self.add_path(kurbo::Circle::new(
kurbo::Point {
x: x.into(),
y: y.into(),
},
r.into(),
))
}
#[inline]
fn ellipse(
&mut self,
x: impl Into<f64>,
y: impl Into<f64>,
rx: impl Into<f64>,
ry: impl Into<f64>,
x_rot: impl Into<f64>,
) -> &mut Self {
self.add_path(kurbo::Ellipse::new(
(x.into(), y.into()),
(rx.into(), ry.into()),
x_rot.into(),
))
}
#[inline]
fn line(
&mut self,
x1: impl Into<f64>,
y1: impl Into<f64>,
x2: impl Into<f64>,
y2: impl Into<f64>,
) -> &mut Self {
self.add_path(kurbo::Line::new(
(x1.into(), y1.into()),
(x2.into(), y2.into()),
))
}
fn polyline(
&mut self,
points: impl IntoIterator<Item = impl Into<Point>>,
close: bool,
) -> &mut Self {
let mut polyline = Polyline::from_iter(points);
if close {
polyline.close();
}
self.add_path(polyline)
}
#[inline]
fn rect(
&mut self,
x: impl Into<f64>,
y: impl Into<f64>,
w: impl Into<f64>,
h: impl Into<f64>,
) -> &mut Self {
let x = x.into();
let y = y.into();
let w = w.into();
let h = h.into();
self.add_path(kurbo::Rect::new(
x - w * 0.5,
y - h * 0.5,
x + w * 0.5,
y + h * 0.5,
))
}
#[allow(clippy::too_many_arguments)]
#[inline]
fn rounded_rect(
&mut self,
x: impl Into<f64>,
y: impl Into<f64>,
w: impl Into<f64>,
h: impl Into<f64>,
tl: impl Into<f64>,
tr: impl Into<f64>,
br: impl Into<f64>,
bl: impl Into<f64>,
) -> &mut Self {
let x = x.into();
let y = y.into();
let w = w.into();
let h = h.into();
self.add_path(kurbo::RoundedRect::new(
x - w * 0.5,
y - h * 0.5,
x + w * 0.5,
y + h * 0.5,
(tl.into(), tr.into(), br.into(), bl.into()),
))
}
fn catmull_rom(
&mut self,
points: impl IntoIterator<Item = impl Into<Point>>,
tension: f64,
) -> &mut Self {
self.add_path(crate::CatmullRom::from_points(points).tension(tension));
self
}
fn svg_path(&mut self, path: &str) -> Result<&mut Self, kurbo::SvgParseError> {
self.add_path(kurbo::BezPath::from_svg(path)?);
Ok(self)
}
}