Browse Source

Small progress

Krzysztof Krysiński 4 years ago
parent
commit
1af02f3fa2

+ 10 - 13
PixiEditor/Models/Tools/Tools/BrightnessTool.cs

@@ -1,11 +1,10 @@
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
-using System.Windows.Controls;
 using System.Windows.Input;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media;
+using System.Windows.Media.Imaging;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.Colors;
 using PixiEditor.Models.Colors;
-using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
@@ -26,8 +25,9 @@ namespace PixiEditor.Models.Tools.Tools
             Toolbar = new BrightnessToolToolbar(CorrectionFactor);
             Toolbar = new BrightnessToolToolbar(CorrectionFactor);
         }
         }
 
 
-		public override bool UsesShift => false;
-		public override string Tooltip => "Makes pixel brighter or darker pixel (U). Hold Ctrl to make pixel darker.";
+        public override bool UsesShift => false;
+
+        public override string Tooltip => "Makes pixel brighter or darker pixel (U). Hold Ctrl to make pixel darker.";
 
 
         public BrightnessMode Mode { get; set; } = BrightnessMode.Default;
         public BrightnessMode Mode { get; set; } = BrightnessMode.Default;
 
 
@@ -58,18 +58,17 @@ namespace PixiEditor.Models.Tools.Tools
             float correctionFactor = Toolbar.GetSetting<FloatSetting>("CorrectionFactor").Value;
             float correctionFactor = Toolbar.GetSetting<FloatSetting>("CorrectionFactor").Value;
             Mode = Toolbar.GetEnumSetting<BrightnessMode>("BrightnessMode").Value;
             Mode = Toolbar.GetEnumSetting<BrightnessMode>("BrightnessMode").Value;
 
 
-            LayerChange[] layersChanges = new LayerChange[1];
             if (Keyboard.IsKeyDown(Key.LeftCtrl))
             if (Keyboard.IsKeyDown(Key.LeftCtrl))
             {
             {
-                layersChanges[0] = new LayerChange(ChangeBrightness(layer, coordinates[0], toolSize, -correctionFactor), layer);
+                ChangeBrightness(layer, coordinates[0], toolSize, -correctionFactor);
             }
             }
             else
             else
             {
             {
-                layersChanges[0] = new LayerChange(ChangeBrightness(layer, coordinates[0], toolSize, correctionFactor), layer);
+                ChangeBrightness(layer, coordinates[0], toolSize, correctionFactor);
             }
             }
         }
         }
 
 
-        public BitmapPixelChanges ChangeBrightness(Layer layer, Coordinates coordinates, int toolSize, float correctionFactor)
+        public void ChangeBrightness(Layer layer, Coordinates coordinates, int toolSize, float correctionFactor)
         {
         {
             DoubleCords centeredCoords = CoordinatesCalculator.CalculateThicknessCenter(coordinates, toolSize);
             DoubleCords centeredCoords = CoordinatesCalculator.CalculateThicknessCenter(coordinates, toolSize);
             IEnumerable<Coordinates> rectangleCoordinates = CoordinatesCalculator.RectangleToCoordinates(
             IEnumerable<Coordinates> rectangleCoordinates = CoordinatesCalculator.RectangleToCoordinates(
@@ -77,7 +76,8 @@ namespace PixiEditor.Models.Tools.Tools
                 centeredCoords.Coords1.Y,
                 centeredCoords.Coords1.Y,
                 centeredCoords.Coords2.X,
                 centeredCoords.Coords2.X,
                 centeredCoords.Coords2.Y);
                 centeredCoords.Coords2.Y);
-            BitmapPixelChanges changes = new BitmapPixelChanges(new Dictionary<Coordinates, Color>());
+
+            using var ctx = layer.LayerBitmap.GetBitmapContext();
 
 
             foreach (Coordinates coordinate in rectangleCoordinates)
             foreach (Coordinates coordinate in rectangleCoordinates)
             {
             {
@@ -95,12 +95,9 @@ namespace PixiEditor.Models.Tools.Tools
                 Color newColor = ExColor.ChangeColorBrightness(
                 Color newColor = ExColor.ChangeColorBrightness(
                     Color.FromArgb(pixel.A, pixel.R, pixel.G, pixel.B),
                     Color.FromArgb(pixel.A, pixel.R, pixel.G, pixel.B),
                     correctionFactor);
                     correctionFactor);
-                changes.ChangedPixels.Add(
-                    new Coordinates(coordinate.X, coordinate.Y),
-                    newColor);
+                layer.SetPixel(new Coordinates(coordinate.X, coordinate.Y), newColor);
             }
             }
 
 
-            return changes;
         }
         }
     }
     }
 }
 }

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

