Browse Source

New preview layer and highlight wip

Krzysztof Krysiński 3 years ago
parent
commit
d7acc86b13

+ 30 - 32
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -20,9 +20,13 @@ namespace PixiEditor.Models.Controllers
     [DebuggerDisplay("{Documents.Count} Document(s)")]
     [DebuggerDisplay("{Documents.Count} Document(s)")]
     public class BitmapManager : NotifyableObject
     public class BitmapManager : NotifyableObject
     {
     {
+        private int previewLayerSize;
         private Document activeDocument;
         private Document activeDocument;
         private Tool selectedTool;
         private Tool selectedTool;
         private Coordinates? startPosition = null;
         private Coordinates? startPosition = null;
+        private IEnumerable<Coordinates> cachedHighlight;
+        private int halfSize;
+        private BitmapPixelChanges cachedPixels;
 
 
         public BitmapManager()
         public BitmapManager()
         {
         {
@@ -35,6 +39,7 @@ namespace PixiEditor.Models.Controllers
             MouseController.OnMouseDownCoordinates += MouseController_OnMouseDownCoordinates;
             MouseController.OnMouseDownCoordinates += MouseController_OnMouseDownCoordinates;
             BitmapOperations = new BitmapOperationsUtility(this);
             BitmapOperations = new BitmapOperationsUtility(this);
             ReadonlyToolUtility = new ReadonlyToolUtility();
             ReadonlyToolUtility = new ReadonlyToolUtility();
+            DocumentChanged += BitmapManager_DocumentChanged;
         }
         }
 
 
         public event EventHandler<DocumentChangedEventArgs> DocumentChanged;
         public event EventHandler<DocumentChangedEventArgs> DocumentChanged;
@@ -154,14 +159,15 @@ namespace PixiEditor.Models.Controllers
 
 
         public void SetActiveTool(Tool tool)
         public void SetActiveTool(Tool tool)
         {
         {
-            if (ActiveDocument != null)
-            {
-                ActiveDocument.PreviewLayer = null;
-            }
-
+            ActiveDocument?.PreviewLayer?.Clear();
             SelectedTool?.Toolbar.SaveToolbarSettings();
             SelectedTool?.Toolbar.SaveToolbarSettings();
             SelectedTool = tool;
             SelectedTool = tool;
             SelectedTool.Toolbar.LoadSharedSettings();
             SelectedTool.Toolbar.LoadSharedSettings();
+        }
+
+        private void BitmapManager_DocumentChanged(object sender, DocumentChangedEventArgs e)
+        {
+            e.NewDocument.GeneratePreviewLayer();
         }
         }
 
 
         private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
         private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
@@ -207,7 +213,7 @@ namespace PixiEditor.Models.Controllers
             SelectedTool.OnRecordingLeftMouseDown(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
             SelectedTool.OnRecordingLeftMouseDown(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
             if (ActiveDocument != null)
             if (ActiveDocument != null)
             {
             {
-                ActiveDocument.PreviewLayer = null;
+                ActiveDocument.PreviewLayer.Clear();
             }
             }
         }
         }
 
 
@@ -229,38 +235,30 @@ namespace PixiEditor.Models.Controllers
                 return;
                 return;
             }
             }
 
 
-            IEnumerable<Coordinates> highlightArea = CoordinatesCalculator.RectangleToCoordinates(
-                CoordinatesCalculator.CalculateThicknessCenter(newPosition, ToolSize));
-            if (CanChangeHighlightOffset(highlightArea))
-            {
-                Coordinates start = highlightArea.First();
-                ActiveDocument.PreviewLayer.Offset = new Thickness(start.X, start.Y, 0, 0);
-            }
-            else if (!IsInsideBounds(highlightArea))
-            {
-                ActiveDocument.PreviewLayer = null;
+            if (ToolSize != previewLayerSize)
+            {
+                cachedHighlight = CoordinatesCalculator.RectangleToCoordinates(
+                    CoordinatesCalculator.CalculateThicknessCenter(newPosition, ToolSize));
+
+                previewLayerSize = ToolSize;
+                halfSize = (int)Math.Floor(ToolSize / 2f);
+
+                cachedPixels = BitmapPixelChanges.FromSingleColoredArray(cachedHighlight, new SKColor(0, 0, 0, 77));
+
+                ActiveDocument.PreviewLayer.SetPixels(cachedPixels);
+                ActiveDocument.PreviewLayer.ClipCanvas();
             }
             }
