use std::fmt::Debug; use binrw::{BinRead, BinWrite}; use diff::Diff; use crate::{ field_of::FieldOf, types::{Coordinate, ObjectType, F64}, }; use super::{ObjectCore, Translate, ObjectModified}; #[cfg_attr(feature = "default-debug", derive(Debug))] #[derive(BinRead, BinWrite, Clone, Diff, PartialEq)] #[diff(attr( #[derive(Debug, PartialEq)] ))] pub struct Rectangle { pub core: ObjectCore, #[brw(magic(8u32))] // Number of following fields in struct pub corner_a: FieldOf, pub corner_b: FieldOf, pub round_bottom_left: F64, pub round_bottom_right: F64, pub round_top_right: F64, pub round_top_left: F64, pub modified: ObjectModified, } // Custom Debug implementation to only print known fields #[cfg(not(feature = "default-debug"))] impl Debug for Rectangle { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Rectangle") .field("core", &self.core) .field("corner_a", &self.corner_a) .field("corner_b", &self.corner_b) .field("round_bottom_left", &self.round_bottom_left) .field("round_bottom_right", &self.round_bottom_right) .field("round_top_right", &self.round_top_right) .field("round_top_left", &self.round_top_left) .finish() } } impl Default for Rectangle { fn default() -> Self { Self { core: ObjectCore::default(ObjectType::Rectangle), corner_a: Coordinate { x: 0.0, y: 0.0 }.into(), corner_b: Coordinate { x: 0.0, y: 0.0 }.into(), round_bottom_left: 0.0.into(), round_bottom_right: 0.0.into(), round_top_right: 0.0.into(), round_top_left: 0.0.into(), modified: ObjectModified::default(), } } } impl Translate for Rectangle { fn move_absolute(&mut self, origin: Option, z: Option) { origin.map(|origin| { let delta: Coordinate = *self.core.origin - origin; *self.corner_a += delta; *self.corner_b += delta; }); self.core.move_absolute(origin, z); } fn move_relative(&mut self, delta: Option, z: Option) { delta.map(|delta| { *self.corner_a += delta; *self.corner_b += delta; }); self.core.move_relative(delta, z); } }