line.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. use std::fmt::Debug;
  2. use binrw::{binrw, BinRead, BinWrite};
  3. use diff::Diff;
  4. use crate::{array_of::ArrayOf, types::Point};
  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: Point },
  14. #[brw(magic = 0x0100u16)]
  15. Line { _zero: u32, points: ArrayOf<Point> },
  16. #[brw(magic = 0x0300u16)]
  17. Bezier { _zero: u32, points: ArrayOf<Point> },
  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 Translate for LineType {
  33. fn translate(&mut self, origin: Option<Point>, _z: Option<f64>) {
  34. origin.map(|origin| match self {
  35. LineType::Point { _zero, point } => *point = origin,
  36. LineType::Line { _zero, points } => {
  37. points.iter_mut().for_each(|pt| *pt += origin);
  38. }
  39. LineType::Bezier { _zero, points } => {
  40. points.iter_mut().for_each(|pt| *pt += origin);
  41. }
  42. });
  43. }
  44. }
  45. #[binrw]
  46. #[derive(Clone, Debug, Diff, PartialEq)]
  47. #[diff(attr(
  48. #[derive(Debug, PartialEq)]
  49. ))]
  50. pub struct Lines {
  51. pub core: ObjectCore,
  52. #[br(temp)]
  53. #[bw(try_calc(u32::try_from(lines.len())))]
  54. pub num_lines: u32,
  55. pub _unknown_1: u32,
  56. #[br(count = num_lines)]
  57. pub lines: Vec<LineType>,
  58. }
  59. impl Translate for Lines {
  60. fn translate(&mut self, origin: Option<Point>, z: Option<f64>) {
  61. self.core.translate(origin, z);
  62. self.lines.iter_mut().for_each(|x| x.translate(origin, z));
  63. }
  64. }