From 48c425a193cb13012eb9303df56ac04b9d683ed4 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Fri, 30 Oct 2020 22:32:28 +0100 Subject: Rewrite project to use raylib instead of piston Sorry piston.. I really tried liking you, but I just couldn't :/ It's not you, it's me. What am I saying? It's you, sorry not sorry. --- src/transform.rs | 76 ++++++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 41 deletions(-) (limited to 'src/transform.rs') diff --git a/src/transform.rs b/src/transform.rs index a91d7da..fa24636 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -1,19 +1,19 @@ //! Transformation module //! -//! Useful to turn on-screen coordinates into the measurements in the "real" world the map -//! describes and the other way around. +//! Useful to turn on-screen coordinates into measurements of the "real" world the map describes +//! and the other way around. -use piston_window::math; +use raylib::prelude::*; -const STANDARD_PIXELS_PER_M: f64 = 64.; -const MIN_PIXELS_PER_M: f64 = 0.5; -const MAX_PIXELS_PER_M: f64 = 10_000.; +const STANDARD_PIXELS_PER_M: f32 = 64.; +const MIN_PIXELS_PER_M: f32 = 0.5; +const MAX_PIXELS_PER_M: f32 = 10_000.; pub struct Transform { /// The (not necessarily natural) number of pixels per m, i.e. the current scale of the map - pixels_per_m: f64, + pixels_per_m: f32, /// The vector the entire on-screen map is moved by in pixels - translation_px: [f64; 2], + translation_px: Vector2, } impl Transform { @@ -21,66 +21,60 @@ impl Transform { pub fn new() -> Self { Self { pixels_per_m: STANDARD_PIXELS_PER_M, - translation_px: [0., 0.], + translation_px: Vector2::new(0., 0.), } } /// Convert a point that is given in meters into the corresponding point in pixels. #[inline] - pub fn point_m_to_px(&self, point: [f64; 2]) -> [f64; 2] { + pub fn point_m_to_px(&self, point: Vector2) -> Vector2 { // Start by converting the absolute position in meters into the absolute position in // pixels, then add the translation of the screen. - math::add( - math::mul_scalar(point, self.pixels_per_m), - self.translation_px, - ) + (point * self.pixels_per_m) + self.translation_px } /// Convert an on-screen point into an absolute point with values in meters. #[inline] - pub fn point_px_to_m(&self, point: [f64; 2]) -> [f64; 2] { + pub fn point_px_to_m(&self, point: Vector2) -> Vector2 { // Start by subtracting the pixel translation and afterwards convert these absolute pixel // measurements into meters. - math::mul_scalar( - math::sub(point, self.translation_px), - 1. / self.pixels_per_m, - ) + (point - self.translation_px) / self.pixels_per_m } /// Convert a length given in meters into a length in pixels #[inline] - pub fn length_m_to_px(&self, length: f64) -> f64 { + pub fn length_m_to_px(&self, length: f32) -> f32 { length * self.pixels_per_m } /// Convert a length given in pixels into a length in meters #[inline] - pub fn length_px_to_m(&self, length: f64) -> f64 { + pub fn length_px_to_m(&self, length: f32) -> f32 { length / self.pixels_per_m } /// Convert a rectangle which has measurements in meters into one of pixels #[inline] - pub fn rect_m_to_px(&self, rect: [f64; 4]) -> [f64; 4] { - let left_upper = self.point_m_to_px([rect[0], rect[1]]); - [ - left_upper[0], - left_upper[1], - self.length_m_to_px(rect[2]), - self.length_m_to_px(rect[3]), - ] + pub fn rect_m_to_px(&self, rect: Rectangle) -> Rectangle { + let left_upper = self.point_m_to_px(Vector2::new(rect.x, rect.y)); + Rectangle::new( + left_upper.x, + left_upper.y, + self.length_m_to_px(rect.width), + self.length_m_to_px(rect.height), + ) } /// Convert a rectangle which has measurements in pixels into one of meters #[inline] - pub fn rect_px_to_m(&self, rect: [f64; 4]) -> [f64; 4] { - let left_upper = self.point_px_to_m([rect[0], rect[1]]); - [ - left_upper[0], - left_upper[1], - self.length_px_to_m(rect[2]), - self.length_px_to_m(rect[3]), - ] + pub fn rect_px_to_m(&self, rect: Rectangle) -> Rectangle { + let left_upper = self.point_px_to_m(Vector2::new(rect.x, rect.y)); + Rectangle::new( + left_upper.x, + left_upper.y, + self.length_px_to_m(rect.width), + self.length_px_to_m(rect.height), + ) } /* Helper function to make sure the standard zoom factor is always exact. This helps @@ -122,14 +116,14 @@ impl Transform { } /// Move the canvas by the vector in pixels. - pub fn move_by_px(&mut self, by: [f64; 2]) { - self.translation_px = math::add(self.translation_px, by); + pub fn move_by_px(&mut self, by: Vector2) { + self.translation_px += by; } - pub fn pixels_per_m(&self) -> f64 { + pub fn pixels_per_m(&self) -> f32 { self.pixels_per_m } - pub fn translation_px(&self) -> [f64; 2] { + pub fn translation_px(&self) -> Vector2 { self.translation_px } } -- cgit v1.2.3-70-g09d2