Browse Source

Changed bitmap operations utility structure

flabbet 5 years ago
parent
commit
aa264486f9

+ 198 - 0
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -0,0 +1,198 @@
+using PixiEditor.Helpers;
+using PixiEditor.Models.Enums;
+using PixiEditor.Models.Layers;
+using PixiEditor.Models.Position;
+using PixiEditor.Models.Tools;
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Linq;
+using System.Text;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+
+namespace PixiEditor.Models.Controllers
+{
+    public class BitmapManager : NotifyableObject
+    {
+        public MouseMovementController MouseController { get; set; }
+        private Tool _selectedTool;
+        public Tool SelectedTool
+        {
+            get => _selectedTool;
+            private set
+            {
+                _selectedTool = value;
+                RaisePropertyChanged("SelectedTool");
+            }
+        }
+
+        private ObservableCollection<Layer> _layers = new ObservableCollection<Layer>();
+
+        public ObservableCollection<Layer> Layers
+        {
+            get => _layers;
+            set { if (_layers != value) { _layers = value; } }
+        }
+        private int _activeLayerIndex;
+        public int ActiveLayerIndex
+        {
+            get => _activeLayerIndex;
+            set
+            {
+                _activeLayerIndex = value;
+                RaisePropertyChanged("ActiveLayerIndex");
+                RaisePropertyChanged("ActiveLayer");
+            }
+        }
+
+        private Layer _previewLayer;
+
+        public Layer PreviewLayer
+        {
+            get { return _previewLayer; }
+            set
+            {
+                _previewLayer = value;
+                RaisePropertyChanged("PreviewLayer");
+            }
+        }
+
+
+        public Layer ActiveLayer => Layers.Count > 0 ? Layers[ActiveLayerIndex] : null;
+
+        public Color PrimaryColor { get; set; }
+
+        public int ToolSize => SelectedTool.Toolbar.GetSetting("ToolSize") != null ? (int)SelectedTool.Toolbar.GetSetting("ToolSize").Value : 1;
+
+        public event EventHandler<LayersChangedEventArgs> LayersChanged;
+
+        public BitmapOperationsUtility BitmapOperations { get; set; }
+
+        public void SetActiveTool(Tool tool)
+        {
+            if (PreviewLayer != null)
+            {
+                PreviewLayer.Clear();
+            }
+            if (SelectedTool != null)
+            {
+                SelectedTool.Toolbar.SaveToolbarSettings();
+            }
+            SelectedTool = tool;
+            SelectedTool.Toolbar.LoadSharedSettings();
+        }
+
+        public BitmapManager()
+        {
+            MouseController = new MouseMovementController();
+            MouseController.StartedRecordingChanges += MouseController_StartedRecordingChanges;
+            MouseController.MousePositionChanged += Controller_MousePositionChanged;
+            MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
+            BitmapOperations = new BitmapOperationsUtility(this);
+        }
+
+        public void SetActiveLayer(int index)
+        {
+            if (ActiveLayerIndex <= Layers.Count - 1)
+            {
+                ActiveLayer.IsActive = false;
+            }
+            ActiveLayerIndex = index;
+            ActiveLayer.IsActive = true;
+            LayersChanged?.Invoke(this, new LayersChangedEventArgs(index, LayerAction.SetActive));
+        }
+
+        public void AddNewLayer(string name, int width, int height, bool setAsActive = true)
+        {
+            Layers.Add(new Layer(name, width, height));
+            if (setAsActive)
+            {
+                SetActiveLayer(Layers.Count - 1);
+            }
+            LayersChanged?.Invoke(this, new LayersChangedEventArgs(0, LayerAction.Add));
+        }
+
+        public void RemoveLayer(int layerIndex)
+        {
+            if (Layers.Count <= 1) return;
+
+            bool wasActive = Layers[layerIndex].IsActive;
+            Layers.RemoveAt(layerIndex);
+            if (wasActive)
+            {
+                SetActiveLayer(0);
+            }
+            else if (ActiveLayerIndex > Layers.Count - 1)
+            {
+                SetActiveLayer(Layers.Count - 1);
+            }
+        }
+
+        private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
+        {
+            if (IsOperationTool(SelectedTool))
+            {
+                BitmapOperations.TriggerAction(e.NewPosition, 
+                    MouseController.LastMouseMoveCoordinates.ToList(), (BitmapOperationTool)SelectedTool);
+            }
+        }
+
+        private void MouseController_StartedRecordingChanges(object sender, EventArgs e)
+        {
+            if (PreviewLayer != null)
+            {
+                PreviewLayer.Clear();
+            }
+        }
+
+        private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
+        {
+            if (IsOperationTool(SelectedTool) && (SelectedTool as BitmapOperationTool).RequiresPreviewLayer)
+            {
+                BitmapOperations.StopAction();
+            }
+        }
+
+        public WriteableBitmap GetCombinedLayersBitmap()
+        {
+            WriteableBitmap finalBitmap = Layers[0].LayerBitmap.Clone();
+            finalBitmap.Lock();
+            for (int i = 1; i < Layers.Count; i++)
+            {
+                for (int y = 0; y < finalBitmap.Height; y++)
+                {
+                    for (int x = 0; x < finalBitmap.Width; x++)
+                    {
+                        Color color = Layers[i].LayerBitmap.GetPixel(x, y);
+                        if (color.A != 0 || color.R != 0 || color.B != 0 || color.G != 0)
+                        {
+                            finalBitmap.SetPixel(x, y, color);
+                        }
+                    }
+                }
+            }
+            finalBitmap.Unlock();
+            return finalBitmap;
+        }
+
+
+        private bool IsOperationTool(Tool tool)
+        {
+            return typeof(BitmapOperationTool).IsAssignableFrom(tool.GetType());
+        }
+
+    }
+
+    public class LayersChangedEventArgs : EventArgs
+    {
+        public int LayerAffected { get; set; }
+        public LayerAction LayerChangeType { get; set; }
+
+        public LayersChangedEventArgs(int layerAffected, LayerAction layerChangeType)
+        {
+            LayerAffected = layerAffected;
+            LayerChangeType = layerChangeType;
+        }
+    }
+}

