Files
Graphite/core/document/src/layers/polyline.rs
Paul Kupper a297c9de8a Implement viewport selection (#178)
* Begin implementing viewport selection

* Implement viewport click and drag selection for ellipse and rectangle

* Begin implementing line selection

* Remove debug prints

* Run cargo format

* Use DVec2 instead of kurbo::Point

* Line and polyline intersection

* Run cargo format

* Add fix for missing layer panel update

* Replace point selection with box selection

* Formatting

Co-authored-by: Keavon Chambers <keavon@keavon.com>
2021-07-04 16:05:12 -07:00

67 lines
2.0 KiB
Rust

use crate::{intersection::intersect_quad_bez_path, LayerId};
use glam::{DAffine2, DVec2};
use serde::{Deserialize, Serialize};
use std::fmt::Write;
use super::{style, LayerData};
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct PolyLine {
points: Vec<glam::DVec2>,
}
impl PolyLine {
pub fn new(points: Vec<impl Into<glam::DVec2>>) -> PolyLine {
PolyLine {
points: points.into_iter().map(|it| it.into()).collect(),
}
}
}
impl LayerData for PolyLine {
fn to_kurbo_path(&self, transform: glam::DAffine2, _style: style::PathStyle) -> kurbo::BezPath {
let mut path = kurbo::BezPath::new();
self.points
.iter()
.map(|v| transform.transform_point2(*v))
.map(|v| kurbo::Point { x: v.x, y: v.y })
.enumerate()
.for_each(|(i, p)| if i == 0 { path.move_to(p) } else { path.line_to(p) });
path
}
fn render(&mut self, svg: &mut String, transform: glam::DAffine2, style: style::PathStyle) {
if self.points.is_empty() {
return;
}
let _ = write!(svg, r#"<polyline points=""#);
let mut points = self.points.iter().map(|v| transform.transform_point2(*v));
let first = points.next().unwrap();
let _ = write!(svg, "{:.3} {:.3}", first.x, first.y);
for point in points {
let _ = write!(svg, " {:.3} {:.3}", point.x, point.y);
}
let _ = write!(svg, r#""{} />"#, style.render());
}
fn intersects_quad(&self, quad: [DVec2; 4], path: &mut Vec<LayerId>, intersections: &mut Vec<Vec<LayerId>>, style: style::PathStyle) {
if intersect_quad_bez_path(quad, &self.to_kurbo_path(DAffine2::IDENTITY, style), false) {
intersections.push(path.clone());
}
}
}
#[cfg(test)]
#[test]
fn polyline_should_render() {
use super::style::PathStyle;
use glam::DVec2;
let mut polyline = PolyLine {
points: vec![DVec2::new(3.0, 4.12354), DVec2::new(1.0, 5.54)],
};
let mut svg = String::new();
polyline.render(&mut svg, glam::DAffine2::IDENTITY, PathStyle::default());
assert_eq!(r##"<polyline points="3.000 4.124 1.000 5.540" />"##, svg);
}