-            else if (ActiveDocument.PreviewLayer == null)
+
+            Coordinates start = newPosition - halfSize;
+            ActiveDocument.PreviewLayer.Offset = new Thickness(start.X, start.Y, 0, 0);
+
+            if (!IsInsideBounds(cachedHighlight))
             {
             {
-                ActiveDocument.GeneratePreviewLayer();
-                ActiveDocument.PreviewLayer.SetPixels(
-                    BitmapPixelChanges.FromSingleColoredArray(highlightArea, new SKColor(0, 0, 0, 77)));
-            }
-            else
-            {
                 ActiveDocument.PreviewLayer.Clear();
                 ActiveDocument.PreviewLayer.Clear();
-                ActiveDocument.PreviewLayer.SetPixels(
-                    BitmapPixelChanges.FromSingleColoredArray(highlightArea, new SKColor(0, 0, 0, 77)));
+                previewLayerSize = -1;
             }
             }
         }
         }
 
 
-        private bool CanChangeHighlightOffset(IEnumerable<Coordinates> highlightArea)
-        {
-            int count = highlightArea.Count();
-            return count > 0 && ActiveDocument.PreviewLayer != null &&
-                   IsInsideBounds(highlightArea) && count == ActiveDocument.PreviewLayer.Width * ActiveDocument.PreviewLayer.Height;
-        }
-
         private bool IsInsideBounds(IEnumerable<Coordinates> highlightArea)
         private bool IsInsideBounds(IEnumerable<Coordinates> highlightArea)
         {
         {
             Coordinates start = highlightArea.First();
             Coordinates start = highlightArea.First();

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

@@ -181,9 +181,9 @@ namespace PixiEditor.Models.Controllers
         {
         {
             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.IsCleared)
                 {
                 {
-                    Manager.ActiveDocument.GeneratePreviewLayer();
+                    Manager.ActiveDocument.PreviewLayer.Clear();
                 }
                 }
 
 
                 // TODO: Use on preview layer
                 // TODO: Use on preview layer

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

@@ -199,15 +199,19 @@ namespace PixiEditor.Models.Layers
 
 
         public int MaxHeight { get; set; } = int.MaxValue;
         public int MaxHeight { get; set; } = int.MaxValue;
 
 
+        public bool IsCleared { get; private set; }
+
         public event EventHandler<Int32Rect> LayerBitmapChanged;
         public event EventHandler<Int32Rect> LayerBitmapChanged;
 
 
         public void InvokeLayerBitmapChange()
         public void InvokeLayerBitmapChange()
         {
         {
+            IsCleared = false;
             LayerBitmapChanged?.Invoke(this, new Int32Rect(OffsetX, OffsetY, Width, Height));
             LayerBitmapChanged?.Invoke(this, new Int32Rect(OffsetX, OffsetY, Width, Height));
         }
         }
 
 
         public void InvokeLayerBitmapChange(Int32Rect dirtyArea)
         public void InvokeLayerBitmapChange(Int32Rect dirtyArea)
         {
         {
+            IsCleared = false;
             LayerBitmapChanged?.Invoke(this, dirtyArea);
             LayerBitmapChanged?.Invoke(this, dirtyArea);
         }
         }
 
 
@@ -462,6 +466,7 @@ namespace PixiEditor.Models.Layers
         /// </summary>
         /// </summary>
         public void Clear()
         public void Clear()
         {
         {
+            IsCleared = true;
             LayerBitmap.SkiaSurface.Canvas.Clear();
             LayerBitmap.SkiaSurface.Canvas.Clear();
             ClipCanvas();
             ClipCanvas();
             InvokeLayerBitmapChange();
             InvokeLayerBitmapChange();

+ 5 - 0
PixiEditor/Models/Position/Coordinates.cs

@@ -19,6 +19,11 @@ namespace PixiEditor.Models.Position
             return new Coordinates(tuple.width, tuple.height);
             return new Coordinates(tuple.width, tuple.height);
         }
         }
 
 
+        public static Coordinates operator -(Coordinates coordiantes, int size)
+        {
+            return new Coordinates(coordiantes.X - size, coordiantes.Y - size);
+        }
+
         public static bool operator ==(Coordinates c1, Coordinates c2)
         public static bool operator ==(Coordinates c1, Coordinates c2)
         {
         {
             return c2.X == c1.X && c2.Y == c1.Y;
             return c2.X == c1.X && c2.Y == c1.Y;

+ 1 - 1
PixiEditor/ViewModels/ViewModelMain.cs

@@ -238,7 +238,7 @@ namespace PixiEditor.ViewModels
         {
         {
             foreach (var document in BitmapManager.Documents)
             foreach (var document in BitmapManager.Documents)
             {
             {
-                document.PreviewLayer = null;
+                document.PreviewLayer.Clear();
             }
             }
         }
         }