use crate::colour::Colour;
use crate::overlay::*;
#[derive(Debug, Clone, PartialEq)]
pub enum OverlayMeshColours {
Uniform(Colour),
PerVertex(Vec<Colour>),
}
impl Default for OverlayMeshColours {
fn default() -> Self {
OverlayMeshColours::Uniform(Colour::WHITE)
}
}
impl OverlayMeshColours {
pub fn at(&self, i: usize, len: usize) -> Colour {
match self {
OverlayMeshColours::Uniform(c) => *c,
OverlayMeshColours::PerVertex(list) if list.len() == len => list[i],
OverlayMeshColours::PerVertex(_) => Colour::WHITE,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct OverlayMeshItem {
pub positions: Vec<[f32; 2]>,
pub indices: Vec<u32>,
pub colours: OverlayMeshColours,
pub uvs: Option<Vec<[f32; 2]>>,
pub anchoring: OverlayAnchoring,
pub transform: OverlayTransform,
pub style: OverlayStyle,
pub animations: Option<Box<OverlayAnimations>>,
pub clip: OverlayClip,
pub z_order: i32,
}
impl Default for OverlayMeshItem {
fn default() -> Self {
Self {
positions: Vec::new(),
indices: Vec::new(),
colours: OverlayMeshColours::default(),
uvs: None,
anchoring: OverlayAnchoring::default(),
transform: OverlayTransform::IDENTITY,
style: OverlayStyle::solid(Colour::WHITE),
animations: None,
clip: OverlayClip::default(),
z_order: 0,
}
}
}
impl OverlayMeshItem {
pub fn new(positions: Vec<[f32; 2]>, indices: Vec<u32>) -> Self {
Self {
positions,
indices,
..Default::default()
}
}
pub fn with_colour(mut self, colour: impl Into<Colour>) -> Self {
self.colours = OverlayMeshColours::Uniform(colour.into());
self
}
pub fn with_colours(mut self, colours: Vec<Colour>) -> Self {
self.colours = OverlayMeshColours::PerVertex(colours);
self
}
pub fn with_uvs(mut self, uvs: Vec<[f32; 2]>) -> Self {
self.uvs = Some(uvs);
self
}
pub fn with_fill(mut self, fill: OverlayFill) -> Self {
self.style.fill = fill;
self
}
pub fn with_style(mut self, style: OverlayStyle) -> Self {
self.style = style;
self
}
pub fn with_position(mut self, position: [f32; 2]) -> Self {
self.transform.translate = position;
self
}
pub fn with_transform(mut self, transform: OverlayTransform) -> Self {
self.transform = transform;
self
}
pub fn with_anchor(mut self, anchor: OverlayOrigin) -> Self {
self.anchoring.origin = anchor;
self
}
pub fn with_opacity(mut self, opacity: f32) -> Self {
self.style.opacity = opacity;
self
}
pub fn with_blend(mut self, blend: 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: OverlayClip) -> Self {
self.clip = clip;
self
}
pub fn with_animations(mut self, animations: OverlayAnimations) -> Self {
self.animations = Some(Box::new(animations));
self
}
pub fn bounds(&self) -> Option<([f32; 2], [f32; 2])> {
let (first, rest) = self.positions.split_first()?;
let (mut min, mut max) = (*first, *first);
for p in rest {
min = [min[0].min(p[0]), min[1].min(p[1])];
max = [max[0].max(p[0]), max[1].max(p[1])];
}
Some((min, max))
}
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 (min, max) = self.bounds().unwrap_or(([0.0; 2], [0.0; 2]));
Some([
origin[0]
+ self.transform.translate[0]
+ self.anchoring.align.x.align_shift(max[0] - min[0]),
origin[1]
+ self.transform.translate[1]
+ self.anchoring.align.y.align_shift(max[1] - min[1], None),
])
}
pub fn contains(&self, point: [f32; 2]) -> bool {
let Some((min, max)) = self.bounds() else {
return false;
};
let Some(inv) = self.transform.inverse_linear() else {
return false;
};
let t = self.transform.translate;
let piv = [
t[0] + (min[0] + max[0]) * 0.5 + self.transform.pivot[0],
t[1] + (min[1] + max[1]) * 0.5 + self.transform.pivot[1],
];
let d = [point[0] - piv[0], point[1] - piv[1]];
let q = [
piv[0] + inv[0][0] * d[0] + inv[0][1] * d[1] - t[0],
piv[1] + inv[1][0] * d[0] + inv[1][1] * d[1] - t[1],
];
let cross = |a: [f32; 2], b: [f32; 2], p: [f32; 2]| {
(b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0])
};
self.indices.chunks_exact(3).any(|tri| {
let get = |i: u32| self.positions.get(i as usize).copied();
let (Some(a), Some(b), Some(c)) = (get(tri[0]), get(tri[1]), get(tri[2])) else {
return false;
};
let d0 = cross(a, b, q);
let d1 = cross(b, c, q);
let d2 = cross(c, a, q);
let neg = d0 < 0.0 || d1 < 0.0 || d2 < 0.0;
let pos = d0 > 0.0 || d1 > 0.0 || d2 > 0.0;
!(neg && pos)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
fn quad() -> OverlayMeshItem {
OverlayMeshItem::new(
vec![[0.0, 0.0], [10.0, 0.0], [10.0, 10.0], [0.0, 10.0]],
vec![0, 1, 2, 0, 2, 3],
)
}
#[test]
fn contains_tests_each_triangle() {
let m = quad().with_position([100.0, 50.0]);
assert!(m.contains([105.0, 55.0]));
assert!(m.contains([110.0, 60.0]));
assert!(!m.contains([95.0, 55.0]));
assert!(!m.contains([105.0, 61.0]));
}
#[test]
fn contains_follows_the_transform() {
let strip = OverlayMeshItem::new(
vec![[0.0, 4.0], [10.0, 4.0], [10.0, 6.0], [0.0, 6.0]],
vec![0, 1, 2, 0, 2, 3],
)
.with_transform(OverlayTransform::IDENTITY.with_rotation(std::f32::consts::FRAC_PI_2));
assert!(strip.contains([5.0, 1.0]));
assert!(!strip.contains([1.0, 5.0]));
}
#[test]
fn a_partial_or_out_of_range_triangle_is_skipped() {
let m = OverlayMeshItem::new(vec![[0.0, 0.0], [10.0, 0.0], [0.0, 10.0]], vec![0, 1, 7, 0]);
assert!(!m.contains([1.0, 1.0]));
}
}