Browse Source

Beginning of a big rework

Krzysztof Krysiński 4 years ago
parent
commit
e86fbf39cc

+ 6 - 115
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -19,10 +19,6 @@ namespace PixiEditor.Models.Controllers
 {
 {
     public class BitmapOperationsUtility
     public class BitmapOperationsUtility
     {
     {
-        public List<LayerChange> PreviewLayerChanges => previewLayerChanges;
-
-        private List<LayerChange> previewLayerChanges;
-
         private Coordinates lastMousePos;
         private Coordinates lastMousePos;
 
 
         private SizeSetting sizeSetting;
         private SizeSetting sizeSetting;
@@ -43,16 +39,8 @@ namespace PixiEditor.Models.Controllers
                 return;
                 return;
             }
             }
 
 
-            BitmapPixelChanges changes = BitmapPixelChanges.FromSingleColoredArray(pixels, Color.FromArgb(0, 0, 0, 0));
-            Dictionary<Guid, Color[]> oldValues = BitmapUtils.GetPixelsForSelection(layers, pixels);
-            LayerChange[] old = new LayerChange[layers.Length];
-            LayerChange[] newChange = new LayerChange[layers.Length];
             for (int i = 0; i < layers.Length; i++)
             for (int i = 0; i < layers.Length; i++)
             {
             {
-                Guid guid = layers[i].LayerGuid;
-                old[i] = new LayerChange(
-                    BitmapPixelChanges.FromArrays(pixels, oldValues[layers[i].LayerGuid]), guid);
-                newChange[i] = new LayerChange(changes, guid);
                 layers[i].SetPixels(changes);
                 layers[i].SetPixels(changes);
             }
             }
 
 
@@ -85,37 +73,8 @@ namespace PixiEditor.Models.Controllers
         /// </summary>
         /// </summary>
         public void ApplyPreviewLayer()
         public void ApplyPreviewLayer()
         {
         {
-            if (previewLayerChanges == null)
-            {
-                return;
-            }
-
-            Layer[] layers = new Layer[previewLayerChanges.Count];
-
-            for (int i = 0; i < layers.Length; i++)
-            {
-                layers[i] = Manager.ActiveDocument.Layers.First(x => x.LayerGuid == previewLayerChanges[i].LayerGuid);
-            }
-
-            if (layers.Length > 0)
-            {
-                IEnumerable<LayerChange> oldValues =
-                    ApplyToLayers(layers, previewLayerChanges.ToArray());
-
-                foreach (var oldValue in oldValues)
-                {
-                    var previewChanges = previewLayerChanges.First(x => x.LayerGuid == oldValue.LayerGuid);
-
-                    BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(
-                        previewChanges.PixelChanges,
-                        oldValue.PixelChanges,
-                        previewChanges.LayerGuid));
-                }
-
-                Manager.ActiveDocument.GeneratePreviewLayer();
-            }
-
-            previewLayerChanges = null;
+            // Don't forget about firing BitmapChanged
+            Manager.ActiveDocument.GeneratePreviewLayer();
         }
         }
 
 
         private void UseTool(List<Coordinates> mouseMoveCords, BitmapOperationTool tool, Color color)
         private void UseTool(List<Coordinates> mouseMoveCords, BitmapOperationTool tool, Color color)
@@ -145,54 +104,14 @@ namespace PixiEditor.Models.Controllers
 
 
             if (!tool.RequiresPreviewLayer)
             if (!tool.RequiresPreviewLayer)
             {
             {
-                LayerChange[] modifiedLayers = tool.Use(Manager.ActiveLayer, mouseMoveCords, color);
-                LayerChange[] oldPixelsValues = new LayerChange[modifiedLayers.Length];
-                for (int i = 0; i < modifiedLayers.Length; i++)
-                {
-                    Layer layer = Manager.ActiveDocument.Layers.First(x => x.LayerGuid == modifiedLayers[i].LayerGuid);
-                    oldPixelsValues[i] = ApplyToLayer(layer, modifiedLayers[i]);
-
-                    BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(
-                        modifiedLayers[i].PixelChanges,
-                        oldPixelsValues[i].PixelChanges,
-                        modifiedLayers[i].LayerGuid));
-                }
+                tool.Use(Manager.ActiveLayer, mouseMoveCords, color);
+                // BitmapChanged was here
             }
             }
             else
             else
             {
             {
                 UseToolOnPreviewLayer(mouseMoveCords, tool.ClearPreviewLayerOnEachIteration);
                 UseToolOnPreviewLayer(mouseMoveCords, tool.ClearPreviewLayerOnEachIteration);
             }
             }
         }
         }
