1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
use crate::{Line, Point, Size}; use ::std::convert::From; use ::std::ops::Add; #[derive(Copy, Clone, PartialEq)] pub struct Transform { scale: Size, position: Point, } impl Transform { pub fn new() -> Self { Self { position: Point(0.0, 0.0), scale: Size(1.0, 1.0), } } pub fn set_position(mut self, position: Point) -> Self { self.position = position; self } pub fn position(&self) -> Point<f32> { self.position } pub fn flip_x(&self) -> bool { self.scale.width() < 1.0 } pub fn flip_y(&self) -> bool { self.scale.height() < 1.0 } pub fn set_flip_x_from_dir(self, dir: f32) -> Self { self.set_flip_x_from_bool(dir < 0.0) } pub fn set_flip_y_from_dir(self, dir: f32) -> Self { self.set_flip_y_from_bool(dir < 0.0) } pub fn set_flip_x_from_bool(self, is_flipped: bool) -> Self { if is_flipped { self.set_scale_width(-1.0) } else { self.set_scale_width(1.0) } } pub fn set_flip_y_from_bool(self, is_flipped: bool) -> Self { if is_flipped { self.set_scale_height(-1.0) } else { self.set_scale_height(1.0) } } fn set_scale_width(mut self, scale_width: f32) -> Self { self.scale.set_width(scale_width); self } fn set_scale_height(mut self, scale_height: f32) -> Self { self.scale.set_height(scale_height); self } pub fn set_scale(mut self, scale: Size) -> Self { self.scale = scale; self } pub fn scale(self) -> Size { self.scale } } impl Add<Line> for Transform { type Output = Line; fn add(self, line: Self::Output) -> Self::Output { (line * self.scale()) + self.position() } } impl From<Point<f32>> for Transform { fn from(p: Point<f32>) -> Transform { Transform::new().set_position(p) } } impl From<Size<f32>> for Transform { fn from(s: Size<f32>) -> Transform { Transform::new().set_scale(s) } }