Browse Source

Preview Layer without clearing on update WIP

flabbet 4 years ago
parent
commit
2610e6df0f

+ 27 - 9
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -1,9 +1,11 @@
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Linq;
 using System.Linq;
 using System.Windows.Input;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Media.Imaging;
+using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.ImageManipulation;
 using PixiEditor.Models.ImageManipulation;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
@@ -82,16 +84,16 @@ namespace PixiEditor.Models.Controllers
                 return;
                 return;
             }
             }
 
 
-            for (int i = 0; i < lastModifiedLayers.Length; i++)
+            foreach (var modifiedLayer in lastModifiedLayers)
             {
             {
-                Layer layer = Manager.ActiveDocument.Layers[lastModifiedLayers[i].LayerIndex];
+                Layer layer = Manager.ActiveDocument.Layers[modifiedLayer.LayerIndex];
 
 
-                BitmapPixelChanges oldValues = ApplyToLayer(layer, lastModifiedLayers[i]).PixelChanges;
+                BitmapPixelChanges oldValues = ApplyToLayer(layer, modifiedLayer).PixelChanges;
 
 
                 BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(
                 BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(
-                    lastModifiedLayers[i].PixelChanges,
+                    modifiedLayer.PixelChanges,
                     oldValues,
                     oldValues,
-                    lastModifiedLayers[i].LayerIndex));
+                    modifiedLayer.LayerIndex));
                 Manager.ActiveDocument.GeneratePreviewLayer();
                 Manager.ActiveDocument.GeneratePreviewLayer();
             }
             }
         }
         }
@@ -120,7 +122,7 @@ namespace PixiEditor.Models.Controllers
             }
             }
             else
             else
             {
             {
-                UseToolOnPreviewLayer(mouseMoveCords);
+                UseToolOnPreviewLayer(mouseMoveCords, tool.ClearPreviewLayerOnEachIteration);
             }
             }
         }
         }
 
 
@@ -179,19 +181,35 @@ namespace PixiEditor.Models.Controllers
             return new BitmapPixelChanges(values);
             return new BitmapPixelChanges(values);
         }
         }
 
 
-        private void UseToolOnPreviewLayer(List<Coordinates> mouseMove)
+        private void UseToolOnPreviewLayer(List<Coordinates> mouseMove, bool clearPreviewLayer = true)
         {
         {
             LayerChange[] modifiedLayers;
             LayerChange[] modifiedLayers;
             if (mouseMove.Count > 0 && mouseMove[0] != lastMousePos)
             if (mouseMove.Count > 0 && mouseMove[0] != lastMousePos)
             {
             {
-                Manager.ActiveDocument.GeneratePreviewLayer();
+                if (clearPreviewLayer || Manager.ActiveDocument.PreviewLayer == null)
+                {
+                    Manager.ActiveDocument.GeneratePreviewLayer();
+                }
+
                 modifiedLayers = ((BitmapOperationTool)Manager.SelectedTool).Use(
                 modifiedLayers = ((BitmapOperationTool)Manager.SelectedTool).Use(
                     Manager.ActiveDocument.ActiveLayer,
                     Manager.ActiveDocument.ActiveLayer,
                     mouseMove.ToArray(),
                     mouseMove.ToArray(),
                     Manager.PrimaryColor);
                     Manager.PrimaryColor);
                 BitmapPixelChanges[] changes = modifiedLayers.Select(x => x.PixelChanges).ToArray();
                 BitmapPixelChanges[] changes = modifiedLayers.Select(x => x.PixelChanges).ToArray();
                 Manager.ActiveDocument.PreviewLayer.SetPixels(BitmapPixelChanges.CombineOverride(changes));
                 Manager.ActiveDocument.PreviewLayer.SetPixels(BitmapPixelChanges.CombineOverride(changes));
-                lastModifiedLayers = modifiedLayers;
+                if (clearPreviewLayer || lastModifiedLayers == null)
+                {
+                    lastModifiedLayers = modifiedLayers;
+                }
+                else
+                {
+                    for (int i = 0; i < modifiedLayers.Length; i++)
+                    {
+                        lastModifiedLayers[i] = new LayerChange(
+                            BitmapPixelChanges.CombineOverride(
+                                lastModifiedLayers[i].PixelChanges, modifiedLayers[i].PixelChanges), lastModifiedLayers[i].LayerIndex);
+                    }
+                }
             }
             }
         }
         }
     }
     }

