From b1179849c28e50c39ac3c94af9dda86ee24beca0 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Mon, 11 Jan 2021 14:21:03 +0100 Subject: Rename PolygonRoom to just Room --- src/map/data.rs | 18 ++++---- src/map/mod.rs | 30 ++++++------- src/map/polygon_room.rs | 97 ------------------------------------------ src/map/room.rs | 98 +++++++++++++++++++++++++++++++++++++++++++ src/tool/polygon_room_tool.rs | 2 +- src/tool/rect_room_tool.rs | 4 +- 6 files changed, 123 insertions(+), 126 deletions(-) delete mode 100644 src/map/polygon_room.rs create mode 100644 src/map/room.rs (limited to 'src') diff --git a/src/map/data.rs b/src/map/data.rs index 3258512..20f193d 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, WallData}; +use super::{IconData, Map, RoomData, WallData}; use ron::de::from_reader; use ron::ser::{to_string_pretty, PrettyConfig}; use serde::{Deserialize, Serialize}; @@ -13,20 +13,16 @@ use std::path::Path; /// it easily corruptable. #[derive(Serialize, Deserialize)] pub struct MapData { - pub(super) polygon_rooms: Vec, + pub(super) rooms: Vec, pub(super) walls: Vec, pub(super) icons: Vec, } impl MapData { /// Create a serialisable map data type from the data elements contained in a map. - pub fn new( - polygon_rooms: Vec, - walls: Vec, - icons: Vec, - ) -> Self { + pub fn new(rooms: Vec, walls: Vec, icons: Vec) -> Self { Self { - polygon_rooms, + rooms, walls, icons, } @@ -38,10 +34,10 @@ impl MapData { /// included. pub fn extract_data(map: &Map) -> Self { Self { - polygon_rooms: map - .polygon_rooms() + rooms: map + .rooms() .iter() - .map(|p| (p as &PolygonRoomData).clone()) + .map(|p| (p as &RoomData).clone()) .collect(), walls: map .walls() diff --git a/src/map/mod.rs b/src/map/mod.rs index 2040706..e1def09 100644 --- a/src/map/mod.rs +++ b/src/map/mod.rs @@ -15,13 +15,13 @@ pub mod data; pub mod icon; pub mod icon_renderer; pub mod mappable; -pub mod polygon_room; +pub mod room; pub mod wall; pub use data::MapData; pub use icon::*; pub use mappable::Mappable; -pub use polygon_room::*; +pub use room::*; pub use wall::*; use crate::transform::Transform; @@ -32,7 +32,7 @@ use std::rc::Rc; /// The map containing all map elements that are seen on the screen. pub struct Map { - polygon_rooms: Vec, + rooms: Vec, walls: Vec, icons: Vec, icon_renderer: Rc, @@ -42,7 +42,7 @@ impl Map { /// Create a new, empty map/world. pub fn new(rl: &mut RaylibHandle, rlt: &RaylibThread) -> Self { Self { - polygon_rooms: Vec::new(), + rooms: Vec::new(), walls: Vec::new(), icons: Vec::new(), icon_renderer: Rc::new(IconRenderer::new(rl, rlt)), @@ -51,8 +51,8 @@ impl Map { /// 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) { - self.polygon_rooms.push(PolygonRoom::from_data(room_data)); + pub fn push_room(&mut self, room_data: RoomData) { + self.rooms.push(Room::from_data(room_data)); } /// Add a wall to the world. @@ -104,7 +104,7 @@ impl Map { F: FnMut(&dyn Mappable) -> bool, { // Call retain on all vectors containing the maps actual types. - self.polygon_rooms.retain(|p| f(p as &dyn Mappable)); + self.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)); } @@ -112,7 +112,7 @@ 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 { - self.polygon_rooms + self.rooms .iter() .map(|p| p as &dyn Mappable) .chain(self.walls.iter().map(|w| w as &dyn Mappable)) @@ -122,16 +122,16 @@ 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 { - self.polygon_rooms + self.rooms .iter_mut() .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 polygon rooms of this map. - pub fn polygon_rooms(&self) -> &Vec { - &self.polygon_rooms + /// Get the rooms of this map. + pub fn rooms(&self) -> &Vec { + &self.rooms } /// Get the walls of this map. @@ -148,7 +148,7 @@ impl Map { pub fn set_data(&mut self, data: MapData) { // Remove all data. self.icons.clear(); - self.polygon_rooms.clear(); + self.rooms.clear(); self.walls.clear(); // Add all data from the map data. @@ -161,8 +161,8 @@ impl Map { for i in data.icons { self.push_icon(Icon::from_data(i, self.icon_renderer.clone())) } - for p in data.polygon_rooms { - self.push_polygon_room(p); + for p in data.rooms { + self.push_room(p); } for w in data.walls { self.push_wall(w); diff --git a/src/map/polygon_room.rs b/src/map/polygon_room.rs deleted file mode 100644 index 2a29436..0000000 --- a/src/map/polygon_room.rs +++ /dev/null @@ -1,97 +0,0 @@ -//! Polygon rooms are the standard rooms in graf karto. They can take the form of anything that a -//! [Polygon](crate::math::Polygon) can have. - -use super::Mappable; -use crate::colours::DEFAULT_COLOURS; -use crate::math::{self, Polygon, Rect, Triangle}; -use crate::transform::Transform; -use crate::transformable::NonRigidTransformable; -use crate::FLOAT_MARGIN; -use nalgebra::{Matrix3, Point2}; -use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; -use std::ops::Deref; - -/// Data type for the Polygon room. -pub type PolygonRoomData = Polygon; - -/// A polygon room, which can be placed and modified in the world. -pub struct PolygonRoom { - data: PolygonRoomData, - // The polygon shape, but in triangles, so the polygon does not have to be triangulated every frame. - triangulated: Vec>, - selected: bool, -} - -impl PolygonRoom { - /// Create a room from the given polygon data. - pub fn from_data(data: PolygonRoomData) -> Self { - Self { - data: data.clone(), - triangulated: math::triangulate(data, FLOAT_MARGIN), - selected: false, - } - } - - // When the internal polygon changes, it must be retriangulated to be drawn on the screen - // properly, so this function must be called any time that happens. - fn retriangulate(&mut self) { - self.triangulated = math::triangulate(self.data.clone(), FLOAT_MARGIN); - } -} - -impl Mappable for PolygonRoom { - fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) { - for triangle in &self.triangulated { - rld.draw_triangle( - transform.point_m_to_px(&triangle.corners()[0]), - transform.point_m_to_px(&triangle.corners()[1]), - transform.point_m_to_px(&triangle.corners()[2]), - 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 { - Rect::bounding_rect_n(&self.data.corners()) - } - - 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) - } -} - -impl NonRigidTransformable for PolygonRoom { - fn apply_matrix(&mut self, matrix: &Matrix3) { - for corner in self.data.corners_mut() { - *corner = matrix - .transform_point(&Point2::new(corner.x, corner.y)) - .into(); - } - - self.retriangulate(); - } -} - -impl Deref for PolygonRoom { - type Target = PolygonRoomData; - - fn deref(&self) -> &Self::Target { - &self.data - } -} diff --git a/src/map/room.rs b/src/map/room.rs new file mode 100644 index 0000000..3050763 --- /dev/null +++ b/src/map/room.rs @@ -0,0 +1,98 @@ +//! Polygon rooms are the standard rooms in graf karto. They can take the form of anything that a +//! [Polygon](crate::math::Polygon) can have. + +use super::Mappable; +use crate::colours::DEFAULT_COLOURS; +use crate::math::{self, Polygon, Rect, Triangle}; +use crate::transform::Transform; +use crate::transformable::NonRigidTransformable; +use crate::FLOAT_MARGIN; +use nalgebra::{Matrix3, Point2}; +use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; +use std::ops::Deref; + +/// Data type for the Polygon room. +pub type RoomData = Polygon; + +/// A polygon room, which can be placed and modified in the world. +pub struct Room { + data: RoomData, + // The polygon shape, but in triangles, so the polygon does not have to be triangulated every frame. + triangulated: Vec>, + selected: bool, +} + +impl Room { + /// Create a room from the given polygon data. + pub fn from_data(data: RoomData) -> Self { + Self { + data: data.clone(), + triangulated: math::triangulate(data, FLOAT_MARGIN), + selected: false, + } + } + + /* When the internal polygon changes, it must be retriangulated to be drawn on the screen + * properly, so this function must be called any time that happens. + */ + fn retriangulate(&mut self) { + self.triangulated = math::triangulate(self.data.clone(), FLOAT_MARGIN); + } +} + +impl Mappable for Room { + fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) { + for triangle in &self.triangulated { + rld.draw_triangle( + transform.point_m_to_px(&triangle.corners()[0]), + transform.point_m_to_px(&triangle.corners()[1]), + transform.point_m_to_px(&triangle.corners()[2]), + 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 { + Rect::bounding_rect_n(&self.data.corners()) + } + + 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) + } +} + +impl NonRigidTransformable for Room { + fn apply_matrix(&mut self, matrix: &Matrix3) { + for corner in self.data.corners_mut() { + *corner = matrix + .transform_point(&Point2::new(corner.x, corner.y)) + .into(); + } + + self.retriangulate(); + } +} + +impl Deref for Room { + type Target = RoomData; + + fn deref(&self) -> &Self::Target { + &self.data + } +} diff --git a/src/tool/polygon_room_tool.rs b/src/tool/polygon_room_tool.rs index d181669..d7c188f 100644 --- a/src/tool/polygon_room_tool.rs +++ b/src/tool/polygon_room_tool.rs @@ -43,7 +43,7 @@ impl PolygonRoomTool { .bounding_polygon(FLOAT_MARGIN) { Some(polygon) => { - map.push_polygon_room(polygon); + map.push_room(polygon); self.unfinished_room = None; true } diff --git a/src/tool/rect_room_tool.rs b/src/tool/rect_room_tool.rs index da23fd7..ec0f0ec 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_polygon_room(Rect::bounding_rect(pos1, pos2).into()); + map.push_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_polygon_room(Rect::bounding_rect(pos1, pos2).into()); + map.push_room(Rect::bounding_rect(pos1, pos2).into()); self.unfinished_rect = None; } } -- cgit v1.2.3-70-g09d2