-
-        private LayerChange ApplyToLayer(Layer layer, LayerChange change)
-        {
-            return ApplyToLayers(new Layer[] { layer }, new LayerChange[] { change })[0];
-        }
-
-        private LayerChange[] ApplyToLayers(Layer[] layers, LayerChange[] changes)
-        {
-            LayerChange[] oldPixelValues = new LayerChange[changes.Length];
-            for (int i = 0; i < layers.Length; i++)
-            {
-                Layer layer = layers[i];
-                LayerChange change = changes.First(x => x.LayerGuid == layer.LayerGuid);
-                layer.DynamicResize(change.PixelChanges);
-
-                oldPixelValues[i] = new LayerChange(
-                GetOldPixelsValues(change.PixelChanges.ChangedPixels.Keys.ToArray()),
-                change.LayerGuid);
-            }
-
-            for (int i = 0; i < layers.Length; i++)
-            {
-                Layer layer = layers[i];
-                LayerChange change = changes.First(x => x.LayerGuid == layer.LayerGuid);
-                layer.SetPixels(change.PixelChanges, false);
-            }
-
-            return oldPixelValues;
-        }
-
         private bool MouseCordsNotInLine(List<Coordinates> cords, int thickness)
         private bool MouseCordsNotInLine(List<Coordinates> cords, int thickness)
         {
         {
             return (cords[0].X > cords[^1].X - thickness && cords[0].X < cords[^1].X + thickness)
             return (cords[0].X > cords[^1].X - thickness && cords[0].X < cords[^1].X + thickness)
@@ -258,45 +177,17 @@ namespace PixiEditor.Models.Controllers
 
 
         private void UseToolOnPreviewLayer(List<Coordinates> mouseMove, bool clearPreviewLayer = true)
         private void UseToolOnPreviewLayer(List<Coordinates> mouseMove, bool clearPreviewLayer = true)
         {
         {
-            LayerChange[] modifiedLayers;
             if (mouseMove.Count > 0 && mouseMove[0] != lastMousePos)
             if (mouseMove.Count > 0 && mouseMove[0] != lastMousePos)
             {
             {
                 if (clearPreviewLayer || Manager.ActiveDocument.PreviewLayer == null)
                 if (clearPreviewLayer || Manager.ActiveDocument.PreviewLayer == null)
                 {
                 {
                     Manager.ActiveDocument.GeneratePreviewLayer();
                     Manager.ActiveDocument.GeneratePreviewLayer();
                 }
                 }
-
-                modifiedLayers = ((BitmapOperationTool)Manager.SelectedTool).Use(
+                // TODO: Use on preview layer
+                ((BitmapOperationTool)Manager.SelectedTool).Use(
                     Manager.ActiveDocument.ActiveLayer,
                     Manager.ActiveDocument.ActiveLayer,
                     mouseMove,
                     mouseMove,
                     Manager.PrimaryColor);
                     Manager.PrimaryColor);
-
-                BitmapPixelChanges[] changes = modifiedLayers.Select(x => x.PixelChanges).ToArray();
-                if (changes.Length == 0)
-                {
-                    return;
-                }
-
-                Manager.ActiveDocument.PreviewLayer.SetPixels(BitmapPixelChanges.CombineOverride(changes));
-
-                if (clearPreviewLayer || previewLayerChanges == null)
-                {
-                    previewLayerChanges = new List<LayerChange>(modifiedLayers);
-                }
-                else
-                {
-                    InjectPreviewLayerChanges(modifiedLayers);
-                }
-            }
-        }
-
-        private void InjectPreviewLayerChanges(LayerChange[] modifiedLayers)
-        {
-            for (int i = 0; i < modifiedLayers.Length; i++)
-            {
-                var layer = previewLayerChanges.First(x => x.LayerGuid == modifiedLayers[i].LayerGuid);
-                layer.PixelChanges.ChangedPixels.AddRangeOverride(modifiedLayers[i].PixelChanges.ChangedPixels);
-                layer.PixelChanges = layer.PixelChanges.WithoutTransparentPixels();
             }
             }
         }
         }
     }
     }

+ 0 - 105
PixiEditor/Models/Controllers/PixelChangesController.cs