+ 2 - 0
PixiEditor/Models/Tools/BitmapOperationTool.cs

@@ -8,6 +8,8 @@ namespace PixiEditor.Models.Tools
     public abstract class BitmapOperationTool : Tool
     public abstract class BitmapOperationTool : Tool
     {
     {
         public bool RequiresPreviewLayer { get; set; }
         public bool RequiresPreviewLayer { get; set; }
+
+        public bool ClearPreviewLayerOnEachIteration { get; set; } = true;
 
 
         public bool UseDefaultUndoMethod { get; set; } = true;
         public bool UseDefaultUndoMethod { get; set; } = true;
 
 

+ 17 - 0
PixiEditor/Models/Tools/ToolSettings/Toolbars/PenToolbar.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using PixiEditor.Models.Tools.ToolSettings.Settings;
+
+namespace PixiEditor.Models.Tools.ToolSettings.Toolbars
+{
+    public class PenToolbar : BasicToolbar
+    {
+        public PenToolbar()
+        {
+            Settings.Add(new BoolSetting("PixelPerfectEnabled", "Pixel perfect"));
+        }
+    }
+}

+ 19 - 3
PixiEditor/Models/Tools/Tools/PenTool.cs

@@ -1,27 +1,43 @@
-using System.Windows.Input;
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media;
+using PixiEditor.Helpers.Extensions;
+using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools.ToolSettings.Settings;
 using PixiEditor.Models.Tools.ToolSettings.Settings;
 using PixiEditor.Models.Tools.ToolSettings.Toolbars;
 using PixiEditor.Models.Tools.ToolSettings.Toolbars;
-using PixiEditor.ViewModels;
 
 
 namespace PixiEditor.Models.Tools.Tools
 namespace PixiEditor.Models.Tools.Tools
 {
 {
     public class PenTool : BitmapOperationTool
     public class PenTool : BitmapOperationTool
     {
     {
         private readonly SizeSetting toolSizeSetting;
         private readonly SizeSetting toolSizeSetting;
+        private readonly BoolSetting pixelPerfectSetting;
         private readonly LineTool lineTool;
         private readonly LineTool lineTool;
+        private IEnumerable<Coordinates> lastCords = Array.Empty<Coordinates>();
 
 
         public PenTool()
         public PenTool()
         {
         {
             Cursor = Cursors.Pen;
             Cursor = Cursors.Pen;
             ActionDisplay = "Click and move to draw.";
             ActionDisplay = "Click and move to draw.";
             Tooltip = "Standard brush. (B)";
             Tooltip = "Standard brush. (B)";
-            Toolbar = new BasicToolbar();
+            Toolbar = new PenToolbar();
             toolSizeSetting = Toolbar.GetSetting<SizeSetting>("ToolSize");
             toolSizeSetting = Toolbar.GetSetting<SizeSetting>("ToolSize");
+            pixelPerfectSetting = Toolbar.GetSetting<BoolSetting>("PixelPerfectEnabled");
             lineTool = new LineTool();
             lineTool = new LineTool();
+            RequiresPreviewLayer = true;
+            ClearPreviewLayerOnEachIteration = false;
+        }
+
+        public override void AfterAddedUndo(UndoManager undoManager)
+        {
+            base.AfterAddedUndo(undoManager);
+            lastCords = Array.Empty<Coordinates>();
         }
         }
 
 
         public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color)
         public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color)