+ 48 - 195
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -13,149 +13,73 @@ using System.Windows.Media.Imaging;
 
 
 namespace PixiEditor.Models.Controllers
 namespace PixiEditor.Models.Controllers
 {
 {
-    public class BitmapOperationsUtility : NotifyableObject
+    public class BitmapOperationsUtility
     {
     {
-        public MouseMovementController MouseController { get; set; }
-        private Tool _selectedTool;
-        public Tool SelectedTool
-        {
-            get => _selectedTool;
-            private set
-            {                
-                _selectedTool = value;
-                RaisePropertyChanged("SelectedTool");
-            }
-        }
-
-        private ObservableCollection<Layer> _layers = new ObservableCollection<Layer>();
-
-        public ObservableCollection<Layer> Layers
-        {
-            get => _layers;
-            set { if (_layers != value) { _layers = value; } }
-        }
-        private int _activeLayerIndex;
-        public int ActiveLayerIndex
-        {
-            get => _activeLayerIndex;
-            set
-            {
-                _activeLayerIndex = value;
-                RaisePropertyChanged("ActiveLayerIndex");
-                RaisePropertyChanged("ActiveLayer");
-            }
-        }
-
-        private Layer _previewLayer;
-
-        public Layer PreviewLayer
-        {
-            get { return _previewLayer; }
-            set 
-            {
-                _previewLayer = value;
-                RaisePropertyChanged("PreviewLayer");
-            }
-        }
-
-
-        public Layer ActiveLayer => Layers.Count > 0 ? Layers[ActiveLayerIndex] : null;
-
-        public Color PrimaryColor { get; set; }
 
 
-        public int ToolSize => SelectedTool.Toolbar.GetSetting("ToolSize") != null ? (int)SelectedTool.Toolbar.GetSetting("ToolSize").Value : 1;
+        private Coordinates _lastMousePos;
+        private BitmapPixelChanges _lastChangedPixels;
 
 
         public event EventHandler<BitmapChangedEventArgs> BitmapChanged;
         public event EventHandler<BitmapChangedEventArgs> BitmapChanged;
-        public event EventHandler<LayersChangedEventArgs> LayersChanged;
 
 
-        private Coordinates _lastMousePos;
-        private BitmapPixelChanges _lastChangedPixels;
+        public BitmapManager Manager { get; set; }
 
 
-        public BitmapOperationsUtility()
+        public BitmapOperationsUtility(BitmapManager manager)
         {
         {
-            MouseController = new MouseMovementController();
-            MouseController.StartedRecordingChanges += MouseController_StartedRecordingChanges;
-            MouseController.MousePositionChanged += Controller_MousePositionChanged;
-            MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
+            Manager = manager;
         }
         }
 
 
-        public void SetActiveTool(Tool tool)
+        public void TriggerAction(Coordinates newPos, List<Coordinates> mouseMove, BitmapOperationTool tool)
         {
         {
-            if(PreviewLayer != null)
-            {
-                PreviewLayer.Clear();
-            }
-            if (SelectedTool != null)
+            if (tool != null && tool.ToolType != ToolType.None && Mouse.LeftButton == MouseButtonState.Pressed)
             {
             {
-                SelectedTool.Toolbar.SaveToolbarSettings();
-            }
-            SelectedTool = tool;
-            SelectedTool.Toolbar.LoadSharedSettings();
-        }
+                if (Manager.Layers.Count == 0 || mouseMove.Count == 0) return;
+                mouseMove.Reverse();
+                UseTool(mouseMove, tool, Manager.PrimaryColor);
 
 
-        private void MouseController_StartedRecordingChanges(object sender, EventArgs e)
-        {
-            if (PreviewLayer != null)
-            {
-                PreviewLayer.Clear();
+                _lastMousePos = newPos;
             }
             }
-        }
-
-        private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
-        {
-            if(SelectedTool.RequiresPreviewLayer)
+            else if (Mouse.LeftButton == MouseButtonState.Released)
             {
             {
-                BitmapPixelChanges oldValues = GetOldPixelsValues(_lastChangedPixels.ChangedPixels.Keys.ToArray());
-                Layers[ActiveLayerIndex].ApplyPixels(_lastChangedPixels);
-                BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(_lastChangedPixels, oldValues, ActiveLayerIndex));
-                _previewLayer.Clear();
+                HighlightPixels(newPos);
             }
             }
         }
         }
 
 
-        private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
+        public void StopAction()
         {
         {
-            if(SelectedTool != null && SelectedTool.ToolType != ToolType.None && Mouse.LeftButton == MouseButtonState.Pressed)
-            {
-                var mouseMove = MouseController.LastMouseMoveCoordinates.ToList();
-                if (Layers.Count == 0 || mouseMove.Count == 0) return;
-                mouseMove.Reverse();
-                UseTool(mouseMove);
-                
-                _lastMousePos = e.NewPosition;
-            }
-            else if(Mouse.LeftButton == MouseButtonState.Released)
-            {
-                HighlightPixels(e.NewPosition);
-            }
+            BitmapPixelChanges oldValues = GetOldPixelsValues(_lastChangedPixels.ChangedPixels.Keys.ToArray());
+            Manager.ActiveLayer.ApplyPixels(_lastChangedPixels);
+            BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(_lastChangedPixels, oldValues, Manager.ActiveLayerIndex));
+            Manager.PreviewLayer.Clear();
         }
         }
 
 
+       
         private void HighlightPixels(Coordinates newPosition)
         private void HighlightPixels(Coordinates newPosition)
         {
         {
-            if (Layers.Count == 0 || SelectedTool.HideHighlight) return;
+            if (Manager.Layers.Count == 0 || Manager.SelectedTool.HideHighlight) return;
             GeneratePreviewLayer();
             GeneratePreviewLayer();
-            PreviewLayer.Clear();
-            Coordinates[] highlightArea = CoordinatesCalculator.RectangleToCoordinates(CoordinatesCalculator.CalculateThicknessCenter(newPosition, ToolSize));    
-            PreviewLayer.ApplyPixels(BitmapPixelChanges.FromSingleColoredArray(highlightArea, Color.FromArgb(77,0,0,0)));
+            Manager.PreviewLayer.Clear();
+            Coordinates[] highlightArea = CoordinatesCalculator.RectangleToCoordinates(
+                CoordinatesCalculator.CalculateThicknessCenter(newPosition, Manager.ToolSize));    
+            Manager.PreviewLayer.ApplyPixels(BitmapPixelChanges.FromSingleColoredArray(highlightArea, Color.FromArgb(77,0,0,0)));
         }
         }
 
 
-        private void UseTool(List<Coordinates> mouseMoveCords)
+        private void UseTool(List<Coordinates> mouseMoveCords, BitmapOperationTool tool, Color color)
         {
         {
-            if (SelectedTool.PerformsOperationOnBitmap == false) return;
             if (Keyboard.IsKeyDown(Key.LeftShift) && !MouseCordsNotInLine(mouseMoveCords))
             if (Keyboard.IsKeyDown(Key.LeftShift) && !MouseCordsNotInLine(mouseMoveCords))
             {
             {
                 mouseMoveCords = GetSquareCoordiantes(mouseMoveCords);
                 mouseMoveCords = GetSquareCoordiantes(mouseMoveCords);
-            }
-            if (!SelectedTool.RequiresPreviewLayer)
-            {
-                BitmapPixelChanges changedPixels = SelectedTool.Use(Layers[ActiveLayerIndex], mouseMoveCords.ToArray(), PrimaryColor);
-                BitmapPixelChanges oldPixelsValues = GetOldPixelsValues(changedPixels.ChangedPixels.Keys.ToArray());
-                ActiveLayer.ApplyPixels(changedPixels);
-                BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(changedPixels, oldPixelsValues, ActiveLayerIndex));
-            }
-            else
-            {
-                UseToolOnPreviewLayer(mouseMoveCords);
-            }
+            };
+                if (!tool.RequiresPreviewLayer)
+                {
+                    BitmapPixelChanges changedPixels = tool.Use(Manager.ActiveLayer, mouseMoveCords.ToArray(), color);
+                    BitmapPixelChanges oldPixelsValues = GetOldPixelsValues(changedPixels.ChangedPixels.Keys.ToArray());
+                    Manager.ActiveLayer.ApplyPixels(changedPixels);
+                    BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(changedPixels, oldPixelsValues, Manager.ActiveLayerIndex));
+                }
+                else
+                {
+                    UseToolOnPreviewLayer(mouseMoveCords);
+                }           
         }
         }
 
 
         private bool MouseCordsNotInLine(List<Coordinates> cords)
         private bool MouseCordsNotInLine(List<Coordinates> cords)
