line.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. use std::fmt::Debug;
  2. use binrw::{binrw, BinRead, BinWrite};
  3. use diff::Diff;
  4. use crate::{array_of::ArrayOf, types::Coordinate};
  5. use super::{ObjectCore, Translate};
  6. #[cfg_attr(feature = "default-debug", derive(Debug))]
  7. #[derive(BinRead, BinWrite, Clone, Diff, PartialEq)]
  8. #[diff(attr(
  9. #[derive(Debug, PartialEq)]
  10. ))]
  11. pub enum LineType {
  12. #[brw(magic = 0x0001u16)]
  13. Point { _zero: u32, point: Coordinate },
  14. #[brw(magic = 0x0100u16)]
  15. Line { _zero: u32, points: ArrayOf<Coordinate> },
  16. #[brw(magic = 0x0300u16)]
  17. Bezier { _zero: u32, points: ArrayOf<Coordinate> },
  18. }
  19. // Custom Debug implementation to only print known fields
  20. #[cfg(not(feature = "default-debug"))]
  21. impl Debug for LineType {
  22. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  23. match self {
  24. Self::Point { _zero, point } => f.debug_struct("Point").field("point", point).finish(),
  25. Self::Line { _zero, points } => f.debug_struct("Line").field("points", points).finish(),
  26. Self::Bezier { _zero, points } => {
  27. f.debug_struct("Bezier").field("points", points).finish()
  28. }
  29. }
  30. }
  31. }
  32. impl LineType {
  33. fn move_relative(&mut self, delta: Coordinate) {
  34. match self {
  35. LineType::Point { _zero, point } => {
  36. *point += delta;
  37. },
  38. LineType::Line { _zero, points } => {
  39. points.iter_mut().for_each(|pt| *pt += delta);
  40. },
  41. LineType::Bezier { _zero, points } => {
  42. points.iter_mut().for_each(|pt| *pt += delta);
  43. },
  44. }
  45. }
  46. }
  47. #[binrw]
  48. #[derive(Clone, Debug, Diff, PartialEq)]
  49. #[diff(attr(
  50. #[derive(Debug, PartialEq)]
  51. ))]
  52. pub struct Lines {
  53. pub core: ObjectCore,
  54. #[br(temp)]
  55. #[bw(try_calc(u32::try_from(lines.len())))]
  56. pub num_lines: u32,
  57. pub _unknown_1: u32,
  58. #[br(count = num_lines)]
  59. pub lines: Vec<LineType>,
  60. }
  61. impl Translate for Lines {
  62. fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
  63. origin.map(|origin| {
  64. let delta: Coordinate = *self.core.origin - origin;
  65. self.lines.iter_mut().for_each(|x| x.move_relative(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.lines.iter_mut().for_each(|x| x.move_relative(delta));
  72. });
  73. self.core.move_relative(delta, z);
  74. }
  75. }