#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Rotation {
#[default]
Deg0,
Deg90,
Deg180,
Deg270,
}
impl Rotation {
pub const fn size(self, w: u32, h: u32) -> (u32, u32) {
match self {
Rotation::Deg0 | Rotation::Deg180 => (w, h),
Rotation::Deg90 | Rotation::Deg270 => (h, w),
}
}
pub const fn to_native(self, w: u32, h: u32, x: u32, y: u32) -> Option<(u32, u32)> {
let (lw, lh) = self.size(w, h);
if x >= lw || y >= lh {
return None;
}
Some(match self {
Rotation::Deg0 => (x, y),
Rotation::Deg90 => (w - 1 - y, x),
Rotation::Deg180 => (w - 1 - x, h - 1 - y),
Rotation::Deg270 => (y, h - 1 - x),
})
}
}
const _: () = assert!(matches!(Rotation::Deg0.size(400, 300), (400, 300)));
const _: () = assert!(matches!(Rotation::Deg90.size(400, 300), (300, 400)));
const _: () = assert!(matches!(Rotation::Deg180.size(400, 300), (400, 300)));
const _: () = assert!(matches!(Rotation::Deg270.size(400, 300), (300, 400)));
const _: () = assert!(matches!(
Rotation::Deg0.to_native(400, 300, 0, 0),
Some((0, 0))
));
const _: () = assert!(matches!(
Rotation::Deg90.to_native(400, 300, 0, 0),
Some((399, 0))
));
const _: () = assert!(matches!(
Rotation::Deg180.to_native(400, 300, 0, 0),
Some((399, 299))
));
const _: () = assert!(matches!(
Rotation::Deg270.to_native(400, 300, 0, 0),
Some((0, 299))
));
const _: () = assert!(Rotation::Deg90.to_native(400, 300, 300, 0).is_none());
const _: () = assert!(Rotation::Deg90.to_native(400, 300, 0, 400).is_none());