Browse Source

Additional pen cloning

Kevin Lee 1 year ago
parent
commit
8c4a188ada
4 changed files with 40 additions and 32 deletions
  1. 17 25
      README.md
  2. 3 1
      src/config/mod.rs
  3. 20 5
      src/config/pen.rs
  4. 0 1
      src/ezcad/objects/mod.rs

+ 17 - 25
README.md

@@ -28,7 +28,7 @@ Setting values of specific fields for a given pen:
 ``` yaml
 Ops: 
   - !PatchPen
-    Pen: 0                  # Required - target pen
+    Pen: 0                  # Target pen
     Color: [127, 127, 127]  # Optional - target pen RGB color
     Enabled: true           # Optional - target pen enable flag
     LoopCount: 3            # Optional - target pen loop count
@@ -42,8 +42,8 @@ Cloning one pen to another (and optionally override settings):
 ``` yaml
 Ops: 
   - !ClonePen
-    From: 0                 # Required - source pen
-    To: 1                   # Required - target pen
+    From: 0                 # Source pen
+    To: 1                   # Target pen
     # Color: [64, 64, 64]   # Optional - target pen RGB color
     # Enabled: true         # Optional - target pen enable flag
     # LoopCount: 3          # Optional - target pen loop count
@@ -52,13 +52,22 @@ Ops:
     # Frequency: 10000      # Optional - target pen frequency
 ```
 
+Cloning one pen to another pen (and all pens in between)
+
+``` yaml
+Ops:
+  - !ClonePenRange
+    From: 0
+    To: 3
+```
+
 Incrementing a specific field by some value across a sequence of pens:
 
 ``` yaml
 Ops: 
   - !PatternPen
-    From: 1                 # Required - starting pen to copy settings from
-    To: 5                   # Required - ending pen to stop at (inclusive)
+    From: 1                   # Starting pen to copy settings from
+    To: 5                     # Ending pen to stop at (inclusive)
     # Field: !Loops 1         # Choose one
     # Field: !Speed 100.0     # Choose one
     # Field: !Power 10.0      # Choose one
@@ -95,22 +104,13 @@ Ops:
     Path: "exported_object.bin"
 ```
 
-Importing and append an object from a file:
-
-``` yaml
-Ops:
-  - !ImportObject
-    Layer: 0
-    Path: "exported_object.bin"
-```
-
-Importing and replace an existing object from a file:
+Importing an object from a file:
 
 ``` yaml
 Ops:
   - !ImportObject
     Layer: 0
-    Object: 3
+    Object: 3 # Optional, will replace specified object instead
     Path: "exported_object.bin"
 ```
 
@@ -122,13 +122,5 @@ Deleting all objects from a layer:
 Ops:
   - !DeleteObjects
     Layer: 0
-```
-
-Deleting a specific object from a layer:
-
-``` yaml
-Ops:
-  - !DeleteObjects
-    Layer: 0
-    Object: 3
+    Object: 3 # Optional, will delete specific object instead
 ```

+ 3 - 1
src/config/mod.rs

@@ -13,6 +13,7 @@ pub mod pen;
 pub enum Operation {
     PatchPen(PatchPen),
     ClonePen(ClonePen),
+    ClonePenRange(ClonePen),
     PatternPen(PatternPen),
     ExportPen(ImportExportPen),
     ImportPen(ImportExportPen),
@@ -31,7 +32,8 @@ impl Operations for Vec<Operation> {
         for op in self {
             match op {
                 Operation::PatchPen(x) => x.patch(pens),
-                Operation::ClonePen(x) => x.clone(pens),
+                Operation::ClonePen(x) => x.clone_single(pens),
+                Operation::ClonePenRange(x) => x.clone_range(pens),
                 Operation::PatternPen(x) => x.pattern(pens),
                 Operation::ImportPen(x) => x.import(pens),
                 Operation::ExportPen(x) => x.export(pens),

+ 20 - 5
src/config/pen.rs

@@ -110,12 +110,8 @@ pub struct ClonePen {
 }
 
 impl ClonePen {
-    pub fn clone(&self, pens: &mut Vec<Pen>) {
+    pub fn clone_single(&self, pens: &mut Vec<Pen>) {
         debug!("Cloning pen #{} to #{}", self.from, self.to);
-        assert!(
-            self.to > self.from,
-            "Target pen must be greater than source pen"
-        );
 
         // Clone pen
         let src: Pen = pens.get(self.from).expect("Invalid pen index").clone();
@@ -127,6 +123,25 @@ impl ClonePen {
             patch.patch(self.to, pens);
         }
     }
+
+    pub fn clone_range(&self, pens: &mut Vec<Pen>) {
+        debug!(
+            "Cloning pen #{} to #{} (and all in between)",
+            self.from, self.to
+        );
+        assert!(
+            self.to > self.from,
+            "Target pen(s) must be greater than source pen"
+        );
+
+        // Clone pen
+        let src: Pen = pens.get(self.from).expect("Invalid pen index").clone();
+
+        for idx in (self.from..=self.to).skip(1) {
+            let dst: &mut Pen = pens.get_mut(idx).expect("Invalid pen index");
+            *dst = src.clone();
+        }
+    }
 }
 
 #[derive(Debug, Serialize, Deserialize)]

+ 0 - 1
src/ezcad/objects/mod.rs

@@ -8,7 +8,6 @@ use modular_bitfield::{
 };
 
 use crate::{
-    array_of::ArrayOfPrimitive,
     field_of::FieldOf,
     types::{Field, ObjectType, Point, WString, F64, U16, U32},
 };