123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- use std::fmt::Debug;
- use binrw::{BinRead, BinWrite};
- use diff::Diff;
- use crate::{
- field_of::FieldOf,
- types::{ObjectType, Coordinate, F64, U32},
- };
- 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 Polygon {
- pub core: ObjectCore,
- #[brw(magic(10u32))] // Number of following fields in struct
- pub invert_shape: U32,
- pub corner_a: FieldOf<Coordinate>,
- pub corner_b: FieldOf<Coordinate>,
- pub offset_cx: F64,
- pub offset_cy: F64,
- pub offset_dx: F64,
- pub offset_dy: F64,
- pub edges: U32,
- pub modified: ObjectModified,
- }
- // Custom Debug implementation to only print known fields
- #[cfg(not(feature = "default-debug"))]
- impl Debug for Polygon {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- f.debug_struct("Polygon")
- .field("core", &self.core)
- .field("invert_shape", &self.invert_shape)
- .field("corner_a", &self.corner_a)
- .field("corner_b", &self.corner_b)
- .field("offset_cx", &self.offset_cx)
- .field("offset_cy", &self.offset_cy)
- .field("offset_dx", &self.offset_dx)
- .field("offset_dy", &self.offset_dy)
- .field("edges", &self.edges)
- .finish()
- }
- }
- impl Default for Polygon {
- fn default() -> Self {
- Self {
- core: ObjectCore::default(ObjectType::Polygon),
- invert_shape: 1.into(),
- corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
- corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
- offset_cx: 0.0.into(),
- offset_cy: 0.0.into(),
- offset_dx: 0.0.into(),
- offset_dy: 0.0.into(),
- edges: 6.into(),
- modified: ObjectModified::default(),
- }
- }
- }
- impl Translate for Polygon {
- 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);
- }
- }
|