use crate::Color;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[repr(C)]
pub struct Vertex {
pub pos: [f32; 2],
pub uv: [f32; 2],
pub color: Color,
}
const _: () = assert!(size_of::<Vertex>() == 20);
impl Vertex {
#[must_use]
pub const fn new(pos: [f32; 2], uv: [f32; 2], color: Color) -> Self {
Self { pos, uv, color }
}
pub(crate) fn non_finite_field(self) -> Option<crate::VertexField> {
if !self.pos.iter().all(|value| value.is_finite()) {
Some(crate::VertexField::Position)
} else if !self.uv.iter().all(|value| value.is_finite()) {
Some(crate::VertexField::Uv)
} else {
None
}
}
pub(crate) const fn pos2(&self) -> Pos2 {
pos2(self.pos[0], self.pos[1])
}
pub(crate) const fn uv2(&self) -> Pos2 {
pos2(self.uv[0], self.uv[1])
}
#[cfg(all(test, feature = "egui"))]
pub(crate) const fn to_egui(self) -> egui::epaint::Vertex {
egui::epaint::Vertex {
pos: egui::pos2(self.pos[0], self.pos[1]),
uv: egui::pos2(self.uv[0], self.uv[1]),
color: self.color.to_egui(),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct Pos2 {
pub(crate) x: f32,
pub(crate) y: f32,
}
impl Pos2 {
pub(crate) const ZERO: Self = pos2(0.0, 0.0);
}
pub(crate) const fn pos2(x: f32, y: f32) -> Pos2 {
Pos2 { x, y }
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct Vec2 {
pub(crate) x: f32,
pub(crate) y: f32,
}
pub(crate) const fn vec2(x: f32, y: f32) -> Vec2 {
Vec2 { x, y }
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct RasterMesh<'a> {
pub(crate) vertices: &'a [Vertex],
pub(crate) indices: &'a [u32],
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Rect {
pub min: [f32; 2],
pub max: [f32; 2],
}
impl Rect {
pub const FULL_UV: Self = Self::from_min_max([0.0, 0.0], [1.0, 1.0]);
#[must_use]
pub const fn from_min_max(min: [f32; 2], max: [f32; 2]) -> Self {
Self { min, max }
}
#[must_use]
pub fn from_min_size(min: [f32; 2], size: [f32; 2]) -> Self {
Self::from_min_max(min, [min[0] + size[0], min[1] + size[1]])
}
pub(crate) fn is_finite(self) -> bool {
self.min
.iter()
.chain(&self.max)
.all(|value| value.is_finite())
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ClipRect {
pub min_x: u32,
pub min_y: u32,
pub max_x: u32,
pub max_y: u32,
}
impl ClipRect {
pub const ALL: Self = Self::new(0, 0, u32::MAX, u32::MAX);
#[must_use]
pub const fn new(min_x: u32, min_y: u32, max_x: u32, max_y: u32) -> Self {
Self {
min_x,
min_y,
max_x,
max_y,
}
}
pub(crate) fn to_bounds(self, width: usize, height: usize) -> crate::raster::ClipBounds {
let clamp = |value: u32, max: usize| usize::try_from(value).map_or(max, |v| v.min(max));
crate::raster::ClipBounds {
min_x: clamp(self.min_x, width),
min_y: clamp(self.min_y, height),
max_x: clamp(self.max_x, width),
max_y: clamp(self.max_y, height),
}
}
}