Browse Source

Wrote some tests and serialize groups WIP

flabbet 4 years ago
parent
commit
4f45b76847

+ 49 - 8
PixiEditor/Helpers/Extensions/ParserHelpers.cs

@@ -6,6 +6,7 @@ using System.Windows.Media;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.ImageManipulation;
 using PixiEditor.Models.Layers;
+using PixiEditor.Parser;
 
 namespace PixiEditor.Helpers.Extensions
 {
@@ -20,6 +21,8 @@ namespace PixiEditor.Helpers.Extensions
                     Color.FromArgb(x.Item1, x.Item2, x.Item3, x.Item4)))
             };
 
+            //document.LayerStructure = new LayerStructure(ToGroups(serializableDocument), document);
+
             return document;
         }
 
@@ -29,25 +32,52 @@ namespace PixiEditor.Helpers.Extensions
             for (int i = 0; i < serializableDocument.Layers.Length; i++)
             {
                 Parser.SerializableLayer serLayer = serializableDocument.Layers[i];
-                Layer layer =
-                    new Layer(serLayer.Name, BitmapUtils.BytesToWriteableBitmap(serLayer.Width, serLayer.Height, serLayer.BitmapBytes))
-                    {
-                        IsVisible = serLayer.IsVisible,
-                        Offset = new Thickness(serLayer.OffsetX, serLayer.OffsetY, 0, 0),
-                        Opacity = serLayer.Opacity
-                    };
+                Layer layer = new(serLayer.Name, BitmapUtils.BytesToWriteableBitmap(serLayer.Width, serLayer.Height, serLayer.BitmapBytes))
+                {
+                    IsVisible = serLayer.IsVisible,
+                    Offset = new Thickness(serLayer.OffsetX, serLayer.OffsetY, 0, 0),
+                    Opacity = serLayer.Opacity
+                };
                 layers.Add(layer);
             }
 
             return layers;
         }
 
-        public static Parser.SerializableDocument ToSerializable(this Document document)
+        /*public static ObservableCollection<GuidStructureItem> ToGroups(this Parser.SerializableDocument serializableDocument)
+        {
+            return ToGroups(serializableDocument.Groups);
+        }
+
+        private static ObservableCollection<GuidStructureItem> ToGroups(SerializableGuidStructureItem[] items)
+        {
+            ObservableCollection<GuidStructureItem> groups = new();
+
+            for (int i = 0; i < items.Length; i++)
+            {
+                SerializableGuidStructureItem item = items[i];
+                groups.Add(ToGroup(item));
+            }
+            return groups;
+        }
+
+        private static GuidStructureItem ToGroup(SerializableGuidStructureItem item)
+        {
+            return new GuidStructureItem(
+                item.Name,
+                item.StartLayerGuid,
+                item.EndLayerGuid,
+                ToGroups(item.Subgroups),
+                ToGroup(item.Parent));
+        }*/
+
+        public static SerializableDocument ToSerializable(this Document document)
         {
             Parser.SerializableDocument serializable = new Parser.SerializableDocument
             {
                 Width = document.Width,
                 Height = document.Height,
+                //Groups = document.LayerStructure.Groups.Select(x => x.ToSerializable()).ToArray(),
                 Layers = document.Layers.Select(x => x.ToSerializable()).ToArray(),
                 Swatches = document.Swatches.Select(x => new Tuple<byte, byte, byte, byte>(x.A, x.R, x.G, x.B)).ToArray()
             };
@@ -55,6 +85,17 @@ namespace PixiEditor.Helpers.Extensions
             return serializable;
         }
 
