use super::{Drawable, PointCollection};
use crate::drawing::backend::{BackendCoord, DrawingBackend, DrawingErrorKind};
use crate::style::ShapeStyle;
pub struct Pixel<'a, Coord> {
pos: Coord,
style: ShapeStyle<'a>,
}
impl<'a, Coord> Pixel<'a, Coord> {
pub fn new<P: Into<Coord>, S: Into<ShapeStyle<'a>>>(pos: P, style: S) -> Self {
Self {
pos: pos.into(),
style: style.into(),
}
}
}
impl<'b, 'a, Coord: 'a> PointCollection<'a, Coord> for &'a Pixel<'b, Coord> {
type Borrow = &'a Coord;
type IntoIter = std::iter::Once<&'a Coord>;
fn point_iter(self) -> Self::IntoIter {
std::iter::once(&self.pos)
}
}
impl<'a, Coord: 'a, DB: DrawingBackend> Drawable<DB> for Pixel<'a, Coord> {
fn draw<I: Iterator<Item = BackendCoord>>(
&self,
mut points: I,
backend: &mut DB,
) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
if let Some((x, y)) = points.next() {
return backend.draw_pixel((x, y), &Box::new(self.style.color));
}
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_pixel_element() {
use crate::prelude::*;
let da = crate::create_mocked_drawing_area(300, 300, |m| {
m.check_draw_pixel(|c, (x, y)| {
assert_eq!(x, 150);
assert_eq!(y, 152);
assert_eq!(c.0, 255);
assert_eq!(c.1, 0);
assert_eq!(c.2, 0);
assert_eq!(c.3, 1.0);
});
m.drop_check(|b| {
assert_eq!(b.num_draw_pixel_call, 1);
assert_eq!(b.draw_count, 1);
});
});
da.draw(&Pixel::new((150, 152), &Red))
.expect("Drawing Failure");
}
pub struct Path<'a, Coord> {
points: Vec<Coord>,
style: ShapeStyle<'a>,
}
impl<'a, Coord> Path<'a, Coord> {
pub fn new<P: Into<Vec<Coord>>, S: Into<ShapeStyle<'a>>>(points: P, style: S) -> Self {
Self {
points: points.into(),
style: style.into(),
}
}
}
impl<'b, 'a, Coord: 'a> PointCollection<'a, Coord> for &'a Path<'b, Coord> {
type Borrow = &'a Coord;
type IntoIter = &'a [Coord];
fn point_iter(self) -> &'a [Coord] {
&self.points
}
}
impl<'a, Coord: 'a, DB: DrawingBackend> Drawable<DB> for Path<'a, Coord> {
fn draw<I: Iterator<Item = BackendCoord>>(
&self,
points: I,
backend: &mut DB,
) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
backend.draw_path(points, &Box::new(self.style.color))
}
}
#[cfg(test)]
#[test]
fn test_path_element() {
use crate::prelude::*;
let da = crate::create_mocked_drawing_area(300, 300, |m| {
m.check_draw_path(|c, path| {
assert_eq!(c.0, 0);
assert_eq!(c.1, 0);
assert_eq!(c.2, 255);
assert_eq!(c.3, 1.0);
assert_eq!(path, vec![(100, 101), (105, 107), (150, 157)]);
});
m.drop_check(|b| {
assert_eq!(b.num_draw_path_call, 1);
assert_eq!(b.draw_count, 1);
});
});
da.draw(&Path::new(vec![(100, 101), (105, 107), (150, 157)], &Blue))
.expect("Drawing Failure");
}
pub struct Rectangle<'a, Coord> {
points: [Coord; 2],
style: ShapeStyle<'a>,
margin: (u32, u32, u32, u32),
}
impl<'a, Coord> Rectangle<'a, Coord> {
pub fn new<S: Into<ShapeStyle<'a>>>(points: [Coord; 2], style: S) -> Self {
Self {
points,
style: style.into(),
margin: (0, 0, 0, 0),
}
}
pub fn set_margin(&mut self, t: u32, b: u32, l: u32, r: u32) -> &mut Self {
self.margin = (t, b, l, r);
self
}
}
impl<'b, 'a, Coord: 'a> PointCollection<'a, Coord> for &'a Rectangle<'b, Coord> {
type Borrow = &'a Coord;
type IntoIter = &'a [Coord];
fn point_iter(self) -> &'a [Coord] {
&self.points
}
}
impl<'a, Coord: 'a, DB: DrawingBackend> Drawable<DB> for Rectangle<'a, Coord> {
fn draw<I: Iterator<Item = BackendCoord>>(
&self,
mut points: I,
backend: &mut DB,
) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
match (points.next(), points.next()) {
(Some(mut a), Some(mut b)) => {
a.1 += self.margin.0 as i32;
b.1 -= self.margin.1 as i32;
a.0 += self.margin.2 as i32;
b.0 -= self.margin.3 as i32;
backend.draw_rect(a, b, &Box::new(self.style.color), self.style.filled)
}
_ => Ok(()),
}
}
}
#[cfg(test)]
#[test]
fn test_rect_element() {
use crate::prelude::*;
let da = crate::create_mocked_drawing_area(300, 300, |m| {
m.check_draw_rect(|c, f, u, d| {
assert_eq!(c.0, 0);
assert_eq!(c.1, 0);
assert_eq!(c.2, 255);
assert_eq!(c.3, 1.0);
assert_eq!(f, false);
assert_eq!([u, d], [(100, 101), (105, 107)]);
});
m.drop_check(|b| {
assert_eq!(b.num_draw_rect_call, 1);
assert_eq!(b.draw_count, 1);
});
});
da.draw(&Rectangle::new([(100, 101), (105, 107)], &Blue))
.expect("Drawing Failure");
}
pub struct Circle<'a, Coord> {
center: Coord,
size: u32,
style: ShapeStyle<'a>,
}
impl<'a, Coord> Circle<'a, Coord> {
pub fn new<S: Into<ShapeStyle<'a>>>(coord: Coord, size: u32, style: S) -> Self {
Self {
center: coord,
size,
style: style.into(),
}
}
}
impl<'b, 'a, Coord: 'a> PointCollection<'a, Coord> for &'a Circle<'b, Coord> {
type Borrow = &'a Coord;
type IntoIter = std::iter::Once<&'a Coord>;
fn point_iter(self) -> std::iter::Once<&'a Coord> {
std::iter::once(&self.center)
}
}
impl<'a, Coord: 'a, DB: DrawingBackend> Drawable<DB> for Circle<'a, Coord> {
fn draw<I: Iterator<Item = BackendCoord>>(
&self,
mut points: I,
backend: &mut DB,
) -> Result<(), DrawingErrorKind<DB::ErrorType>> {
if let Some((x, y)) = points.next() {
return backend.draw_circle(
(x, y),
self.size,
&Box::new(self.style.color),
self.style.filled,
);
}
Ok(())
}
}
#[cfg(test)]
#[test]
fn test_circle_element() {
use crate::prelude::*;
let da = crate::create_mocked_drawing_area(300, 300, |m| {
m.check_draw_circle(|c, f, s, r| {
assert_eq!(c.0, 0);
assert_eq!(c.1, 0);
assert_eq!(c.2, 255);
assert_eq!(c.3, 1.0);
assert_eq!(f, false);
assert_eq!(s, (150, 151));
assert_eq!(r, 20);
});
m.drop_check(|b| {
assert_eq!(b.num_draw_circle_call, 1);
assert_eq!(b.draw_count, 1);
});
});
da.draw(&Circle::new((150, 151), 20, &Blue))
.expect("Drawing Failure");
}