use crate::colour::Colour;
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,
Round,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LineCap {
#[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, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct OverlayStroke {
pub width: f32,
pub colour: crate::colour::Colour,
pub pattern: StrokePattern,
pub join: LineJoin,
pub mitre_limit: f32,
pub cap: LineCap,
}
impl Default for OverlayStroke {
fn default() -> Self {
Self {
width: 2.0,
colour: Colour::linear(1.0, 1.0, 1.0, 1.0),
pattern: StrokePattern::Solid,
join: LineJoin::Mitre,
mitre_limit: 4.0,
cap: LineCap::Butt,
}
}
}
impl OverlayStroke {
pub fn new(width: f32, colour: impl Into<crate::colour::Colour>) -> Self {
Self {
width,
colour: colour.into(),
..Default::default()
}
}
pub fn hairline(colour: impl Into<crate::colour::Colour>) -> Self {
Self::new(0.0, colour)
}
pub fn with_pattern(mut self, pattern: StrokePattern) -> Self {
self.pattern = pattern;
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: LineCap) -> Self {
self.cap = cap;
self
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct OverlayPolylineItem {
pub points: Vec<[f32; 2]>,
pub anchoring: OverlayAnchoring,
pub transform: OverlayTransform,
pub style: OverlayStyle,
pub animations: Option<Box<OverlayAnimations>>,
pub clip: OverlayClip,
pub stroke: Option<OverlayStroke>,
pub widths: Option<Vec<f32>>,
pub closed: bool,
pub uvs: Option<Vec<[f32; 2]>>,
pub z_order: i32,
}
impl Default for OverlayPolylineItem {
fn default() -> Self {
Self {
points: Vec::new(),
anchoring: crate::overlay::OverlayAnchoring::default(),
transform: OverlayTransform::IDENTITY,
style: OverlayStyle::default(),
animations: None,
clip: OverlayClip::default(),
stroke: Some(OverlayStroke::default()),
widths: None,
closed: false,
uvs: None,
z_order: 0,
}
}
}
impl OverlayPolylineItem {
pub fn new(points: Vec<[f32; 2]>) -> Self {
Self {
points,
..Default::default()
}
}
pub fn with_anchor(mut self, anchor: OverlayOrigin) -> Self {
self.anchoring.origin = anchor;
self
}
pub fn with_world_anchor(mut self, pos: impl crate::scene::IntoWorldPosition) -> Self {
self.anchoring.origin = OverlayOrigin::World(pos.into_world_position());
self
}
pub fn with_position(mut self, position: [f32; 2]) -> Self {
self.transform.translate = position;
self
}
pub fn with_align(mut self, align: Alignment) -> Self {
self.anchoring.align = align;
self
}
pub fn resolve_offset(
&self,
viewport_size: [f32; 2],
view: &glam::Mat4,
proj: &glam::Mat4,
) -> Option<[f32; 2]> {
self.resolve_offset_at(viewport_size, view, proj, glam::DVec3::ZERO)
}
pub fn resolve_offset_at(
&self,
viewport_size: [f32; 2],
view: &glam::Mat4,
proj: &glam::Mat4,
world_origin: glam::DVec3,
) -> Option<[f32; 2]> {
let origin = crate::overlay::resolve_anchor_origin_at(
&self.anchoring.origin,
viewport_size,
view,
proj,
world_origin,
)?;
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.transform.translate[0]
+ self.anchoring.align.x.align_shift(max_x - min_x),
origin[1]
+ self.transform.translate[1]
+ self.anchoring.align.y.align_shift(max_y - min_y, None),
])
}
pub fn with_style(mut self, style: crate::overlay::OverlayStyle) -> Self {
self.style = style;
self
}
pub fn with_inner_shadows(mut self, shadows: Vec<crate::overlay::ShadowLayer>) -> Self {
self.style.inner_shadows = shadows;
self
}
pub fn with_inner_shadow(mut self, shadow: crate::overlay::ShadowLayer) -> Self {
self.style.inner_shadows.push(shadow);
self
}
pub fn with_screen_anchor(mut self, pos: [f32; 2]) -> Self {
self.anchoring =
crate::overlay::OverlayAnchoring::default().with_align(self.anchoring.align);
self.transform.translate = pos;
self
}
pub fn with_stroke(mut self, stroke: OverlayStroke) -> Self {
self.stroke = Some(stroke);
self
}
pub fn without_stroke(mut self) -> Self {
self.stroke = None;
self
}
fn stroke_mut(&mut self) -> &mut OverlayStroke {
self.stroke.get_or_insert_with(OverlayStroke::default)
}
pub fn with_widths(mut self, widths: Vec<f32>) -> Self {
self.widths = Some(widths);
self
}
pub fn with_thickness(mut self, thickness: f32) -> Self {
self.stroke_mut().width = thickness;
self
}
pub fn with_colour(mut self, colour: impl Into<crate::colour::Colour>) -> Self {
self.stroke_mut().colour = colour.into();
self
}
pub fn with_join(mut self, join: LineJoin) -> Self {
self.stroke_mut().join = join;
self
}
pub fn with_mitre_limit(mut self, mitre_limit: f32) -> Self {
self.stroke_mut().mitre_limit = mitre_limit;
self
}
pub fn with_cap(mut self, cap: LineCap) -> Self {
self.stroke_mut().cap = cap;
self
}
pub fn with_stroke_pattern(mut self, stroke_pattern: StrokePattern) -> Self {
self.stroke_mut().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.style.fill = fill;
self
}
pub fn with_texture(mut self, texture: OverlayTextureId) -> Self {
self.style.fill = OverlayFill::texture(texture);
self
}
pub fn with_uvs(mut self, uvs: Vec<[f32; 2]>) -> Self {
self.uvs = Some(uvs);
self
}
pub fn with_opacity(mut self, opacity: f32) -> Self {
self.style.opacity = opacity;
self
}
pub fn with_blend(mut self, blend: crate::overlay::OverlayBlend) -> Self {
self.style.blend = blend;
self
}
pub fn with_z_order(mut self, z_order: i32) -> Self {
self.z_order = z_order;
self
}
pub fn with_clip(mut self, clip_id: u32) -> Self {
self.clip.mask = Some(clip_id);
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),
stroke: Some(OverlayStroke::new(thickness, colour)),
..Default::default()
}
}
pub fn closed_from_path(
path: impl Fn(f32) -> [f32; 2],
samples: u32,
fill: OverlayFill,
stroke_colour: impl Into<crate::colour::Colour>,
thickness: f32,
) -> Self {
Self {
points: sample_closed_path(path, samples),
stroke: Some(OverlayStroke::new(thickness, stroke_colour)),
closed: true,
style: OverlayStyle {
fill,
..Default::default()
},
..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)
};
}
pub fn with_shadows(mut self, shadows: Vec<crate::overlay::ShadowLayer>) -> Self {
self.style.shadows = shadows;
self
}
pub fn with_shadow(mut self, shadow: crate::overlay::ShadowLayer) -> Self {
self.style.shadows.push(shadow);
self
}
pub fn with_outline(
mut self,
colour: impl Into<crate::colour::Colour>,
width: f32,
mode: crate::overlay::OutlineMode,
) -> Self {
if width <= 0.0 {
return self;
}
let colour = colour.into();
let band = |spread: f32| {
crate::overlay::ShadowLayer::new(colour, 0.0, [0.0, 0.0]).with_spread(spread)
};
match mode {
crate::overlay::OutlineMode::Inset => self.style.inner_shadows.push(band(width)),
crate::overlay::OutlineMode::Outer => self.style.shadows.push(band(width)),
crate::overlay::OutlineMode::Centre => {
self.style.inner_shadows.push(band(width * 0.5));
self.style.shadows.push(band(width * 0.5));
}
}
self
}
pub fn with_transform(mut self, transform: OverlayTransform) -> Self {
self.transform = transform;
self
}
pub fn with_rotation(mut self, radians: f32) -> Self {
self.transform.rotation = radians;
self
}
pub fn with_rotation_pivot(mut self, pivot: [f32; 2]) -> Self {
self.transform.pivot = pivot;
self
}
pub fn with_scale(mut self, scale: f32) -> Self {
self.transform.scale = scale;
self
}
pub fn with_tint(mut self, tint: [f32; 4]) -> Self {
self.style.tint = tint;
self
}
pub fn with_animations(mut self, animations: OverlayAnimations) -> Self {
self.animations = Some(Box::new(animations));
self
}
pub fn with_clip_rect(mut self, clip_rect: [f32; 4]) -> Self {
self.clip.rect = Some(clip_rect);
self
}
}
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()
}
#[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, Colour::WHITE);
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 = OverlayFill::Solid(Colour::linear(0.2, 0.4, 0.6, 1.0));
let item =
OverlayPolylineItem::closed_from_path(circle, 4, fill.clone(), Colour::WHITE, 3.0);
assert!(item.closed);
assert_eq!(item.style.fill, fill);
assert_eq!(item.stroke.as_ref().unwrap().width, 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(OverlayOrigin::Viewport(Alignment::new(
AnchorX::Right,
AnchorY::Bottom,
)))
.with_align(Alignment::new(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);
}
}