+        /*public static SerializableGuidStructureItem ToSerializable(this GuidStructureItem group)
+        {
+            return new(
+                    group.GroupGuid,
+                    group.Name,
+                    group.StartLayerGuid,
+                    group.EndLayerGuid,
+                    group.Subgroups.Select(x => x.ToSerializable()).ToArray(),
+                    group.Parent?.ToSerializable());
+        }*/
+
         public static Parser.SerializableLayer ToSerializable(this Layer layer)
         {
             Parser.SerializableLayer serializable = new Parser.SerializableLayer

+ 0 - 34
PixiEditor/Models/DataHolders/SerializableGuidStructureItem.cs

@@ -1,34 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace PixiEditor.Models.DataHolders
-{
-    [Serializable]
-    public class SerializableGuidStructureItem
-    {
-        public Guid GroupGuid { get; set; }
-
-        public string Name { get; set; }
-
-        public Guid StartLayerGuid { get; set; }
-
-        public Guid EndLayerGuid { get; set; }
-
-        public SerializableGuidStructureItem[] Subgroups { get; set; }
-
-        public SerializableGuidStructureItem Parent { get; set; }
-
-        public SerializableGuidStructureItem(Guid groupGuid, string name, Guid startLayerGuid, Guid endLayerGuid, SerializableGuidStructureItem[] subgroups, SerializableGuidStructureItem parent)
-        {
-            GroupGuid = groupGuid;
-            Name = name;
-            StartLayerGuid = startLayerGuid;
-            EndLayerGuid = endLayerGuid;
-            Subgroups = subgroups;
-            Parent = parent;
-        }
-    }
-}

+ 1 - 2
PixiEditor/Models/IO/Exporter.cs

@@ -6,7 +6,6 @@ using Microsoft.Win32;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Dialogs;
-using PixiEditor.Parser;
 
 namespace PixiEditor.Models.IO
 {
@@ -42,7 +41,7 @@ namespace PixiEditor.Models.IO
         /// <returns>Path.</returns>
         public static string SaveAsEditableFile(Document document, string path)
         {
-            PixiParser.Serialize(document.ToSerializable(), path);
+            Parser.PixiParser.Serialize(document.ToSerializable(), path);
             return path;
         }
 

+ 3 - 3
PixiEditor/Models/IO/Importer.cs

@@ -5,7 +5,7 @@ using System.Windows.Media.Imaging;
 using PixiEditor.Exceptions;
 using PixiEditor.Helpers;
 using PixiEditor.Helpers.Extensions;
-using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.DataHolders;
 using PixiEditor.Parser;
 
 namespace PixiEditor.Models.IO
@@ -61,7 +61,7 @@ namespace PixiEditor.Models.IO
         {
             try
             {
-                Document doc = PixiParser.Deserialize(path).ToDocument();
+                Document doc = PixiEditor.Parser.PixiParser.Deserialize(path).ToDocument();
                 doc.DocumentFilePath = path;
                 return doc;
             }
@@ -77,7 +77,7 @@ namespace PixiEditor.Models.IO
             {
                 using FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
 
-                Document doc = PixiParser.DeserializeOld(stream).ToDocument();
+                Document doc = PixiEditor.Parser.PixiParser.DeserializeOld(stream).ToDocument();
                 doc.DocumentFilePath = path;
                 return doc;
             }

+ 33 - 46
PixiEditor/Models/Layers/LayerStructure.cs

@@ -144,6 +144,38 @@ namespace PixiEditor.Models.Layers
             LayerStructureChanged?.Invoke(this, EventArgs.Empty);
         }
 
+        /// <summary>
+        /// Checks if group is nested inside parent group.
+        /// </summary>
+        /// <param name="group">Group to check.</param>
+        /// <param name="parent">Parent of that group.</param>
+        /// <returns>True if group is nested inside parent, false if not.</returns>
+        public bool IsChildOf(GuidStructureItem? group, GuidStructureItem parent)
+        {
+            if (group == null)
+            {
+                return false;
+            }
+
+            foreach (var subgroup in parent.Subgroups)
+            {
+                if (subgroup == group)
+                {
+                    return true;
+                }
+
+                if (subgroup.Subgroups.Count > 0)
+                {
+                    if (IsChildOf(group, subgroup))
+                    {
+                        return true;
+                    }
+                }
+            }
+
+            return false;
+        }
+
         public void PreMoveReassignBounds(GroupData parentGroup, Guid layer)
         {
             PreMoveReassignBounds(GetGroupByGuid(parentGroup.GroupGuid), layer);
@@ -216,38 +248,6 @@ namespace PixiEditor.Models.Layers
             }
         }
 
-        /// <summary>
-        /// Checks if group is nested inside parent group.
-        /// </summary>
-        /// <param name="group">Group to check.</param>
-        /// <param name="parent">Parent of that group.</param>
-        /// <returns>True if group is nested inside parent, false if not.</returns>
-        public bool IsChildOf(GuidStructureItem group, GuidStructureItem parent)
-        {
-            if (group == null)
-            {
-                return false;
-            }
-
-            foreach (var subgroup in parent.Subgroups)
-            {
-                if (subgroup == group)
-                {
-                    return true;
-                }
-
-                if (subgroup.Subgroups.Count > 0)
-                {
-                    if (IsChildOf(group, subgroup))
-                    {
-                        return true;
-                    }
-                }
-            }
-
-            return false;
-        }
-
         /// <summary>
         /// Checks if layer is nested inside parent group.
         /// </summary>
@@ -262,21 +262,8 @@ namespace PixiEditor.Models.Layers
             {
                 return true;
             }
-            else
-            {
-                GuidStructureItem nextParent = parent;
-                while (nextParent.Parent != null)
-                {
-                    if (nextParent == parent)
-                    {
-                        return true;
-                    }
-
-                    nextParent = nextParent.Parent;
-                }
-            }
 
-            return false;
+            return IsChildOf(layerParent, parent);
         }
 
         public void PostMoveReassignBounds(GroupData parentGroup, Guid layerGuid)

