Browse Source

Added rectangle thickness and fixed moving layer bug

flabbet 5 years ago
parent
commit
c837650cfa

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

@@ -141,6 +141,8 @@ namespace PixiEditor.Models.Controllers
             Layers[ActiveLayerIndex].LayerBitmap.Lock();
             Layers[ActiveLayerIndex].LayerBitmap.Lock();
             for (int i = 0; i < coordinates.Length; i++)
             for (int i = 0; i < coordinates.Length; i++)
             {
             {
+                if (coordinates[i].X < 0 || coordinates[i].X > Layers[0].Width - 1 || coordinates[i].Y < 0 || coordinates[i].Y > Layers[0].Height - 1) 
+                    continue;
                 values.Add(coordinates[i], Layers[ActiveLayerIndex].LayerBitmap.GetPixel(coordinates[i].X, coordinates[i].Y));
                 values.Add(coordinates[i], Layers[ActiveLayerIndex].LayerBitmap.GetPixel(coordinates[i].X, coordinates[i].Y));
             }
             }
             Layers[ActiveLayerIndex].LayerBitmap.Unlock();
             Layers[ActiveLayerIndex].LayerBitmap.Unlock();

+ 22 - 4
PixiEditor/Models/Tools/Tools/RectangleTool.cs

@@ -1,5 +1,6 @@
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
+using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
 using System.Windows.Media;
 using System.Windows.Media;
@@ -21,10 +22,27 @@ namespace PixiEditor.Models.Tools.Tools
             return BitmapPixelChanges.FromSingleColoredArray(CreateRectangle(coordinates, toolSize), color);
             return BitmapPixelChanges.FromSingleColoredArray(CreateRectangle(coordinates, toolSize), color);
         }
         }
 
 
-        public Coordinates[] CreateRectangle(Coordinates[] coordinates, int toolSize)
+        public Coordinates[] CreateRectangle(Coordinates[] coordinates, int thickness)
         {
         {
             DoubleCords fixedCoordinates = CalculateCoordinatesForShapeRotation(coordinates[^1], coordinates[0]);
             DoubleCords fixedCoordinates = CalculateCoordinatesForShapeRotation(coordinates[^1], coordinates[0]);
-            return CalculateRectanglePoints(fixedCoordinates, Filled);
+            List<Coordinates> output = new List<Coordinates>();
+            Coordinates[] rectangle =  CalculateRectanglePoints(fixedCoordinates, Filled);
+            output.AddRange(rectangle);
+
+            for (int i = 1; i < (int)Math.Floor(thickness / 2f) + 1; i++)
+            {
+                output.AddRange(CalculateRectanglePoints(new DoubleCords(
+                    new Coordinates(fixedCoordinates.Coords1.X - i, fixedCoordinates.Coords1.Y - i),
+                    new Coordinates(fixedCoordinates.Coords2.X + i, fixedCoordinates.Coords2.Y + i)), false));
+            }
+            for (int i = 1; i < (int)Math.Ceiling(thickness / 2f); i++)
+            {
+                output.AddRange(CalculateRectanglePoints(new DoubleCords(
+                    new Coordinates(fixedCoordinates.Coords1.X + i, fixedCoordinates.Coords1.Y + i),
+                    new Coordinates(fixedCoordinates.Coords2.X - i, fixedCoordinates.Coords2.Y - i)), false));
+            }
+
+            return output.Distinct().ToArray();
         }
         }
 
 
         private Coordinates[] CalculateRectanglePoints(DoubleCords coordinates, bool filled)
         private Coordinates[] CalculateRectanglePoints(DoubleCords coordinates, bool filled)
@@ -40,13 +58,13 @@ namespace PixiEditor.Models.Tools.Tools
             {
             {
                 finalCoordinates.Add(new Coordinates(coordinates.Coords1.X, i));
                 finalCoordinates.Add(new Coordinates(coordinates.Coords1.X, i));
                 finalCoordinates.Add(new Coordinates(coordinates.Coords2.X, i));
                 finalCoordinates.Add(new Coordinates(coordinates.Coords2.X, i));
-            }
+            }            
 
 
             if (filled)
             if (filled)
             {
             {
                 finalCoordinates.AddRange(CalculatedFillForRectangle(coordinates));
                 finalCoordinates.AddRange(CalculatedFillForRectangle(coordinates));
             }
             }
-            return finalCoordinates.Distinct().ToArray();
+            return finalCoordinates.ToArray();
         }
         }
 
 
         private Coordinates[] CalculatedFillForRectangle(DoubleCords cords)
         private Coordinates[] CalculatedFillForRectangle(DoubleCords cords)

+ 8 - 0
PixiEditor/ViewModels/ViewModelMain.cs

@@ -235,12 +235,20 @@ namespace PixiEditor.ViewModels
         {
         {
             int oldIndex = (int)parameter;
             int oldIndex = (int)parameter;
             BitmapUtility.Layers.Move(oldIndex, oldIndex + 1);
             BitmapUtility.Layers.Move(oldIndex, oldIndex + 1);
+            if(BitmapUtility.ActiveLayerIndex == oldIndex)
+            {
+                BitmapUtility.SetActiveLayer(oldIndex + 1);
+            }
         }
         }
 
 
         public void MoveLayerToBack(object parameter)
         public void MoveLayerToBack(object parameter)
         {
         {
             int oldIndex = (int)parameter;
             int oldIndex = (int)parameter;
             BitmapUtility.Layers.Move(oldIndex, oldIndex - 1);
             BitmapUtility.Layers.Move(oldIndex, oldIndex - 1);
+            if (BitmapUtility.ActiveLayerIndex == oldIndex)
+            {
+                BitmapUtility.SetActiveLayer(oldIndex - 1);
+            }
         }
         }
 
 
         public bool CanMoveToFront(object property)
         public bool CanMoveToFront(object property)