use lievisual::geometry::BezPath;
use lievisual::geometry::{Point, Rect};
use lievisual::text::TextStyle as LieTextStyle;
pub use lievisual::geometry::{Color, Point as GeoPoint, Rect as GeoRect, Transform};
pub use lievisual::scene::{
Element, Fill, FillStrokeStyle, GradientStop, LinearGradient, SceneNode, Stroke,
};
pub use lievisual::text::{FontStyle, TextAlign, TextBaseline, TextStyle};
pub const Z_BACKGROUND: i32 = 0;
pub const Z_GRID: i32 = 10;
pub const Z_SERIES: i32 = 20;
pub const Z_SERIES_FILL: i32 = 20;
pub const Z_SERIES_LINE: i32 = 21;
pub const Z_SERIES_POINT: i32 = 22;
pub const Z_AXIS: i32 = 30;
pub const Z_LABEL: i32 = 40;
pub const Z_TITLE: i32 = 50;
pub const Z_SUBGRAPH: i32 = 5;
pub const Z_SUBGRAPH_LABEL: i32 = 6;
pub fn text_style(
color: Color,
font_size: f64,
font_family: impl Into<String>,
align: TextAlign,
baseline: TextBaseline,
) -> TextStyle {
LieTextStyle::new(color, font_size, font_family)
.with_align(align)
.with_baseline(baseline)
}
pub fn stroke(color: Color, width: f64) -> Stroke {
Stroke::new(color, width)
}
pub fn dashed_stroke(color: Color, width: f64, dash_array: Vec<f64>) -> Stroke {
Stroke::dashed(color, width, dash_array)
}
pub fn fs_fill(color: Color) -> FillStrokeStyle {
FillStrokeStyle::fill(color)
}
pub fn fs_stroke(color: Color, width: f64) -> FillStrokeStyle {
FillStrokeStyle::stroke(color, width)
}
pub fn fs_both(fill: Color, stroke: Color, width: f64) -> FillStrokeStyle {
FillStrokeStyle {
fill: Some(fill.into()),
stroke: Some(Stroke::new(stroke, width)),
}
}
pub fn rect_node(rect: Rect, radius: Option<f64>, style: FillStrokeStyle, z: i32) -> SceneNode {
let el = match radius {
Some(r) => Element::rounded_rect(rect, r, style),
None => Element::rect(rect, style),
};
SceneNode::from(el).with_z(z)
}
pub fn circle_node(center: Point, radius: f64, style: FillStrokeStyle, z: i32) -> SceneNode {
SceneNode::from(Element::circle(center, radius, style)).with_z(z)
}
pub fn line_node(start: Point, end: Point, style: Stroke, z: i32) -> SceneNode {
SceneNode::from(Element::line(start, end, style)).with_z(z)
}
pub fn polyline_node(points: Vec<Point>, style: Stroke, z: i32) -> SceneNode {
SceneNode::from(Element::poly(points, style)).with_z(z)
}
pub fn path_node(path: BezPath, style: FillStrokeStyle, z: i32) -> SceneNode {
SceneNode::from(Element::Path {
path,
style,
closed: false,
})
.with_z(z)
}
fn smooth_curve(pts: &[Point]) -> BezPath {
let mut p = BezPath::new();
if pts.len() < 2 {
return p;
}
p.move_to((pts[0].x, pts[0].y));
if pts.len() == 2 {
let a = pts[0];
let b = pts[1];
let c1 = Point::new(a.x + (b.x - a.x) * 0.33, a.y + (b.y - a.y) * 0.5);
let c2 = Point::new(a.x + (b.x - a.x) * 0.67, a.y + (b.y - a.y) * 0.5);
p.curve_to((c1.x, c1.y), (c2.x, c2.y), (b.x, b.y));
return p;
}
if pts.windows(2).all(|w| is_axis_aligned(w[0], w[1])) {
smooth_orthogonal(&mut p, pts);
return p;
}
for i in 0..pts.len() - 1 {
let p0 = if i == 0 { pts[0] } else { pts[i - 1] };
let p1 = pts[i];
let p2 = pts[i + 1];
let p3 = if i + 2 < pts.len() {
pts[i + 2]
} else {
pts[i + 1]
};
let c1 = Point::new(p1.x + (p2.x - p0.x) / 6.0, p1.y + (p2.y - p0.y) / 6.0);
let c2 = Point::new(p2.x - (p3.x - p1.x) / 6.0, p2.y - (p3.y - p1.y) / 6.0);
p.curve_to((c1.x, c1.y), (c2.x, c2.y), (p2.x, p2.y));
}
p
}
fn is_axis_aligned(a: Point, b: Point) -> bool {
(a.x - b.x).abs() < 0.5 || (a.y - b.y).abs() < 0.5
}
fn smooth_orthogonal(p: &mut BezPath, pts: &[Point]) {
const K: f64 = 0.5523; const RADIUS: f64 = 10.0;
let n = pts.len();
let mut cur = pts[0];
for i in 1..n - 1 {
let a = pts[i - 1];
let b = pts[i];
let c = pts[i + 1];
let d_in = unit_dir(b, a);
let d_out = unit_dir(c, b);
let r = RADIUS.min((b.distance(a) * 0.5).min(b.distance(c) * 0.5));
let start = Point::new(b.x - d_in.x * r, b.y - d_in.y * r);
let end = Point::new(b.x + d_out.x * r, b.y + d_out.y * r);
p.line_to((start.x, start.y));
let c1 = Point::new(start.x - d_in.x * r * K, start.y - d_in.y * r * K);
let c2 = Point::new(end.x + d_out.x * r * K, end.y + d_out.y * r * K);
p.curve_to((c1.x, c1.y), (c2.x, c2.y), (end.x, end.y));
cur = end;
}
let last = pts[n - 1];
if cur.distance(last) > 1e-6 {
p.line_to((last.x, last.y));
}
}
fn unit_dir(to: Point, from: Point) -> Point {
let dx = to.x - from.x;
let dy = to.y - from.y;
let len = (dx * dx + dy * dy).sqrt().max(1e-6);
Point::new(dx / len, dy / len)
}
pub fn curved_edge_node(points: Vec<Point>, style: Stroke, z: i32) -> SceneNode {
let path = smooth_curve(&points);
let fs = FillStrokeStyle {
fill: None,
stroke: Some(style),
};
SceneNode::from(Element::Path {
path,
style: fs,
closed: false,
})
.with_z(z)
}
pub fn cubic_bezier_edge(
start: Point,
c1: Point,
c2: Point,
end: Point,
style: Stroke,
z: i32,
) -> SceneNode {
let mut path = BezPath::new();
path.move_to((start.x, start.y));
path.curve_to((c1.x, c1.y), (c2.x, c2.y), (end.x, end.y));
let fs = FillStrokeStyle {
fill: None,
stroke: Some(style),
};
SceneNode::from(Element::Path {
path,
style: fs,
closed: false,
})
.with_z(z)
}
pub fn gradient_path_node(
path: BezPath,
gradient: LinearGradient,
stroke: Option<Stroke>,
z: i32,
) -> SceneNode {
SceneNode::from(Element::GradientPath {
path,
gradient,
stroke,
})
.with_z(z)
}
pub fn text_node(
content: impl Into<String>,
position: Point,
style: TextStyle,
rotation: f64,
max_width: Option<f64>,
z: i32,
) -> SceneNode {
let mut s = style;
if rotation != 0.0 {
s.rotation = rotation;
}
if max_width.is_some() {
s.max_width = max_width;
}
SceneNode::from(Element::text(content, position, s)).with_z(z)
}
pub fn group_node(children: Vec<SceneNode>, transform: Option<Transform>, z: i32) -> SceneNode {
let mut n = SceneNode::new(Element::Group { children });
if let Some(t) = transform {
n = n.with_transform(t);
}
n.with_z(z)
}
pub fn draw_arrow_head(
elements: &mut Vec<SceneNode>,
tip: &Point,
dir: &Point,
style: &Stroke,
filled: bool,
) {
let sz = 10.0;
let perp_x = -dir.y;
let perp_y = dir.x;
let base = Point::new(tip.x - dir.x * sz, tip.y - dir.y * sz);
let p1 = Point::new(base.x + perp_x * sz * 0.5, base.y + perp_y * sz * 0.5);
let p2 = Point::new(base.x - perp_x * sz * 0.5, base.y - perp_y * sz * 0.5);
let mut path = BezPath::new();
path.move_to(Point::new(tip.x, tip.y));
path.line_to(Point::new(p1.x, p1.y));
path.line_to(Point::new(p2.x, p2.y));
path.close_path();
let node_style = if filled {
fs_both(style.color, style.color, style.width)
} else {
fs_stroke(style.color, style.width)
};
elements.push(path_node(path, node_style, Z_AXIS));
}
pub fn draw_arrow_circle(elements: &mut Vec<SceneNode>, tip: &Point, style: &Stroke) {
let r = 5.0;
let mut path = BezPath::new();
let k = 0.5522847498;
path.move_to(Point::new(tip.x + r, tip.y));
path.curve_to(
Point::new(tip.x + r, tip.y + r * k),
Point::new(tip.x + r * k, tip.y + r),
Point::new(tip.x, tip.y + r),
);
path.curve_to(
Point::new(tip.x - r * k, tip.y + r),
Point::new(tip.x - r, tip.y + r * k),
Point::new(tip.x - r, tip.y),
);
path.curve_to(
Point::new(tip.x - r, tip.y - r * k),
Point::new(tip.x - r * k, tip.y - r),
Point::new(tip.x, tip.y - r),
);
path.curve_to(
Point::new(tip.x + r * k, tip.y - r),
Point::new(tip.x + r, tip.y - r * k),
Point::new(tip.x + r, tip.y),
);
path.close_path();
elements.push(path_node(
path,
fs_both(style.color, style.color, style.width),
Z_AXIS,
));
}
pub fn draw_arrow_cross(elements: &mut Vec<SceneNode>, tip: &Point, style: &Stroke) {
let sz = 5.0;
let mut path = BezPath::new();
path.move_to(Point::new(tip.x - sz, tip.y - sz));
path.line_to(Point::new(tip.x + sz, tip.y + sz));
path.move_to(Point::new(tip.x - sz, tip.y + sz));
path.line_to(Point::new(tip.x + sz, tip.y - sz));
elements.push(path_node(path, fs_stroke(style.color, style.width), Z_AXIS));
}
pub fn gradient_def(angle_deg: f64, stops: Vec<(f64, Color)>) -> LinearGradient {
let a = angle_deg.to_radians();
let dx = a.cos();
let dy = a.sin();
let half = 0.5;
let start = lievisual::geometry::Point::new(0.5 - dx * half, 0.5 - dy * half);
let end = lievisual::geometry::Point::new(0.5 + dx * half, 0.5 + dy * half);
LinearGradient {
start,
end,
stops: stops
.into_iter()
.map(|(offset, color)| GradientStop { offset, color })
.collect(),
}
}
pub use crate::builder::theme;