+ 3 - 2
PixiEditor/PixiEditor.csproj

@@ -57,7 +57,7 @@
     </None>
   </ItemGroup>
   <ItemGroup>
-    <PackageReference Include="Dirkster.AvalonDock" Version="4.50.1" />
+    <PackageReference Include="Dirkster.AvalonDock" Version="4.50.3" />
     <PackageReference Include="DiscordRichPresence" Version="1.0.175" />
     <PackageReference Include="Expression.Blend.Sdk">
       <Version>1.0.2</Version>
@@ -65,9 +65,10 @@
     <PackageReference Include="Extended.Wpf.Toolkit" Version="3.8.2" />
     <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
     <PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
-    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
+    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
     <PackageReference Include="PixiEditor.ColorPicker" Version="2.0.0" />
     <PackageReference Include="PixiEditor.Parser" Version="1.0.1.1" />
+    <PackageReference Include="System.Drawing.Common" Version="5.0.2" />
     <PackageReference Include="WriteableBitmapEx">
       <Version>1.6.7</Version>
     </PackageReference>

+ 1 - 1
PixiEditor/Properties/AssemblyInfo.cs

@@ -10,7 +10,7 @@ using System.Windows;
 [assembly: AssemblyConfiguration("")]
 [assembly: AssemblyCompany("PixiEditor")]
 [assembly: AssemblyProduct("PixiEditor")]
-[assembly: AssemblyCopyright("Copyright PixiEditor © 2018 - 2021")]
+[assembly: AssemblyCopyright("Copyright PixiEditor © 2017 - 2021")]
 [assembly: AssemblyTrademark("")]
 [assembly: AssemblyCulture("")]
 

+ 0 - 4
PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.IO;
 using System.Linq;
@@ -9,12 +8,9 @@ using Microsoft.Win32;
 using Newtonsoft.Json.Linq;
 using PixiEditor.Exceptions;
 using PixiEditor.Helpers;
-using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Dialogs;
-using PixiEditor.Models.Enums;
 using PixiEditor.Models.IO;
-using PixiEditor.Models.Layers;
 using PixiEditor.Models.UserPreferences;
 using PixiEditor.Parser;
 

+ 5 - 4
PixiEditor/Views/UserControls/LayerGroupControl.xaml.cs

@@ -66,7 +66,7 @@ namespace PixiEditor.Views.UserControls
                     nameof(IsVisibleUndoTriggerable), 
                     e.OldValue,
                     e.NewValue,
-                    $"Change {control.GroupName} visiblity",
+                    $"Change {control.GroupName} visibility",
                     control), true);
         }
 
@@ -149,9 +149,10 @@ namespace PixiEditor.Views.UserControls
             LayerItem.RemoveDragEffect(grid);
         }
 
