rectangle.rs 2.4 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::{Coordinate, ObjectType, F64},
  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 Rectangle {
  15. pub core: ObjectCore,
  16. #[brw(magic(8u32))] // Number of following fields in struct
  17. pub corner_a: FieldOf<Coordinate>,
  18. pub corner_b: FieldOf<Coordinate>,
  19. pub round_bottom_left: F64,
  20. pub round_bottom_right: F64,
  21. pub round_top_right: F64,
  22. pub round_top_left: F64,
  23. pub modified: ObjectModified,
  24. }
  25. // Custom Debug implementation to only print known fields
  26. #[cfg(not(feature = "default-debug"))]
  27. impl Debug for Rectangle {
  28. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  29. f.debug_struct("Rectangle")
  30. .field("core", &self.core)
  31. .field("corner_a", &self.corner_a)
  32. .field("corner_b", &self.corner_b)
  33. .field("round_bottom_left", &self.round_bottom_left)
  34. .field("round_bottom_right", &self.round_bottom_right)
  35. .field("round_top_right", &self.round_top_right)
  36. .field("round_top_left", &self.round_top_left)
  37. .finish()
  38. }
  39. }
  40. impl Default for Rectangle {
  41. fn default() -> Self {
  42. Self {
  43. core: ObjectCore::default(ObjectType::Rectangle),
  44. corner_a: Coordinate { x: 0.0, y: 0.0 }.into(),
  45. corner_b: Coordinate { x: 0.0, y: 0.0 }.into(),
  46. round_bottom_left: 0.0.into(),
  47. round_bottom_right: 0.0.into(),
  48. round_top_right: 0.0.into(),
  49. round_top_left: 0.0.into(),
  50. modified: ObjectModified::default(),
  51. }
  52. }
  53. }
  54. impl Translate for Rectangle {
  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. }