mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 18:10:14 +01:00
convert between origin units
This commit is contained in:
parent
78b5d1180b
commit
46cb48c126
|
@ -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<Tiles>> for Origin<Pixels> {
|
||||
fn from(value: &Origin<Tiles>) -> Self {
|
||||
Self {
|
||||
x: value.x * TILE_SIZE,
|
||||
y: value.y * TILE_SIZE,
|
||||
phantom_data: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&Origin<Pixels>> for Origin<Tiles> {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: &Origin<Pixels>) -> Result<Self, Self::Error> {
|
||||
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<Tiles> = Origin::new(1, 2);
|
||||
let actual: Origin<Pixels> = Origin::from(&tile);
|
||||
let expected: Origin<Pixels> = Origin::new(8, 16);
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn origin_pixel_to_tile() {
|
||||
let pixel: Origin<Pixels> = Origin::new(8, 16);
|
||||
let actual: Origin<Tiles> = Origin::try_from(&pixel).unwrap();
|
||||
let expected: Origin<Tiles> = Origin::new(1, 2);
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn origin_pixel_to_tile_fail_y() {
|
||||
let pixel: Origin<Pixels> = Origin::new(8, 15);
|
||||
let _: Origin<Tiles> = Origin::try_from(&pixel).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn origin_pixel_to_tile_fail_x() {
|
||||
let pixel: Origin<Pixels> = Origin::new(7, 16);
|
||||
let _: Origin<Tiles> = Origin::try_from(&pixel).unwrap();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue