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>;
#[inline]
fn sub_surface(&self, offset: Point, size: Size) -> SubSurface<&Self, T>
where
Self: Sized,
{
SubSurface::new(self, offset, size)
}
#[inline]
fn offset_surface(&self, offset: Point) -> SubSurface<&Self, T>
where
Self: Sized,
{
let sur_size = self.surface_size();
SubSurface::new(
self,
offset,
size(sur_size.x - offset.x, sur_size.y - offset.y),
)
}
#[inline]
fn sub_surface_mut(&mut self, offset: Point, size: Size) -> SubSurface<&mut Self, T>
where
Self: Sized,
{
SubSurface::new(self, offset, size)
}
#[inline]
fn offset_surface_mut(&mut self, offset: Point) -> SubSurface<&mut Self, T>
where
Self: Sized,
{
let sur_size = self.surface_size();
SubSurface::new(
self,
offset,
size(sur_size.x - offset.x, sur_size.y - offset.y),
)
}
#[inline]
fn into_sub_surface(self, offset: Point, size: Size) -> SubSurface<Self, T>
where
Self: Sized,
{
SubSurface::new(self, offset, size)
}
#[inline]
fn into_offset_surface(self, offset: Point) -> SubSurface<Self, T>
where
Self: Sized,
{
let sur_size = self.surface_size();
SubSurface::new(
self,
offset,
size(sur_size.x - offset.x, sur_size.y - offset.y),
)
}
}
impl<S, T> Surface<T> for &S
where
S: Surface<T>,
{
#[inline]
fn surface_size(&self) -> Size {
(**self).surface_size()
}
#[inline]
fn surface_get(&self, pt: Point) -> Option<&T> {
(**self).surface_get(pt)
}
}
impl<S, T> Surface<T> for &mut S
where
S: Surface<T>,
{
#[inline]
fn surface_size(&self) -> Size {
(**self).surface_size()
}
#[inline]
fn surface_get(&self, pt: Point) -> Option<&T> {
(**self).surface_get(pt)
}
}
pub trait SurfaceMut<T>: Surface<T> {
fn surface_get_mut(&mut self, pt: Point) -> Option<&mut T>;
}
impl<S, T> SurfaceMut<T> for &mut S
where
S: SurfaceMut<T>,
{
#[inline]
fn surface_get_mut(&mut self, pt: Point) -> Option<&mut T> {
(*self).surface_get_mut(pt)
}
}
#[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,
}
}
#[inline]
pub fn into_inner(self) -> Slice {
self.slice
}
}
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 }
}
#[inline]
pub fn value(&self) -> &T {
&self.value
}
#[inline]
pub fn value_mut(&mut self) -> &mut T {
&mut self.value
}
#[inline]
pub fn into_inner(self) -> T {
self.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)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SubSurface<S, Item> {
surface: S,
offset: Point,
size: Size,
ghost: PhantomData<Item>,
}
impl<S, Item> SubSurface<S, Item> {
#[inline]
pub fn offset(&self) -> Point {
self.offset
}
#[inline]
pub fn size(&self) -> Size {
self.size
}
#[inline]
pub fn inner(&self) -> &S {
&self.surface
}
#[inline]
pub fn inner_mut(&mut self) -> &mut S {
&mut self.surface
}
#[inline]
pub fn into_inner(self) -> S {
self.surface
}
}
impl<S, Item> SubSurface<S, Item>
where
S: Surface<Item>,
{
#[inline]
pub fn new(surface: S, offset: Point, size: Size) -> Self {
Self {
surface,
offset,
size,
ghost: PhantomData,
}
}
}
impl<S, Item> Surface<Item> for SubSurface<S, Item>
where
S: Surface<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 {
self.surface
.surface_get(point(pt.x + self.offset.x, pt.y + self.offset.y))
} else {
None
}
}
}
impl<S, Item> SurfaceMut<Item> for SubSurface<S, Item>
where
S: SurfaceMut<Item>,
{
#[inline]
fn surface_get_mut(&mut self, pt: Point) -> Option<&mut Item> {
if pt.x < self.size.x && pt.y < self.size.y {
self.surface
.surface_get_mut(point(pt.x + self.offset.x, pt.y + self.offset.y))
} else {
None
}
}
}