Browse Source

Added readonly tool separate logic

flabbet 5 years ago
parent
commit
25b61388c3

+ 35 - 3
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -8,6 +8,7 @@ using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Linq;
 using System.Text;
+using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 
@@ -68,6 +69,7 @@ namespace PixiEditor.Models.Controllers
         public event EventHandler<LayersChangedEventArgs> LayersChanged;
 
         public BitmapOperationsUtility BitmapOperations { get; set; }
+        public ReadonlyToolUtility ReadonlyToolUtility { get; set; }
 
         public void SetActiveTool(Tool tool)
         {
@@ -90,6 +92,7 @@ namespace PixiEditor.Models.Controllers
             MouseController.MousePositionChanged += Controller_MousePositionChanged;
             MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
             BitmapOperations = new BitmapOperationsUtility(this);
+            ReadonlyToolUtility = new ReadonlyToolUtility(this);
         }
 
         public void SetActiveLayer(int index)
@@ -131,10 +134,21 @@ namespace PixiEditor.Models.Controllers
 
         private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
         {
-            if (IsOperationTool(SelectedTool))
+            if (Mouse.LeftButton == MouseButtonState.Pressed)
             {
-                BitmapOperations.TriggerAction(e.NewPosition, 
-                    MouseController.LastMouseMoveCoordinates.ToList(), (BitmapOperationTool)SelectedTool);
+                if (IsOperationTool(SelectedTool))
+                {
+                    BitmapOperations.TriggerAction(e.NewPosition,
+                        MouseController.LastMouseMoveCoordinates.ToList(), (BitmapOperationTool)SelectedTool);
+                }
+                else
+                {
+                    ReadonlyToolUtility.ExecuteTool((ReadonlyTool)SelectedTool);
+                }
+            }            
+            else if(Mouse.LeftButton == MouseButtonState.Released)
+            {
+                HighlightPixels(e.NewPosition);
             }
         }
 
@@ -154,6 +168,24 @@ namespace PixiEditor.Models.Controllers
             }
         }
 
+        public void GeneratePreviewLayer()
+        {
+            if (PreviewLayer == null)
+            {
+                PreviewLayer = new Layer("_previewLayer", Layers[0].Width, Layers[0].Height);
+            }
+        }
+
+        private void HighlightPixels(Coordinates newPosition)
+        {
+            if (Layers.Count == 0 || SelectedTool.HideHighlight) return;
+            GeneratePreviewLayer();
+            PreviewLayer.Clear();
+            Coordinates[] highlightArea = CoordinatesCalculator.RectangleToCoordinates(
+                CoordinatesCalculator.CalculateThicknessCenter(newPosition, ToolSize));
+            PreviewLayer.ApplyPixels(BitmapPixelChanges.FromSingleColoredArray(highlightArea, Color.FromArgb(77, 0, 0, 0)));
+        }
+
         public WriteableBitmap GetCombinedLayersBitmap()
         {
             WriteableBitmap finalBitmap = Layers[0].LayerBitmap.Clone();

+ 4 - 25
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -30,7 +30,7 @@ namespace PixiEditor.Models.Controllers
 
         public void TriggerAction(Coordinates newPos, List<Coordinates> mouseMove, BitmapOperationTool tool)
         {
-            if (tool != null && tool.ToolType != ToolType.None && Mouse.LeftButton == MouseButtonState.Pressed)
+            if (tool != null && tool.ToolType != ToolType.None)
             {
                 if (Manager.Layers.Count == 0 || mouseMove.Count == 0) return;
                 mouseMove.Reverse();
@@ -38,10 +38,6 @@ namespace PixiEditor.Models.Controllers
 
                 _lastMousePos = newPos;
             }
-            else if (Mouse.LeftButton == MouseButtonState.Released)
-            {
-                HighlightPixels(newPos);
-            }
         }
 
         public void StopAction()
@@ -52,16 +48,7 @@ namespace PixiEditor.Models.Controllers
             Manager.PreviewLayer.Clear();
         }
 
-       
-        private void HighlightPixels(Coordinates newPosition)
-        {
-            if (Manager.Layers.Count == 0 || Manager.SelectedTool.HideHighlight) return;
-            GeneratePreviewLayer();
-            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, BitmapOperationTool tool, Color color)
         {
@@ -127,21 +114,13 @@ namespace PixiEditor.Models.Controllers
             BitmapPixelChanges changedPixels;
             if (mouseMove.Count > 0 && mouseMove[0] != _lastMousePos)
             {
-                GeneratePreviewLayer();
+                Manager.GeneratePreviewLayer();
                 Manager.PreviewLayer.Clear();
                 changedPixels = ((BitmapOperationTool)Manager.SelectedTool).Use(Manager.ActiveLayer, mouseMove.ToArray(), Manager.PrimaryColor);
                 Manager.PreviewLayer.ApplyPixels(changedPixels);
                 _lastChangedPixels = changedPixels;
             }
-        }
-
-        private void GeneratePreviewLayer()
-        {
-            if (Manager.PreviewLayer == null)
-            {
-                Manager.PreviewLayer = new Layer("_previewLayer", Manager.Layers[0].Width, Manager.Layers[0].Height);
-            }
-        }       
+        }      
     }
 }
 

