Browse Source

Optimize dynamic resize

Equbuxu 3 years ago
parent
commit
c1c3031280

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

@@ -166,7 +166,7 @@ namespace PixiEditor.Models.Controllers
 
 
         private void BitmapManager_DocumentChanged(object sender, DocumentChangedEventArgs e)
         private void BitmapManager_DocumentChanged(object sender, DocumentChangedEventArgs e)
         {
         {
-            e.NewDocument.GeneratePreviewLayer();
+            e.NewDocument?.GeneratePreviewLayer();
         }
         }
 
 
         private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
         private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)

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

@@ -181,7 +181,7 @@ namespace PixiEditor.Models.Controllers
         {
         {
             if (mouseMove.Count > 0 && mouseMove[0] != lastMousePos)
             if (mouseMove.Count > 0 && mouseMove[0] != lastMousePos)
             {
             {
-                if (clearPreviewLayer || !Manager.ActiveDocument.PreviewLayer.IsCleared)
+                if (clearPreviewLayer)
                 {
                 {
                     Manager.ActiveDocument.PreviewLayer.Clear();
                     Manager.ActiveDocument.PreviewLayer.Clear();
                 }
                 }

+ 25 - 8
PixiEditor/Models/Layers/Layer.cs

@@ -36,6 +36,7 @@ namespace PixiEditor.Models.Layers
         {
         {
             Name = name;
             Name = name;
             LayerBitmap = new Surface(1, 1);
             LayerBitmap = new Surface(1, 1);
+            IsCleared = true;
             Width = 1;
             Width = 1;
             Height = 1;
             Height = 1;
             LayerGuid = Guid.NewGuid();
             LayerGuid = Guid.NewGuid();
@@ -45,6 +46,7 @@ namespace PixiEditor.Models.Layers
         {
         {
             Name = name;
             Name = name;
             LayerBitmap = new Surface(width, height);
             LayerBitmap = new Surface(width, height);
+            IsCleared = true;
             Width = width;
             Width = width;
             Height = height;
             Height = height;
             LayerGuid = Guid.NewGuid();
             LayerGuid = Guid.NewGuid();
@@ -413,7 +415,7 @@ namespace PixiEditor.Models.Layers
 
 
             if (!(pixels.WasBuiltAsSingleColored && pixels.ChangedPixels.First().Value.Alpha == 0))
             if (!(pixels.WasBuiltAsSingleColored && pixels.ChangedPixels.First().Value.Alpha == 0))
             {
             {
-                DynamicResize(newMaxX, newMaxY, newMinX, newMinY);
+                DynamicResizeRelative(newMaxX, newMaxY, newMinX, newMinY);
             }
             }
 
 
             // if clip is requested
             // if clip is requested
@@ -423,23 +425,37 @@ 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)
+        public void DynamicResizeAbsolute(int newMaxX, int newMaxY, int newMinX, int newMinY)
         {
         {
             if (newMinX < 0) newMinX = 0;
             if (newMinX < 0) newMinX = 0;
             if (newMinY < 0) newMinY = 0;
             if (newMinY < 0) newMinY = 0;
-            if (newMaxX > MaxWidth) newMaxX = MaxWidth;
-            if (newMaxY > MaxHeight) newMaxY = MaxHeight;
+            if (newMaxX >= MaxWidth) newMaxX = MaxWidth - 1;
+            if (newMaxY >= MaxHeight) newMaxY = MaxHeight - 1;
+
+            if (IsCleared)
+            {
+                Offset = new Thickness(newMinX, newMinY, 0, 0);
+            }
 
 
+            DynamicResizeRelative(newMaxX - OffsetX, newMaxY - OffsetY, newMinX - OffsetX, newMinY - OffsetY);
+        }
+
+        /// <summary>
+        ///     Resizes canvas to fit pixels outside current bounds. Clamped to MaxHeight and MaxWidth.
+        /// </summary>
+        public void DynamicResizeRelative(int newMaxX, int newMaxY, int newMinX, int newMinY)
+        {
             if ((newMaxX + 1 > Width && Width < MaxWidth) || (newMaxY + 1 > Height && Height < MaxHeight))
             if ((newMaxX + 1 > Width && Width < MaxWidth) || (newMaxY + 1 > Height && Height < MaxHeight))
             {
             {
+                newMaxX = Math.Max(newMaxX, (int)(Width * 1.5f));
+                newMaxY = Math.Max(newMaxY, (int)(Height * 1.5f));
                 IncreaseSizeToBottomAndRight(newMaxX, newMaxY);
                 IncreaseSizeToBottomAndRight(newMaxX, newMaxY);
             }
             }
 
 
             if ((newMinX < 0 && Width < MaxWidth) || (newMinY < 0 && Height < MaxHeight))
             if ((newMinX < 0 && Width < MaxWidth) || (newMinY < 0 && Height < MaxHeight))
             {
             {
+                newMinX = Math.Min(newMinX, Width - (int)(Width * 1.5f));
+                newMinY = Math.Min(newMinY, Height - (int)(Height * 1.5f));
                 IncreaseSizeToTopAndLeft(newMinX, newMinY);
                 IncreaseSizeToTopAndLeft(newMinX, newMinY);
             }
             }
         }
         }
@@ -471,6 +487,8 @@ namespace PixiEditor.Models.Layers
         /// </summary>
         /// </summary>
         public void Clear()
         public void Clear()
         {
         {
+            if (IsCleared)
+                return;
             var dirtyRect = new Int32Rect(OffsetX, OffsetY, Width, Height);
             var dirtyRect = new Int32Rect(OffsetX, OffsetY, Width, Height);
             LayerBitmap?.Dispose();
             LayerBitmap?.Dispose();
             LayerBitmap = new Surface(1, 1);
             LayerBitmap = new Surface(1, 1);
@@ -612,7 +630,6 @@ namespace PixiEditor.Models.Layers
         private void ResizeCanvas(int offsetX, int offsetY, int offsetXSrc, int offsetYSrc, int newWidth, int newHeight)
         private void ResizeCanvas(int offsetX, int offsetY, int offsetXSrc, int offsetYSrc, int newWidth, int newHeight)
         {
         {
             Surface result = new Surface(newWidth, newHeight);
             Surface result = new Surface(newWidth, newHeight);
-
             LayerBitmap.SkiaSurface.Draw(result.SkiaSurface.Canvas, offsetX - offsetXSrc, offsetY - offsetYSrc, Surface.ReplacingPaint);
             LayerBitmap.SkiaSurface.Draw(result.SkiaSurface.Canvas, offsetX - offsetXSrc, offsetY - offsetYSrc, Surface.ReplacingPaint);
             LayerBitmap?.Dispose();
             LayerBitmap?.Dispose();
             LayerBitmap = result;
             LayerBitmap = result;

+ 4 - 8
PixiEditor/Models/Tools/Tools/CircleTool.cs

@@ -1,11 +1,9 @@
-using PixiEditor.Helpers.Extensions;
-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 SkiaSharp;
 using SkiaSharp;
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
-using System.Linq;
 using System.Windows;
 using System.Windows;
 using System.Windows.Input;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media;
@@ -49,16 +47,14 @@ namespace PixiEditor.Models.Tools.Tools
                 fixedCoordinates.Coords1.Y - halfThickness,
                 fixedCoordinates.Coords1.Y - halfThickness,
                 fixedCoordinates.Coords2.X + halfThickness * 2 - fixedCoordinates.Coords1.X,
                 fixedCoordinates.Coords2.X + halfThickness * 2 - fixedCoordinates.Coords1.X,
                 fixedCoordinates.Coords2.Y + halfThickness * 2 - fixedCoordinates.Coords1.Y);
                 fixedCoordinates.Coords2.Y + halfThickness * 2 - fixedCoordinates.Coords1.Y);
-            Int32Rect curLayerRect = new(layer.OffsetX, layer.OffsetY, layer.Width, layer.Height);
-            Int32Rect expanded = dirtyRect.Expand(curLayerRect);
-            layer.DynamicResize(expanded.X + expanded.Width - 1, expanded.Y + expanded.Height - 1, expanded.X, expanded.Y);
+            layer.DynamicResizeAbsolute(dirtyRect.X + dirtyRect.Width - 1, dirtyRect.Y + dirtyRect.Height - 1, dirtyRect.X, dirtyRect.Y);
 
 
             using (SKPaint paint = new SKPaint())
             using (SKPaint paint = new SKPaint())
             {
             {
                 float radiusX = (fixedCoordinates.Coords2.X - fixedCoordinates.Coords1.X) / 2.0f;
                 float radiusX = (fixedCoordinates.Coords2.X - fixedCoordinates.Coords1.X) / 2.0f;
                 float radiusY = (fixedCoordinates.Coords2.Y - fixedCoordinates.Coords1.Y) / 2.0f;
                 float radiusY = (fixedCoordinates.Coords2.Y - fixedCoordinates.Coords1.Y) / 2.0f;
-                float centerX = (fixedCoordinates.Coords1.X + fixedCoordinates.Coords2.X + 1) / 2.0f;
-                float centerY = (fixedCoordinates.Coords1.Y + fixedCoordinates.Coords2.Y + 1) / 2.0f;
+                float centerX = (fixedCoordinates.Coords1.X + fixedCoordinates.Coords2.X + 1) / 2.0f - layer.OffsetX;
+                float centerY = (fixedCoordinates.Coords1.Y + fixedCoordinates.Coords2.Y + 1) / 2.0f - layer.OffsetY;
 
 
                 if (hasFillColor)
                 if (hasFillColor)
                 {
                 {

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

@@ -1,5 +1,4 @@
-using PixiEditor.Helpers.Extensions;
-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;
@@ -69,10 +68,12 @@ namespace PixiEditor.Models.Tools.Tools
                 dirtyY,
                 dirtyY,
                 Math.Max(x1, x) + thickness - dirtyX,
                 Math.Max(x1, x) + thickness - dirtyX,
                 Math.Max(y1, y) + thickness - dirtyY);
                 Math.Max(y1, y) + thickness - dirtyY);
-            Int32Rect curLayerRect = new(layer.OffsetX, layer.OffsetY, layer.Width, layer.Height);
-            Int32Rect expanded = dirtyRect.Expand(curLayerRect);
+            layer.DynamicResizeAbsolute(dirtyRect.X + dirtyRect.Width - 1, dirtyRect.Y + dirtyRect.Height - 1, dirtyRect.X, dirtyRect.Y);
 
 
-            layer.DynamicResize(expanded.X + expanded.Width - 1, expanded.Y + expanded.Height - 1, expanded.X, expanded.Y);
+            x -= layer.OffsetX;
+            y -= layer.OffsetY;
+            x1 -= layer.OffsetX;
+            y1 -= layer.OffsetY;
 
 
             paint.StrokeWidth = thickness;
             paint.StrokeWidth = thickness;
             paint.Color = color;
             paint.Color = color;

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

@@ -52,7 +52,7 @@ namespace PixiEditor.Models.Tools.Tools
         public override void Use(Layer layer, List<Coordinates> coordinates, SKColor color)
         public override void Use(Layer layer, List<Coordinates> coordinates, SKColor color)
         {
         {
             Coordinates startingCords = coordinates.Count > 1 ? coordinates[1] : coordinates[0];
             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));
+            layer.DynamicResizeAbsolute(coordinates.Max(x => x.X), coordinates.Max(x => x.Y), coordinates.Min(x => x.X), coordinates.Min(x => x.Y));
             Draw(
             Draw(
                 layer,
                 layer,
                 startingCords,
                 startingCords,

+ 2 - 5
PixiEditor/Models/Tools/Tools/RectangleTool.cs

@@ -1,5 +1,4 @@
-using PixiEditor.Helpers.Extensions;
-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 SkiaSharp;
 using SkiaSharp;
@@ -59,9 +58,7 @@ namespace PixiEditor.Models.Tools.Tools
                 fixedCoordinates.Coords1.Y - halfThickness,
                 fixedCoordinates.Coords1.Y - halfThickness,
                 fixedCoordinates.Coords2.X + halfThickness * 2 - fixedCoordinates.Coords1.X,
                 fixedCoordinates.Coords2.X + halfThickness * 2 - fixedCoordinates.Coords1.X,
                 fixedCoordinates.Coords2.Y + halfThickness * 2 - fixedCoordinates.Coords1.Y);
                 fixedCoordinates.Coords2.Y + halfThickness * 2 - fixedCoordinates.Coords1.Y);
-            Int32Rect curLayerRect = new(layer.OffsetX, layer.OffsetY, layer.Width, layer.Height);
-            Int32Rect expanded = dirtyRect.Expand(curLayerRect);
-            layer.DynamicResize(expanded.X + expanded.Width - 1, expanded.Y + expanded.Height - 1, expanded.X, expanded.Y);
+            layer.DynamicResizeAbsolute(dirtyRect.X + dirtyRect.Width - 1, dirtyRect.Y + dirtyRect.Height - 1, dirtyRect.X, dirtyRect.Y);
 
 
             using (SKPaint paint = new SKPaint())
             using (SKPaint paint = new SKPaint())
             {
             {