@@ -187,14 +111,14 @@ namespace PixiEditor.Models.Controllers
         private BitmapPixelChanges GetOldPixelsValues(Coordinates[] coordinates)
         private BitmapPixelChanges GetOldPixelsValues(Coordinates[] coordinates)
         {
         {
             Dictionary<Coordinates, Color> values = new Dictionary<Coordinates, Color>();
             Dictionary<Coordinates, Color> values = new Dictionary<Coordinates, Color>();
-            Layers[ActiveLayerIndex].LayerBitmap.Lock();
+            Manager.ActiveLayer.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) 
+                if (coordinates[i].X < 0 || coordinates[i].X > Manager.Layers[0].Width - 1 || coordinates[i].Y < 0 || coordinates[i].Y > Manager.Layers[0].Height - 1) 
                     continue;
                     continue;
-                values.Add(coordinates[i], Layers[ActiveLayerIndex].LayerBitmap.GetPixel(coordinates[i].X, coordinates[i].Y));
+                values.Add(coordinates[i], Manager.ActiveLayer.LayerBitmap.GetPixel(coordinates[i].X, coordinates[i].Y));
             }
             }
-            Layers[ActiveLayerIndex].LayerBitmap.Unlock();
+            Manager.ActiveLayer.LayerBitmap.Unlock();
             return new BitmapPixelChanges(values);
             return new BitmapPixelChanges(values);
         }
         }
 
 