@@ -1,105 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using PixiEditor.Models.DataHolders;
-
-namespace PixiEditor.Models.Controllers
-{
-    public class PixelChangesController
-    {
-        private Dictionary<Guid, LayerChange> LastChanges { get; set; }
-
-        private Dictionary<Guid, LayerChange> LastOldValues { get; set; }
-
-        /// <summary>
-        ///     Adds layer changes to controller.
-        /// </summary>
-        /// <param name="changes">New changes.</param>
-        /// <param name="oldValues">Old values of changes.</param>
-        public void AddChanges(LayerChange changes, LayerChange oldValues)
-        {
-            if (changes.PixelChanges.ChangedPixels.Count > 0)
-            {
-                if (LastChanges == null)
-                {
-                    LastChanges = new Dictionary<Guid, LayerChange> { { changes.LayerGuid, changes } };
-                    LastOldValues = new Dictionary<Guid, LayerChange> { { oldValues.LayerGuid, oldValues } };
-                }
-                else if (LastChanges.ContainsKey(changes.LayerGuid))
-                {
-                    AddToExistingLayerChange(changes, oldValues);
-                }
-                else
-                {
-                    AddNewLayerChange(changes, oldValues);
-                }
-            }
-        }
-
-        /// <summary>
-        ///     Returns all changes and deletes them from controller.
-        /// </summary>
-        /// <returns>Tuple array with new changes and old values.</returns>
-        public Tuple<LayerChange, LayerChange>[] PopChanges()
-        {
-            // Maybe replace Tuple with custom data type
-            if (LastChanges == null)
-            {
-                return null;
-            }
-
-            Tuple<LayerChange, LayerChange>[] result = new Tuple<LayerChange, LayerChange>[LastChanges.Count];
-            int i = 0;
-            foreach (KeyValuePair<Guid, LayerChange> change in LastChanges)
-            {
-                Dictionary<Position.Coordinates, System.Windows.Media.Color> pixelChanges =
-                    change.Value.PixelChanges.ChangedPixels.ToDictionary(entry => entry.Key, entry => entry.Value);
-                Dictionary<Position.Coordinates, System.Windows.Media.Color> oldValues = LastOldValues[change.Key].PixelChanges.ChangedPixels
-                    .ToDictionary(entry => entry.Key, entry => entry.Value);
-
-                LayerChange tmp = new LayerChange(new BitmapPixelChanges(pixelChanges), change.Key);
-                LayerChange oldValuesTmp = new LayerChange(new BitmapPixelChanges(oldValues), change.Key);
-
-                result[i] = new Tuple<LayerChange, LayerChange>(tmp, oldValuesTmp);
-                i++;
-            }
-
-            LastChanges = null;
-            LastOldValues = null;
-            return result;
-        }
-
-        private void AddNewLayerChange(LayerChange changes, LayerChange oldValues)
-        {
-            LastChanges[changes.LayerGuid] = changes;
-            LastOldValues[changes.LayerGuid] = oldValues;
-        }
-
-        private void AddToExistingLayerChange(LayerChange layerChange, LayerChange oldValues)
-        {
-            foreach (KeyValuePair<Position.Coordinates, System.Windows.Media.Color> change in layerChange.PixelChanges.ChangedPixels)
-            {
-                if (LastChanges[layerChange.LayerGuid].PixelChanges.ChangedPixels.ContainsKey(change.Key))
-                {
-                    continue;
-                }
-                else
-                {
-                    LastChanges[layerChange.LayerGuid].PixelChanges.ChangedPixels.Add(change.Key, change.Value);
-                }
-            }
-
-            foreach (KeyValuePair<Position.Coordinates, System.Windows.Media.Color> change in oldValues.PixelChanges.ChangedPixels)
-            {
-                if (LastOldValues[layerChange.LayerGuid].PixelChanges.ChangedPixels.ContainsKey(change.Key))
-                {
-                    continue;
-                }
-                else
-                {
-                    LastOldValues[layerChange.LayerGuid].PixelChanges.ChangedPixels.Add(change.Key, change.Value);
-                }
-            }
-        }
-    }
-}

+ 0 - 24
PixiEditor/Models/DataHolders/LayerChange.cs

@@ -1,24 +0,0 @@
-using System;
-using PixiEditor.Models.Layers;
-
-namespace PixiEditor.Models.DataHolders
-{
-    public class LayerChange
-    {
-        public LayerChange(BitmapPixelChanges pixelChanges, Guid layerGuid)
-        {
-            PixelChanges = pixelChanges;
-            LayerGuid = layerGuid;
-        }
-
-        public LayerChange(BitmapPixelChanges pixelChanges, Layer layer)
-        {
-            PixelChanges = pixelChanges;
-            LayerGuid = layer.LayerGuid;
-        }
-
-        public BitmapPixelChanges PixelChanges { get; set; }
-
-        public Guid LayerGuid { get; set; }
-    }
-}

+ 1 - 15
PixiEditor/Models/Tools/BitmapOperationTool.cs

@@ -17,20 +17,6 @@ namespace PixiEditor.Models.Tools
         public bool UseDefaultUndoMethod { get; set; } = true;
         public bool UseDefaultUndoMethod { get; set; } = true;
         public virtual bool UsesShift => true;
         public virtual bool UsesShift => true;
 
 
