From 9799d3c6a8f0c242668203a1c70d7b6cfed3e855 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Tue, 15 Dec 2020 00:46:54 +0100 Subject: Refactor to make interaction between tools easier --- src/map/rect_room.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/map/rect_room.rs (limited to 'src/map/rect_room.rs') diff --git a/src/map/rect_room.rs b/src/map/rect_room.rs new file mode 100644 index 0000000..07f201c --- /dev/null +++ b/src/map/rect_room.rs @@ -0,0 +1,68 @@ +use crate::map::Mappable; +use crate::math::{Rect, Vec2}; +use crate::scaleable::Scaleable; +use crate::transform::Transform; +use raylib::drawing::{RaylibDraw, RaylibDrawHandle}; +use raylib::ffi::Color; +use serde::Serialize; +use std::ops::{Deref, DerefMut}; + +pub type RectRoomData = Rect; + +#[derive(Serialize)] +pub struct RectRoom { + data: RectRoomData, +} + +impl RectRoom { + pub fn from_data(data: RectRoomData) -> Self { + RectRoom { data } + } +} + +impl Mappable for RectRoom { + fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) { + rld.draw_rectangle_rec( + transform.rect_m_to_px(&self.data), + Color { + r: 180, + g: 180, + b: 180, + a: 255, + }, + ); + } + + 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 { + 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 + } +} -- cgit v1.2.3-70-g09d2