Bladeren bron

Skew support and origin refactoring

Kevin Lee 6 maanden geleden
bovenliggende
commit
664cacfc23
5 gewijzigde bestanden met toevoegingen van 81 en 61 verwijderingen
  1. 6 8
      src/ezcad/objects/circle.rs
  2. 18 16
      src/ezcad/objects/ellipse.rs
  3. 26 6
      src/ezcad/objects/mod.rs
  4. 18 18
      src/ezcad/objects/polygon.rs
  5. 13 13
      src/ezcad/objects/rectangle.rs

+ 6 - 8
src/ezcad/objects/circle.rs

@@ -28,15 +28,13 @@ impl Display for Circle {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         write!(
             f,
-            "{}, Origin: {}, Radius: {:.2}",
+            "{}, Origin: {}, Width : {:.2}, Height: {:.2}, Start Radian: {:.2}, Clockwise: {}",
             self.core,
-            Coordinate {
-                x: self.modifier.modifiers.correction.x
-                    + self.drawn_origin.x * self.modifier.modifiers.x_scale,
-                y: self.modifier.modifiers.correction.y
-                    + self.drawn_origin.y * self.modifier.modifiers.y_scale,
-            },
-            self.radius,
+            self.modifier.correction() + *self.drawn_origin * self.modifier.scale(),
+            *self.radius * self.modifier.modifiers.x_scale * 2.0,
+            *self.radius * self.modifier.modifiers.y_scale * 2.0,
+            *self.start_angle,
+            *self.clockwise != 0,
         )
     }
 }

+ 18 - 16
src/ezcad/objects/ellipse.rs

@@ -2,7 +2,6 @@ use std::fmt::{Debug, Display};
 
 use binrw::{BinRead, BinWrite};
 use diff::Diff;
-use num::abs;
 
 use crate::{
     field_of::FieldOf,
@@ -29,26 +28,29 @@ pub struct Ellipse {
 
 impl Display for Ellipse {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        let width: f64 = (self.drawn_corner_a.x.max(self.drawn_corner_b.x)
+            - self.drawn_corner_a.x.min(self.drawn_corner_b.x))
+            * self.modifier.modifiers.x_scale;
+        let height: f64 = (self.drawn_corner_a.y.max(self.drawn_corner_b.y)
+            - self.drawn_corner_a.y.min(self.drawn_corner_b.y))
+            * self.modifier.modifiers.y_scale;
+
+        let drawn_origin: Coordinate =
+            (*self.drawn_corner_a + *self.drawn_corner_b) / Coordinate { x: 2.0, y: 2.0 };
+
         write!(
             f,
             "{}, Origin: {}, Width: {:.2}, Height: {:.2}, Start Radian: {:.2}, End Radian: {:.2}, Open Curve: {}", 
             self.core,
-            Coordinate {
-                x: self.modifier.modifiers.correction.x
-                    + (self.drawn_corner_a.x + self.drawn_corner_b.x) / 2.0
-                        * self.modifier.modifiers.x_scale,
-                y: self.modifier.modifiers.correction.y
-                    + (self.drawn_corner_a.y + self.drawn_corner_b.y) / 2.0
-                        * self.modifier.modifiers.y_scale,
-            },
-            (abs(self.drawn_corner_a.x) + abs(self.drawn_corner_b.x)) / 2.0
-                * self.modifier.modifiers.x_scale,
-            (abs(self.drawn_corner_a.y) + abs(self.drawn_corner_b.y)) / 2.0
-                * self.modifier.modifiers.y_scale,
-                self.start_angle,
-                self.end_angle,
+            self.modifier.correction() + drawn_origin * self.modifier.scale(),
+            width,
+            height,
+                *self.start_angle,
+                *self.end_angle,
                 *self.open_curve != 0,
-        )
+        )?;
+        write!(f, "\n{:?}", self.modifier)?;
+        Ok(())
     }
 }
 

+ 26 - 6
src/ezcad/objects/mod.rs

@@ -291,6 +291,24 @@ impl ObjectModifier {
         self.default = 255.into();
         self.modifiers.correction += delta;
     }
