Browse Source

Performance progress

Krzysztof Krysiński 4 years ago
parent
commit
fae97088ab

+ 31 - 10
PixiEditor/Models/Layers/Layer.cs

@@ -32,6 +32,8 @@ namespace PixiEditor.Models.Layers
 
         private string layerHighlightColor = "#666666";
 
+        private BitmapPixelChanges singleCache = BitmapPixelChanges.Empty;
+
         public Layer(string name)
         {
             Name = name;
@@ -287,6 +289,16 @@ namespace PixiEditor.Models.Layers
             return LayerBitmap.GetPixel(x, y);
         }
 
+        public void SetPixelWithOffset(Coordinates coordinates, Color color)
+        {
+            LayerBitmap.SetPixel(coordinates.X - OffsetX, coordinates.Y - OffsetY, color);
+        }
+
+        public void SetPixelWithOffset(int x, int y, Color color)
+        {
+            LayerBitmap.SetPixel(x - OffsetX, y - OffsetY, color);
+        }
+
         /// <summary>
         ///     Applies pixel to layer.
         /// </summary>
@@ -296,7 +308,8 @@ namespace PixiEditor.Models.Layers
         /// <param name="applyOffset">Converts pixels coordinates to relative to bitmap.</param>
         public void SetPixel(Coordinates coordinates, Color color, bool dynamicResize = true, bool applyOffset = true)
         {
-            SetPixels(BitmapPixelChanges.FromSingleColoredArray(new[] { coordinates }, color), dynamicResize, applyOffset);
+            singleCache.ChangedPixels[coordinates] = color;
+            SetPixels(singleCache, dynamicResize, applyOffset);
         }
 
         /// <summary>
@@ -375,15 +388,7 @@ namespace PixiEditor.Models.Layers
 
             if (!(pixels.WasBuiltAsSingleColored && pixels.ChangedPixels.First().Value.A == 0))
             {
-                if ((newMaxX + 1 > Width && Width < MaxWidth) || (newMaxY + 1 > Height && Height < MaxHeight))
-                {
-                    IncreaseSizeToBottomAndRight(newMaxX, newMaxY);
-                }
-
-                if ((newMinX < 0 && Width < MaxWidth) || (newMinY < 0 && Height < MaxHeight))
-                {
-                    IncreaseSizeToTopAndLeft(newMinX, newMinY);
-                }
+                DynamicResize(newMaxX, newMaxY, newMinX, newMinY);
             }
 
             // if clip is requested
@@ -393,6 +398,22 @@ namespace PixiEditor.Models.Layers
             }
         }
 
+        /// <summary>
+        ///     Resizes canvas to fit pixels outside current bounds. Clamped to MaxHeight and MaxWidth.
+        /// </summary>
+        public void DynamicResize(int newMaxX, int newMaxY, int newMinX, int newMinY)
+        {
+            if ((newMaxX + 1 > Width && Width < MaxWidth) || (newMaxY + 1 > Height && Height < MaxHeight))
+            {
+                IncreaseSizeToBottomAndRight(newMaxX, newMaxY);
+            }
+
+            if ((newMinX < 0 && Width < MaxWidth) || (newMinY < 0 && Height < MaxHeight))
+            {
+                IncreaseSizeToTopAndLeft(newMinX, newMinY);
+            }
+        }
+
         /// <summary>
         ///     Changes size of bitmap to fit content.
         /// </summary>

+ 16 - 1
PixiEditor/Models/Position/CoordinatesCalculator.cs

@@ -1,4 +1,5 @@
-using System;
+using PixiEditor.Models.Layers;
+using System;
 using System.Collections.Generic;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
@@ -63,6 +64,20 @@ namespace PixiEditor.Models.Position
             return coordinates;
         }
 
