Sfoglia il codice sorgente

Serialize document wip

flabbet 2 anni fa
parent
commit
ed95a13b37

+ 93 - 22
src/PixiEditor/Helpers/Extensions/ParserHelpers.cs

@@ -1,4 +1,7 @@
-using PixiEditor.DrawingApi.Core.ColorsImpl;
+using ChunkyImageLib;
+using ChunkyImageLib.DataHolders;
+using ChunkyImageLib.Operations;
+using PixiEditor.DrawingApi.Core.ColorsImpl;
 using PixiEditor.DrawingApi.Core.Surface.ImageData;
 using PixiEditor.Parser;
 using PixiEditor.Parser.Collections;
@@ -157,6 +160,95 @@ internal static class ParserHelpers
         }
     }
     
+    public static SerializableDocument ToSerializable(this DocumentViewModel document)
+    {
+        return new SerializableDocument(document.Width, document.Height,
+                ToSerializableGroups(document.StructureRoot, document),
+                ToSerializableLayers(document.StructureRoot))
+            .AddSwatches(document.Swatches)
+            .AddPalette(document.Palette);
+    }
+
+    private static List<SerializableLayer> ToSerializableLayers(FolderViewModel documentStructureRoot)
+    {
+        List<SerializableLayer> layers = new List<SerializableLayer>();
+        
+        Traverse(documentStructureRoot, member =>
+        {
+            if (member is LayerViewModel layer)
+            {
+                layers.Add(layer.ToSerializable());
+            }
+        });
+        
+        return layers;
+    }
+
+    private static SerializableLayer ToSerializable(this LayerViewModel layer)
+    {
+        return new SerializableLayer();
+    }
+
+    private static List<SerializableGroup> ToSerializableGroups(FolderViewModel documentStructureRoot, DocumentViewModel documentViewModel, int passIndex = 0)
+    {
+        List<SerializableGroup> group = new List<SerializableGroup>();
+        
+        int currentLayerIndex = passIndex;
+        foreach (var memberViewModel in documentStructureRoot.Children)
+        {
+            if (memberViewModel is FolderViewModel folder && folder != documentViewModel.StructureRoot)
+            {
+                int startIndex = currentLayerIndex;
+                int endIndex = GetEndIndex(folder, startIndex);
+                group.Add(new SerializableGroup(folder.NameBindable, startIndex, endIndex, ToSerializableGroups(folder, documentViewModel, passIndex)));        
+            }
+            else if(memberViewModel is LayerViewModel layer)
+            {
+                currentLayerIndex++;
+            }
+        }
+        
+        return group;
+    }
+
+    private static int GetEndIndex(FolderViewModel folder, int startIndex)
+    {
+        int endIndex = startIndex;
+        Traverse(folder, member =>
+        {
+            if (member is LayerViewModel)
+            {
+                endIndex++;
+            }
+        });
+        
+        return endIndex;
+    }
+
+    private static void Traverse(FolderViewModel root, Action<StructureMemberViewModel> action)
+    {
+        foreach (var child in root.Children)
+        {
+            action(child);
+            if (child is FolderViewModel folder)
+            {
+                Traverse(folder, action);
+            }
+        }
+    }
+
+    private static SerializableDocument AddSwatches(this SerializableDocument document, IEnumerable<Color> colors)
+    {
+        document.Swatches.AddRange(colors.Select(x => System.Drawing.Color.FromArgb(x.A, x.R, x.G, x.B)));
+        return document;
+    }
+
+    private static SerializableDocument AddPalette(this SerializableDocument document, IEnumerable<Color> palette)
+    {
+        document.Palette.AddRange(palette.Select(x => System.Drawing.Color.FromArgb(x.A, x.R, x.G, x.B)));
+        return document;
+    }
+    
     /*
     public static WpfObservableRangeCollection<Layer> ToLayers(this SerializableDocument document)
     {
@@ -212,15 +304,6 @@ internal static class ParserHelpers
         return group;
     }
 
-    public static SerializableDocument ToSerializable(this Document document)
-    {
-        return new SerializableDocument(document.Width, document.Height,
-                document.LayerStructure.Groups.ToSerializable(document),
-                document.Layers.ToSerializable())
-            .AddSwatches(document.Swatches)
-            .AddPalette(document.Palette);
-    }
-
     public static IEnumerable<SerializableLayer> ToSerializable(this IEnumerable<Layer> layers)
     {
         foreach (Layer layer in layers)
@@ -277,17 +360,5 @@ internal static class ParserHelpers
         {
             yield return sgroup.ToGroup(parent, document);
         }
-    }
-
-    private static SerializableDocument AddSwatches(this SerializableDocument document, IEnumerable<SKColor> colors)
-    {
-        document.Swatches.AddRange(colors);
-        return document;
-    }
-
-    private static SerializableDocument AddPalette(this SerializableDocument document, IEnumerable<SKColor> palette)
-    {
-        document.Palette.AddRange(palette);
-        return document;
     }*/
 }

