ellipse.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 Ellipse {
  15. pub core: ObjectCore,
  16. #[brw(magic(8u32))] // Number of following fields in struct
  17. pub clockwise: U32,
  18. pub corner_a: FieldOf<Coordinate>,
  19. pub corner_b: FieldOf<Coordinate>,
  20. pub start_angle: F64, // Radians
  21. pub end_angle: F64, // Radians
  22. pub modified: ObjectModified,
  23. pub open_curve: U32,
  24. }
  25. // Custom Debug implementation to only print known fields
  26. #[cfg(not(feature = "default-debug"))]
  27. impl Debug for Ellipse {
  28. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  29. f.debug_struct("Ellipse")
  30. .field("core", &self.core)
  31. .field("clockwise", &self.clockwise)
  32. .field("corner_a", &self.corner_a)
  33. .field("corner_b", &self.corner_b)
  34. .field("start_angle", &self.start_angle)
  35. .field("end_angle", &self.end_angle)
  36. .field("open_curve", &self.open_curve)
  37. .finish()
  38. }
  39. }
  40. impl Default for Ellipse {
  41. fn default() -> Self {
  42. Self {
  43. core: ObjectCore::default(ObjectType::Ellipse),
  44. clockwise: 0.into(),
  45. corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
  46. corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
  47. start_angle: 0.0.into(),
  48. end_angle: 0.0.into(),
  49. modified: ObjectModified::default(),
  50. open_curve: 0.into(),
  51. }
  52. }
  53. }
  54. impl Translate for Ellipse {
  55. fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
  56. origin.map(|origin| {
  57. let delta: Coordinate = *self.core.origin - origin;
  58. *self.corner_a += delta;
  59. *self.corner_b += delta;
  60. });
  61. self.core.move_absolute(origin, z);
  62. }
  63. fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
  64. delta.map(|delta| {
  65. *self.corner_a += delta;
  66. *self.corner_b += delta;
  67. });
  68. self.core.move_relative(delta, z);
  69. }
  70. }