tek/crates/tek_core/src/space.rs

117 lines
2.1 KiB
Rust

use crate::*;
pub trait Point<N: Number> {
fn x (&self) -> N;
fn y (&self) -> N;
fn w (&self) -> N {
self.x()
}
fn h (&self) -> N {
self.y()
}
}
impl<N: Number> Point<N> for (N, N) {
fn x (&self) -> N {
self.0
}
fn y (&self) -> N {
self.1
}
}
impl<N: Number> Point<N> for [N;2] {
fn x (&self) -> N {
self[0]
}
fn y (&self) -> N {
self[1]
}
}
pub trait Area<N: Number> {
fn x (&self) -> N;
fn y (&self) -> N;
fn w (&self) -> N;
fn h (&self) -> N;
fn x2 (&self) -> N {
self.x() + self.w()
}
fn y2 (&self) -> N {
self.y() + self.h()
}
}
impl<N: Number> Area<N> for (N, N, N, N) {
fn x (&self) -> N {
self.0
}
fn y (&self) -> N {
self.1
}
fn w (&self) -> N {
self.2
}
fn h (&self) -> N {
self.3
}
}
impl<N: Number> Area<N> for [N;4] {
fn x (&self) -> N {
self[0]
}
fn y (&self) -> N {
self[1]
}
fn w (&self) -> N {
self[2]
}
fn h (&self) -> N {
self[3]
}
}
macro_rules! impl_axis_common { ($A:ident $T:ty) => {
impl $A<$T> {
pub fn start_inc (&mut self) -> $T {
self.start += 1;
self.start
}
pub fn start_dec (&mut self) -> $T {
self.start = self.start.saturating_sub(1);
self.start
}
pub fn point_inc (&mut self) -> Option<$T> {
self.point = self.point.map(|p|p + 1);
self.point
}
pub fn point_dec (&mut self) -> Option<$T> {
self.point = self.point.map(|p|p.saturating_sub(1));
self.point
}
}
} }
pub struct FixedAxis<T> {
pub start: T,
pub point: Option<T>
}
impl_axis_common!(FixedAxis u16);
impl_axis_common!(FixedAxis usize);
pub struct ScaledAxis<T> {
pub start: T,
pub scale: T,
pub point: Option<T>
}
impl_axis_common!(ScaledAxis u16);
impl_axis_common!(ScaledAxis usize);
impl<T: Copy> ScaledAxis<T> {
pub fn scale_mut (&mut self, cb: &impl Fn(T)->T) {
self.scale = cb(self.scale)
}
}