@@ -32,7 +32,7 @@ namespace PixiEditor.Models.Tools.Tools
         public void 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);
+            pen.Draw(layer, startingCords, coordinates[0], System.Windows.Media.Colors.Transparent, toolSize);
         }
         }
     }
     }
 }
 }

+ 12 - 9
PixiEditor/Models/Tools/Tools/LineTool.cs

@@ -51,9 +51,9 @@ namespace PixiEditor.Models.Tools.Tools
                 CapType.Square);
                 CapType.Square);
         }
         }
 
 
-        public void CreateLine(Layer layer, Color color, Coordinates start, Coordinates end, int thickness)
+        public List<Coordinates> CreateLine(Layer layer, Color color, Coordinates start, Coordinates end, int thickness)
         {
         {
-            CreateLineFastest(layer, color, start, end, thickness);
+            return CreateLineFastest(layer, color, start, end, thickness);
         }
         }
 
 
         public void CreateLine(Layer layer, Color color, Coordinates start, Coordinates end, int thickness, CapType startCap, CapType endCap)
         public void CreateLine(Layer layer, Color color, Coordinates start, Coordinates end, int thickness, CapType startCap, CapType endCap)
@@ -61,35 +61,36 @@ namespace PixiEditor.Models.Tools.Tools
             CreateLine(layer, color, new List<Coordinates>() { end, start }, thickness, startCap, endCap);
             CreateLine(layer, color, new List<Coordinates>() { end, start }, thickness, startCap, endCap);
         }
         }
 
 
-        private void CreateLine(Layer layer, Color color, IEnumerable<Coordinates> coordinates, int thickness, CapType startCap, CapType endCap)
+        private List<Coordinates> CreateLine(Layer layer, Color color, 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)
             {
             {
-                BresenhamLine(layer, color, startingCoordinates.X, startingCoordinates.Y, latestCoordinates.X, latestCoordinates.Y);
+                return BresenhamLine(layer, color, startingCoordinates.X, startingCoordinates.Y, latestCoordinates.X, latestCoordinates.Y);
             }
             }
 
 
-            GenerateLine(layer, color, startingCoordinates, latestCoordinates, thickness, startCap, endCap);
+            return GenerateLine(layer, color, startingCoordinates, latestCoordinates, thickness, startCap, endCap);
         }
         }
 
 
-        private void CreateLineFastest(Layer layer, Color color, Coordinates start, Coordinates end, int thickness)
+        private List<Coordinates> CreateLineFastest(Layer layer, Color color, Coordinates start, Coordinates end, int thickness)
         {
         {
             var line = BresenhamLine(layer, color, start.X, start.Y, end.X, end.Y);
             var line = BresenhamLine(layer, color, start.X, start.Y, end.X, end.Y);
             if (thickness == 1)
             if (thickness == 1)
             {
             {
-                return;
+                return line;
             }
             }
 
 
             ThickenShape(layer, color, line, thickness);
             ThickenShape(layer, color, line, thickness);
+            return line;
         }
         }
 
 
-        private void GenerateLine(Layer layer, Color color, Coordinates start, Coordinates end, int thickness, CapType startCap, CapType endCap)
+        private List<Coordinates> GenerateLine(Layer layer, Color color, Coordinates start, Coordinates end, int thickness, CapType startCap, CapType endCap)
         {
         {
             ApplyCap(layer, color, startCap, start, thickness);
             ApplyCap(layer, color, startCap, start, thickness);
             if (start == end)
             if (start == end)
             {
             {
-                return;
+                return new List<Coordinates>() { start };
             }
             }
 
 
             var line = BresenhamLine(layer, color, start.X, start.Y, end.X, end.Y);
             var line = BresenhamLine(layer, color, start.X, start.Y, end.X, end.Y);
@@ -99,6 +100,8 @@ namespace PixiEditor.Models.Tools.Tools
             {
             {
                 ThickenShape(layer, color, line.Except(new[] { start, end }), thickness);
                 ThickenShape(layer, color, line.Except(new[] { start, end }), thickness);
             }
             }
+
+            return line;
         }
         }
 
 
         private void ApplyCap(Layer layer, Color color, CapType cap, Coordinates position, int thickness)
         private void ApplyCap(Layer layer, Color color, CapType cap, Coordinates position, int thickness)