@@ -204,79 +128,20 @@ namespace PixiEditor.Models.Controllers
             if (mouseMove.Count > 0 && mouseMove[0] != _lastMousePos)
             if (mouseMove.Count > 0 && mouseMove[0] != _lastMousePos)
             {
             {
                 GeneratePreviewLayer();
                 GeneratePreviewLayer();
-                PreviewLayer.Clear();
-                changedPixels = SelectedTool.Use(Layers[ActiveLayerIndex], mouseMove.ToArray(), PrimaryColor);
-                PreviewLayer.ApplyPixels(changedPixels);
+                Manager.PreviewLayer.Clear();
+                changedPixels = ((BitmapOperationTool)Manager.SelectedTool).Use(Manager.ActiveLayer, mouseMove.ToArray(), Manager.PrimaryColor);
+                Manager.PreviewLayer.ApplyPixels(changedPixels);
                 _lastChangedPixels = changedPixels;
                 _lastChangedPixels = changedPixels;
             }
             }
         }
         }
 
 
         private void GeneratePreviewLayer()
         private void GeneratePreviewLayer()
         {
         {
-            if (PreviewLayer == null)
-            {
-                PreviewLayer = new Layer("_previewLayer", Layers[0].Width, Layers[0].Height);
-            }
-        }
-
-        public void RemoveLayer(int layerIndex)
-        {
-            if (Layers.Count <= 1) return;
-
-            bool wasActive = Layers[layerIndex].IsActive;
-            Layers.RemoveAt(layerIndex);
-            if (wasActive)
-            {
-                SetActiveLayer(0);
-            }
-            else if(ActiveLayerIndex > Layers.Count - 1)
-            {
-                SetActiveLayer(Layers.Count - 1);
-            }
-        }
-
-        public void AddNewLayer(string name, int width, int height, bool setAsActive = true)
-        {
-            Layers.Add(new Layer(name, width, height));
-            if (setAsActive)
+            if (Manager.PreviewLayer == null)
             {
             {
-                SetActiveLayer(Layers.Count - 1);
+                Manager.PreviewLayer = new Layer("_previewLayer", Manager.Layers[0].Width, Manager.Layers[0].Height);
             }
             }
-            LayersChanged?.Invoke(this, new LayersChangedEventArgs(0, LayerAction.Add));
-        }
-
-        public void SetActiveLayer(int index)
-        {
-            if (ActiveLayerIndex <= Layers.Count - 1)
-            {
-                Layers[ActiveLayerIndex].IsActive = false;
-            }
-                ActiveLayerIndex = index;
-            Layers[ActiveLayerIndex].IsActive = true;
-            LayersChanged?.Invoke(this, new LayersChangedEventArgs(index, LayerAction.SetActive));
-        }
-
-        public WriteableBitmap GetCombinedLayersBitmap()
-        {
-            WriteableBitmap finalBitmap = Layers[0].LayerBitmap.Clone();
-            finalBitmap.Lock();
-            for (int i = 1; i < Layers.Count; i++)
-            {
-                for (int y = 0; y < finalBitmap.Height; y++)
-                {
-                    for (int x = 0; x < finalBitmap.Width; x++)
-                    {
-                        Color color = Layers[i].LayerBitmap.GetPixel(x, y);
-                        if (color.A != 0 || color.R != 0 || color.B != 0 || color.G != 0)
-                        {
-                            finalBitmap.SetPixel(x, y, color);
-                        }
-                    }
-                }
-            }
-            finalBitmap.Unlock();
-            return finalBitmap;
-        }
+        }       
     }
     }
 }
 }
 
 
@@ -292,16 +157,4 @@ public class BitmapChangedEventArgs : EventArgs
         OldPixelsValues = oldPixelsValues;
         OldPixelsValues = oldPixelsValues;
         ChangedLayerIndex = changedLayerIndex;
         ChangedLayerIndex = changedLayerIndex;
     }
     }
-}
-
-public class LayersChangedEventArgs : EventArgs
-{
-    public int LayerAffected { get; set; }
-    public LayerAction LayerChangeType { get; set; }
-
-    public LayersChangedEventArgs(int layerAffected, LayerAction layerChangeType)
-    {
-        LayerAffected = layerAffected;
-        LayerChangeType = layerChangeType;
-    }
 }
 }

+ 0 - 1
PixiEditor/Models/Controllers/UndoManager.cs

