diff options
| author | Arne Dußin | 2020-11-01 17:44:51 +0100 |
|---|---|---|
| committer | Arne Dußin | 2020-11-01 17:44:51 +0100 |
| commit | 2db23f3213ff1bb2fe0cf005e922536da7ff5cd7 (patch) | |
| tree | 912b6d829ea8c8c28a2ed257e70e0fd6814bef50 /src/transform.rs | |
| parent | a6be14ada463cb5a3fc0a810b8b341093abbb68d (diff) | |
| download | graf_karto-2db23f3213ff1bb2fe0cf005e922536da7ff5cd7.tar.gz graf_karto-2db23f3213ff1bb2fe0cf005e922536da7ff5cd7.zip | |
Refactor a major part of the project
In order to be able to save and load the map, a major rework of the code
seemed necessary, since Vector2 and Rectangle of raylib do not implement
serialize, and it seems cleanest to use the serialize/deserialize traits
of serde, to save for instance to RON. ToolShed was renamed to Editor,
since it should better show, that it does quite a bit more than harbour
tools. The map data is now centrally saved in the editor, instead of
decentralised in the tool structs.
Diffstat (limited to 'src/transform.rs')
| -rw-r--r-- | src/transform.rs | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/src/transform.rs b/src/transform.rs index fa24636..f44888b 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -3,7 +3,7 @@ //! Useful to turn on-screen coordinates into measurements of the "real" world the map describes //! and the other way around. -use raylib::prelude::*; +use crate::math::{Rect, Vec2}; const STANDARD_PIXELS_PER_M: f32 = 64.; const MIN_PIXELS_PER_M: f32 = 0.5; @@ -13,7 +13,7 @@ pub struct Transform { /// The (not necessarily natural) number of pixels per m, i.e. the current scale of the map pixels_per_m: f32, /// The vector the entire on-screen map is moved by in pixels - translation_px: Vector2, + translation_px: Vec2<f32>, } impl Transform { @@ -21,13 +21,13 @@ impl Transform { pub fn new() -> Self { Self { pixels_per_m: STANDARD_PIXELS_PER_M, - translation_px: Vector2::new(0., 0.), + translation_px: Vec2::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: Vector2) -> Vector2 { + pub fn point_m_to_px(&self, point: Vec2<f32>) -> Vec2<f32> { // Start by converting the absolute position in meters into the absolute position in // pixels, then add the translation of the screen. (point * self.pixels_per_m) + self.translation_px @@ -35,7 +35,7 @@ impl Transform { /// Convert an on-screen point into an absolute point with values in meters. #[inline] - pub fn point_px_to_m(&self, point: Vector2) -> Vector2 { + pub fn point_px_to_m(&self, point: Vec2<f32>) -> Vec2<f32> { // Start by subtracting the pixel translation and afterwards convert these absolute pixel // measurements into meters. (point - self.translation_px) / self.pixels_per_m @@ -55,25 +55,25 @@ impl Transform { /// Convert a rectangle which has measurements in meters into one of pixels #[inline] - 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( + pub fn rect_m_to_px(&self, rect: Rect<f32>) -> Rect<f32> { + let left_upper = self.point_m_to_px(Vec2::new(rect.x, rect.y)); + Rect::new( left_upper.x, left_upper.y, - self.length_m_to_px(rect.width), - self.length_m_to_px(rect.height), + self.length_m_to_px(rect.w), + self.length_m_to_px(rect.h), ) } /// Convert a rectangle which has measurements in pixels into one of meters #[inline] - 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( + pub fn rect_px_to_m(&self, rect: Rect<f32>) -> Rect<f32> { + let left_upper = self.point_px_to_m(Vec2::new(rect.x, rect.y)); + Rect::new( left_upper.x, left_upper.y, - self.length_px_to_m(rect.width), - self.length_px_to_m(rect.height), + self.length_px_to_m(rect.w), + self.length_px_to_m(rect.h), ) } @@ -116,14 +116,14 @@ impl Transform { } /// Move the canvas by the vector in pixels. - pub fn move_by_px(&mut self, by: Vector2) { + pub fn move_by_px(&mut self, by: Vec2<f32>) { self.translation_px += by; } pub fn pixels_per_m(&self) -> f32 { self.pixels_per_m } - pub fn translation_px(&self) -> Vector2 { + pub fn translation_px(&self) -> Vec2<f32> { self.translation_px } } |
