123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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<Coordinate>,
- pub corner_b: FieldOf<Coordinate>,
- 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<Coordinate>, z: Option<f64>) {
- 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<Coordinate>, z: Option<f64>) {
- delta.map(|delta| {
- *self.corner_a += delta;
- *self.corner_b += delta;
- });
- self.core.move_relative(delta, z);
- }
- }
|