@@ -10,7 +10,6 @@ namespace PixiEditor.Models.Controllers
     {
     {
         public static StackEx<Change> UndoStack { get; set; } = new StackEx<Change>();
         public static StackEx<Change> UndoStack { get; set; } = new StackEx<Change>();
         public static StackEx<Change> RedoStack { get; set; } = new StackEx<Change>();
         public static StackEx<Change> RedoStack { get; set; } = new StackEx<Change>();
-        private static bool _stopRecording = false;
         private static List<Change> _recordedChanges = new List<Change>();
         private static List<Change> _recordedChanges = new List<Change>();
         private static bool _lastChangeWasUndo = false;
         private static bool _lastChangeWasUndo = false;
         public static bool CanUndo
         public static bool CanUndo

+ 16 - 0
PixiEditor/Models/Tools/BitmapOperationTool.cs

@@ -0,0 +1,16 @@
+using PixiEditor.Models.Layers;
+using PixiEditor.Models.Position;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Windows.Media;
+
+namespace PixiEditor.Models.Tools
+{
+    public abstract class BitmapOperationTool : Tool
+    {
+        public abstract BitmapPixelChanges Use(Layer layer, Coordinates[] pixels, Color color);
+        public bool RequiresPreviewLayer { get; set; }
+
+    }
+}

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

@@ -10,7 +10,7 @@ using System.Windows.Media;
 
 
 namespace PixiEditor.Models.Tools
 namespace PixiEditor.Models.Tools
 {
 {
-    public abstract class ShapeTool : Tool
+    public abstract class ShapeTool : BitmapOperationTool
     {
     {
         public override abstract ToolType ToolType { get; }
         public override abstract ToolType ToolType { get; }
 
 

+ 1 - 4
PixiEditor/Models/Tools/Tool.cs

@@ -8,13 +8,10 @@ using System.Windows.Media;
 namespace PixiEditor.Models.Tools
 namespace PixiEditor.Models.Tools
 {
 {
     public abstract class Tool : NotifyableObject
     public abstract class Tool : NotifyableObject
-    {
-        public abstract BitmapPixelChanges Use(Layer layer, Coordinates[] pixels, Color color);
+    {        
         public abstract ToolType ToolType { get; }
         public abstract ToolType ToolType { get; }
         public string ImagePath => $"/Images/{ToolType}Image.png";
         public string ImagePath => $"/Images/{ToolType}Image.png";
-        public bool PerformsOperationOnBitmap { get; set; } = true;
         public bool HideHighlight { get; set; } = false;
         public bool HideHighlight { get; set; } = false;
-        public bool RequiresPreviewLayer { get; set; }
         public string Tooltip { get; set; }
         public string Tooltip { get; set; }
 
 
         private bool _isActive = false;
         private bool _isActive = false;

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

@@ -10,7 +10,7 @@ using System.Windows.Media.Imaging;
 
 
 namespace PixiEditor.Models.Tools.Tools
 namespace PixiEditor.Models.Tools.Tools
 {
 {
-    public class BrightnessTool : Tool
+    public class BrightnessTool : BitmapOperationTool
     {
     {
         public override ToolType ToolType => ToolType.Brightness;
         public override ToolType ToolType => ToolType.Brightness;
         private const float CorrectionFactor = 5f; //Initial correction factor
         private const float CorrectionFactor = 5f; //Initial correction factor

+ 0 - 6
PixiEditor/Models/Tools/Tools/ColorPickerTool.cs

@@ -13,13 +13,7 @@ namespace PixiEditor.Models.Tools.Tools
 
 
         public ColorPickerTool()
         public ColorPickerTool()
         {
         {
-            PerformsOperationOnBitmap = false;
             HideHighlight = true;
             HideHighlight = true;
         }
         }
-
-        public override BitmapPixelChanges Use(Layer layer, Coordinates[] pixels, Color color)
-        {
-            return new BitmapPixelChanges();
-        }
     }
     }
 }
 }

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

@@ -8,7 +8,7 @@ using System.Windows.Media;
 
 
 namespace PixiEditor.Models.Tools.Tools
 namespace PixiEditor.Models.Tools.Tools
 {
 {
-    public class EarserTool : Tool
+    public class EarserTool : BitmapOperationTool
     {
     {
         public override ToolType ToolType => ToolType.Earser;
         public override ToolType ToolType => ToolType.Earser;
 
 

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

@@ -7,7 +7,7 @@ using System.Windows.Media.Imaging;
 
 
 namespace PixiEditor.Models.Tools.Tools
 namespace PixiEditor.Models.Tools.Tools
 {
 {
-    public class FloodFill : Tool
+    public class FloodFill : BitmapOperationTool
     {
     {
         public override ToolType ToolType => ToolType.Bucket;
         public override ToolType ToolType => ToolType.Bucket;
 
 

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

@@ -6,7 +6,7 @@ using System.Windows.Media;
 
 
 namespace PixiEditor.Models.Tools.Tools
 namespace PixiEditor.Models.Tools.Tools
 {
 {
-    public class PenTool : Tool
+    public class PenTool : BitmapOperationTool
     {
     {
         public override ToolType ToolType => ToolType.Pen;
         public override ToolType ToolType => ToolType.Pen;
         private int _toolSizeIndex;
         private int _toolSizeIndex;

+ 41 - 40
PixiEditor/ViewModels/ViewModelMain.cs

@@ -69,7 +69,7 @@ namespace PixiEditor.ViewModels
                 if (_primaryColor != value)
                 if (_primaryColor != value)
                 {
                 {
                     _primaryColor = value;
                     _primaryColor = value;
-                    BitmapUtility.PrimaryColor = value;
+                    BitmapManager.PrimaryColor = value;
                     RaisePropertyChanged("PrimaryColor");
                     RaisePropertyChanged("PrimaryColor");
                 }
                 }
             }
             }
@@ -107,7 +107,7 @@ namespace PixiEditor.ViewModels
             set 
             set 
             { 
             { 
                 _undoChanges = value;
                 _undoChanges = value;
-                BitmapUtility.Layers[value.LayerIndex].ApplyPixels(value.PixelChanges);
+                BitmapManager.Layers[value.LayerIndex].ApplyPixels(value.PixelChanges);
             }
             }
         }
         }
 
 
@@ -124,7 +124,7 @@ namespace PixiEditor.ViewModels
 
 
 
 
 
 
-        public BitmapOperationsUtility BitmapUtility { get; set; }
+        public BitmapManager BitmapManager { get; set; }
         public PixelChangesController ChangesController { get; set; }
         public PixelChangesController ChangesController { get; set; }
 
 
         public ShortcutController ShortcutController { get; set; }
         public ShortcutController ShortcutController { get; set; }
@@ -132,9 +132,9 @@ namespace PixiEditor.ViewModels
         public ViewModelMain()
         public ViewModelMain()
         {
         {
             PixiFilesManager.InitializeTempDirectories();
             PixiFilesManager.InitializeTempDirectories();
-            BitmapUtility = new BitmapOperationsUtility();
-            BitmapUtility.BitmapChanged += BitmapUtility_BitmapChanged;
-            BitmapUtility.MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
+            BitmapManager = new BitmapManager();
+            BitmapManager.BitmapOperations.BitmapChanged += BitmapUtility_BitmapChanged;
+            BitmapManager.MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
             ChangesController = new PixelChangesController();
             ChangesController = new PixelChangesController();
             SelectToolCommand = new RelayCommand(SetTool);
             SelectToolCommand = new RelayCommand(SetTool);
             GenerateDrawAreaCommand = new RelayCommand(GenerateDrawArea);
             GenerateDrawAreaCommand = new RelayCommand(GenerateDrawArea);
@@ -178,7 +178,7 @@ namespace PixiEditor.ViewModels
             };
             };
             UndoManager.SetMainRoot(this);
             UndoManager.SetMainRoot(this);
             SetActiveTool(ToolType.Pen);
             SetActiveTool(ToolType.Pen);
-            BitmapUtility.PrimaryColor = PrimaryColor;
+            BitmapManager.PrimaryColor = PrimaryColor;
         }
         }
 
 
         public void SetTool(object parameter)
         public void SetTool(object parameter)
@@ -188,7 +188,7 @@ namespace PixiEditor.ViewModels
 
 
         public void RenameLayer(object parameter)
         public void RenameLayer(object parameter)
         {
         {
-            BitmapUtility.Layers[(int)parameter].IsRenaming = true;
+            BitmapManager.Layers[(int)parameter].IsRenaming = true;
         }
         }
 
 
         public void KeyDown(object parameter)
         public void KeyDown(object parameter)
@@ -198,7 +198,7 @@ namespace PixiEditor.ViewModels
 
 
         private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
         private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
         {
         {
-            if (BitmapUtility.SelectedTool.PerformsOperationOnBitmap)
+            if (BitmapManager.SelectedTool is BitmapOperationTool)
             {
             {
                 Tuple<LayerChanges, LayerChanges> changes = ChangesController.PopChanges();
                 Tuple<LayerChanges, LayerChanges> changes = ChangesController.PopChanges();
                 if (changes.Item1.PixelChanges.ChangedPixels.Count > 0)
                 if (changes.Item1.PixelChanges.ChangedPixels.Count > 0)
@@ -222,26 +222,26 @@ namespace PixiEditor.ViewModels
         public void MoveLayerToFront(object parameter)
         public void MoveLayerToFront(object parameter)
         {
         {
             int oldIndex = (int)parameter;
             int oldIndex = (int)parameter;
-            BitmapUtility.Layers.Move(oldIndex, oldIndex + 1);
-            if(BitmapUtility.ActiveLayerIndex == oldIndex)
+            BitmapManager.Layers.Move(oldIndex, oldIndex + 1);
+            if(BitmapManager.ActiveLayerIndex == oldIndex)
             {
             {
-                BitmapUtility.SetActiveLayer(oldIndex + 1);
+                BitmapManager.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);
-            if (BitmapUtility.ActiveLayerIndex == oldIndex)
+            BitmapManager.Layers.Move(oldIndex, oldIndex - 1);
+            if (BitmapManager.ActiveLayerIndex == oldIndex)
             {
             {
-                BitmapUtility.SetActiveLayer(oldIndex - 1);
+                BitmapManager.SetActiveLayer(oldIndex - 1);
             }
             }
         }
         }
 
 
         public bool CanMoveToFront(object property)
         public bool CanMoveToFront(object property)
         {
         {
-            return BitmapUtility.Layers.Count - 1 > (int)property;
+            return BitmapManager.Layers.Count - 1 > (int)property;
         }
         }
 
 
         public bool CanMoveToBack(object property)
         public bool CanMoveToBack(object property)
@@ -251,17 +251,17 @@ namespace PixiEditor.ViewModels
 
 
         public void SetActiveLayer(object parameter)
         public void SetActiveLayer(object parameter)
         {
         {
-            BitmapUtility.SetActiveLayer((int)parameter);
+            BitmapManager.SetActiveLayer((int)parameter);
         }
         }
 
 
         public void DeleteLayer(object parameter)
         public void DeleteLayer(object parameter)
         {
         {
-            BitmapUtility.RemoveLayer((int)parameter);
+            BitmapManager.RemoveLayer((int)parameter);
         }
         }
 
 
         public bool CanDeleteLayer(object property)
         public bool CanDeleteLayer(object property)
         {
         {
-            return BitmapUtility.Layers.Count > 1;
+            return BitmapManager.Layers.Count > 1;
         }
         }
 
 
         #region Undo/Redo
         #region Undo/Redo
@@ -303,14 +303,15 @@ namespace PixiEditor.ViewModels
 
 
         private void SetActiveTool(ToolType tool)
         private void SetActiveTool(ToolType tool)
         {
         {
-            BitmapUtility.SetActiveTool(ToolSet.First(x=> x.ToolType == tool));
+            Tool foundTool = ToolSet.First(x => x.ToolType == tool);
             Tool activeTool = ToolSet.FirstOrDefault(x => x.IsActive);
             Tool activeTool = ToolSet.FirstOrDefault(x => x.IsActive);
             if(activeTool != null)
             if(activeTool != null)
             {
             {
                 activeTool.IsActive = false;
                 activeTool.IsActive = false;
             }
             }
 
 
-            BitmapUtility.SelectedTool.IsActive = true;
+            foundTool.IsActive = true;
+            BitmapManager.SetActiveTool(foundTool);
             SetToolCursor(tool);
             SetToolCursor(tool);
         }
         }
 
 
@@ -318,7 +319,7 @@ namespace PixiEditor.ViewModels
         {
         {
             if (tool != ToolType.None && tool != ToolType.ColorPicker)
             if (tool != ToolType.None && tool != ToolType.ColorPicker)
             {
             {
-                ToolCursor = BitmapUtility.SelectedTool.Cursor;
+                ToolCursor = BitmapManager.SelectedTool.Cursor;
             }
             }
             else
             else
             {
             {
@@ -332,22 +333,22 @@ namespace PixiEditor.ViewModels
         /// <param name="parameter"></param>
         /// <param name="parameter"></param>
         private void MouseUp(object parameter)
         private void MouseUp(object parameter)
         {
         {
-            BitmapUtility.MouseController.StopRecordingMouseMovementChanges();
+            BitmapManager.MouseController.StopRecordingMouseMovementChanges();
         }
         }
 
 
         private void MouseDown(object parameter)
         private void MouseDown(object parameter)
         {
         {
-            if (BitmapUtility.Layers.Count == 0) return;
-            if(BitmapUtility.SelectedTool.ToolType == ToolType.ColorPicker)
+            if (BitmapManager.Layers.Count == 0) return;
+            if(BitmapManager.SelectedTool.ToolType == ToolType.ColorPicker)
             {
             {
                 ExecuteColorPicker();
                 ExecuteColorPicker();
             }
             }
-            else if(Mouse.LeftButton == MouseButtonState.Pressed && BitmapUtility.SelectedTool.PerformsOperationOnBitmap)
+            else if(Mouse.LeftButton == MouseButtonState.Pressed && BitmapManager.SelectedTool is BitmapOperationTool)
             {
             {
-                if (!BitmapUtility.MouseController.IsRecordingChanges)
+                if (!BitmapManager.MouseController.IsRecordingChanges)
                 {
                 {
-                    BitmapUtility.MouseController.StartRecordingMouseMovementChanges();
-                    BitmapUtility.MouseController.RecordMouseMovementChange(MousePositionConverter.CurrentCoordinates);
+                    BitmapManager.MouseController.StartRecordingMouseMovementChanges();
+                    BitmapManager.MouseController.RecordMouseMovementChange(MousePositionConverter.CurrentCoordinates);
                 }
                 }
             }
             }
         }
         }
@@ -361,13 +362,13 @@ namespace PixiEditor.ViewModels
             Coordinates cords = new Coordinates((int)MouseXOnCanvas, (int)MouseYOnCanvas);
             Coordinates cords = new Coordinates((int)MouseXOnCanvas, (int)MouseYOnCanvas);
             MousePositionConverter.CurrentCoordinates = cords;
             MousePositionConverter.CurrentCoordinates = cords;
 
 
-            if (BitmapUtility.MouseController.IsRecordingChanges && Mouse.LeftButton == MouseButtonState.Pressed)
+            if (BitmapManager.MouseController.IsRecordingChanges && Mouse.LeftButton == MouseButtonState.Pressed)
             {
             {
-                BitmapUtility.MouseController.RecordMouseMovementChange(cords);
+                BitmapManager.MouseController.RecordMouseMovementChange(cords);
             }
             }
             else
             else
             {
             {
-                BitmapUtility.MouseController.MouseMoved(cords);
+                BitmapManager.MouseController.MouseMoved(cords);
             }
             }
         }
         }
 
 
@@ -408,7 +409,7 @@ namespace PixiEditor.ViewModels
         /// <param name="parameter"></param>
         /// <param name="parameter"></param>
         private void SaveFile(object parameter)
         private void SaveFile(object parameter)
         {
         {
-            WriteableBitmap bitmap = BitmapUtility.GetCombinedLayersBitmap();
+            WriteableBitmap bitmap = BitmapManager.GetCombinedLayersBitmap();
             if (Exporter.SavePath == null || (string)parameter == "AsNew")
             if (Exporter.SavePath == null || (string)parameter == "AsNew")
             {
             {
                 Exporter.Export(FileType.PNG, bitmap, new Size(bitmap.PixelWidth, bitmap.PixelHeight));
                 Exporter.Export(FileType.PNG, bitmap, new Size(bitmap.PixelWidth, bitmap.PixelHeight));
@@ -425,7 +426,7 @@ namespace PixiEditor.ViewModels
         /// <returns></returns>
         /// <returns></returns>
         private bool CanSave(object property)
         private bool CanSave(object property)
         {
         {
-            return BitmapUtility.ActiveLayer != null;
+            return BitmapManager.ActiveLayer != null;
         }
         }
         #endregion
         #endregion
 
 
@@ -439,15 +440,15 @@ namespace PixiEditor.ViewModels
             if (dialog.ShowDialog())
             if (dialog.ShowDialog())
             {
             {
                 NewDocument(dialog.FileWidth, dialog.FileHeight);
                 NewDocument(dialog.FileWidth, dialog.FileHeight);
-                BitmapUtility.ActiveLayer.LayerBitmap = Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight);
+                BitmapManager.ActiveLayer.LayerBitmap = Importer.ImportImage(dialog.FilePath, dialog.FileWidth, dialog.FileHeight);
             }
             }
         }
         }
 
 
         private void NewDocument(int width, int height)
         private void NewDocument(int width, int height)
         {
         {
-            BitmapUtility.Layers.Clear();
-            BitmapUtility.AddNewLayer("Base Layer", width, height, true);
-            BitmapUtility.PreviewLayer = null;
+            BitmapManager.Layers.Clear();
+            BitmapManager.AddNewLayer("Base Layer", width, height, true);
+            BitmapManager.PreviewLayer = null;
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -461,12 +462,12 @@ namespace PixiEditor.ViewModels
 
 
         public void NewLayer(object parameter)
         public void NewLayer(object parameter)
         {
         {
-            BitmapUtility.AddNewLayer($"New Layer {BitmapUtility.Layers.Count}", BitmapUtility.Layers[0].Width, BitmapUtility.Layers[0].Height);         
+            BitmapManager.AddNewLayer($"New Layer {BitmapManager.Layers.Count}", BitmapManager.Layers[0].Width, BitmapManager.Layers[0].Height);         
         }
         }
 
 
         public bool CanCreateNewLayer(object parameter)
         public bool CanCreateNewLayer(object parameter)
         {
         {
-                return BitmapUtility.Layers.Count > 0;
+                return BitmapManager.Layers.Count > 0;
         }
         }
     }
     }
 }
 }

+ 6 - 6
PixiEditor/Views/MainWindow.xaml

@@ -83,7 +83,7 @@
             </StackPanel>
             </StackPanel>
         </DockPanel>
         </DockPanel>
         <StackPanel Background="#404040" Orientation="Horizontal" Grid.ColumnSpan="2" Margin="0,30,0,0" Grid.RowSpan="2">
         <StackPanel Background="#404040" Orientation="Horizontal" Grid.ColumnSpan="2" Margin="0,30,0,0" Grid.RowSpan="2">
-            <ItemsControl ItemsSource="{Binding BitmapUtility.SelectedTool.Toolbar.Settings}">
+            <ItemsControl ItemsSource="{Binding BitmapManager.SelectedTool.Toolbar.Settings}">
                 <ItemsControl.ItemsPanel>
                 <ItemsControl.ItemsPanel>
                     <ItemsPanelTemplate>
                     <ItemsPanelTemplate>
                         <StackPanel Orientation="Horizontal" Margin="10, 0, 0, 0"/>
                         <StackPanel Orientation="Horizontal" Margin="10, 0, 0, 0"/>
@@ -114,15 +114,15 @@
                         <behaviors:MouseBehaviour RelativeTo="{Binding ElementName=DrawingPanel, Path=Item}" MouseX="{Binding MouseXOnCanvas, Mode=OneWayToSource}" MouseY="{Binding MouseYOnCanvas, Mode=OneWayToSource}"/>
                         <behaviors:MouseBehaviour RelativeTo="{Binding ElementName=DrawingPanel, Path=Item}" MouseX="{Binding MouseXOnCanvas, Mode=OneWayToSource}" MouseY="{Binding MouseYOnCanvas, Mode=OneWayToSource}"/>
                     </i:Interaction.Behaviors>
                     </i:Interaction.Behaviors>
                     <vws:MainDrawingPanel.Item>
                     <vws:MainDrawingPanel.Item>
-                        <Canvas Width="{Binding BitmapUtility.ActiveLayer.Width}" Height="{Binding BitmapUtility.ActiveLayer.Height}" VerticalAlignment="Center" HorizontalAlignment="Center">
+                        <Canvas Width="{Binding BitmapManager.ActiveLayer.Width}" Height="{Binding BitmapManager.ActiveLayer.Height}" VerticalAlignment="Center" HorizontalAlignment="Center">
                             <i:Interaction.Triggers>
                             <i:Interaction.Triggers>
                                 <i:EventTrigger EventName="MouseDown">
                                 <i:EventTrigger EventName="MouseDown">
                                     <i:InvokeCommandAction Command="{Binding MouseDownCommand}"/>
                                     <i:InvokeCommandAction Command="{Binding MouseDownCommand}"/>
                                 </i:EventTrigger>
                                 </i:EventTrigger>
                             </i:Interaction.Triggers>
                             </i:Interaction.Triggers>
-                            <Image Source="/Images/transparentbg.png" Height="{Binding BitmapUtility.ActiveLayer.Height}" Width="{Binding BitmapUtility.ActiveLayer.Width}" Opacity="0.9" Stretch="UniformToFill"/>
-                            <Image Source="{Binding BitmapUtility.PreviewLayer.LayerBitmap}" Panel.ZIndex="2" RenderOptions.BitmapScalingMode="NearestNeighbor" Stretch="Uniform" Width="{Binding BitmapUtility.PreviewLayer.Width}" Height="{Binding BitmapUtility.PreviewLayer.Height}"/>
-                            <ItemsControl ItemsSource="{Binding BitmapUtility.Layers}">
+                            <Image Source="/Images/transparentbg.png" Height="{Binding BitmapManager.ActiveLayer.Height}" Width="{Binding BitmapManager.ActiveLayer.Width}" Opacity="0.9" Stretch="UniformToFill"/>
+                            <Image Source="{Binding BitmapManager.PreviewLayer.LayerBitmap}" Panel.ZIndex="2" RenderOptions.BitmapScalingMode="NearestNeighbor" Stretch="Uniform" Width="{Binding BitmapManager.PreviewLayer.Width}" Height="{Binding BitmapManager.PreviewLayer.Height}"/>
+                            <ItemsControl ItemsSource="{Binding BitmapManager.Layers}">
                                 <ItemsControl.ItemsPanel>
                                 <ItemsControl.ItemsPanel>
                                     <ItemsPanelTemplate>
                                     <ItemsPanelTemplate>
                                         <Grid/>
                                         <Grid/>
@@ -192,7 +192,7 @@
                             <xcad:LayoutAnchorable ContentId="layers" Title="Layers" CanHide="False" CanClose="False" CanAutoHide="False" CanDockAsTabbedDocument="False" CanFloat="True">
                             <xcad:LayoutAnchorable ContentId="layers" Title="Layers" CanHide="False" CanClose="False" CanAutoHide="False" CanDockAsTabbedDocument="False" CanFloat="True">
                                 <StackPanel  Orientation="Vertical">
                                 <StackPanel  Orientation="Vertical">
                                     <Button Command="{Binding NewLayerCommand}" Height="30" Content="New Layer" HorizontalAlignment="Stretch" Margin="5" Style="{StaticResource DarkRoundButton}"/>
                                     <Button Command="{Binding NewLayerCommand}" Height="30" Content="New Layer" HorizontalAlignment="Stretch" Margin="5" Style="{StaticResource DarkRoundButton}"/>
-                                    <ItemsControl ItemsSource="{Binding BitmapUtility.Layers}" x:Name="layersItemsControl" AlternationCount="9999">
+                                    <ItemsControl ItemsSource="{Binding BitmapManager.Layers}" x:Name="layersItemsControl" AlternationCount="9999">
                                         <ItemsControl.ItemsPanel>
                                         <ItemsControl.ItemsPanel>
                                             <ItemsPanelTemplate>
                                             <ItemsPanelTemplate>
                                                 <ui:ReversedOrderStackPanel Orientation="Vertical"/>
                                                 <ui:ReversedOrderStackPanel Orientation="Vertical"/>