diff options
| author | Arne Dußin | 2020-12-21 21:12:01 +0100 |
|---|---|---|
| committer | GitHub | 2020-12-21 21:12:01 +0100 |
| commit | c435f278eddcada279fdc424120e4a1448843c20 (patch) | |
| tree | be9a5601e99608966d4ccd146c3bfb3a70c7fc02 /src/map/polygon_room.rs | |
| parent | 3bc690803fb59493ea8180fd630d65b3e26642d0 (diff) | |
| parent | 82d11b7d3e15d8175accf7579db1fbe528fc6583 (diff) | |
| download | graf_karto-c435f278eddcada279fdc424120e4a1448843c20.tar.gz graf_karto-c435f278eddcada279fdc424120e4a1448843c20.zip | |
Merge pull request #24 from LordSentox/refactor
Refactor to make interaction between tools easier
Diffstat (limited to 'src/map/polygon_room.rs')
| -rw-r--r-- | src/map/polygon_room.rs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/map/polygon_room.rs b/src/map/polygon_room.rs new file mode 100644 index 0000000..a209a7c --- /dev/null +++ b/src/map/polygon_room.rs @@ -0,0 +1,77 @@ +use super::Mappable; +use crate::colours::DEFAULT_COLOURS; +use crate::math::{self, Polygon, Rect, Triangle, Vec2}; +use crate::scaleable::Scaleable; +use crate::transform::Transform; +use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; + +pub type PolygonRoomData = Polygon<f64>; + +pub struct PolygonRoom { + data: PolygonRoomData, + // The polygon shape, but in triangles, so the polygon does not have to be triangulated every frame. + triangulated: Vec<Triangle<f64>>, + selected: bool, +} + +impl PolygonRoom { + pub fn from_data(data: PolygonRoomData) -> Self { + Self { + data: data.clone(), + triangulated: math::triangulate(data), + selected: false, + } + } + + fn retriangulate(&mut self) { + self.triangulated = math::triangulate(self.data.clone()); + } +} + +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<f64> { + Rect::bounding_rect_n(&self.data.corners()) + } + + fn as_scaleable(&self) -> Option<&dyn Scaleable> { + Some(self as &dyn Scaleable) + } +} + +impl Scaleable for PolygonRoom { + fn scale(&mut self, by: &Vec2<f64>) { + if by.x < 0. || by.y < 0. { + panic!("Cannot set dimensions with negative size"); + } + + for corner in self.data.corners_mut() { + corner.x *= by.x; + corner.y *= by.y; + } + + self.retriangulate(); + } +} |
