use core::{
marker::PhantomData,
ops::{Deref, DerefMut, Index, IndexMut},
};
pub type Point = mint::Point2<u32>;
pub type Size = mint::Vector2<u32>;
#[inline]
pub const fn point(x: u32, y: u32) -> Point {
Point { x, y }
}
#[inline]
pub const fn size(x: u32, y: u32) -> Size {
Size { x, y }
}
pub trait Surface<T> {
fn surface_size(&self) -> Size;
fn surface_get(&self, pt: Point) -> Option<&T>;
}
pub trait SurfaceMut<T>: Surface<T> {
fn surface_get_mut(&mut self, pt: Point) -> Option<&mut T>;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GenericSurface<Slice, Item> {
slice: Slice,
size: Size,
ghost: PhantomData<Item>,
}
impl<Slice, Item> GenericSurface<Slice, Item>
where
Slice: AsRef<[Item]>,
{
#[inline]
pub fn new(slice: Slice, size: Size) -> Option<Self> {
if slice.as_ref().len() == (size.x * size.y) as _ {
Some(Self {
slice,
size,
ghost: PhantomData,
})
} else {
None
}
}
#[inline]
pub fn new_infer(slice: Slice, width: u32) -> Self {
Self {
size: size(width, slice.as_ref().len() as u32 / width),
slice,
ghost: PhantomData,
}
}
}
impl<Slice, Item> Deref for GenericSurface<Slice, Item>
where
Slice: AsRef<[Item]>,
{
type Target = [Item];
#[inline]
fn deref(&self) -> &Self::Target {
self.slice.as_ref()
}
}
impl<Slice, Item> DerefMut for GenericSurface<Slice, Item>
where
Slice: AsRef<[Item]> + AsMut<[Item]>,
{
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.slice.as_mut()
}
}
impl<Slice, Item> Surface<Item> for GenericSurface<Slice, Item>
where
Slice: AsRef<[Item]>,
{
#[inline]
fn surface_size(&self) -> Size {
self.size
}
#[inline]
fn surface_get(&self, pt: Point) -> Option<&Item> {
if pt.x < self.size.x && pt.y < self.size.y {
Some(
self.slice
.as_ref()
.index((pt.y * self.size.x + pt.x) as usize),
)
} else {
None
}
}
}
impl<Slice, Item> SurfaceMut<Item> for GenericSurface<Slice, Item>
where
Slice: AsRef<[Item]> + AsMut<[Item]>,
{
#[inline]
fn surface_get_mut(&mut self, pt: Point) -> Option<&mut Item> {
if pt.x < self.size.x && pt.y < self.size.y {
Some(
self.slice
.as_mut()
.index_mut((pt.y * self.size.x + pt.x) as usize),
)
} else {
None
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SingleValueSurface<T> {
pub size: Size,
pub value: T,
}
impl<T> SingleValueSurface<T> {
#[inline]
pub const fn new(value: T, size: Size) -> Self {
Self { size, value }
}
}
impl<T> Surface<T> for SingleValueSurface<T> {
#[inline]
fn surface_size(&self) -> Size {
self.size
}
#[inline]
fn surface_get(&self, _pt: Point) -> Option<&T> {
Some(&self.value)
}
}
impl<T> SurfaceMut<T> for SingleValueSurface<T> {
#[inline]
fn surface_get_mut(&mut self, _pt: Point) -> Option<&mut T> {
Some(&mut self.value)
}
}