+
+    pub fn correction(&self) -> Coordinate {
+        self.modifiers.correction
+    }
+
+    pub fn scale(&self) -> Coordinate {
+        Coordinate {
+            x: self.modifiers.x_scale,
+            y: self.modifiers.y_scale,
+        }
+    }
+
+    pub fn skew(&self) -> Coordinate {
+        Coordinate {
+            x: self.modifiers.x_skew,
+            y: self.modifiers.y_skew,
+        }
+    }
 }
 
 impl Default for ObjectModifier {
@@ -308,12 +326,12 @@ impl Default for ObjectModifier {
 ))]
 pub struct ScaleValues {
     pub x_scale: f64, // X scaled ratio on drawn object
-    #[br(assert(_unknown_1 == 0.0))]
-    _unknown_1: f64,
+    // #[br(assert(_unknown_1 == 0.0))]
+    pub x_skew: f64,
     #[br(assert(_unknown_2 == 0.0))]
     _unknown_2: f64,
-    #[br(assert(_unknown_3 == 0.0))]
-    _unknown_3: f64,
+    // #[br(assert(_unknown_3 == 0.0))]
+    pub y_skew: f64,
     pub y_scale: f64, // Y scaled ratio on drawn object
     #[br(assert(_unknown_4 == 0.0))]
     _unknown_4: f64,
@@ -326,9 +344,9 @@ impl Default for ScaleValues {
     fn default() -> Self {
         Self {
             x_scale: 1.0.into(),
-            _unknown_1: 0.0.into(),
+            x_skew: 0.0.into(),
             _unknown_2: 0.0.into(),
-            _unknown_3: 0.0.into(),
+            y_skew: 0.0.into(),
             y_scale: 1.0.into(),
             _unknown_4: 0.0.into(),
             correction: (0.0, 0.0).into(),
@@ -343,6 +361,8 @@ impl Debug for ScaleValues {
         f.debug_struct("ScaleValues")
             .field("x_scale", &self.x_scale)
             .field("y_scale", &self.y_scale)
+            .field("x_skew", &self.x_skew)
+            .field("y_skew", &self.y_skew)
             .field("correction", &self.correction)
             .finish()
     }

+ 18 - 18
src/ezcad/objects/polygon.rs

@@ -2,7 +2,6 @@ use std::fmt::{Debug, Display};
 
 use binrw::{BinRead, BinWrite};
 use diff::Diff;
-use num::abs;
 
 use crate::{
     field_of::FieldOf,
@@ -31,28 +30,29 @@ pub struct Polygon {
 
 impl Display for Polygon {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        let width: f64 = (self.drawn_corner_a.x.max(self.drawn_corner_b.x)
+            - self.drawn_corner_a.x.min(self.drawn_corner_b.x))
+            * self.modifier.modifiers.x_scale;
+        let height: f64 = (self.drawn_corner_a.y.max(self.drawn_corner_b.y)
+            - self.drawn_corner_a.y.min(self.drawn_corner_b.y))
+            * self.modifier.modifiers.y_scale;
+
+        let drawn_origin: Coordinate =
+            (*self.drawn_corner_a + *self.drawn_corner_b) / Coordinate { x: 2.0, y: 2.0 };
+
         write!(
             f,
             "{}, Origin: {}, Width: {:.2}, Height: {:.2}, Inverted: {}, Offset CX: {:.2}, Offset CY: {:.2}, Offset DX: {:.2}, Offset: DY: {:.2}, Edges: {}",
             self.core,
-            Coordinate {
-                x: self.modifier.modifiers.correction.x
-                    + (self.drawn_corner_a.x + self.drawn_corner_b.x) / 2.0
-                        * self.modifier.modifiers.x_scale,
-                y: self.modifier.modifiers.correction.y
-                    + (self.drawn_corner_a.y + self.drawn_corner_b.y) / 2.0
-                        * self.modifier.modifiers.y_scale,
-            },
-            (abs(self.drawn_corner_a.x) + abs(self.drawn_corner_b.x)) / 2.0
-                * self.modifier.modifiers.x_scale,
-            (abs(self.drawn_corner_a.y) + abs(self.drawn_corner_b.y)) / 2.0
-                * self.modifier.modifiers.y_scale,
+            self.modifier.correction() + drawn_origin * self.modifier.scale(),
+            width,
+            height,
             *self.invert_shape != 0,
-            self.offset_cx,
-            self.offset_cy,
-            self.offset_dx,
-            self.offset_dy,
-            self.edges,
+            *self.offset_cx,
+            *self.offset_cy,
+            *self.offset_dx,
+            *self.offset_dy,
+            *self.edges,
         )
     }
 }

+ 13 - 13
src/ezcad/objects/rectangle.rs

@@ -2,7 +2,6 @@ use std::fmt::{Debug, Display};
 
 use binrw::{BinRead, BinWrite};
 use diff::Diff;
-use num::abs;
 
 use crate::{
     field_of::FieldOf,
@@ -29,22 +28,23 @@ pub struct Rectangle {
 
 impl Display for Rectangle {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        let width: f64 = (self.drawn_corner_a.x.max(self.drawn_corner_b.x)
+            - self.drawn_corner_a.x.min(self.drawn_corner_b.x))
+            * self.modifier.modifiers.x_scale;
+        let height: f64 = (self.drawn_corner_a.y.max(self.drawn_corner_b.y)
+            - self.drawn_corner_a.y.min(self.drawn_corner_b.y))
+            * self.modifier.modifiers.y_scale;
+
+        let drawn_origin: Coordinate =
+            (*self.drawn_corner_a + *self.drawn_corner_b) / Coordinate { x: 2.0, y: 2.0 };
+
         write!(
             f,
             "{}, Origin: {}, Width: {:.2}, Height: {:.2}",
             self.core,
-            Coordinate {
-                x: self.modifier.modifiers.correction.x
-                    + (self.drawn_corner_a.x + self.drawn_corner_b.x) / 2.0
-                        * self.modifier.modifiers.x_scale,
-                y: self.modifier.modifiers.correction.y
-                    + (self.drawn_corner_a.y + self.drawn_corner_b.y) / 2.0
-                        * self.modifier.modifiers.y_scale,
-            },
-            (abs(self.drawn_corner_a.x) + abs(self.drawn_corner_b.x)) / 2.0
-                * self.modifier.modifiers.x_scale,
-            (abs(self.drawn_corner_a.y) + abs(self.drawn_corner_b.y)) / 2.0
-                * self.modifier.modifiers.y_scale,
+            self.modifier.correction() + drawn_origin * self.modifier.scale(),
+            width,
+            height,
         )
     }
 }