Browse Source

Fixed shape serialization

flabbet 11 months ago
parent
commit
8f74642d4d

+ 15 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/VectorLayerNode.cs

@@ -2,6 +2,8 @@
 using PixiEditor.ChangeableDocument.Changeables.Animations;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Shapes.Data;
+using PixiEditor.ChangeableDocument.Changeables.Interfaces;
+using PixiEditor.ChangeableDocument.ChangeInfos.Vectors;
 using PixiEditor.ChangeableDocument.Rendering;
 using PixiEditor.DrawingApi.Core;
 using PixiEditor.DrawingApi.Core.Numerics;
@@ -50,6 +52,19 @@ public class VectorLayerNode : LayerNode, ITransformableObject, IReadOnlyVectorN
         return texture;
     }
 
+    public override void SerializeAdditionalData(Dictionary<string, object> additionalData)
+    {
+        base.SerializeAdditionalData(additionalData);
+        additionalData["ShapeData"] = ShapeData;
+    }
+
+    internal override OneOf<None, IChangeInfo, List<IChangeInfo>> DeserializeAdditionalData(IReadOnlyDocument target, IReadOnlyDictionary<string, object> data)
+    {
+        base.DeserializeAdditionalData(target, data);
+        ShapeData = (ShapeVectorData)data["ShapeData"];
+        return new VectorShape_ChangeInfo(Id);
+    }
+
     protected override bool CacheChanged(RenderingContext context)
     {
         return base.CacheChanged(context) || (ShapeData?.GetCacheHash() ?? -1) != lastCacheHash;

+ 14 - 9
src/PixiEditor/Helpers/ServiceCollectionHelpers.cs

@@ -108,15 +108,7 @@ internal static class ServiceCollectionHelpers
             .AddSingleton<IoFileType, Mp4FileType>()
             .AddSingleton<IoFileType, SvgFileType>()
             // Serialization Factories
-            .AddSingleton<SerializationFactory, SurfaceSerializationFactory>()
-            .AddSingleton<SerializationFactory, ChunkyImageSerializationFactory>()
-            .AddSingleton<SerializationFactory, KernelSerializationFactory>()
-            .AddSingleton<SerializationFactory, VecDSerializationFactory>()
-            .AddSingleton<SerializationFactory, VecISerializationFactory>()
-            .AddSingleton<SerializationFactory, ColorSerializationFactory>()
-            .AddSingleton<SerializationFactory, ColorMatrixSerializationFactory>()
-            .AddSingleton<SerializationFactory, VecD3SerializationFactory>()
-            .AddSingleton<SerializationFactory, TextureSerializationFactory>()
+            .AddAssemblyTypes<SerializationFactory>()
             // Palette Parsers
             .AddSingleton<IPalettesProvider, PaletteProvider>()
             .AddSingleton<PaletteFileParser, JascFileParser>()
@@ -160,6 +152,19 @@ internal static class ServiceCollectionHelpers
         }
     }
     
