123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- use std::fmt::Debug;
- use binrw::{binrw, BinRead, BinWrite};
- use diff::Diff;
- use crate::{array_of::ArrayOf, types::Coordinate};
- use super::{ObjectCore, Translate};
- #[cfg_attr(feature = "default-debug", derive(Debug))]
- #[derive(BinRead, BinWrite, Clone, Diff, PartialEq)]
- #[diff(attr(
- #[derive(Debug, PartialEq)]
- ))]
- pub enum LineType {
- #[brw(magic = 0x0001u16)]
- Point { _zero: u32, point: Coordinate },
- #[brw(magic = 0x0100u16)]
- Line {
- _zero: u32,
- points: ArrayOf<Coordinate>,
- },
- #[brw(magic = 0x0300u16)]
- Bezier {
- _zero: u32,
- points: ArrayOf<Coordinate>,
- },
- }
- // Custom Debug implementation to only print known fields
- #[cfg(not(feature = "default-debug"))]
- impl Debug for LineType {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match self {
- Self::Point { _zero, point } => f.debug_struct("Point").field("point", point).finish(),
- Self::Line { _zero, points } => f.debug_struct("Line").field("points", points).finish(),
- Self::Bezier { _zero, points } => {
- f.debug_struct("Bezier").field("points", points).finish()
- }
- }
- }
- }
- impl LineType {
- fn move_relative(&mut self, delta: Coordinate) {
- match self {
- LineType::Point { _zero, point } => {
- *point += delta;
- }
- LineType::Line { _zero, points } => {
- points.iter_mut().for_each(|pt| *pt += delta);
- }
- LineType::Bezier { _zero, points } => {
- points.iter_mut().for_each(|pt| *pt += delta);
- }
- }
- }
- }
- #[binrw]
- #[derive(Clone, Debug, Diff, PartialEq)]
- #[diff(attr(
- #[derive(Debug, PartialEq)]
- ))]
- pub struct Lines {
- pub core: ObjectCore,
- #[br(temp)]
- #[bw(try_calc(u32::try_from(lines.len())))]
- pub num_lines: u32,
- pub _unknown_1: u32,
- #[br(count = num_lines)]
- pub lines: Vec<LineType>,
- }
- impl Translate for Lines {
- fn move_absolute(&mut self, origin: Option<Coordinate>, z: Option<f64>) {
- origin.map(|origin| {
- let delta: Coordinate = *self.core.origin - origin;
- self.lines.iter_mut().for_each(|x| x.move_relative(delta));
- });
- self.core.move_absolute(origin, z);
- }
- fn move_relative(&mut self, delta: Option<Coordinate>, z: Option<f64>) {
- delta.map(|delta| {
- self.lines.iter_mut().for_each(|x| x.move_relative(delta));
- });
- self.core.move_relative(delta, z);
- }
- }
|