mod point;
mod size;
pub use point::Point;
pub use size::Size;
use crate::primitives::Rectangle;
pub trait Dimensions {
fn bounding_box(&self) -> Rectangle;
}
pub trait OriginDimensions {
fn size(&self) -> Size;
}
impl<T> Dimensions for T
where
T: OriginDimensions,
{
fn bounding_box(&self) -> Rectangle {
Rectangle::new(Point::zero(), self.size())
}
}
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
pub enum AnchorPoint {
TopLeft,
TopCenter,
TopRight,
CenterLeft,
Center,
CenterRight,
BottomLeft,
BottomCenter,
BottomRight,
}
impl AnchorPoint {
pub fn from_xy(x: AnchorX, y: AnchorY) -> Self {
match (y, x) {
(AnchorY::Top, AnchorX::Left) => AnchorPoint::TopLeft,
(AnchorY::Top, AnchorX::Center) => AnchorPoint::TopCenter,
(AnchorY::Top, AnchorX::Right) => AnchorPoint::TopRight,
(AnchorY::Center, AnchorX::Left) => AnchorPoint::CenterLeft,
(AnchorY::Center, AnchorX::Center) => AnchorPoint::Center,
(AnchorY::Center, AnchorX::Right) => AnchorPoint::CenterRight,
(AnchorY::Bottom, AnchorX::Left) => AnchorPoint::BottomLeft,
(AnchorY::Bottom, AnchorX::Center) => AnchorPoint::BottomCenter,
(AnchorY::Bottom, AnchorX::Right) => AnchorPoint::BottomRight,
}
}
pub fn x(self) -> AnchorX {
match self {
AnchorPoint::TopLeft | AnchorPoint::CenterLeft | AnchorPoint::BottomLeft => {
AnchorX::Left
}
AnchorPoint::TopCenter | AnchorPoint::Center | AnchorPoint::BottomCenter => {
AnchorX::Center
}
AnchorPoint::TopRight | AnchorPoint::CenterRight | AnchorPoint::BottomRight => {
AnchorX::Right
}
}
}
pub fn y(self) -> AnchorY {
match self {
AnchorPoint::TopLeft | AnchorPoint::TopCenter | AnchorPoint::TopRight => AnchorY::Top,
AnchorPoint::CenterLeft | AnchorPoint::Center | AnchorPoint::CenterRight => {
AnchorY::Center
}
AnchorPoint::BottomLeft | AnchorPoint::BottomCenter | AnchorPoint::BottomRight => {
AnchorY::Bottom
}
}
}
}
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
pub enum AnchorX {
Left,
Center,
Right,
}
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
pub enum AnchorY {
Top,
Center,
Bottom,
}
#[cfg(test)]
mod tests {
use super::*;
#[rustfmt::skip]
const ANCHOR_TESTS: &[((AnchorY, AnchorX), AnchorPoint)] = &[
((AnchorY::Top, AnchorX::Left), AnchorPoint::TopLeft),
((AnchorY::Top, AnchorX::Center), AnchorPoint::TopCenter),
((AnchorY::Top, AnchorX::Right), AnchorPoint::TopRight),
((AnchorY::Center, AnchorX::Left), AnchorPoint::CenterLeft),
((AnchorY::Center, AnchorX::Center), AnchorPoint::Center),
((AnchorY::Center, AnchorX::Right), AnchorPoint::CenterRight),
((AnchorY::Bottom, AnchorX::Left), AnchorPoint::BottomLeft),
((AnchorY::Bottom, AnchorX::Center), AnchorPoint::BottomCenter),
((AnchorY::Bottom, AnchorX::Right), AnchorPoint::BottomRight),
];
#[test]
fn anchor_conversion() {
for ((y, x), p) in ANCHOR_TESTS.iter().copied() {
assert_eq!(p.x(), x);
assert_eq!(p.y(), y);
assert_eq!(AnchorPoint::from_xy(x, y), p);
}
}
}