wip: generic layout!

This commit is contained in:
🪞👃🪞 2024-09-06 21:52:23 +03:00
parent 0bbf74e915
commit 93ba611e33
11 changed files with 267 additions and 82 deletions

View file

@ -1,3 +1,77 @@
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 Rectangle<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> Rectangle<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> Rectangle<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 {
@ -19,16 +93,25 @@ macro_rules! impl_axis_common { ($A:ident $T:ty) => {
}
} }
pub struct FixedAxis<T> { pub start: T, pub point: Option<T> }
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> }
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)
}
}