From 48f321a80970ebeb8374072b1d2e0a4d297aa348 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Sun, 20 Dec 2020 03:58:46 +0100 Subject: Add dimensional indicator with scaling --- src/map/mappable.rs | 8 ++++++-- src/map/polygon_room.rs | 26 ++++++++++++++------------ src/map/rect_room.rs | 18 +----------------- src/map/wall.rs | 29 ++++++++++++++++++----------- 4 files changed, 39 insertions(+), 42 deletions(-) (limited to 'src/map') diff --git a/src/map/mappable.rs b/src/map/mappable.rs index b348c4b..323361a 100644 --- a/src/map/mappable.rs +++ b/src/map/mappable.rs @@ -2,8 +2,8 @@ //! dimension on the map and may be scaleable use crate::math::Rect; -use crate::scaleable::Scaleable; use crate::transform::Transform; +use crate::transformable::NonRigidTransformable; use raylib::drawing::RaylibDrawHandle; pub trait Mappable { @@ -21,7 +21,11 @@ pub trait Mappable { /// Get the rectangle that contains the mappable object in its entirety without excess. fn bounding_rect(&self) -> Rect; - fn as_scaleable(&self) -> Option<&dyn Scaleable> { + fn as_non_rigid(&self) -> Option<&dyn NonRigidTransformable> { + None + } + + fn as_non_rigid_mut(&mut self) -> Option<&mut dyn NonRigidTransformable> { None } } diff --git a/src/map/polygon_room.rs b/src/map/polygon_room.rs index a209a7c..65eeaab 100644 --- a/src/map/polygon_room.rs +++ b/src/map/polygon_room.rs @@ -1,9 +1,10 @@ use super::Mappable; use crate::colours::DEFAULT_COLOURS; -use crate::math::{self, Polygon, Rect, Triangle, Vec2}; -use crate::scaleable::Scaleable; +use crate::math::{self, Polygon, Rect, Triangle}; use crate::transform::Transform; +use crate::transformable::NonRigidTransformable; use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; +use nalgebra::{Matrix3, Point2}; pub type PolygonRoomData = Polygon; @@ -56,20 +57,21 @@ impl Mappable for PolygonRoom { Rect::bounding_rect_n(&self.data.corners()) } - fn as_scaleable(&self) -> Option<&dyn Scaleable> { - Some(self as &dyn Scaleable) + fn as_non_rigid(&self) -> Option<&dyn NonRigidTransformable> { + Some(self as &dyn NonRigidTransformable) } -} -impl Scaleable for PolygonRoom { - fn scale(&mut self, by: &Vec2) { - if by.x < 0. || by.y < 0. { - panic!("Cannot set dimensions with negative size"); - } + fn as_non_rigid_mut(&mut self) -> Option<&mut dyn NonRigidTransformable> { + Some(self as &mut dyn NonRigidTransformable) + } +} +impl NonRigidTransformable for PolygonRoom { + fn apply_matrix(&mut self, matrix: &Matrix3) { for corner in self.data.corners_mut() { - corner.x *= by.x; - corner.y *= by.y; + *corner = matrix + .transform_point(&Point2::new(corner.x, corner.y)) + .into(); } self.retriangulate(); diff --git a/src/map/rect_room.rs b/src/map/rect_room.rs index 5008c63..ee184fb 100644 --- a/src/map/rect_room.rs +++ b/src/map/rect_room.rs @@ -1,7 +1,6 @@ use crate::colours::DEFAULT_COLOURS; use crate::map::Mappable; -use crate::math::{Rect, Vec2}; -use crate::scaleable::Scaleable; +use crate::math::Rect; use crate::transform::Transform; use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; use serde::Serialize; @@ -47,21 +46,6 @@ impl Mappable for RectRoom { fn bounding_rect(&self) -> Rect { self.data.clone() } - - fn as_scaleable(&self) -> Option<&dyn Scaleable> { - Some(self as &dyn Scaleable) - } -} - -impl Scaleable for RectRoom { - fn scale(&mut self, by: &Vec2) { - if by.x < 0. || by.y < 0. { - panic!("Cannot set dimensions with negative size"); - } - - self.data.x *= by.x; - self.data.y *= by.y; - } } impl Deref for RectRoom { diff --git a/src/map/wall.rs b/src/map/wall.rs index 22393bb..d18096b 100644 --- a/src/map/wall.rs +++ b/src/map/wall.rs @@ -1,10 +1,11 @@ use super::Mappable; use crate::colours::DEFAULT_COLOURS; use crate::math::{LineSegment, Rect, Vec2}; -use crate::scaleable::Scaleable; use crate::transform::Transform; +use crate::transformable::NonRigidTransformable; use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; use std::ops::{Deref, DerefMut}; +use nalgebra::{Matrix3, Point2}; pub type WallData = LineSegment; @@ -81,18 +82,24 @@ impl Mappable for Wall { fn bounding_rect(&self) -> Rect { Rect::bounding_rect(self.data.start, self.data.end) } -} -impl Scaleable for Wall { - fn scale(&mut self, by: &Vec2) { - if by.x <= 0. || by.y <= 0. { - panic!("Cannot set dimensions with negative size"); - } + fn as_non_rigid(&self) -> Option<&dyn NonRigidTransformable> { + Some(self as &dyn NonRigidTransformable) + } + + fn as_non_rigid_mut(&mut self) -> Option<&mut dyn NonRigidTransformable> { + Some(self as &mut dyn NonRigidTransformable) + } +} - self.data.start.x *= by.x; - self.data.start.y *= by.y; - self.data.end.x *= by.x; - self.data.end.y *= by.y; +impl NonRigidTransformable for Wall { + fn apply_matrix(&mut self, matrix: &Matrix3) { + self.data.start = matrix + .transform_point(&self.data.start.into()) + .into(); + self.data.end = matrix + .transform_point(&self.data.end.into()) + .into(); } } -- cgit v1.2.3-70-g09d2