line.rs 2.5 KB

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