+ 23 - 0
PixiEditor/Models/Controllers/ReadonlyToolUtility.cs

@@ -0,0 +1,23 @@
+using PixiEditor.Models.Tools;
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace PixiEditor.Models.Controllers
+{
+    public class ReadonlyToolUtility
+    {
+        public BitmapManager Manager { get; set; }
+
+        public ReadonlyToolUtility(BitmapManager manager)
+        {
+            Manager = manager;
+        }
+
+        public void ExecuteTool(ReadonlyTool tool)
+        {            
+            tool.Use();
+        }
+
+    }
+}

+ 11 - 0
PixiEditor/Models/Tools/ReadonlyTool.cs

@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace PixiEditor.Models.Tools
+{
+    public abstract class ReadonlyTool : Tool
+    {
+        public abstract void Use();
+    }
+}

+ 15 - 1
PixiEditor/Models/Tools/Tools/ColorPickerTool.cs

@@ -1,5 +1,6 @@
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
+using PixiEditor.ViewModels;
 using System;
 using System.Collections.Generic;
 using System.Text;
@@ -7,7 +8,7 @@ using System.Windows.Media;
 
 namespace PixiEditor.Models.Tools.Tools
 {
-    public class ColorPickerTool : Tool
+    public class ColorPickerTool : ReadonlyTool
     {
         public override ToolType ToolType => ToolType.ColorPicker;
 
@@ -15,5 +16,18 @@ namespace PixiEditor.Models.Tools.Tools
         {
             HideHighlight = true;
         }
+
+        public override void Use()
+        {
+            using (var bitmap = new System.Drawing.Bitmap(1, 1))
+            {
+                using (var graphics = System.Drawing.Graphics.FromImage(bitmap))
+                {
+                    graphics.CopyFromScreen(MousePositionConverter.GetCursorPosition(), new System.Drawing.Point(0, 0), new System.Drawing.Size(1, 1));
+                }
+                var color = bitmap.GetPixel(0, 0);
+                ViewModelMain.Current.PrimaryColor = Color.FromArgb(color.A, color.R, color.G, color.B);
+            }
+        }
     }
 }

+ 4 - 25
PixiEditor/ViewModels/ViewModelMain.cs

@@ -20,7 +20,7 @@ namespace PixiEditor.ViewModels
 {
     class ViewModelMain : ViewModelBase
     {
-
+        public static ViewModelMain Current { get; set; } = null;
         public RelayCommand SelectToolCommand { get; set; } //Command that handles tool switching 
         public RelayCommand GenerateDrawAreaCommand { get; set; } //Command that generates draw area
         public RelayCommand MouseMoveCommand { get; set; } //Command that is used to draw
@@ -179,6 +179,7 @@ namespace PixiEditor.ViewModels
             UndoManager.SetMainRoot(this);
             SetActiveTool(ToolType.Pen);
             BitmapManager.PrimaryColor = PrimaryColor;
+            Current = this;
         }
 
         public void SetTool(object parameter)
@@ -317,7 +318,7 @@ namespace PixiEditor.ViewModels
 
         private void SetToolCursor(ToolType tool)
         {
-            if (tool != ToolType.None && tool != ToolType.ColorPicker)
+            if (tool != ToolType.None)
             {
                 ToolCursor = BitmapManager.SelectedTool.Cursor;
             }
@@ -339,11 +340,7 @@ namespace PixiEditor.ViewModels
         private void MouseDown(object parameter)
         {
             if (BitmapManager.Layers.Count == 0) return;
-            if(BitmapManager.SelectedTool.ToolType == ToolType.ColorPicker)
-            {
-                ExecuteColorPicker();
-            }
-            else if(Mouse.LeftButton == MouseButtonState.Pressed && BitmapManager.SelectedTool is BitmapOperationTool)
+            if(Mouse.LeftButton == MouseButtonState.Pressed)
             {
                 if (!BitmapManager.MouseController.IsRecordingChanges)
                 {
@@ -370,24 +367,6 @@ namespace PixiEditor.ViewModels
             {
                 BitmapManager.MouseController.MouseMoved(cords);
             }
-        }
-
-
-
-        private void ExecuteColorPicker()
-        {
-            if (Mouse.LeftButton == MouseButtonState.Pressed)
-            {
-                using (var bitmap = new System.Drawing.Bitmap(1, 1))
-                {
-                    using (var graphics = System.Drawing.Graphics.FromImage(bitmap))
-                    {
-                        graphics.CopyFromScreen(MousePositionConverter.GetCursorPosition(), new System.Drawing.Point(0, 0), new System.Drawing.Size(1, 1));
-                    }
-                    var color = bitmap.GetPixel(0, 0);
-                    PrimaryColor = Color.FromArgb(color.A, color.R, color.G, color.B);
-                }
-            }
         }
 
         /// <summary>