-        private readonly LayerChange[] onlyLayerArr = new LayerChange[] { new LayerChange(BitmapPixelChanges.Empty, Guid.Empty) };
-
-        public abstract LayerChange[] Use(Layer layer, List<Coordinates> mouseMove, Color color);
-
-        protected LayerChange[] Only(BitmapPixelChanges changes, Layer layer)
-        {
-            onlyLayerArr[0] = new LayerChange(changes, layer);
-            return onlyLayerArr;
-        }
-
-        protected LayerChange[] Only(BitmapPixelChanges changes, Guid layerGuid)
-        {
-            onlyLayerArr[0] = new LayerChange(changes, layerGuid);
-            return onlyLayerArr;
-        }
+        public abstract void Use(Layer layer, List<Coordinates> mouseMove, Color color);
     }
     }
 }
 }

+ 1 - 1
PixiEditor/Models/Tools/ShapeTool.cs

@@ -56,7 +56,7 @@ namespace PixiEditor.Models.Tools
         }
         }
 
 
         // TODO: Add cache for lines 31, 32 (hopefully it would speed up calculation)
         // TODO: Add cache for lines 31, 32 (hopefully it would speed up calculation)
-        public abstract override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color);
+        public abstract override void Use(Layer layer, List<Coordinates> coordinates, Color color);
 
 
         protected static IEnumerable<Coordinates> GetThickShape(IEnumerable<Coordinates> shape, int thickness)
         protected static IEnumerable<Coordinates> GetThickShape(IEnumerable<Coordinates> shape, int thickness)
         {
         {

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

@@ -1,13 +1,17 @@
 using System;
 using System;
+using System.Collections.Generic;
 using System.Text;
 using System.Text;
 using System.Windows.Input;
 using System.Windows.Input;
 using PixiEditor.Helpers;
 using PixiEditor.Helpers;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Controllers;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools.ToolSettings;
 using PixiEditor.Models.Tools.ToolSettings;
 using PixiEditor.Models.Tools.ToolSettings.Toolbars;
 using PixiEditor.Models.Tools.ToolSettings.Toolbars;
-
+using PixiEditor.Models.Undo;
+
 namespace PixiEditor.Models.Tools
 namespace PixiEditor.Models.Tools
 {
 {
     public abstract class Tool : NotifyableObject
     public abstract class Tool : NotifyableObject
@@ -31,7 +35,7 @@ namespace PixiEditor.Models.Tools
             set
             set
             {
             {
                 actionDisplay = value;
                 actionDisplay = value;
-                RaisePropertyChanged("ActionDisplay");
+                RaisePropertyChanged(nameof(ActionDisplay));
             }
             }
         }
         }
 
 
@@ -41,7 +45,7 @@ namespace PixiEditor.Models.Tools
             set
             set
             {
             {
                 isActive = value;
                 isActive = value;
-                RaisePropertyChanged("IsActive");
+                RaisePropertyChanged(nameof(IsActive));
             }
             }
         }
         }
 
 
@@ -82,6 +86,18 @@ namespace PixiEditor.Models.Tools
         public virtual void OnMouseMove(MouseEventArgs e)
         public virtual void OnMouseMove(MouseEventArgs e)
         {
         {
         }
         }
+
+        public virtual void AddUndoProcess(Document document)
+        {
+            //StorageBasedChange change = new StorageBasedChange(document, affectedLayers, false);
+
+            //manager.AddUndoChange(change.ToChange(), )
+        }
+
+        public virtual void RedoProcess(UndoManager manager)
+        {
+
+        }
 
 
         public virtual void AfterAddedUndo(UndoManager undoManager)
         public virtual void AfterAddedUndo(UndoManager undoManager)
         {
         {

+ 1 - 3
PixiEditor/Models/Tools/Tools/BrightnessTool.cs

@@ -52,7 +52,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
             }
         }
         }
 
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
+        public override void Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
         {
             int toolSize = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             int toolSize = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             float correctionFactor = Toolbar.GetSetting<FloatSetting>("CorrectionFactor").Value;
             float correctionFactor = Toolbar.GetSetting<FloatSetting>("CorrectionFactor").Value;
@@ -67,8 +67,6 @@ namespace PixiEditor.Models.Tools.Tools
             {
             {
                 layersChanges[0] = new LayerChange(ChangeBrightness(layer, coordinates[0], toolSize, correctionFactor), layer);
                 layersChanges[0] = new LayerChange(ChangeBrightness(layer, coordinates[0], toolSize, correctionFactor), layer);
             }
             }
-
-            return layersChanges;
         }
         }
 
 
         public BitmapPixelChanges ChangeBrightness(Layer layer, Coordinates coordinates, int toolSize, float correctionFactor)
         public BitmapPixelChanges ChangeBrightness(Layer layer, Coordinates coordinates, int toolSize, float correctionFactor)

