polygon.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. use std::fmt::Debug;
  2. use binrw::{BinRead, BinWrite};
  3. use diff::Diff;
  4. use crate::{
  5. field_of::FieldOf,
  6. types::{ObjectType, Coordinate, F64, U32},
  7. };
  8. use super::{ObjectCore, Translate, ObjectModified};
  9. #[cfg_attr(feature = "default-debug", derive(Debug))]
  10. #[derive(BinRead, BinWrite, Clone, Diff, PartialEq)]
  11. #[diff(attr(
  12. #[derive(Debug, PartialEq)]
  13. ))]
  14. pub struct Polygon {
  15. pub core: ObjectCore,
  16. #[brw(magic(10u32))] // Number of following fields in struct
  17. pub invert_shape: U32,
  18. pub corner_a: FieldOf<Coordinate>,
  19. pub corner_b: FieldOf<Coordinate>,
  20. pub offset_cx: F64,
  21. pub offset_cy: F64,
  22. pub offset_dx: F64,
  23. pub offset_dy: F64,
  24. pub edges: U32,
  25. pub modified: ObjectModified,
  26. }
  27. // Custom Debug implementation to only print known fields
  28. #[cfg(not(feature = "default-debug"))]
  29. impl Debug for Polygon {
  30. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  31. f.debug_struct("Polygon")
  32. .field("core", &self.core)
  33. .field("invert_shape", &self.invert_shape)
  34. .field("corner_a", &self.corner_a)
  35. .field("corner_b", &self.corner_b)
  36. .field("offset_cx", &self.offset_cx)
  37. .field("offset_cy", &self.offset_cy)
  38. .field("offset_dx", &self.offset_dx)
  39. .field("offset_dy", &self.offset_dy)
  40. .field("edges", &self.edges)
  41. .finish()
  42. }
  43. }
  44. impl Default for Polygon {
  45. fn default() -> Self {
  46. Self {
  47. core: ObjectCore::default(ObjectType::Polygon),
  48. invert_shape: 1.into(),
  49. corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
  50. corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
  51. offset_cx: 0.0.into(),
  52. offset_cy: 0.0.into(),
  53. offset_dx: 0.0.into(),
  54. offset_dy: 0.0.into(),
  55. edges: 6.into(),
  56. modified: ObjectModified::default(),
  57. }
  58. }
  59. }
  60. impl Translate for Polygon {
  61. fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
  62. origin.map(|origin| {
  63. let delta: Coordinate = *self.core.origin - origin;
  64. *self.corner_a += delta;
  65. *self.corner_b += delta;
  66. });
  67. self.core.move_absolute(origin, z);
  68. }
  69. fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
  70. delta.map(|delta| {
  71. *self.corner_a += delta;
  72. *self.corner_b += delta;
  73. });
  74. self.core.move_relative(delta, z);
  75. }
  76. }