diff options
| author | Arne Dußin | 2021-01-11 12:10:16 +0100 |
|---|---|---|
| committer | Arne Dußin | 2021-01-11 12:10:16 +0100 |
| commit | ec071d5bc677101c0168b5fb3065f2d928234ed9 (patch) | |
| tree | 963ae90a6563c1f08d8e52078afd62db32e182f0 | |
| parent | ad1e79a517ce64eda7b06bb1567d3df070813dca (diff) | |
| download | graf_karto-ec071d5bc677101c0168b5fb3065f2d928234ed9.tar.gz graf_karto-ec071d5bc677101c0168b5fb3065f2d928234ed9.zip | |
Rect rooms are now normal polygon rooms in data
Before there was an extra data type for rectangular rooms. This is now
changed, with the rectangle tool remaining, to create these often
required rooms faster, but the data type is just a normal polygon that
is generated from a rect to reduce redundancy.
| -rw-r--r-- | src/map/data.rs | 10 | ||||
| -rw-r--r-- | src/map/mod.rs | 35 | ||||
| -rw-r--r-- | src/map/rect_room.rs | 68 | ||||
| -rw-r--r-- | src/math/polygon/mod.rs | 10 | ||||
| -rw-r--r-- | src/math/rect.rs | 17 | ||||
| -rw-r--r-- | src/tool/rect_room_tool.rs | 4 |
6 files changed, 31 insertions, 113 deletions
diff --git a/src/map/data.rs b/src/map/data.rs index 0c11d1c..3258512 100644 --- a/src/map/data.rs +++ b/src/map/data.rs @@ -1,6 +1,6 @@ //! Module containing the raw map data version of the map. -use super::{IconData, Map, PolygonRoomData, RectRoomData, WallData}; +use super::{IconData, Map, PolygonRoomData, WallData}; use ron::de::from_reader; use ron::ser::{to_string_pretty, PrettyConfig}; use serde::{Deserialize, Serialize}; @@ -13,7 +13,6 @@ use std::path::Path; /// it easily corruptable. #[derive(Serialize, Deserialize)] pub struct MapData { - pub(super) rect_rooms: Vec<RectRoomData>, pub(super) polygon_rooms: Vec<PolygonRoomData>, pub(super) walls: Vec<WallData>, pub(super) icons: Vec<IconData>, @@ -22,13 +21,11 @@ pub struct MapData { impl MapData { /// Create a serialisable map data type from the data elements contained in a map. pub fn new( - rect_rooms: Vec<RectRoomData>, polygon_rooms: Vec<PolygonRoomData>, walls: Vec<WallData>, icons: Vec<IconData>, ) -> Self { Self { - rect_rooms, polygon_rooms, walls, icons, @@ -41,11 +38,6 @@ impl MapData { /// included. pub fn extract_data(map: &Map) -> Self { Self { - rect_rooms: map - .rect_rooms() - .iter() - .map(|r| *(r as &RectRoomData)) - .collect(), polygon_rooms: map .polygon_rooms() .iter() diff --git a/src/map/mod.rs b/src/map/mod.rs index 70f65b3..2040706 100644 --- a/src/map/mod.rs +++ b/src/map/mod.rs @@ -16,14 +16,12 @@ pub mod icon; pub mod icon_renderer; pub mod mappable; pub mod polygon_room; -pub mod rect_room; pub mod wall; pub use data::MapData; pub use icon::*; pub use mappable::Mappable; pub use polygon_room::*; -pub use rect_room::*; pub use wall::*; use crate::transform::Transform; @@ -34,7 +32,6 @@ use std::rc::Rc; /// The map containing all map elements that are seen on the screen. pub struct Map { - rect_rooms: Vec<RectRoom>, polygon_rooms: Vec<PolygonRoom>, walls: Vec<Wall>, icons: Vec<Icon>, @@ -45,7 +42,6 @@ impl Map { /// Create a new, empty map/world. pub fn new(rl: &mut RaylibHandle, rlt: &RaylibThread) -> Self { Self { - rect_rooms: Vec::new(), polygon_rooms: Vec::new(), walls: Vec::new(), icons: Vec::new(), @@ -53,13 +49,6 @@ impl Map { } } - /// Add a rectangularly shaped room to the world. Since the polygon room tool was added, and - /// afaik rects are polygon rooms, this will be phased out in favour of only having polygon - /// rooms, which will then become just normal rooms. - pub fn push_rect_room(&mut self, room_data: RectRoomData) { - self.rect_rooms.push(RectRoom::from_data(room_data)); - } - /// Add a room to the map. Currently, holes are not supported in the polygon, but this might /// change later. pub fn push_polygon_room(&mut self, room_data: PolygonRoomData) { @@ -115,7 +104,6 @@ impl Map { F: FnMut(&dyn Mappable) -> bool, { // Call retain on all vectors containing the maps actual types. - self.rect_rooms.retain(|r| f(r as &dyn Mappable)); self.polygon_rooms.retain(|p| f(p as &dyn Mappable)); self.walls.retain(|w| f(w as &dyn Mappable)); self.icons.retain(|i| f(i as &dyn Mappable)); @@ -124,10 +112,9 @@ impl Map { /// Iterator over all elements as objects when an operation needs to go over all elements of the /// map. pub fn elements(&self) -> impl Iterator<Item = &dyn Mappable> { - self.rect_rooms + self.polygon_rooms .iter() - .map(|r| r as &dyn Mappable) - .chain(self.polygon_rooms.iter().map(|p| p as &dyn Mappable)) + .map(|p| p as &dyn Mappable) .chain(self.walls.iter().map(|w| w as &dyn Mappable)) .chain(self.icons.iter().map(|i| i as &dyn Mappable)) } @@ -135,23 +122,13 @@ impl Map { /// Iterator over all elements, but the individual elements can be mutated. It is however /// impossible to add or remove elements in this way. For that, use the dedicated functions. pub fn elements_mut(&mut self) -> impl Iterator<Item = &mut dyn Mappable> { - self.rect_rooms + self.polygon_rooms .iter_mut() - .map(|r| r as &mut dyn Mappable) - .chain( - self.polygon_rooms - .iter_mut() - .map(|p| p as &mut dyn Mappable), - ) + .map(|p| p as &mut dyn Mappable) .chain(self.walls.iter_mut().map(|w| w as &mut dyn Mappable)) .chain(self.icons.iter_mut().map(|i| i as &mut dyn Mappable)) } - /// Get the rectangular rooms of this map. - pub fn rect_rooms(&self) -> &Vec<RectRoom> { - &self.rect_rooms - } - /// Get the polygon rooms of this map. pub fn polygon_rooms(&self) -> &Vec<PolygonRoom> { &self.polygon_rooms @@ -172,7 +149,6 @@ impl Map { // Remove all data. self.icons.clear(); self.polygon_rooms.clear(); - self.rect_rooms.clear(); self.walls.clear(); // Add all data from the map data. @@ -188,9 +164,6 @@ impl Map { for p in data.polygon_rooms { self.push_polygon_room(p); } - for r in data.rect_rooms { - self.push_rect_room(r); - } for w in data.walls { self.push_wall(w); } diff --git a/src/map/rect_room.rs b/src/map/rect_room.rs deleted file mode 100644 index ae10327..0000000 --- a/src/map/rect_room.rs +++ /dev/null @@ -1,68 +0,0 @@ -//! Deprecated simple rectangular room type. - -use crate::colours::DEFAULT_COLOURS; -use crate::map::Mappable; -use crate::math::Rect; -use crate::transform::Transform; -use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; -use serde::Serialize; -use std::ops::{Deref, DerefMut}; - -#[allow(missing_docs)] -pub type RectRoomData = Rect<f64>; - -#[allow(missing_docs)] -#[derive(Serialize)] -pub struct RectRoom { - data: RectRoomData, - selected: bool, -} - -impl RectRoom { - #[allow(missing_docs)] - pub fn from_data(data: RectRoomData) -> Self { - RectRoom { - data, - selected: false, - } - } -} - -impl Mappable for RectRoom { - fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) { - rld.draw_rectangle_rec( - transform.rect_m_to_px(&self.data), - if self.selected() { - DEFAULT_COLOURS.room_selected - } else { - DEFAULT_COLOURS.room_normal - }, - ); - } - - fn set_selected(&mut self, selected: bool) { - self.selected = selected; - } - - fn selected(&self) -> bool { - self.selected - } - - fn bounding_rect(&self) -> Rect<f64> { - self.data - } -} - -impl Deref for RectRoom { - type Target = RectRoomData; - - fn deref(&self) -> &Self::Target { - &self.data - } -} - -impl DerefMut for RectRoom { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.data - } -} diff --git a/src/math/polygon/mod.rs b/src/math/polygon/mod.rs index bc145ed..02d8abb 100644 --- a/src/math/polygon/mod.rs +++ b/src/math/polygon/mod.rs @@ -58,13 +58,11 @@ impl<T: Scalar + Copy> Polygon<T> { } /// Like new, but does not perform any validity checks, so be careful when using this function. - pub fn new_unchecked<M>(corners: Vec<Vec2<T>>, t_margin: M) -> Self + pub(crate) fn new_unchecked<M>(corners: Vec<Vec2<T>>, t_margin: M) -> Self where T: RealField + ApproxEq<Margin = M>, M: Copy, { - assert!(Polygon::check_validity(&corners, t_margin).is_ok()); - let corners = if combined_angle(&corners, t_margin) > T::zero() { corners } else { @@ -74,6 +72,12 @@ impl<T: Scalar + Copy> Polygon<T> { Self { corners } } + /// Create a polygon from the sorted vertices. This will create an invalid polygon if the + /// vertices are not sorted so that the edges turn counterclockwise, so use with caution. + pub(crate) fn from_vertices(corners: Vec<Vec2<T>>) -> Self { + Self { corners } + } + /// Checks if a set of corners can be made into a polygon or not. Returns Ok if they can, or /// the reason they cannot in form of a PolygonError. pub fn check_validity<M>(corners: &[Vec2<T>], t_margin: M) -> Result<(), PolygonError<T>> diff --git a/src/math/rect.rs b/src/math/rect.rs index b019ad5..adb608b 100644 --- a/src/math/rect.rs +++ b/src/math/rect.rs @@ -229,6 +229,23 @@ impl<T: Scalar + Copy + ToPrimitive> Into<raylib::ffi::Rectangle> for Rect<T> { } } +/* Convert the rectangle into a polygon. This is the same as creating a convex hull from the corner + * points, but is a specific case. + */ +impl<T: Scalar + Copy + ToPrimitive, P: Scalar + Copy> Into<Polygon<P>> for Rect<T> +where + T: Into<P> + Add<Output = T>, +{ + fn into(self) -> Polygon<P> { + Polygon::from_vertices(vec![ + Vec2::new(self.x.into(), self.y.into()), + Vec2::new(self.x.into(), (self.y + self.h).into()), + Vec2::new((self.x + self.w).into(), (self.y + self.h).into()), + Vec2::new((self.x + self.w).into(), self.y.into()), + ]) + } +} + #[cfg(test)] mod test { use super::*; diff --git a/src/tool/rect_room_tool.rs b/src/tool/rect_room_tool.rs index 9445787..da23fd7 100644 --- a/src/tool/rect_room_tool.rs +++ b/src/tool/rect_room_tool.rs @@ -55,7 +55,7 @@ impl Tool for RectRoomTool { return; } - map.push_rect_room(Rect::bounding_rect(pos1, pos2)); + map.push_polygon_room(Rect::bounding_rect(pos1, pos2).into()); self.unfinished_rect = None; } else { self.unfinished_rect = Some((*mouse_pos_m, *mouse_pos_m)); @@ -69,7 +69,7 @@ impl Tool for RectRoomTool { return; } - map.push_rect_room(Rect::bounding_rect(pos1, pos2)); + map.push_polygon_room(Rect::bounding_rect(pos1, pos2).into()); self.unfinished_rect = None; } } |