+ 1 - 3
PixiEditor/Models/Tools/Tools/CircleTool.cs

@@ -36,7 +36,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
             }
         }
         }
 
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
+        public override void Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
         {
             int thickness = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             int thickness = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             DoubleCords fixedCoordinates = CalculateCoordinatesForShapeRotation(coordinates[^1], coordinates[0]);
             DoubleCords fixedCoordinates = CalculateCoordinatesForShapeRotation(coordinates[^1], coordinates[0]);
@@ -49,8 +49,6 @@ namespace PixiEditor.Models.Tools.Tools
                     BitmapPixelChanges.FromSingleColoredArray(CalculateFillForEllipse(outline), fillColor)
                     BitmapPixelChanges.FromSingleColoredArray(CalculateFillForEllipse(outline), fillColor)
                         .ChangedPixels);
                         .ChangedPixels);
             }
             }
-
-            return new[] { new LayerChange(pixels, layer) };
         }
         }
 
 
         /// <summary>
         /// <summary>

+ 5 - 5
PixiEditor/Models/Tools/Tools/EraserTool.cs

@@ -21,18 +21,18 @@ namespace PixiEditor.Models.Tools.Tools
         }
         }
 
 
         public override bool UsesShift => false;
         public override bool UsesShift => false;
-		public override string Tooltip => "Erasers color from pixel. (E)";
+
+        public override string Tooltip => "Erasers color from pixel. (E)";
 
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
+        public override void Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
         {
-            return Erase(layer, coordinates, Toolbar.GetSetting<SizeSetting>("ToolSize").Value);
+            Erase(layer, coordinates, Toolbar.GetSetting<SizeSetting>("ToolSize").Value);
         }
         }
 
 
-        public LayerChange[] Erase(Layer layer, List<Coordinates> coordinates, int toolSize)
+        public void Erase(Layer layer, List<Coordinates> coordinates, int toolSize)
         {
         {
             Coordinates startingCords = coordinates.Count > 1 ? coordinates[1] : coordinates[0];
             Coordinates startingCords = coordinates.Count > 1 ? coordinates[1] : coordinates[0];
             BitmapPixelChanges pixels = pen.Draw(startingCords, coordinates[0], System.Windows.Media.Colors.Transparent, toolSize);
             BitmapPixelChanges pixels = pen.Draw(startingCords, coordinates[0], System.Windows.Media.Colors.Transparent, toolSize);
-            return Only(pixels, layer);
         }
         }
     }
     }
 }
 }

+ 2 - 2
PixiEditor/Models/Tools/Tools/FloodFill.cs

@@ -19,9 +19,9 @@ namespace PixiEditor.Models.Tools.Tools
 
 
         public override string Tooltip => "Fills area with color. (G)";
         public override string Tooltip => "Fills area with color. (G)";
 
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
+        public override void Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
         {
-            return Only(ForestFire(layer, coordinates[0], color), layer);
+            ForestFire(layer, coordinates[0], color);
         }
         }
 
 
         public BitmapPixelChanges ForestFire(Layer layer, Coordinates startingCoords, Color newColor)
         public BitmapPixelChanges ForestFire(Layer layer, Coordinates startingCoords, Color newColor)

+ 33 - 40
PixiEditor/Models/Tools/Tools/LineTool.cs

@@ -2,6 +2,7 @@
 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 PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
@@ -40,38 +41,35 @@ namespace PixiEditor.Models.Tools.Tools
             }
             }
         }
         }
 
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
+        public override void Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
         {
-            BitmapPixelChanges pixels =
-                BitmapPixelChanges.FromSingleColoredArray(
-                    CreateLine(
-                        coordinates,
-                        Toolbar.GetSetting<SizeSetting>("ToolSize").Value,
-                        CapType.Square,
-                        CapType.Square), color);
-            return Only(pixels, layer);
+            CreateLine(
+                coordinates,
+                Toolbar.GetSetting<SizeSetting>("ToolSize").Value,
+                CapType.Square,
+                CapType.Square);
         }
         }
 
 
-        public IEnumerable<Coordinates> CreateLine(Coordinates start, Coordinates end, int thickness)
+        public void CreateLine(Coordinates start, Coordinates end, int thickness)
         {
         {
-            return CreateLineFastest(start, end, thickness);
+            CreateLineFastest(start, end, thickness);
         }
         }
 
 
-        public IEnumerable<Coordinates> CreateLine(Coordinates start, Coordinates end, int thickness, CapType startCap, CapType endCap)
+        public void CreateLine(Coordinates start, Coordinates end, int thickness, CapType startCap, CapType endCap)
         {
         {
-            return CreateLine(new List<Coordinates>() { end, start }, thickness, startCap, endCap);
+            CreateLine(new List<Coordinates>() { end, start }, thickness, startCap, endCap);
         }
         }
 
 