+        public static void DrawRectangle(Layer layer, Color color, int x1, int y1, int x2, int y2)
+        {
+            using var ctx = layer.LayerBitmap.GetBitmapContext();
+            x2++;
+            y2++;
+            for (int y = y1; y < y1 + (y2 - y1); y++)
+            {
+                for (int x = x1; x < x1 + (x2 - x1); x++)
+                {
+                    layer.SetPixelWithOffset(x, y, color);
+                }
+            }
+        }
+
         public static IEnumerable<Coordinates> RectangleToCoordinates(DoubleCords coordinates)
         {
             return RectangleToCoordinates(coordinates.Coords1.X, coordinates.Coords1.Y, coordinates.Coords2.X, coordinates.Coords2.Y);

+ 7 - 4
PixiEditor/Models/Tools/ShapeTool.cs

@@ -62,11 +62,14 @@ namespace PixiEditor.Models.Tools
         {
             foreach (Coordinates item in shape)
             {
-                var changes = BitmapPixelChanges.FromSingleColoredArray(
-                    CoordinatesCalculator.RectangleToCoordinates(
-                   CoordinatesCalculator.CalculateThicknessCenter(item, thickness)), color);
-                layer.SetPixels(changes);
+                ThickenShape(layer, color, item, thickness);
             }
         }
+
+        protected static void ThickenShape(Layer layer, Color color, Coordinates coords, int thickness)
+        {
+            var dcords = CoordinatesCalculator.CalculateThicknessCenter(coords, thickness);
+            CoordinatesCalculator.DrawRectangle(layer, color, dcords.Coords1.X, dcords.Coords1.Y, dcords.Coords2.X, dcords.Coords2.Y);
+        }
     }
 }

+ 7 - 5
PixiEditor/Models/Tools/Tools/LineTool.cs

@@ -44,6 +44,8 @@ namespace PixiEditor.Models.Tools.Tools
 
         public override void Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
+            layer.DynamicResize(coordinates.Max(x => x.X), coordinates.Max(x => x.Y), coordinates.Min(x => x.X), coordinates.Min(x => x.Y));
+
             CreateLine(
                 layer, color,
                 coordinates,
@@ -115,7 +117,7 @@ namespace PixiEditor.Models.Tools.Tools
                     break;
 
                 default:
-                    ThickenShape(layer, color, new[] { position }, thickness);
+                    ThickenShape(layer, color, position, thickness);
                     break;
             }
         }
@@ -140,7 +142,7 @@ namespace PixiEditor.Models.Tools.Tools
             if (x1 == x2 && y1 == y2)
             {
                 cords = new Coordinates(x1, y1);
-                layer.SetPixel(cords, color);
+                layer.SetPixelWithOffset(cords, color);
                 linePoints.Add(cords);
             }
 
@@ -170,7 +172,7 @@ namespace PixiEditor.Models.Tools.Tools
             }
 
             cords = new Coordinates(x, y);
-            layer.SetPixel(cords, color);
+            layer.SetPixelWithOffset(cords, color);
             linePoints.Add(cords);
 
             if (dx > dy)
@@ -194,7 +196,7 @@ namespace PixiEditor.Models.Tools.Tools
                     }
 
                     cords = new Coordinates(x, y);
-                    layer.SetPixel(cords, color);
+                    layer.SetPixelWithOffset(cords, color);
                     linePoints.Add(cords);
                 }
             }
@@ -219,7 +221,7 @@ namespace PixiEditor.Models.Tools.Tools
                     }
 
                     cords = new Coordinates(x, y);
-                    layer.SetPixel(cords, color);
+                    layer.SetPixelWithOffset(cords, color);
                     linePoints.Add(cords);
                 }
             }

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

@@ -53,6 +53,7 @@ namespace PixiEditor.Models.Tools.Tools
         public override void Use(Layer layer, List<Coordinates> coordinates, Color color)
         {
             Coordinates startingCords = coordinates.Count > 1 ? coordinates[1] : coordinates[0];
+            layer.DynamicResize(coordinates.Max(x => x.X), coordinates.Max(x => x.Y), coordinates.Min(x => x.X), coordinates.Min(x => x.Y));
             Draw(
                 layer,
                 startingCords,