use crate::point::{Vec2D, WorldUnit, ScreenUnit};
use crate::color::Color;
use crate::path_command::PathCommand::{self, *};
use crate::geom::Ellipse;
use crate::style::Style;
use crate::transform::Transform;
#[derive(Debug, PartialEq, Clone)]
pub enum DrawCommand {
Path {
commands: Vec<PathCommand<WorldUnit>>,
style: Style<WorldUnit>,
},
Ellipse {
ellipse: Ellipse<WorldUnit>,
style: Style<WorldUnit>,
},
ScreenPath {
commands: Vec<PathCommand<ScreenUnit>>,
style: Style<ScreenUnit>,
},
ScreenCircle {
center: Vec2D<ScreenUnit>,
radius: ScreenUnit,
style: Style<ScreenUnit>,
},
}
impl DrawCommand {
pub fn color(&self) -> Option<Color> {
match self {
DrawCommand::Path { style, .. } => style.stroke.map(|s| s.color),
DrawCommand::Ellipse { style, .. } => style.stroke.map(|s| s.color),
DrawCommand::ScreenPath { style, .. } => style.stroke.map(|s| s.color),
DrawCommand::ScreenCircle { style, .. } => style.stroke.map(|s| s.color),
}
}
pub fn fill(&self) -> Option<Color> {
match self {
DrawCommand::Path { style, .. } => style.fill,
DrawCommand::Ellipse { style, .. } => style.fill,
DrawCommand::ScreenPath { style, .. } => style.fill,
DrawCommand::ScreenCircle { style, .. } => style.fill,
}
}
}
pub fn circle_helper(center: Vec2D<ScreenUnit>, radius: ScreenUnit) -> DrawCommand {
DrawCommand::ScreenCircle {
center,
radius,
style: Style::circle_helper(),
}
}
fn red_circle_helper(center: Vec2D<ScreenUnit>, radius: ScreenUnit) -> DrawCommand {
DrawCommand::ScreenCircle {
center,
radius,
style: Style::red_circle_helper(),
}
}
pub fn cancel_helper(prev: Vec2D<WorldUnit>, new: Vec2D<WorldUnit>, t: Transform, snap: ScreenUnit) -> DrawCommand {
if t.to_screen_coordinates(new).distance(t.to_screen_coordinates(prev)) < snap {
red_circle_helper(t.to_screen_coordinates(prev), snap)
} else {
circle_helper(t.to_screen_coordinates(prev), snap)
}
}
pub fn path_helper(points: Vec<Vec2D<ScreenUnit>>) -> DrawCommand {
DrawCommand::ScreenPath {
commands: points.into_iter().enumerate().map(|(i, p)| {
if i == 0 {
MoveTo(p)
} else {
LineTo(p)
}
}).collect(),
style: Style::path_helper(),
}
}