Browse Source

Did some cool stuff

CPKreuz 4 years ago
parent
commit
8a69a7e38c

+ 1 - 1
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -115,7 +115,7 @@ namespace PixiEditor.Models.Controllers
                 {
                 {
                     Layer layer = Manager.ActiveDocument.Layers.First(x => x.LayerGuid == modifiedLayers[i].LayerGuid);
                     Layer layer = Manager.ActiveDocument.Layers.First(x => x.LayerGuid == modifiedLayers[i].LayerGuid);
 
 
-                    if (layer is TemplateLayer)
+                    if (layer is ReferenceLayer)
                     {
                     {
                         continue;
                         continue;
                     }
                     }

+ 31 - 1
PixiEditor/Models/DataHolders/Document/Document.Layers.cs

@@ -40,7 +40,7 @@ namespace PixiEditor.Models.DataHolders
                 ActiveLayer.IsActive = false;
                 ActiveLayer.IsActive = false;
             }
             }
 
 
-            if (Layers.Where(x => x is not TemplateLayer).Any(x => x.IsActive))
+            if (Layers.Where(x => x is not ReferenceLayer).Any(x => x.IsActive))
             {
             {
                 var guids = Layers.Where(x => x.IsActive).Select(y => y.LayerGuid);
                 var guids = Layers.Where(x => x.IsActive).Select(y => y.LayerGuid);
                 guids.ToList().ForEach(x => Layers.First(layer => layer.LayerGuid == x).IsActive = false);
                 guids.ToList().ForEach(x => Layers.First(layer => layer.LayerGuid == x).IsActive = false);
@@ -216,6 +216,36 @@ namespace PixiEditor.Models.DataHolders
             return layer;
             return layer;
         }
         }
 
 
