polygon.rs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. use std::fmt::Debug;
  2. use binrw::{BinRead, BinWrite};
  3. use diff::Diff;
  4. use crate::{
  5. field_of::FieldOf,
  6. types::{Coordinate, ObjectType, F64, U32},
  7. };
  8. use super::{ObjectCore, ObjectModifier, Translate};
  9. #[derive(BinRead, BinWrite, Clone, Debug, Diff, PartialEq)]
  10. #[diff(attr(
  11. #[derive(Debug, PartialEq)]
  12. ))]
  13. pub struct Polygon {
  14. pub core: ObjectCore,
  15. #[brw(magic(10u32))] // Number of following fields in struct
  16. pub invert_shape: U32,
  17. pub drawn_corner_a: FieldOf<Coordinate>,
  18. pub drawn_corner_b: FieldOf<Coordinate>,
  19. pub offset_cx: F64,
  20. pub offset_cy: F64,
  21. pub offset_dx: F64,
  22. pub offset_dy: F64,
  23. pub edges: U32,
  24. pub modifier: ObjectModifier,
  25. }
  26. impl Default for Polygon {
  27. fn default() -> Self {
  28. Self {
  29. core: ObjectCore::default(ObjectType::Polygon),
  30. invert_shape: 1.into(),
  31. drawn_corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
  32. drawn_corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
  33. offset_cx: 0.0.into(),
  34. offset_cy: 0.0.into(),
  35. offset_dx: 0.0.into(),
  36. offset_dy: 0.0.into(),
  37. edges: 6.into(),
  38. modifier: ObjectModifier::default(),
  39. }
  40. }
  41. }
  42. impl Translate for Polygon {
  43. fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
  44. origin.map(|origin| {
  45. let delta: Coordinate = origin - *self.core.origin;
  46. self.modifier.move_relative(delta);
  47. });
  48. self.core.move_absolute(origin, z);
  49. }
  50. fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
  51. delta.map(|delta| {
  52. self.modifier.move_relative(delta);
  53. });
  54. self.core.move_relative(delta, z);
  55. }
  56. }