+    private static IServiceCollection AddAssemblyTypes<T>(this IServiceCollection collection)
+    {
+        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
+        IEnumerable<Type> types = assemblies.SelectMany(x => x.GetTypes())
+            .Where(x => typeof(T).IsAssignableFrom(x) && x is { IsInterface: false, IsAbstract: false });
+        foreach (Type type in types)
+        {
+            collection.AddSingleton(typeof(T), type);
+        }
+
+        return collection;
+    }
+    
     private static IServiceCollection AddTool<T, T1>(this IServiceCollection collection)
         where T : class, IToolHandler where T1 : class, T
     {

+ 1 - 1
src/PixiEditor/Models/Serialization/Factories/ByteBuilder.cs

@@ -22,7 +22,7 @@ public class ByteBuilder
     {
         foreach (var value in matrix.Values)
         {
-            _data.AddRange(BitConverter.GetBytes(value));
+            _data.AddRange(BitConverter.GetBytes((double)value));
         } 
         
         return this;

+ 7 - 24
src/PixiEditor/Models/Serialization/Factories/EllipseSerializationFactory.cs

@@ -5,39 +5,22 @@ using PixiEditor.Numerics;
 
 namespace PixiEditor.Models.Serialization.Factories;
 
-public class EllipseSerializationFactory : SerializationFactory<byte[], EllipseVectorData>
+public class EllipseSerializationFactory : VectorShapeSerializationFactory<EllipseVectorData> 
 {
     public override string DeserializationId { get; } = "PixiEditor.EllipseData";
-    public override byte[] Serialize(EllipseVectorData original)
+
+    protected override void AddSpecificData(ByteBuilder builder, EllipseVectorData original)
     {
-        ByteBuilder builder = new ByteBuilder();
         builder.AddVecD(original.Center);
         builder.AddVecD(original.Radius);
-        builder.AddMatrix3X3(original.TransformationMatrix);
-        builder.AddColor(original.StrokeColor);
-        builder.AddColor(original.FillColor);
-        builder.AddInt(original.StrokeWidth);
-        
-        return builder.Build();
     }
 
-    public override bool TryDeserialize(object serialized, out EllipseVectorData original)
+    protected override bool DeserializeVectorData(ByteExtractor extractor, Matrix3X3 matrix, Color strokeColor, Color fillColor,
+        int strokeWidth, out EllipseVectorData original)
     {
-        if (serialized is not byte[] data)
-        {
-            original = null;
-            return false;
-        }
-        
-        ByteExtractor extractor = new ByteExtractor(data);
-        
         VecD center = extractor.GetVecD();
         VecD radius = extractor.GetVecD();
-        Matrix3X3 matrix = extractor.GetMatrix3X3();
-        Color strokeColor = extractor.GetColor();
-        Color fillColor = extractor.GetColor();
-        int strokeWidth = extractor.GetInt();
-        
+
         original = new EllipseVectorData(center, radius)
         {
             StrokeColor = strokeColor,
@@ -45,7 +28,7 @@ public class EllipseSerializationFactory : SerializationFactory<byte[], EllipseV
             StrokeWidth = strokeWidth,
             TransformationMatrix = matrix
         };
-        
+
         return true;
     }
 }

+ 34 - 0
src/PixiEditor/Models/Serialization/Factories/LineSerializationFactory.cs

@@ -0,0 +1,34 @@
+using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Shapes.Data;
+using PixiEditor.DrawingApi.Core.ColorsImpl;
+using PixiEditor.DrawingApi.Core.Numerics;
+using PixiEditor.Numerics;
+
+namespace PixiEditor.Models.Serialization.Factories;
+
+internal class LineSerializationFactory : VectorShapeSerializationFactory<LineVectorData> 
+{
+    public override string DeserializationId { get; } = "PixiEditor.LineData";
+
+    protected override void AddSpecificData(ByteBuilder builder, LineVectorData original)
+    {
+        builder.AddVecD(original.Start);
+        builder.AddVecD(original.End);
+    }
+
+    protected override bool DeserializeVectorData(ByteExtractor extractor, Matrix3X3 matrix, Color strokeColor, Color fillColor,
+        int strokeWidth, out LineVectorData original)
+    {
+        VecD start = extractor.GetVecD();
+        VecD end = extractor.GetVecD();
+
+        original = new LineVectorData(start, end)
+        {
+            StrokeColor = strokeColor,
+            FillColor = fillColor,
+            StrokeWidth = strokeWidth,
+            TransformationMatrix = matrix
+        };
+
+        return true;
+    }
+}

+ 9 - 21
src/PixiEditor/Models/Serialization/Factories/PointsDataSerializationFactory.cs

@@ -1,42 +1,30 @@
 using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Shapes.Data;
 using PixiEditor.DrawingApi.Core.ColorsImpl;
+using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.Numerics;
 
 namespace PixiEditor.Models.Serialization.Factories;
 
-public class PointsDataSerializationFactory : SerializationFactory<byte[], PointsVectorData>
+public class PointsDataSerializationFactory : VectorShapeSerializationFactory<PointsVectorData> 
 {
     public override string DeserializationId { get; } = "PixiEditor.PointsData";
-    public override byte[] Serialize(PointsVectorData original)
+    protected override void AddSpecificData(ByteBuilder builder, PointsVectorData original)
     {
-        ByteBuilder builder = new ByteBuilder();
         builder.AddVecDList(original.Points);
-        builder.AddColor(original.FillColor);
-        builder.AddInt(original.StrokeWidth);
-        
-        return builder.Build();
     }
 
-    public override bool TryDeserialize(object serialized, out PointsVectorData original)
+    protected override bool DeserializeVectorData(ByteExtractor extractor, Matrix3X3 matrix, Color strokeColor, Color fillColor,
+        int strokeWidth, out PointsVectorData original)
     {
-        if (serialized is not byte[] data)
-        {
-            original = null;
-            return false;
-        }
-        
-        ByteExtractor extractor = new ByteExtractor(data);
-        
         List<VecD> points = extractor.GetVecDList();
-        Color fillColor = extractor.GetColor();
-        int strokeWidth = extractor.GetInt();
-        
         original = new PointsVectorData(points)
         {
+            StrokeColor = strokeColor,
             FillColor = fillColor,
-            StrokeWidth = strokeWidth
+            StrokeWidth = strokeWidth,
+            TransformationMatrix = matrix
         };
-        
+
         return true;
     }
 }

+ 35 - 0
src/PixiEditor/Models/Serialization/Factories/RectangleSerializationFactory.cs

@@ -0,0 +1,35 @@
+using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Shapes.Data;
+using PixiEditor.DrawingApi.Core.ColorsImpl;
+using PixiEditor.DrawingApi.Core.Numerics;
+using PixiEditor.Numerics;
+
+namespace PixiEditor.Models.Serialization.Factories;
+
+internal class RectangleSerializationFactory : VectorShapeSerializationFactory<RectangleVectorData> 
+{
+    public override string DeserializationId { get; } = "PixiEditor.RectangleData";
+
+
+    protected override void AddSpecificData(ByteBuilder builder, RectangleVectorData original)
+    {
+        builder.AddVecD(original.Center);
+        builder.AddVecD(original.Size);
+    }
+
+    protected override bool DeserializeVectorData(ByteExtractor extractor, Matrix3X3 matrix, Color strokeColor, Color fillColor,
+        int strokeWidth, out RectangleVectorData original)
+    {
+        VecD center = extractor.GetVecD();
+        VecD size = extractor.GetVecD();
+
+        original = new RectangleVectorData(center, size)
+        {
+            StrokeColor = strokeColor,
+            FillColor = fillColor,
+            StrokeWidth = strokeWidth,
+            TransformationMatrix = matrix
+        };
+
+        return true;
+    }
+}

+ 43 - 0
src/PixiEditor/Models/Serialization/Factories/VectorShapeSerializationFactory.cs

@@ -0,0 +1,43 @@
+using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Shapes.Data;
+using PixiEditor.DrawingApi.Core.ColorsImpl;
+using PixiEditor.DrawingApi.Core.Numerics;
+
+namespace PixiEditor.Models.Serialization.Factories;
+
+public abstract class VectorShapeSerializationFactory<T> : SerializationFactory<byte[], T> where T : ShapeVectorData
+{
+    public override byte[] Serialize(T original)
+    {
+        ByteBuilder builder = new ByteBuilder();
+        builder.AddMatrix3X3(original.TransformationMatrix);
+        builder.AddColor(original.StrokeColor);
+        builder.AddColor(original.FillColor);
+        builder.AddInt(original.StrokeWidth);
+        
+        AddSpecificData(builder, original);
+        
+        return builder.Build();
+    }
+    
+    protected abstract void AddSpecificData(ByteBuilder builder, T original);
+
+    public override bool TryDeserialize(object serialized, out T original)
+    {
+        if (serialized is not byte[] data)
+        {
+            original = null;
+            return false;
+        }
+        
+        ByteExtractor extractor = new ByteExtractor(data);
+        
+        Matrix3X3 matrix = extractor.GetMatrix3X3();
+        Color strokeColor = extractor.GetColor();
+        Color fillColor = extractor.GetColor();
+        int strokeWidth = extractor.GetInt();
+        
+        return DeserializeVectorData(extractor, matrix, strokeColor, fillColor, strokeWidth, out original);
+    }
+    
+    protected abstract bool DeserializeVectorData(ByteExtractor extractor, Matrix3X3 matrix, Color strokeColor, Color fillColor, int strokeWidth, out T original);
+}