use lievisual::geometry::{Point, Rect, Vec2};
use lievisual::scene::{Element, Fill, FillStrokeStyle, Scene, SceneNode, Stroke};
use crate::builder::ir::{
geograph::{RoutePath, RouteSegment},
scenegraph::{SceneGraph, SceneItem},
shape::{EdgeEnds, ShapeGeometry},
};
pub fn run(sg: &SceneGraph) -> Scene {
let mut scene = Scene::new(sg.size.width, sg.size.height).with_background(sg.background);
for item in &sg.items {
let node: SceneNode = match item {
SceneItem::Shape {
geometry,
fill,
stroke,
name,
z,
} => {
let style = FillStrokeStyle {
fill: fill.clone(),
stroke: stroke.clone(),
};
let element = geometry_to_element(geometry, style);
let node = SceneNode::new(element).with_z(*z);
if let Some(n) = name {
node.with_name(n)
} else {
node
}
}
SceneItem::Edge {
path,
stroke,
ends,
z,
} => {
let nodes = run_edge_nodes(path, stroke, ends, *z);
if nodes.is_empty() {
continue;
} else if nodes.len() == 1 {
nodes.into_iter().next().unwrap()
} else {
SceneNode::group(nodes).with_z(*z)
}
}
SceneItem::Label {
text,
position,
style,
anchor: _anchor,
z,
} => {
let element = Element::rich_text(text.clone(), *position, style.clone());
SceneNode::new(element).with_z(*z)
}
SceneItem::Group { children, z } => {
let children: Vec<SceneNode> = children.iter().filter_map(run_item).collect();
SceneNode::group(children).with_z(*z)
}
};
scene.push_node(node);
}
scene
}
fn run_item(item: &SceneItem) -> Option<SceneNode> {
match item {
SceneItem::Shape {
geometry,
fill,
stroke,
name,
z,
} => {
let style = FillStrokeStyle {
fill: fill.clone(),
stroke: stroke.clone(),
};
let node = SceneNode::new(geometry_to_element(geometry, style)).with_z(*z);
Some(if let Some(n) = name {
node.with_name(n)
} else {
node
})
}
SceneItem::Edge {
path,
stroke,
ends,
z,
} => {
let nodes = run_edge_nodes(path, stroke, ends, *z);
if nodes.is_empty() {
None
} else if nodes.len() == 1 {
Some(nodes.into_iter().next().unwrap())
} else {
Some(SceneNode::group(nodes).with_z(*z))
}
}
SceneItem::Label {
text,
position,
style,
anchor: _anchor,
z,
} => Some(
SceneNode::new(Element::rich_text(text.clone(), *position, style.clone())).with_z(*z),
),
SceneItem::Group { children, z } => {
let children: Vec<SceneNode> = children.iter().filter_map(run_item).collect();
Some(SceneNode::group(children).with_z(*z))
}
}
}
fn geometry_to_element(geometry: &ShapeGeometry, style: FillStrokeStyle) -> Element {
match geometry {
ShapeGeometry::Rect { at, size } => {
let rect = Rect::new(at.x, at.y, at.x + size.width, at.y + size.height);
Element::rect(rect, style)
}
ShapeGeometry::RoundedRect { at, size, radius } => {
let rect = Rect::new(at.x, at.y, at.x + size.width, at.y + size.height);
Element::rounded_rect(rect, *radius, style)
}
ShapeGeometry::Stadium { at, size } => {
let rect = Rect::new(at.x, at.y, at.x + size.width, at.y + size.height);
let r = (size.height / 2.0).min(size.width / 2.0);
Element::rounded_rect(rect, r, style)
}
ShapeGeometry::Diamond { center, size } => {
let hw = size.width / 2.0;
let hh = size.height / 2.0;
let pts = vec![
Point::new(center.x, center.y - hh),
Point::new(center.x + hw, center.y),
Point::new(center.x, center.y + hh),
Point::new(center.x - hw, center.y),
];
Element::polygon(pts, style)
}
ShapeGeometry::Polygon { points } => Element::polygon(points.clone(), style),
ShapeGeometry::Ellipse { center, rx, ry } => {
Element::ellipse(*center, Vec2::new(*rx, *ry), 0.0, style)
}
ShapeGeometry::Pie {
center,
radius,
start_angle,
end_angle,
} => Element::pie(*center, *radius, *start_angle, *end_angle, style),
}
}
fn run_edge_nodes(
path: &RoutePath,
stroke: &Stroke,
ends: &(EdgeEnds, EdgeEnds),
z: i32,
) -> Vec<SceneNode> {
let mut nodes = Vec::new();
if path.is_empty() {
return nodes;
}
nodes.push(
SceneNode::new(Element::Path {
path: path_to_bezpath(path),
style: FillStrokeStyle {
fill: None,
stroke: Some(stroke.clone()),
},
closed: false,
})
.with_z(z),
);
let (start_ends, end_ends) = *ends;
if end_ends != EdgeEnds::None {
let d = path.last_direction();
let tip = path.end();
let from = Point::new(tip.x - d.x * 16.0, tip.y - d.y * 16.0);
nodes.extend(arrow_element(from, tip, &end_ends, stroke, z));
}
if start_ends != EdgeEnds::None {
let d = path.first_direction();
let tip = path.start();
let from = Point::new(tip.x + d.x * 16.0, tip.y + d.y * 16.0);
nodes.extend(arrow_element(from, tip, &start_ends, stroke, z));
}
nodes
}
fn path_to_bezpath(path: &RoutePath) -> lievisual::geometry::BezPath {
use lievisual::geometry::BezPath;
let mut bp = BezPath::new();
let mut pen_set = false;
for seg in path.iter() {
match seg {
RouteSegment::Line { from, to } => {
if !pen_set {
bp.move_to((from.x, from.y));
pen_set = true;
}
bp.line_to((to.x, to.y));
}
RouteSegment::CubicBezier { p0, p1, p2, p3 } => {
if !pen_set {
bp.move_to((p0.x, p0.y));
pen_set = true;
}
bp.curve_to((p1.x, p1.y), (p2.x, p2.y), (p3.x, p3.y));
}
}
}
bp
}
fn arrow_element(
from: Point,
tip: Point,
ends: &EdgeEnds,
stroke: &Stroke,
z: i32,
) -> Vec<SceneNode> {
let mut out = Vec::new();
let dx = tip.x - from.x;
let dy = tip.y - from.y;
let len = (dx * dx + dy * dy).sqrt();
if len < 1e-6 {
return out;
}
let ux = dx / len;
let uy = dy / len;
let px = -uy;
let py = ux;
let size = 8.0;
let hollow = FillStrokeStyle {
fill: None,
stroke: Some(stroke.clone()),
};
let solid = FillStrokeStyle {
fill: Some(Fill::Solid(stroke.color)),
stroke: Some(stroke.clone()),
};
let triangle = |f: &FillStrokeStyle| -> Vec<SceneNode> {
let left = Point::new(
tip.x - ux * size + px * size * 0.5,
tip.y - uy * size + py * size * 0.5,
);
let right = Point::new(
tip.x - ux * size - px * size * 0.5,
tip.y - uy * size - py * size * 0.5,
);
vec![SceneNode::new(Element::polygon(vec![left, tip, right], f.clone())).with_z(z)]
};
let diamond = |f: &FillStrokeStyle| -> Vec<SceneNode> {
let front = Point::new(tip.x - ux * size, tip.y - uy * size);
let back = Point::new(tip.x + ux * size, tip.y + uy * size);
let p1 = Point::new(tip.x + px * size * 0.7, tip.y + py * size * 0.7);
let p2 = Point::new(tip.x - px * size * 0.7, tip.y - py * size * 0.7);
vec![SceneNode::new(Element::polygon(vec![front, p1, back, p2], f.clone())).with_z(z)]
};
let open_arrow = |f: &FillStrokeStyle| -> Vec<SceneNode> {
let barb_top = Point::new(
tip.x - ux * size * 1.125 + px * size * 0.75,
tip.y - uy * size * 1.125 + py * size * 0.75,
);
let barb_bot = Point::new(
tip.x - ux * size * 1.125 - px * size * 0.75,
tip.y - uy * size * 1.125 - py * size * 0.75,
);
let notch = Point::new(tip.x - ux * size * 0.5, tip.y - uy * size * 0.5);
vec![
SceneNode::new(Element::polygon(
vec![tip, barb_top, notch, barb_bot],
f.clone(),
))
.with_z(z),
]
};
match ends {
EdgeEnds::Arrow | EdgeEnds::TriangleFilled => out.extend(triangle(&solid)),
EdgeEnds::Triangle => out.extend(triangle(&hollow)),
EdgeEnds::ArrowOpen => out.extend(open_arrow(&solid)),
EdgeEnds::DiamondFilled => out.extend(diamond(&solid)),
EdgeEnds::DiamondHollow => out.extend(diamond(&hollow)),
EdgeEnds::Circle => {
let c = Point::new(tip.x - ux * size * 0.5, tip.y - uy * size * 0.5);
out.push(
SceneNode::new(Element::ellipse(
c,
Vec2::new(size * 0.4, size * 0.4),
0.0,
hollow,
))
.with_z(z),
);
}
EdgeEnds::Cross => {
let ox = ux * size * 0.5;
let oy = uy * size * 0.5;
let qx = px * size * 0.4;
let qy = py * size * 0.4;
let a1 = Point::new(tip.x - ox + qx, tip.y - oy + qy);
let a2 = Point::new(tip.x + ox - qx, tip.y + oy - qy);
let b1 = Point::new(tip.x - ox - qx, tip.y - oy - qy);
let b2 = Point::new(tip.x + ox + qx, tip.y + oy + qy);
out.push(SceneNode::new(Element::line(a1, a2, stroke.clone())).with_z(z));
out.push(SceneNode::new(Element::line(b1, b2, stroke.clone())).with_z(z));
}
_ => {}
}
out
}