use bevy::prelude::*;
use lyon::tessellation::StrokeOptions;
use crate::draw::primitive::path;
use crate::draw::primitive::{PathStroke, Primitive};
use crate::draw::properties::spatial::{orientation, position};
use crate::draw::properties::{SetColor, SetOrientation, SetPosition, SetStroke};
use crate::draw::{self, Drawing};
#[derive(Clone, Debug, Default)]
pub struct Line {
pub path: PathStroke,
pub start: Option<Vec2>,
pub end: Option<Vec2>,
}
pub type DrawingLine<'a> = Drawing<'a, Line>;
impl Line {
pub fn weight(self, weight: f32) -> Self {
self.map_path(|p| p.stroke_weight(weight))
}
pub fn tolerance(self, tolerance: f32) -> Self {
self.map_path(|p| p.stroke_tolerance(tolerance))
}
pub fn start(mut self, start: Vec2) -> Self {
self.start = Some(start);
self
}
pub fn end(mut self, end: Vec2) -> Self {
self.end = Some(end);
self
}
pub fn points(self, start: Vec2, end: Vec2) -> Self {
self.start(start).end(end)
}
fn map_path<F>(self, map: F) -> Self
where
F: FnOnce(PathStroke) -> PathStroke,
{
let Line { path, start, end } = self;
let path = map(path);
Line { path, start, end }
}
}
impl<'a> DrawingLine<'a> {
pub fn weight(self, weight: f32) -> Self {
self.stroke_weight(weight)
}
pub fn tolerance(self, tolerance: f32) -> Self {
self.stroke_tolerance(tolerance)
}
pub fn start(self, start: Vec2) -> Self {
update_line(&self.draw, self.index, |line| line.start = Some(start));
self
}
pub fn end(self, end: Vec2) -> Self {
update_line(&self.draw, self.index, |line| line.end = Some(end));
self
}
pub fn points(self, start: Vec2, end: Vec2) -> Self {
self.start(start).end(end)
}
}
fn update_line(draw: &crate::draw::Draw, index: usize, f: impl FnOnce(&mut Line)) {
crate::draw::drawing::with_primitive(draw, index, |prim| match prim {
Primitive::Line(line) => f(line),
_ => bevy::log::warn_once!("expected a `Line` primitive"),
})
}
impl SetStroke for Line {
fn stroke_options_mut(&mut self) -> &mut StrokeOptions {
SetStroke::stroke_options_mut(&mut self.path)
}
}
impl SetOrientation for Line {
fn properties(&mut self) -> &mut orientation::Properties {
SetOrientation::properties(&mut self.path)
}
}
impl SetPosition for Line {
fn properties(&mut self) -> &mut position::Properties {
SetPosition::properties(&mut self.path)
}
}
impl SetColor for Line {
fn color_mut(&mut self) -> &mut Option<Color> {
SetColor::color_mut(&mut self.path)
}
}
impl From<Line> for Primitive {
fn from(prim: Line) -> Self {
Primitive::Line(prim)
}
}
impl draw::render::RenderPrimitive for Line {
fn render_primitive(self, mut ctxt: draw::render::RenderContext, mesh: &mut Mesh) {
let Line { path, start, end } = self;
let start = start.unwrap_or(Vec2::new(0.0, 0.0));
let end = end.unwrap_or(Vec2::new(0.0, 0.0));
if start == end {
return;
}
let close = false;
let points = [start, end];
let tex_coords = [Vec2::new(0.0, 0.0), Vec2::new(1.0, 0.0)];
let points = points.iter().cloned().zip(tex_coords.iter().copied());
let global_transform = *ctxt.transform;
let local_transform = path.position.transform() * path.orientation.transform();
let transform = global_transform * local_transform;
path::render_path_points_themed(
points,
close,
path.color,
transform,
path::Options::Stroke(path.opts),
&ctxt.theme,
&draw::theme::Primitive::Line,
&mut ctxt.fill_tessellator,
&mut ctxt.stroke_tessellator,
mesh,
);
}
}