pub type Coord<T> = geo::Coord<T>;
pub type Point<T> = geo::Point<T>;
pub type IPos = Point<isize>;
pub type UPos = Point<usize>;
pub type U16Pos = Point<u16>;
pub type Rect<T> = geo::Rect<T>;
pub type IRect = Rect<isize>;
pub type URect = Rect<usize>;
pub type U16Rect = Rect<u16>;
pub trait RectExt<T>
where
T: geo::CoordNum,
{
fn size(&self) -> Size<T>;
}
impl<T> RectExt<T> for Rect<T>
where
T: geo::CoordNum,
{
fn size(&self) -> Size<T> {
Size::new(self.width(), self.height())
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Size<T>
where
T: geo::CoordNum,
{
width: T,
height: T,
}
pub type ISize = Size<isize>;
pub type USize = Size<usize>;
pub type U16Size = Size<u16>;
impl<T> Size<T>
where
T: geo::CoordNum,
{
pub fn new(width: T, height: T) -> Self {
Self { width, height }
}
pub fn width(&self) -> T {
self.width
}
pub fn height(&self) -> T {
self.height
}
pub fn is_zero(&self) -> bool {
self.width == T::from(0).unwrap() || self.height == T::from(0).unwrap()
}
}
#[macro_export]
macro_rules! point {
($x:expr,$y:expr) => {
geo::point!(x: $x, y: $y)
};
}
#[macro_export]
macro_rules! point_as {
($p:ident,$ty:ty) => {
geo::point!(x: $p.x() as $ty, y: $p.y() as $ty)
};
}
#[macro_export]
macro_rules! rect {
($left:expr, $top:expr, $right:expr, $bottom:expr) => {
$crate::coord::Rect::new(($left, $top), ($right, $bottom))
};
(($left:expr, $top:expr),($right:expr, $bottom:expr)) => {
$crate::coord::Rect::new(($left, $top), ($right, $bottom))
};
($min:expr, $max:expr) => {
$crate::coord::Rect::new($min, $max)
};
}
#[macro_export]
macro_rules! rect_as {
($r:ident, $ty:ty) => {
$crate::coord::Rect::new(
($r.min().x as $ty, $r.min().y as $ty),
($r.max().x as $ty, $r.max().y as $ty),
) as $crate::coord::Rect<$ty>
};
}
#[macro_export]
macro_rules! size {
($width:expr, $height:expr) => {
$crate::coord::Size::new($width, $height)
};
}
#[macro_export]
macro_rules! size_as {
($s:ident, $ty:ty) => {
$crate::coord::Size::new($s.width() as $ty, $s.height() as $ty)
as $crate::coord::Size<$ty>
};
}
#[macro_export]
macro_rules! rect_from_size {
($s:ident) => {
$crate::coord::Rect::new((0, 0), ($s.width(), $s.height()))
};
}
#[macro_export]
macro_rules! rect_from_layout {
($l:ident) => {
$crate::coord::Rect::new(
($l.location.x as isize, $l.location.y as isize),
(
($l.location.x + $l.size.width) as isize,
($l.location.y + $l.size.height) as isize,
),
)
};
}