-        private IEnumerable<Coordinates> CreateLine(IEnumerable<Coordinates> coordinates, int thickness, CapType startCap, CapType endCap)
+        private void CreateLine(IEnumerable<Coordinates> coordinates, int thickness, CapType startCap, CapType endCap)
         {
         {
             Coordinates startingCoordinates = coordinates.Last();
             Coordinates startingCoordinates = coordinates.Last();
             Coordinates latestCoordinates = coordinates.First();
             Coordinates latestCoordinates = coordinates.First();
             if (thickness == 1)
             if (thickness == 1)
             {
             {
-                return BresenhamLine(startingCoordinates.X, startingCoordinates.Y, latestCoordinates.X, latestCoordinates.Y);
+                BresenhamLine(startingCoordinates.X, startingCoordinates.Y, latestCoordinates.X, latestCoordinates.Y);
             }
             }
 
 
-            return GetLinePoints(startingCoordinates, latestCoordinates, thickness, startCap, endCap);
+            GetLinePoints(startingCoordinates, latestCoordinates, thickness, startCap, endCap);
         }
         }
 
 
         private IEnumerable<Coordinates> CreateLineFastest(Coordinates start, Coordinates end, int thickness)
         private IEnumerable<Coordinates> CreateLineFastest(Coordinates start, Coordinates end, int thickness)
@@ -85,38 +83,35 @@ namespace PixiEditor.Models.Tools.Tools
             return GetThickShape(line, thickness);
             return GetThickShape(line, thickness);
         }
         }
 
 
-        private IEnumerable<Coordinates> GetLinePoints(Coordinates start, Coordinates end, int thickness, CapType startCap, CapType endCap)
+        private void GetLinePoints(Coordinates start, Coordinates end, int thickness, CapType startCap, CapType endCap)
         {
         {
-            IEnumerable<Coordinates> startingCap = GetCapCoordinates(startCap, start, thickness);
+            GetCapCoordinates(startCap, start, thickness);
             if (start == end)
             if (start == end)
             {
             {
-                return startingCap;
+                return;
             }
             }
 
 
-            IEnumerable<Coordinates> line = BresenhamLine(start.X, start.Y, end.X, end.Y);
-
-            List<Coordinates> output = new List<Coordinates>(startingCap);
+            BresenhamLine(start.X, start.Y, end.X, end.Y);
 
 
-            output.AddRange(GetCapCoordinates(endCap, end, thickness));
+            GetCapCoordinates(endCap, end, thickness);
             if (line.Count() > 2)
             if (line.Count() > 2)
             {
             {
                 output.AddRange(GetThickShape(line.Except(new[] { start, end }), thickness));
                 output.AddRange(GetThickShape(line.Except(new[] { start, end }), thickness));
             }
             }
-
-            return output.Distinct();
         }
         }
 
 
-        private IEnumerable<Coordinates> GetCapCoordinates(CapType cap, Coordinates position, int thickness)
+        private void GetCapCoordinates(CapType cap, Coordinates position, int thickness)
         {
         {
             switch (cap)
             switch (cap)
             {
             {
                 case CapType.Round:
                 case CapType.Round:
-                    {
-                        return GetRoundCap(position, thickness); // Round cap is not working very well, circle tool must be improved
-                    }
+
+                    GetRoundCap(position, thickness); // Round cap is not working very well, circle tool must be improved
+                    break;
 
 
                 default:
                 default:
-                    return GetThickShape(new[] { position }, thickness);
+                    GetThickShape(new[] { position }, thickness);
+                    break;
             }
             }
         }
         }
 
 
@@ -125,19 +120,19 @@ namespace PixiEditor.Models.Tools.Tools
         /// </summary>
         /// </summary>
         /// <param name="position">Starting position of cap.</param>
         /// <param name="position">Starting position of cap.</param>
         /// <param name="thickness">Thickness of cap.</param>
         /// <param name="thickness">Thickness of cap.</param>
-        private IEnumerable<Coordinates> GetRoundCap(Coordinates position, int thickness)
+        private void GetRoundCap(Coordinates position, int thickness)
         {
         {
             IEnumerable<Coordinates> rectangleCords = CoordinatesCalculator.RectangleToCoordinates(
             IEnumerable<Coordinates> rectangleCords = CoordinatesCalculator.RectangleToCoordinates(
                 CoordinatesCalculator.CalculateThicknessCenter(position, thickness));
                 CoordinatesCalculator.CalculateThicknessCenter(position, thickness));
-            return circleTool.CreateEllipse(rectangleCords.First(), rectangleCords.Last(), 1, true);
+            circleTool.CreateEllipse(rectangleCords.First(), rectangleCords.Last(), 1, true);
         }
         }
 
 
