use crate::overlay::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LineJoin {
#[default]
Mitre,
Bevel,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PolylineCap {
#[default]
Butt,
Square,
Round,
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum StrokePattern {
#[default]
Solid,
Dashed {
dash_length: f32,
gap_length: f32,
offset: f32,
},
Dotted {
spacing: f32,
offset: f32,
},
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct OverlayPolylineItem {
pub points: Vec<[f32; 2]>,
pub anchor: OverlayAnchor,
pub position: [f32; 2],
pub align_x: AnchorX,
pub align_y: AnchorY,
pub thickness: f32,
pub colour: crate::colour::Colour,
pub join: LineJoin,
pub mitre_limit: f32,
pub cap: PolylineCap,
pub stroke_pattern: StrokePattern,
pub closed: bool,
pub fill: Option<OverlayFill>,
pub texture: Option<OverlayTextureId>,
pub uvs: Option<Vec<[f32; 2]>>,
pub texture_transform: TextureTransform,
pub opacity: f32,
pub z_order: i32,
}
impl Default for OverlayPolylineItem {
fn default() -> Self {
Self {
points: Vec::new(),
anchor: OverlayAnchor::default(),
position: [0.0, 0.0],
align_x: AnchorX::Left,
align_y: AnchorY::Top,
thickness: 2.0,
colour: [1.0, 1.0, 1.0, 1.0].into(),
join: LineJoin::Mitre,
mitre_limit: 4.0,
cap: PolylineCap::Butt,
stroke_pattern: StrokePattern::Solid,
closed: false,
fill: None,
texture: None,
uvs: None,
texture_transform: TextureTransform::default(),
opacity: 1.0,
z_order: 0,
}
}
}
impl OverlayPolylineItem {
pub fn new(points: Vec<[f32; 2]>) -> Self {
Self {
points,
..Default::default()
}
}
pub fn with_anchor(mut self, anchor: OverlayAnchor) -> Self {
self.anchor = anchor;
self
}
pub fn with_world_anchor(mut self, pos: [f32; 3]) -> Self {
self.anchor = OverlayAnchor::World(pos);
self
}
pub fn with_position(mut self, position: [f32; 2]) -> Self {
self.position = position;
self
}
pub fn with_align(mut self, align_x: AnchorX, align_y: AnchorY) -> Self {
self.align_x = align_x;
self.align_y = align_y;
self
}
pub fn resolve_offset(
&self,
viewport_size: [f32; 2],
view: &glam::Mat4,
proj: &glam::Mat4,
) -> Option<[f32; 2]> {
let origin = resolve_anchor_origin(&self.anchor, viewport_size, view, proj)?;
let (mut min_x, mut min_y, mut max_x, mut max_y) = (0.0, 0.0, 0.0, 0.0);
if let Some((first, rest)) = self.points.split_first() {
min_x = first[0];
min_y = first[1];
max_x = first[0];
max_y = first[1];
for p in rest {
min_x = min_x.min(p[0]);
min_y = min_y.min(p[1]);
max_x = max_x.max(p[0]);
max_y = max_y.max(p[1]);
}
}
Some([
origin[0] + self.position[0] + self.align_x.align_shift(max_x - min_x),
origin[1] + self.position[1] + self.align_y.align_shift(max_y - min_y),
])
}
pub fn with_thickness(mut self, thickness: f32) -> Self {
self.thickness = thickness;
self
}
pub fn with_colour(mut self, colour: impl Into<crate::colour::Colour>) -> Self {
self.colour = colour.into();
self
}
pub fn with_join(mut self, join: LineJoin) -> Self {
self.join = join;
self
}
pub fn with_mitre_limit(mut self, mitre_limit: f32) -> Self {
self.mitre_limit = mitre_limit;
self
}
pub fn with_cap(mut self, cap: PolylineCap) -> Self {
self.cap = cap;
self
}
pub fn with_stroke_pattern(mut self, stroke_pattern: StrokePattern) -> Self {
self.stroke_pattern = stroke_pattern;
self
}
pub fn with_closed(mut self, closed: bool) -> Self {
self.closed = closed;
self
}
pub fn with_fill(mut self, fill: OverlayFill) -> Self {
self.fill = Some(fill);
self
}
pub fn with_texture(mut self, texture: OverlayTextureId) -> Self {
self.texture = Some(texture);
self
}
pub fn with_uvs(mut self, uvs: Vec<[f32; 2]>) -> Self {
self.uvs = Some(uvs);
self
}
pub fn with_texture_transform(mut self, texture_transform: TextureTransform) -> Self {
self.texture_transform = texture_transform;
self
}
pub fn with_opacity(mut self, opacity: f32) -> Self {
self.opacity = opacity;
self
}
pub fn with_z_order(mut self, z_order: i32) -> Self {
self.z_order = z_order;
self
}
pub fn from_path(
path: impl Fn(f32) -> [f32; 2],
samples: u32,
thickness: f32,
colour: impl Into<crate::colour::Colour>,
) -> Self {
Self {
points: sample_open_path(path, samples),
thickness,
colour: colour.into(),
..Default::default()
}
}
pub fn closed_from_path(
path: impl Fn(f32) -> [f32; 2],
samples: u32,
fill: Option<OverlayFill>,
stroke_colour: impl Into<crate::colour::Colour>,
thickness: f32,
) -> Self {
Self {
points: sample_closed_path(path, samples),
thickness,
colour: stroke_colour.into(),
closed: true,
fill,
..Default::default()
}
}
pub fn set_points_from_path(&mut self, path: impl Fn(f32) -> [f32; 2], samples: u32) {
self.points = if self.closed {
sample_closed_path(path, samples)
} else {
sample_open_path(path, samples)
};
}
}
fn sample_open_path(path: impl Fn(f32) -> [f32; 2], samples: u32) -> Vec<[f32; 2]> {
let n = samples.max(1);
(0..=n).map(|i| path(i as f32 / n as f32)).collect()
}
fn sample_closed_path(path: impl Fn(f32) -> [f32; 2], samples: u32) -> Vec<[f32; 2]> {
let n = samples.max(1);
let divisor = (n + 1) as f32;
(0..=n).map(|i| path(i as f32 / divisor)).collect()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LineCap {
#[default]
Round,
Square,
}
#[cfg(test)]
mod path_sample_tests {
use super::*;
fn circle(t: f32) -> [f32; 2] {
let a = t * std::f32::consts::TAU;
[a.cos(), a.sin()]
}
#[test]
fn from_path_samples_endpoint_inclusive() {
let item = OverlayPolylineItem::from_path(circle, 4, 2.0, [1.0; 4]);
assert_eq!(item.points.len(), 5);
assert!((item.points[0][0] - item.points[4][0]).abs() < 1e-5);
assert!((item.points[0][1] - item.points[4][1]).abs() < 1e-5);
}
#[test]
fn closed_from_path_skips_duplicate_endpoint() {
let fill = Some(OverlayFill::Solid([0.2, 0.4, 0.6, 1.0].into()));
let item = OverlayPolylineItem::closed_from_path(circle, 4, fill.clone(), [1.0; 4], 3.0);
assert!(item.closed);
assert_eq!(item.fill, fill);
assert_eq!(item.thickness, 3.0);
assert_eq!(item.points.len(), 5);
assert!((item.points[0][0] - item.points[4][0]).abs() > 1e-3);
let expected_last = circle(4.0 / 5.0);
assert!((item.points[4][0] - expected_last[0]).abs() < 1e-5);
}
#[test]
fn resolve_offset_default_is_zero() {
let p = OverlayPolylineItem::new(vec![[10.0, 20.0], [30.0, 40.0]]);
let off = p
.resolve_offset([800.0, 600.0], &glam::Mat4::IDENTITY, &glam::Mat4::IDENTITY)
.unwrap();
assert_eq!(off, [0.0, 0.0]);
}
#[test]
fn resolve_offset_pins_bottom_right() {
let p = OverlayPolylineItem::new(vec![[0.0, 0.0], [40.0, 20.0]])
.with_anchor(OverlayAnchor::Viewport {
x: AnchorX::Right,
y: AnchorY::Bottom,
})
.with_align(AnchorX::Right, AnchorY::Bottom);
let off = p
.resolve_offset([800.0, 600.0], &glam::Mat4::IDENTITY, &glam::Mat4::IDENTITY)
.unwrap();
assert_eq!(off, [760.0, 580.0]);
}
#[test]
fn set_points_from_path_follows_closed_flag() {
let mut open = OverlayPolylineItem {
closed: false,
..Default::default()
};
open.set_points_from_path(circle, 4);
assert!((open.points[0][0] - open.points[4][0]).abs() < 1e-5);
let mut closed = OverlayPolylineItem {
closed: true,
..Default::default()
};
closed.set_points_from_path(circle, 4);
assert!((closed.points[0][0] - closed.points[4][0]).abs() > 1e-3);
}
}