+ 13 - 9
PixiEditor/Models/Tools/Tools/PenTool.cs

@@ -39,6 +39,8 @@ namespace PixiEditor.Models.Tools.Tools
         }
         }
 
 
         public override string Tooltip => "Standard brush. (B)";
         public override string Tooltip => "Standard brush. (B)";
+        public override bool UsesShift => false;
+
 
 
         public override void OnRecordingLeftMouseDown(MouseEventArgs e)
         public override void OnRecordingLeftMouseDown(MouseEventArgs e)
         {
         {
@@ -74,12 +76,13 @@ namespace PixiEditor.Models.Tools.Tools
                 confirmedPixels.Add(latestCords);
                 confirmedPixels.Add(latestCords);
             }
             }
 
 
-            var latestPixels = lineTool.CreateLine(startingCoords, latestCords, 1);
+            var latestPixels = lineTool.CreateLine(layer, color, startingCoords, latestCords, 1);
             SetPixelToCheck(latestPixels);
             SetPixelToCheck(latestPixels);
 
 
             if (changedPixelsindex == 2)
             if (changedPixelsindex == 2)
             {
             {
-                ApplyPixelPerfectToPixels(
+                var changes = ApplyPixelPerfectToPixels(
+                    layer,
                     lastChangedPixels[0],
                     lastChangedPixels[0],
                     lastChangedPixels[1],
                     lastChangedPixels[1],
                     lastChangedPixels[2],
                     lastChangedPixels[2],
@@ -88,14 +91,15 @@ namespace PixiEditor.Models.Tools.Tools
 
 
                 MovePixelsToCheck(changes);
                 MovePixelsToCheck(changes);
 
 
-                ThickenShape(latestPixels, toolSize);
+                ThickenShape(layer, color, latestPixels, toolSize);
+                return;
             }
             }
 
 
             changedPixelsindex += changedPixelsindex >= 2 ? (byte)0 : (byte)1;
             changedPixelsindex += changedPixelsindex >= 2 ? (byte)0 : (byte)1;
 
 
-            ThickenShape(latestPixels, toolSize);
+            ThickenShape(layer, color, latestPixels, toolSize);
         }
         }
-        public override bool UsesShift => false;
+
         private void MovePixelsToCheck(BitmapPixelChanges changes)
         private void MovePixelsToCheck(BitmapPixelChanges changes)
         {
         {
             if (changes.ChangedPixels[lastChangedPixels[1]].A != 0)
             if (changes.ChangedPixels[lastChangedPixels[1]].A != 0)
@@ -123,15 +127,15 @@ namespace PixiEditor.Models.Tools.Tools
             }
             }
         }
         }
 
 
-        private void ApplyPixelPerfectToPixels(Coordinates p1, Coordinates p2, Coordinates p3, Color color, int toolSize)
+        private BitmapPixelChanges ApplyPixelPerfectToPixels(Layer layer, Coordinates p1, Coordinates p2, Coordinates p3, Color color, int toolSize)
         {
         {
             if (Math.Abs(p3.X - p1.X) == 1 && Math.Abs(p3.Y - p1.Y) == 1 && !confirmedPixels.Contains(p2))
             if (Math.Abs(p3.X - p1.X) == 1 && Math.Abs(p3.Y - p1.Y) == 1 && !confirmedPixels.Contains(p2))
             {
             {
-                ThickenShape(new Coordinates[] { p1, p3 }, toolSize);
-                ThickenShape(new[] { p2 }, toolSize);
+                ThickenShape(layer, color, new Coordinates[] { p1, p3 }, toolSize);
+                ThickenShape(layer, color, new[] { p2 }, toolSize);
             }
             }
 
 
-            ThickenShape(new Coordinates[] { p2, p3 }.Distinct(), toolSize);
+            ThickenShape(layer, color, new Coordinates[] { p2, p3 }.Distinct(), toolSize);
         }
         }
 
 
         private void PixelPerfectSettingValueChanged(object sender, SettingValueChangedEventArgs<bool> e)
         private void PixelPerfectSettingValueChanged(object sender, SettingValueChangedEventArgs<bool> e)