+        public int GetMergeableLayerAboveIndex(int from)
+        {
+            for (int i = from + 1; i < Layers.Count; i++)
+            {
+                Layer layer = Layers[i];
+
+                if (layer.IsMergeable)
+                {
+                    return i;
+                }
+            }
+
+            return -1;
+        }
+
+        public int GetMergeableLayerBelowIndex(int from)
+        {
+            for (int i = from - 1; i > -1; i--)
+            {
+                Layer layer = Layers[i];
+
+                if (layer.IsMergeable)
+                {
+                    return i;
+                }
+            }
+
+            return -1;
+        }
+
         private void MergeLayersProcess(object[] args)
         private void MergeLayersProcess(object[] args)
         {
         {
             if (args.Length > 0 
             if (args.Length > 0 

+ 1 - 1
PixiEditor/Models/DataHolders/Document/Document.Operations.cs

@@ -95,7 +95,7 @@ namespace PixiEditor.Models.DataHolders
 
 
             for (int i = 0; i < Layers.Count; i++)
             for (int i = 0; i < Layers.Count; i++)
             {
             {
-                if (Layers[i] is TemplateLayer)
+                if (Layers[i] is ReferenceLayer)
                 {
                 {
                     continue;
                     continue;
                 }
                 }

+ 1 - 1
PixiEditor/Models/ImageManipulation/BitmapUtils.cs

@@ -92,7 +92,7 @@ namespace PixiEditor.Models.ImageManipulation
             WriteableBitmap previewBitmap = BitmapFactory.New(document.Width, document.Height);
             WriteableBitmap previewBitmap = BitmapFactory.New(document.Width, document.Height);
 
 
             // 0.8 because blit doesn't take into consideration layer opacity. Small images with opacity > 80% are simillar enough.
             // 0.8 because blit doesn't take into consideration layer opacity. Small images with opacity > 80% are simillar enough.
-            foreach (var layer in document.Layers.Where(x => x.IsVisible && x.Opacity > 0.8f && x is not TemplateLayer))
+            foreach (var layer in document.Layers.Where(x => x.IsVisible && x.Opacity > 0.8f && x is not ReferenceLayer))
             {
             {
                 previewBitmap.Blit(
                 previewBitmap.Blit(
                     new Rect(layer.OffsetX, layer.OffsetY, layer.Width, layer.Height),
                     new Rect(layer.OffsetX, layer.OffsetY, layer.Width, layer.Height),

+ 2 - 0
PixiEditor/Models/Layers/BasicLayer.cs

@@ -31,5 +31,7 @@ namespace PixiEditor.Models.Layers
         }
         }
 
 
         public Guid LayerGuid { get; protected set; }
         public Guid LayerGuid { get; protected set; }
+
+        public virtual bool IsMergeable => true;
     }
     }
 }
 }

+ 2 - 0
PixiEditor/Models/Layers/Layer.cs

@@ -35,6 +35,8 @@ namespace PixiEditor.Models.Layers
 
 
         private Brush layerColor;
         private Brush layerColor;
 
 
+        public override bool IsMergeable => true;
+
         public Layer(string name)
         public Layer(string name)
         {
         {
             Name = name;
             Name = name;

+ 12 - 10
PixiEditor/Models/Layers/TemplateLayer.cs → PixiEditor/Models/Layers/ReferenceLayer.cs

@@ -9,7 +9,7 @@ using PixiEditor.Models.IO;
 
 
 namespace PixiEditor.Models.Layers
 namespace PixiEditor.Models.Layers
 {
 {
-    internal class TemplateLayer : Layer
+    internal class ReferenceLayer : Layer
     {
     {
         private static readonly Brush ActiveBrush = new SolidColorBrush(Color.FromRgb(130, 35, 35));
         private static readonly Brush ActiveBrush = new SolidColorBrush(Color.FromRgb(130, 35, 35));
         private static readonly Brush InactiveBrush = new SolidColorBrush(Color.FromArgb(70, 130, 35, 35));
         private static readonly Brush InactiveBrush = new SolidColorBrush(Color.FromArgb(70, 130, 35, 35));
@@ -21,6 +21,8 @@ namespace PixiEditor.Models.Layers
         private bool timerRunning;
         private bool timerRunning;
         private DateTime lastEditTime;
         private DateTime lastEditTime;
 
 
+        public override bool IsMergeable => false;
+
         public string Path
         public string Path
         {
         {
             get => path;
             get => path;
@@ -31,8 +33,8 @@ namespace PixiEditor.Models.Layers
             }
             }
         }
         }
 
 
-        public TemplateLayer(string path, int width, int height)
-            : base("Template Layer")
+        public ReferenceLayer(string path, int width, int height)
+            : base("Reference Layer")
         {
         {
             Path = path;
             Path = path;
             WatcherInit();
             WatcherInit();
@@ -80,26 +82,26 @@ namespace PixiEditor.Models.Layers
 
 
             watcher.EnableRaisingEvents = true;
             watcher.EnableRaisingEvents = true;
 
 
-            watcher.Changed += TemplateChanged;
-            watcher.Deleted += TemplateDeleted;
-            watcher.Renamed += TemplateRenamed;
+            watcher.Changed += FileChanged;
+            watcher.Deleted += FileDeleted;
+            watcher.Renamed += FileRenamed;
         }
         }
 
 
-        private void TemplateRenamed(object sender, RenamedEventArgs e)
+        private void FileRenamed(object sender, RenamedEventArgs e)
         {
         {
             path = e.FullPath;
             path = e.FullPath;
             watcher.Path = System.IO.Path.GetDirectoryName(path);
             watcher.Path = System.IO.Path.GetDirectoryName(path);
             watcher.Filter = System.IO.Path.GetFileName(path);
             watcher.Filter = System.IO.Path.GetFileName(path);
         }
         }
 
 
-        private void TemplateDeleted(object sender, FileSystemEventArgs e)
+        private void FileDeleted(object sender, FileSystemEventArgs e)
         {
         {
             path = null;
             path = null;
             watcher.Dispose();
             watcher.Dispose();
             watcher = null;
             watcher = null;
         }
         }
 
 
-        private void TemplateChanged(object sender, FileSystemEventArgs e)
+        private void FileChanged(object sender, FileSystemEventArgs e)
         {
         {
             lastEditTime = DateTime.Now;
             lastEditTime = DateTime.Now;
 
 
@@ -120,7 +122,7 @@ namespace PixiEditor.Models.Layers
             }
             }
         }
         }
 
 
-        ~TemplateLayer()
+        ~ReferenceLayer()
         {
         {
             watcher.Dispose();
             watcher.Dispose();
             updateTimer.Dispose();
             updateTimer.Dispose();

+ 24 - 7
PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs

@@ -1,4 +1,5 @@
-using PixiEditor.Helpers;
+using Microsoft.Win32;
+using PixiEditor.Helpers;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 
 
@@ -40,11 +41,15 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
 
         public void NewTemplateLayer(object parameter)
         public void NewTemplateLayer(object parameter)
         {
         {
-            ImportFileDialog dialog = new ImportFileDialog();
-
-            if (dialog.ShowDialog())
+            OpenFileDialog path = new OpenFileDialog
+            {
+                Title = "Import path",
+                CheckPathExists = true,
+                Filter = "Image Files|*.png;*.jpeg;*.jpg"
+            };
+            if (path.ShowDialog() == true)
             {
             {
-                TemplateLayer template = new TemplateLayer(dialog.FilePath, Owner.BitmapManager.ActiveDocument.Width, Owner.BitmapManager.ActiveDocument.Height);
+                ReferenceLayer template = new ReferenceLayer(path.FileName, Owner.BitmapManager.ActiveDocument.Width, Owner.BitmapManager.ActiveDocument.Height);
                 Owner.BitmapManager.ActiveDocument.DocumentSizeChanged += template.DocumentSizeChanged;
                 Owner.BitmapManager.ActiveDocument.DocumentSizeChanged += template.DocumentSizeChanged;
                 Owner.BitmapManager.ActiveDocument.Layers.Add(template);
                 Owner.BitmapManager.ActiveDocument.Layers.Add(template);
             }
             }
@@ -117,24 +122,36 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
         public void MergeWithAbove(object parameter)
         public void MergeWithAbove(object parameter)
         {
         {
             int index = (int)parameter;
             int index = (int)parameter;
-            Owner.BitmapManager.ActiveDocument.MergeLayers(index, index + 1, false);
+            Owner.BitmapManager.ActiveDocument.MergeLayers(index, Owner.BitmapManager.ActiveDocument.GetMergeableLayerAboveIndex(index), false);
         }
         }
 
 
         public void MergeWithBelow(object parameter)
         public void MergeWithBelow(object parameter)
         {
         {
             int index = (int)parameter;
             int index = (int)parameter;
-            Owner.BitmapManager.ActiveDocument.MergeLayers(index - 1, index, true);
+            Owner.BitmapManager.ActiveDocument.MergeLayers(Owner.BitmapManager.ActiveDocument.GetMergeableLayerBelowIndex(index), index, true);
         }
         }
 
 
         public bool CanMergeWithAbove(object propery)
         public bool CanMergeWithAbove(object propery)
         {
         {
             int index = (int)propery;
             int index = (int)propery;
+
+            if (!Owner.BitmapManager.ActiveDocument.ActiveLayer.IsMergeable || Owner.BitmapManager.ActiveDocument.GetMergeableLayerAboveIndex(index) == -1)
+            {
+                return false;
+            }
+
             return Owner.DocumentIsNotNull(null) && index != Owner.BitmapManager.ActiveDocument.Layers.Count - 1;
             return Owner.DocumentIsNotNull(null) && index != Owner.BitmapManager.ActiveDocument.Layers.Count - 1;
         }
         }
 
 
         public bool CanMergeWithBelow(object propery)
         public bool CanMergeWithBelow(object propery)
         {
         {
             int index = (int)propery;
             int index = (int)propery;
+
+            if (!Owner.BitmapManager.ActiveDocument.ActiveLayer.IsMergeable || Owner.BitmapManager.ActiveDocument.GetMergeableLayerBelowIndex(index) == -1)
+            {
+                return false;
+            }
+
             return Owner.DocumentIsNotNull(null) && index != 0;
             return Owner.DocumentIsNotNull(null) && index != 0;
         }
         }
     }
     }

+ 1 - 1
PixiEditor/Views/MainWindow.xaml

@@ -293,7 +293,7 @@
                                             <Button Grid.Row="0" Command="{Binding LayersSubViewModel.NewLayerCommand}" Height="30" Content="New Layer"
                                             <Button Grid.Row="0" Command="{Binding LayersSubViewModel.NewLayerCommand}" Height="30" Content="New Layer"
                                             HorizontalAlignment="Stretch" Margin="5"
                                             HorizontalAlignment="Stretch" Margin="5"
                                             Style="{StaticResource DarkRoundButton}" />
                                             Style="{StaticResource DarkRoundButton}" />
-                                            <Button Grid.Row="1" Command="{Binding LayersSubViewModel.NewTemplateLayerCommand}" Height="30" Content="New Template Layer"
+                                            <Button Grid.Row="1" Command="{Binding LayersSubViewModel.NewTemplateLayerCommand}" Height="30" Content="New Reference Layer"
                                             HorizontalAlignment="Stretch" Margin="5"
                                             HorizontalAlignment="Stretch" Margin="5"
                                             Style="{StaticResource DarkRoundButton}" />
                                             Style="{StaticResource DarkRoundButton}" />
                                             <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="10,0">
                                             <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="10,0">