-        private void HandleDrop(IDataObject dataObj, bool above)
+        private void HandleDrop(IDataObject dataObj, Grid grid, bool above)
         {
             Guid referenceLayer = above ? GroupData.EndLayerGuid : GroupData.StartLayerGuid;
+            LayerItem.RemoveDragEffect(grid);
 
             if (dataObj.GetDataPresent(LayerContainerDataName))
             {
@@ -206,12 +207,12 @@ namespace PixiEditor.Views.UserControls
 
         private void Grid_Drop_Top(object sender, DragEventArgs e)
         {
-            HandleDrop(e.Data, true);
+            HandleDrop(e.Data, (Grid)sender, true);
         }
 
         private void Grid_Drop_Bottom(object sender, DragEventArgs e)
         {
-            HandleDrop(e.Data, false);
+            HandleDrop(e.Data, (Grid)sender, false);
         }
 
         private void Border_MouseDown(object sender, MouseButtonEventArgs e)

+ 86 - 0
PixiEditorTests/ModelsTests/DataHoldersTests/LayerStructureTests.cs

@@ -0,0 +1,86 @@
+using System;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Layers;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.DataHoldersTests
+{
+    public class LayerStructureTests
+    {
+        [Fact]
+        public void TestThatAddNewGroupAddsNewGroup()
+        {
+            Document doc = new Document(1, 1);
+            doc.Layers.Add(new("_testLayer"));
+            var testLayer = doc.Layers[^1];
+            doc.LayerStructure.AddNewGroup("test", testLayer.LayerGuid);
+
+            Assert.Single(doc.LayerStructure.Groups);
+            Assert.Equal(testLayer.LayerGuid, doc.LayerStructure.Groups[0].StartLayerGuid);
+            Assert.Equal(testLayer.LayerGuid, doc.LayerStructure.Groups[0].EndLayerGuid);
+        }
+
+        [Fact]
+        public void TestThatAddNewGroupAddsNewGroupAsASubgroup()
+        {
+            Document doc = new Document(1, 1);
+            doc.Layers.Add(new("_testLayer"));
+            var testLayer = doc.Layers[^1];
+            doc.LayerStructure.AddNewGroup("test", testLayer.LayerGuid);
+            doc.LayerStructure.AddNewGroup("test1", testLayer.LayerGuid);
+
+            Assert.Single(doc.LayerStructure.Groups);
+            Assert.Single(doc.LayerStructure.Groups[0].Subgroups);
+            Assert.Equal(testLayer.LayerGuid, doc.LayerStructure.Groups[0].StartLayerGuid);
+            Assert.Equal(testLayer.LayerGuid, doc.LayerStructure.Groups[0].EndLayerGuid);
+            Assert.Equal(testLayer.LayerGuid, doc.LayerStructure.Groups[0].Subgroups[0].StartLayerGuid);
+            Assert.Equal(testLayer.LayerGuid, doc.LayerStructure.Groups[0].Subgroups[0].EndLayerGuid);
+        }
+
+        [Fact]
+        public void TestThatMoveGroupMovesSwapsLayerPlacesWithOtherGroup()
+        {
+            Document doc = new Document(1, 1);
+            doc.Layers.Add(new Layer("_testLayer"));
+            doc.Layers.Add(new Layer("_testLayer1"));
+            var testLayer = doc.Layers[0];
+            var testLayer1 = doc.Layers[^1];
+            doc.LayerStructure.AddNewGroup("test", testLayer.LayerGuid);
+            doc.LayerStructure.AddNewGroup("test1", testLayer1.LayerGuid);
+
+            Assert.Equal(0, doc.Layers.IndexOf(testLayer));
+            Assert.Equal(1, doc.Layers.IndexOf(testLayer1));
+
+            doc.LayerStructure.MoveGroup(doc.LayerStructure.Groups[0].GroupGuid, null, 1);
+
+            Assert.Equal(1, doc.Layers.IndexOf(testLayer));
+            Assert.Equal(0, doc.Layers.IndexOf(testLayer1));
+        }
+
+        [Fact]
+        public void TestThatIsChildOfDetectsNestedGroupCorrectly()
+        {
+            LayerStructure ls = new LayerStructure(new Document(0, 0));
+            Layer testLayer = new Layer("tst");
+            ls.Groups.Add(new GuidStructureItem("group 1", testLayer.LayerGuid));
+            ls.Groups[0].Subgroups.Add(new GuidStructureItem("group 1 nested", testLayer.LayerGuid));
+
+            Assert.True(ls.IsChildOf(ls.Groups[0].Subgroups[0], ls.Groups[0]));
+            Assert.False(ls.IsChildOf(ls.Groups[0], ls.Groups[0].Subgroups[0]));
+        }
+
+        [Fact]
+        public void TestThatIsChildOfDetectsNestedLayersCorrectly()
+        {
+            var doc = new Document(0, 0);
+            doc.Layers.Add(new Layer("tst"));
+            Guid testLayerGuid = doc.Layers[0].LayerGuid;
+            LayerStructure ls = new LayerStructure(doc);
+            ls.AddNewGroup("Test group", testLayerGuid);
+            ls.AddNewGroup("Test group nested", testLayerGuid);
+
+            Assert.True(ls.IsChildOf(testLayerGuid, ls.Groups[0]));
+            Assert.True(ls.IsChildOf(testLayerGuid, ls.Groups[0].Subgroups[0]));
+        }
+    }
+}

+ 3 - 3
PixiEditorTests/PixiEditorTests.csproj

@@ -18,15 +18,15 @@
   </ItemGroup>
 
   <ItemGroup>
-    <PackageReference Include="Codecov" Version="1.12.4" />
+    <PackageReference Include="Codecov" Version="1.13.0" />
     <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.2">
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>
     <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
     <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
-    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
-    <PackageReference Include="Moq" Version="4.16.0" />
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
+    <PackageReference Include="Moq" Version="4.16.1" />
     <PackageReference Include="OpenCover" Version="4.7.922" />
     <PackageReference Include="xunit" Version="2.4.1" />
     <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">