+ 3 - 5
src/PixiEditor/Models/IO/Exporter.cs

@@ -4,6 +4,7 @@ using System.Runtime.InteropServices;
 using System.Windows;
 using System.Windows.Media.Imaging;
 using ChunkyImageLib;
+using ChunkyImageLib.DataHolders;
 using Microsoft.Win32;
 using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.DrawingApi.Core.Surface.ImageData;
@@ -49,8 +50,6 @@ internal class Exporter
     /// <returns>Path.</returns>
     public static string SaveAsEditableFile(DocumentViewModel document, string path, FileType requestedType = FileType.Unset)
     {
-        return path;
-        /*
         var typeFromPath = ParseImageFormat(Path.GetExtension(path));
         FileType finalType = (typeFromPath, requestedType) switch
         {
@@ -67,12 +66,12 @@ internal class Exporter
 
         if (finalType != FileType.Pixi)
         {
-            var bitmap = document.Renderer.FinalBitmap;
+            var bitmap = document.Bitmaps[ChunkResolution.Full];
             SaveAs(encodersFactory[finalType](), path, bitmap.PixelWidth, bitmap.PixelHeight, bitmap);
         }
         else if (Directory.Exists(Path.GetDirectoryName(path)))
         {
-            Parser.PixiParser.Serialize(ParserHelpers.ToSerializable(document), path);
+            Parser.PixiParser.Serialize(document.ToSerializable(), path);
         }
         else
         {
@@ -80,7 +79,6 @@ internal class Exporter
         }
 
         return path;
-        */
     }
 
     private static string AppendExtension(string path, FileTypeDialogData data)

+ 13 - 3
src/PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs

@@ -246,15 +246,25 @@ internal class FileViewModel : SubViewModel<ViewModelMain>
 
     public bool SaveDocument(DocumentViewModel document, bool asNew)
     {
+        string path = "";
+        bool success = false;
         if (asNew || string.IsNullOrEmpty(document.FullFilePath))
         {
-            //doc.SaveWithDialog();
+            success = Exporter.SaveAsEditableFileWithDialog(document, out path);
         }
         else
         {
-            //doc.Save();
+            path = Exporter.SaveAsEditableFile(document, document.FullFilePath);
+            success = path != null;
         }
-        return false;
+
+        document.FullFilePath = path;
+        if (success)
+        {
+            document.MarkAsSaved();
+        }
+
+        return success;
     }
 
     /// <summary>

+ 2 - 1
src/PixiEditor/ViewModels/SubViewModels/Main/IoViewModel.cs

@@ -4,6 +4,7 @@ using ChunkyImageLib.DataHolders;
 using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.Helpers;
 using PixiEditor.Models.Commands;
+using PixiEditor.Models.Commands.Commands;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Events;
@@ -109,7 +110,7 @@ internal class IoViewModel : SubViewModel<ViewModelMain>
         ShortcutController controller = Owner.ShortcutController;
 
         Models.Commands.Commands.Command.ToolCommand? tool = CommandController.Current.Commands
-            .Select(x => x as Models.Commands.Commands.Command.ToolCommand)
+            .OfType<Command.ToolCommand?>()
             .FirstOrDefault(x => x != null && x.TransientKey == args.Key);
 
         if (tool is not null)