From 46cb48c126691f30b47c28470d16f40f679dea20 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 11 Oct 2024 00:08:13 +0200 Subject: [PATCH] convert between origin units --- crates/servicepoint/src/origin.rs | 67 +++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/crates/servicepoint/src/origin.rs b/crates/servicepoint/src/origin.rs index 0e7680a..16ba083 100644 --- a/crates/servicepoint/src/origin.rs +++ b/crates/servicepoint/src/origin.rs @@ -1,3 +1,4 @@ +use crate::TILE_SIZE; use std::marker::PhantomData; /// An origin marks the top left position of a window sent to the display. @@ -46,3 +47,69 @@ pub struct Tiles(); impl DisplayUnit for Pixels {} impl DisplayUnit for Tiles {} + +impl From<&Origin> for Origin { + fn from(value: &Origin) -> Self { + Self { + x: value.x * TILE_SIZE, + y: value.y * TILE_SIZE, + phantom_data: PhantomData, + } + } +} + +impl TryFrom<&Origin> for Origin { + type Error = (); + + fn try_from(value: &Origin) -> Result { + let (x, x_rem) = (value.x / TILE_SIZE, value.x % TILE_SIZE); + if x_rem != 0 { + return Err(()); + } + let (y, y_rem) = (value.y / TILE_SIZE, value.y % TILE_SIZE); + if y_rem != 0 { + return Err(()); + } + + Ok(Self { + x, + y, + phantom_data: PhantomData, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn origin_tile_to_pixel() { + let tile: Origin = Origin::new(1, 2); + let actual: Origin = Origin::from(&tile); + let expected: Origin = Origin::new(8, 16); + assert_eq!(actual, expected); + } + + #[test] + fn origin_pixel_to_tile() { + let pixel: Origin = Origin::new(8, 16); + let actual: Origin = Origin::try_from(&pixel).unwrap(); + let expected: Origin = Origin::new(1, 2); + assert_eq!(actual, expected); + } + + #[test] + #[should_panic] + fn origin_pixel_to_tile_fail_y() { + let pixel: Origin = Origin::new(8, 15); + let _: Origin = Origin::try_from(&pixel).unwrap(); + } + + #[test] + #[should_panic] + fn origin_pixel_to_tile_fail_x() { + let pixel: Origin = Origin::new(7, 16); + let _: Origin = Origin::try_from(&pixel).unwrap(); + } +}