|
@@ -1,31 +1,152 @@
|
|
-using PixiEditor.Models.DataHolders;
|
|
|
|
-using PixiEditor.Parser;
|
|
|
|
|
|
+using PixiEditor.Parser;
|
|
|
|
+using PixiEditor.Parser.Collections;
|
|
|
|
+using PixiEditor.ViewModels.SubViewModels.Document;
|
|
|
|
|
|
namespace PixiEditor.Helpers.Extensions;
|
|
namespace PixiEditor.Helpers.Extensions;
|
|
|
|
|
|
internal static class ParserHelpers
|
|
internal static class ParserHelpers
|
|
{
|
|
{
|
|
- /*public static Document ToDocument(this SerializableDocument serializableDocument)
|
|
|
|
|
|
+ public static DocumentViewModel ToDocument(this SerializableDocument serializableDocument)
|
|
{
|
|
{
|
|
-
|
|
|
|
- Document document = new Document(serializableDocument.Width, serializableDocument.Height)
|
|
|
|
|
|
+ List<SerializableLayer> builtLayers = new List<SerializableLayer>();
|
|
|
|
+ DocumentViewModel vm = DocumentViewModel.Build(builder =>
|
|
{
|
|
{
|
|
- Layers = serializableDocument.ToLayers(),
|
|
|
|
- Swatches = new WpfObservableRangeCollection<SKColor>(serializableDocument.Swatches.ToSKColors()),
|
|
|
|
- Palette = new WpfObservableRangeCollection<SKColor>(serializableDocument.Palette.ToSKColors())
|
|
|
|
- };
|
|
|
|
|
|
+ builder.WithSize(serializableDocument.Width, serializableDocument.Height);
|
|
|
|
+
|
|
|
|
+ if (serializableDocument.Groups != null)
|
|
|
|
+ {
|
|
|
|
+ foreach (var group in serializableDocument.Groups)
|
|
|
|
+ {
|
|
|
|
+ builder.WithFolder((folderBuilder =>
|
|
|
|
+ {
|
|
|
|
+ builtLayers = BuildFolder(folderBuilder, group,
|
|
|
|
+ GatherFolderLayers(group, serializableDocument.Layers),
|
|
|
|
+ serializableDocument);
|
|
|
|
+ }));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ BuildLayers(serializableDocument.Layers.Where(x => !builtLayers.Contains(x)), builder, serializableDocument);
|
|
|
|
+ SortMembers(builder.Children);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return vm;
|
|
|
|
+ }
|
|
|
|
|
|
- document.LayerStructure.Groups = serializableDocument.ToGroups(document);
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Builds folder and its children.
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="folderBuilder">Folder to build.</param>
|
|
|
|
+ /// <param name="group">Serialized folder (group), which will be used to build.</param>
|
|
|
|
+ /// <param name="layers">Layers only in this folder.</param>
|
|
|
|
+ /// <param name="doc">Document which contains all the serialized data.</param>
|
|
|
|
+ /// <returns>List of layers which were built.</returns>
|
|
|
|
+ private static List<SerializableLayer> BuildFolder(DocumentViewModelBuilder.FolderBuilder folderBuilder, SerializableGroup group, List<SerializableLayer> layers, SerializableDocument doc)
|
|
|
|
+ {
|
|
|
|
+ List<SerializableLayer> builtLayers = new List<SerializableLayer>(layers);
|
|
|
|
+ folderBuilder
|
|
|
|
+ .WithName(group.Name)
|
|
|
|
+ .WithOpacity(group.Opacity)
|
|
|
|
+ .WithVisibility(group.IsVisible)
|
|
|
|
+ .WithOrderInStructure(group.StartLayer);
|
|
|
|
+
|
|
|
|
+ folderBuilder.WithChildren((childrenBuilder =>
|
|
|
|
+ {
|
|
|
|
+ if (group.Subgroups != null)
|
|
|
|
+ {
|
|
|
|
+ foreach (var subGroup in group.Subgroups)
|
|
|
|
+ {
|
|
|
|
+ childrenBuilder.WithFolder((subFolderBuilder =>
|
|
|
|
+ {
|
|
|
|
+ builtLayers.AddRange(BuildFolder(subFolderBuilder, subGroup,
|
|
|
|
+ GatherFolderLayers(subGroup, doc.Layers), doc));
|
|
|
|
+ }));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ BuildLayers(layers, childrenBuilder, doc);
|
|
|
|
+ }));
|
|
|
|
+
|
|
|
|
+ return builtLayers;
|
|
|
|
+ }
|
|
|
|
|
|
- if (document.Layers.Count > 0)
|
|
|
|
|
|
+ private static void BuildLayers(IEnumerable<SerializableLayer> layers, ChildrenBuilder builder, SerializableDocument document)
|
|
|
|
+ {
|
|
|
|
+ if (layers != null)
|
|
{
|
|
{
|
|
- document.SetMainActiveLayer(0);
|
|
|
|
|
|
+ foreach (var layer in layers)
|
|
|
|
+ {
|
|
|
|
+ builder.WithLayer((layerBuilder) =>
|
|
|
|
+ {
|
|
|
|
+ layerBuilder
|
|
|
|
+ .WithSize(layer.Width, layer.Height)
|
|
|
|
+ .WithName(layer.Name)
|
|
|
|
+ .WithOpacity(layer.Opacity)
|
|
|
|
+ .WithVisibility(layer.IsVisible)
|
|
|
|
+ .WithRect(layer.Width, layer.Height, layer.OffsetX, layer.OffsetY)
|
|
|
|
+ .WithSurface((surfaceBuilder) => surfaceBuilder.WithImage(layer.PngBytes))
|
|
|
|
+ .WithOrderInStructure(document.Layers.IndexOf(layer));
|
|
|
|
+ });
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- document.Renderer.ForceRerender();
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Gathers all layers which are in the folder. Excludes layers which are in subfolders.
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="group">Group which contains folder data.</param>
|
|
|
|
+ /// <param name="serializableDocumentLayers">All layers in document.</param>
|
|
|
|
+ /// <returns>List of layers in folder, excluding layers in nested folders.</returns>
|
|
|
|
+ private static List<SerializableLayer> GatherFolderLayers(SerializableGroup group, LayerCollection serializableDocumentLayers)
|
|
|
|
+ {
|
|
|
|
+ List<SerializableLayer> layers = new List<SerializableLayer>();
|
|
|
|
|
|
- return document;
|
|
|
|
- throw new NotImplementedException();
|
|
|
|
- }*/
|
|
|
|
|
|
+ for (int i = group.StartLayer; i <= group.EndLayer; i++)
|
|
|
|
+ {
|
|
|
|
+ layers.Add(serializableDocumentLayers[i]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(group.Subgroups is { Count: > 0 })
|
|
|
|
+ {
|
|
|
|
+ foreach (var subGroup in group.Subgroups)
|
|
|
|
+ {
|
|
|
|
+ var nestedGroupLayers = GatherFolderLayers(subGroup, serializableDocumentLayers);
|
|
|
|
+ layers.RemoveAll(x => nestedGroupLayers.Contains(x));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return layers;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Sorts StructureMemberBuilder by its OrderInStructure property.
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="builderChildren">Structure to sort</param>
|
|
|
|
+ private static void SortMembers(List<DocumentViewModelBuilder.StructureMemberBuilder> builderChildren)
|
|
|
|
+ {
|
|
|
|
+ int previousOrder = -1;
|
|
|
|
+ int previousIndex = -1;
|
|
|
|
+
|
|
|
|
+ for (var index = 0; index < builderChildren.Count; index++)
|
|
|
|
+ {
|
|
|
|
+ var child = builderChildren[index];
|
|
|
|
+ if (child is DocumentViewModelBuilder.FolderBuilder folderBuilder)
|
|
|
|
+ {
|
|
|
|
+ SortMembers(folderBuilder.Children);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int order = child.OrderInStructure;
|
|
|
|
+ if (order < previousOrder)
|
|
|
|
+ {
|
|
|
|
+ builderChildren.Remove(child);
|
|
|
|
+ builderChildren.Insert(previousIndex, child);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ previousIndex = builderChildren.IndexOf(child);
|
|
|
|
+ previousOrder = order;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
/*
|
|
/*
|
|
public static WpfObservableRangeCollection<Layer> ToLayers(this SerializableDocument document)
|
|
public static WpfObservableRangeCollection<Layer> ToLayers(this SerializableDocument document)
|
|
{
|
|
{
|