-        private IEnumerable<Coordinates> BresenhamLine(int x1, int y1, int x2, int y2)
+        private void BresenhamLine(Layer layer, Color color, int x1, int y1, int x2, int y2)
         {
         {
-            List<Coordinates> coordinates = new List<Coordinates>();
+            using var context = layer.LayerBitmap.GetBitmapContext();
             if (x1 == x2 && y1 == y2)
             if (x1 == x2 && y1 == y2)
             {
             {
-                return new[] { new Coordinates(x1, y1) };
+                layer.SetPixel(new Coordinates(x1, y1), color);
             }
             }
 
 
             int d, dx, dy, ai, bi, xi, yi;
             int d, dx, dy, ai, bi, xi, yi;
@@ -165,7 +160,7 @@ namespace PixiEditor.Models.Tools.Tools
                 dy = y1 - y2;
                 dy = y1 - y2;
             }
             }
 
 
-            coordinates.Add(new Coordinates(x, y));
+            layer.SetPixel(new Coordinates(x, y), color);
 
 
             if (dx > dy)
             if (dx > dy)
             {
             {
@@ -187,7 +182,7 @@ namespace PixiEditor.Models.Tools.Tools
                         x += xi;
                         x += xi;
                     }
                     }
 
 
-                    coordinates.Add(new Coordinates(x, y));
+                    layer.SetPixel(new Coordinates(x, y), color);
                 }
                 }
             }
             }
             else
             else
@@ -210,11 +205,9 @@ namespace PixiEditor.Models.Tools.Tools
                         y += yi;
                         y += yi;
                     }
                     }
 
 
-                    coordinates.Add(new Coordinates(x, y));
+                    layer.SetPixel(new Coordinates(x, y), color);
                 }
                 }
             }
             }
-
-            return coordinates;
         }
         }
     }
     }
 }
 }

+ 1 - 1
PixiEditor/Models/Tools/Tools/MoveTool.cs

@@ -141,7 +141,7 @@ namespace PixiEditor.Models.Tools.Tools
             startingOffsets = GetOffsets(affectedLayers);
             startingOffsets = GetOffsets(affectedLayers);
         }
         }
 
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> mouseMove, Color color)
+        public override void Use(Layer layer, List<Coordinates> mouseMove, Color color)
         {
         {
             LayerChange[] result = new LayerChange[affectedLayers.Length];
             LayerChange[] result = new LayerChange[affectedLayers.Length];
             var end = mouseMove[0];
             var end = mouseMove[0];

+ 1 - 2
PixiEditor/Models/Tools/Tools/PenTool.cs

@@ -48,7 +48,7 @@ namespace PixiEditor.Models.Tools.Tools
             confirmedPixels.Clear();
             confirmedPixels.Clear();
         }
         }
 
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
+        public override void Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
         {
             Coordinates startingCords = coordinates.Count > 1 ? coordinates[1] : coordinates[0];
             Coordinates startingCords = coordinates.Count > 1 ? coordinates[1] : coordinates[0];
             BitmapPixelChanges pixels = Draw(
             BitmapPixelChanges pixels = Draw(
@@ -58,7 +58,6 @@ namespace PixiEditor.Models.Tools.Tools
                 toolSizeSetting.Value,
                 toolSizeSetting.Value,
                 pixelPerfectSetting.Value,
                 pixelPerfectSetting.Value,
                 BitmapManager.ActiveDocument.PreviewLayer);
                 BitmapManager.ActiveDocument.PreviewLayer);
-            return Only(pixels, layer);
         }
         }
 
 
         public BitmapPixelChanges Draw(Coordinates startingCoords, Coordinates latestCords, Color color, int toolSize, bool pixelPerfect = false, Layer previewLayer = null)
         public BitmapPixelChanges Draw(Coordinates startingCoords, Coordinates latestCords, Color color, int toolSize, bool pixelPerfect = false, Layer previewLayer = null)

+ 1 - 3
PixiEditor/Models/Tools/Tools/RectangleTool.cs

@@ -38,7 +38,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
             }
         }
         }
 
 
-        public override LayerChange[] Use(Layer layer, List<Coordinates> coordinates, Color color)
+        public override void Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
         {
             int thickness = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             int thickness = Toolbar.GetSetting<SizeSetting>("ToolSize").Value;
             BitmapPixelChanges pixels =
             BitmapPixelChanges pixels =
@@ -51,8 +51,6 @@ namespace PixiEditor.Models.Tools.Tools
                             CalculateFillForRectangle(coordinates[^1], coordinates[0], thickness), fillColor)
                             CalculateFillForRectangle(coordinates[^1], coordinates[0], thickness), fillColor)
                         .ChangedPixels);
                         .ChangedPixels);
             }
             }
