瀏覽代碼

Pen config work

Kevin Lee 1 年之前
父節點
當前提交
109f5a1610
共有 8 個文件被更改,包括 144 次插入27 次删除
  1. 4 3
      config.yml
  2. 112 2
      src/config.rs
  3. 2 2
      src/ezcad/array_of.rs
  4. 1 1
      src/ezcad/field_of.rs
  5. 2 2
      src/ezcad/file.rs
  6. 1 1
      src/ezcad/pen.rs
  7. 5 5
      src/ezcad/types.rs
  8. 17 11
      src/main.rs

+ 4 - 3
config.yml

@@ -1,3 +1,4 @@
-- !Pen
-  id: 0
-  color: [255, 255, 255]
+Pens: 
+  - !Patch
+    Id: 0
+    Enabled: true

+ 112 - 2
src/config.rs

@@ -1,12 +1,122 @@
+use ezcad::{pen::Pen, types::Rgba};
+use log::trace;
 use serde::{Deserialize, Serialize};
 
 #[derive(Debug, Serialize, Deserialize)]
-pub struct Pen {
+#[serde(rename_all = "PascalCase")]
+pub struct PatchPen {
     pub id: u8,
     pub color: Option<(u8, u8, u8)>,
-    pub disabled: Option<bool>,
+    pub enabled: Option<bool>,
     pub loop_count: Option<u32>,
     pub speed: Option<f64>,
     pub power: Option<f64>,
     pub frequency: Option<u32>,
 }
+
+impl PatchPen {
+    fn patch(&self, target: &mut Vec<Pen>) {
+        trace!("Patching settings for pen #{}", self.id);
+
+        let to_patch = target.get_mut(self.id as usize).unwrap();
+
+        if let Some(color) = self.color {
+            *to_patch.color = Rgba {
+                red: color.0,
+                green: color.1,
+                blue: color.2,
+                alpha: 0,
+            };
+        }
+
+        if let Some(enabled) = self.enabled {
+            *to_patch.disabled = !enabled as u32;
+        }
+
+        if let Some(loop_count) = self.loop_count {
+            *to_patch.loop_count = loop_count;
+        }
+
+        if let Some(speed) = self.speed {
+            *to_patch.speed = speed;
+        }
+
+        if let Some(power) = self.power {
+            *to_patch.power = power;
+        }
+
+        if let Some(frequency) = self.frequency {
+            *to_patch.frequency = frequency;
+            *to_patch.frequency_2 = frequency.try_into().unwrap();
+        }
+    }
+}
+
+#[derive(Debug, Serialize, Deserialize)]
+pub struct ClonePen {
+    source_id: u8,
+    destination_id: u8,
+}
+
+impl ClonePen {
+    fn clone(&self, target: &mut Vec<Pen>) {
+        trace!(
+            "Cloning pen #{} to #{}",
+            self.source_id,
+            self.destination_id
+        );
+
+        let source = target.get(self.source_id as usize).unwrap().clone();
+        *target.get_mut(self.destination_id as usize).unwrap() = source;
+    }
+}
+
+#[derive(Debug, Serialize, Deserialize)]
+pub struct PatternPen {}
+
+#[derive(Debug, Serialize, Deserialize)]
+pub enum PenOperation {
+    Patch(PatchPen),
+    Clone(ClonePen),
+    Pattern(PatternPen),
+}
+
+pub trait PatchPens {
+    fn patch(&self, target: &mut Vec<Pen>);
+}
+
+impl PatchPens for Vec<PenOperation> {
+    fn patch(&self, target: &mut Vec<Pen>) {
+        for op in self {
+            match op {
+                PenOperation::Patch(x) => x.patch(target),
+                PenOperation::Clone(x) => x.clone(target),
+                PenOperation::Pattern(_) => todo!(),
+            }
+        }
+    }
+}
+
+#[derive(Debug, Serialize, Deserialize)]
+#[serde(rename_all = "PascalCase")]
+pub struct Config {
+    pub pens: Vec<PenOperation>,
+}
+
+// #[test]
+// fn test() {
+//     let config = Config {
+//         modify_pens: vec![ModifyPen {
+//             id: 0,
+//             color: None,
+//             disabled: None,
+//             loop_count: None,
+//             speed: None,
+//             power: None,
+//             frequency: None,
+//         }],
+//     };
+
+//     dbg!(serde_yaml::to_string(&config).unwrap());
+//     panic!()
+// }

+ 2 - 2
src/ezcad/array_of.rs

@@ -18,7 +18,7 @@ impl<T> BinReadWrite for T where
 
 /// Wrapper to prefix array of primitives (u8/u32/etc)
 #[binrw]
-#[derive(PartialEq)]
+#[derive(Clone, PartialEq)]
 pub struct ArrayOfPrimitive<T, S = u32>
 where
     // Static bounds due to https://github.com/jam1garner/binrw/issues/199
@@ -55,7 +55,7 @@ where
 
 /// Wrapper to prefix array of single non-primitive type
 #[binrw]
-#[derive(PartialEq)]
+#[derive(Clone, PartialEq)]
 pub struct ArrayOf<T, S = u32>
 where
     // Static bounds due to https://github.com/jam1garner/binrw/issues/199

+ 1 - 1
src/ezcad/field_of.rs

@@ -7,7 +7,7 @@ use std::{
 
 /// Wrapper to prefix arbitrary types with size of type as u32
 #[binrw]
-#[derive(PartialEq)]
+#[derive(Clone, PartialEq)]
 pub struct FieldOf<T>
 where
     T: for<'a> BinRead<Args<'a> = ()>,

+ 2 - 2
src/ezcad/file.rs

@@ -5,7 +5,7 @@ use binrw::{BinRead, BinResult, BinWrite, BinWriterExt, Endian, FilePtr64};
 use crate::{array_of::ArrayOf, layer::Layer, pen::PenHeader};
 
 #[derive(BinRead, Debug)]
-pub struct Header {
+pub struct EzCadHeader {
     _magic: [u16; 8],
     _unknown_1: [u8; 0x150],
     pub thumbnail_offset: FilePtr64<Thumbnail>,
@@ -17,7 +17,7 @@ pub struct Header {
 
 // Manually implement BinWrite as FilePtr does not support serialization
 // See: https://github.com/jam1garner/binrw/issues/4
-impl BinWrite for Header {
+impl BinWrite for EzCadHeader {
     type Args<'a> = ();
 
     fn write_options<W: Write + Seek>(

+ 1 - 1
src/ezcad/pen.rs

@@ -47,7 +47,7 @@ pub struct Pens {
     pub pens: Vec<Pen>,
 }
 
-#[derive(BinRead, BinWrite)]
+#[derive(BinRead, BinWrite, Clone)]
 #[brw(magic(236u32))] // Number of fields within this struct
 pub struct Pen {
     pub color: FieldOf<Rgba>,

+ 5 - 5
src/ezcad/types.rs

@@ -12,7 +12,7 @@ pub type U16 = FieldOf<u16>;
 pub type F64 = FieldOf<f64>;
 
 #[binrw]
-#[derive(PartialEq)]
+#[derive(Clone, PartialEq)]
 pub struct WString {
     // Temporary variable that holds number of elements
     #[br(temp)]
@@ -48,7 +48,7 @@ impl Display for WString {
     }
 }
 
-#[derive(BinRead, BinWrite, PartialEq, Debug)]
+#[derive(Clone, Debug, PartialEq, BinRead, BinWrite)]
 pub struct Rgba {
     pub red: u8,
     pub green: u8,
@@ -56,13 +56,13 @@ pub struct Rgba {
     pub alpha: u8,
 }
 
-#[derive(BinRead, BinWrite, PartialEq, Debug)]
+#[derive(Clone, Debug, PartialEq, BinRead, BinWrite)]
 pub struct Point {
     pub x: f64,
     pub y: f64,
 }
 
-#[derive(BinRead, BinWrite, PartialEq, Debug, strum::Display)]
+#[derive(Clone, Debug, PartialEq, BinRead, BinWrite, strum::Display)]
 #[brw(repr(u32))]
 #[repr(u32)]
 pub enum WobbleType {
@@ -73,7 +73,7 @@ pub enum WobbleType {
     Hori8 = 4,
 }
 
-#[derive(BinRead, BinWrite, PartialEq, Debug, strum::Display)]
+#[derive(Clone, Debug, PartialEq, BinRead, BinWrite, strum::Display)]
 #[brw(repr(u16))]
 #[repr(u16)]
 pub enum ObjectType {

+ 17 - 11
src/main.rs

@@ -1,17 +1,12 @@
-use std::{
-    fs::{self, read_to_string, File},
-    io::Read,
-    path::PathBuf,
-    time::Instant,
-};
+use std::{fs::File, path::PathBuf, time::Instant};
 
 use binrw::{BinRead, BinWriterExt};
 use clap::Parser;
 use clap_verbosity_flag::{InfoLevel, Verbosity};
-use ezcad::file::Header;
+use ezcad::file::EzCadHeader;
 use log::{debug, info};
 
-use crate::config::Pen;
+use crate::config::{Config, PatchPens};
 
 mod config;
 
@@ -44,13 +39,21 @@ fn main() {
     let mut input: File = File::open(cli.input).unwrap();
 
     let time: Instant = Instant::now();
-    let file: Header = Header::read_le(&mut input).unwrap();
+    let mut file: EzCadHeader = EzCadHeader::read_le(&mut input).unwrap();
     info!("Decode time: {:?}", time.elapsed());
 
-    for pen in file.pens_offset.data.pens.iter().take(1) {
+    // Print info on pens with non-default settings
+    for pen in file
+        .pens_offset
+        .data
+        .pens
+        .iter()
+        .filter(|x| *x.use_default == 0)
+    {
         debug!("{:#?}", pen);
     }
 
+    // Print all objects
     for layer in file.layers_offset.iter() {
         for object in layer.objects.iter() {
             debug!("{:#?}", object);
@@ -60,8 +63,11 @@ fn main() {
     if let Some(config) = cli.config {
         info!("Processing config file {}", config.to_string_lossy());
         let config: String = std::fs::read_to_string(config).unwrap();
-        let config: Vec<Pen> = serde_yaml::from_str(&config).unwrap();
+        let config: Config = serde_yaml::from_str(&config).unwrap();
         debug!("{:#?}", config);
+
+        // Patch pen settings
+        config.pens.patch(&mut file.pens_offset.data.pens);
     }
 
     if let Some(output) = cli.output {