-
-            return new[] { new LayerChange(pixels, layer) };
         }
         }
 
 
         public IEnumerable<Coordinates> CreateRectangle(List<Coordinates> coordinates, int thickness)
         public IEnumerable<Coordinates> CreateRectangle(List<Coordinates> coordinates, int thickness)

+ 5 - 29
PixiEditor/ViewModels/SubViewModels/Main/UndoViewModel.cs

@@ -15,21 +15,6 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
 
         public RelayCommand RedoCommand { get; set; }
         public RelayCommand RedoCommand { get; set; }
 
 
-        private LayerChange[] undoChanges;
-
-        public LayerChange[] UndoChanges // This acts like UndoManager process, but it was implemented before process system, so it can be transformed into it
-        {
-            get => undoChanges;
-            set
-            {
-                undoChanges = value;
-                for (int i = 0; i < value.Length; i++)
-                {
-                    Owner.BitmapManager.ActiveDocument.Layers.First(x => x.LayerGuid == value[i].LayerGuid).SetPixels(value[i].PixelChanges);
-                }
-            }
-        }
-
         public UndoViewModel(ViewModelMain owner)
         public UndoViewModel(ViewModelMain owner)
             : base(owner)
             : base(owner)
         {
         {
@@ -43,21 +28,12 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             ClearUndoTempDirectory();
             ClearUndoTempDirectory();
         }
         }
 
 
-        public void TriggerNewUndoChange(Tool toolUsed)
+        public void TriggerNewUndoChange(Tool selectedTool)
         {
         {
-            if (BitmapManager.IsOperationTool(toolUsed)
-                && ((BitmapOperationTool)toolUsed).UseDefaultUndoMethod)
-            {
-                Tuple<LayerChange, LayerChange>[] changes = Owner.ChangesController.PopChanges();
-                if (changes != null && changes.Length > 0)
-                {
-                    LayerChange[] newValues = changes.Select(x => x.Item1).ToArray();
-                    LayerChange[] oldValues = changes.Select(x => x.Item2).ToArray();
-                    Owner.BitmapManager.ActiveDocument.UndoManager.AddUndoChange(
-                        new Change("UndoChanges", oldValues, newValues, root: this));
-                    toolUsed.AfterAddedUndo(Owner.BitmapManager.ActiveDocument.UndoManager);
-                }
-            }
+            var activeDoc = Owner.BitmapManager.ActiveDocument;
+            if (activeDoc is null) return;
+
+            selectedTool.AddUndoProcess(activeDoc);
         }
         }
 
 
         /// <summary>
         /// <summary>

+ 0 - 6
PixiEditor/ViewModels/ViewModelMain.cs

@@ -70,8 +70,6 @@ namespace PixiEditor.ViewModels
 
 
         public BitmapManager BitmapManager { get; set; }
         public BitmapManager BitmapManager { get; set; }
 
 
-        public PixelChangesController ChangesController { get; set; }
-
         public ShortcutController ShortcutController { get; set; }
         public ShortcutController ShortcutController { get; set; }
 
 
         public StylusViewModel StylusSubViewModel { get; set; }
         public StylusViewModel StylusSubViewModel { get; set; }
@@ -148,7 +146,6 @@ namespace PixiEditor.ViewModels
 
 
             SelectionSubViewModel = new SelectionViewModel(this);
             SelectionSubViewModel = new SelectionViewModel(this);
 
 
-            ChangesController = new PixelChangesController();
             OnStartupCommand = new RelayCommand(OnStartup);
             OnStartupCommand = new RelayCommand(OnStartup);
             CloseWindowCommand = new RelayCommand(CloseWindow);
             CloseWindowCommand = new RelayCommand(CloseWindow);
 
 
@@ -379,9 +376,6 @@ namespace PixiEditor.ViewModels
 
 
         private void BitmapUtility_BitmapChanged(object sender, BitmapChangedEventArgs e)
         private void BitmapUtility_BitmapChanged(object sender, BitmapChangedEventArgs e)
         {
         {
-            ChangesController.AddChanges(
-                new LayerChange(e.PixelsChanged, e.ChangedLayerGuid),
-                new LayerChange(e.OldPixelsValues, e.ChangedLayerGuid));
             BitmapManager.ActiveDocument.ChangesSaved = false;
             BitmapManager.ActiveDocument.ChangesSaved = false;
             if (BitmapManager.IsOperationTool())
             if (BitmapManager.IsOperationTool())
             {
             {