Browse Source

Added move all to select tool

flabbet 5 years ago
parent
commit
be0fb70cf4

+ 3 - 22
PixiEditor/Models/Controllers/ClipboardController.cs

@@ -40,29 +40,10 @@ namespace PixiEditor.Models.Controllers
             int height = selection.Max(x => x.Y) - offsetY + 1;
             return bitmap.Crop(offsetX, offsetY, width, height);
         }
-
-        private Bitmap BitmapFromBitmapSource(BitmapSource bitmap)
-        {
-            Bitmap bmp;
-            using (MemoryStream outStream = new MemoryStream())
-            {
-                BitmapEncoder enc = new BmpBitmapEncoder();
-                enc.Frames.Add(BitmapFrame.Create(bitmap));
-                enc.Save(outStream);
-                bmp = new Bitmap(outStream);
-            }
-            return bmp;
-        }
-
-        private Stream StreamFromBitmapSource(BitmapSource writeBmp)
+        
+        public WriteableBitmap GetFromClipboard()
         {
-            Stream bmp = new MemoryStream();
-
-            BitmapEncoder enc = new BmpBitmapEncoder();
-            enc.Frames.Add(BitmapFrame.Create(writeBmp));
-            enc.Save(bmp);
-
-            return bmp;
+            throw new NotImplementedException();
         }
     }
 }

+ 29 - 14
PixiEditor/Models/Tools/Tools/MoveTool.cs

@@ -3,6 +3,7 @@ using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Position;
 using PixiEditor.ViewModels;
+using System;
 using System.Linq;
 using System.Windows.Input;
 using System.Windows.Media;
@@ -18,6 +19,8 @@ namespace PixiEditor.Models.Tools.Tools
         private Coordinates _lastMouseMove;
         private Color[] _startPixelColors;
         private bool _clearedPixels = false;
+        private Coordinates[] _currentSelection;
+        private bool _updateViewModelSelection = true;
 
         public MoveTool()
         {
@@ -29,45 +32,57 @@ namespace PixiEditor.Models.Tools.Tools
 
         public override BitmapPixelChanges Use(Layer layer, Coordinates[] mouseMove, Color color)
         {
-            if (ViewModelMain.Current.ActiveSelection.SelectedPoints == null) return BitmapPixelChanges.Empty;
+            Coordinates start = mouseMove[^1];
+            if (_lastStartMousePos != start)
+            {
+                ResetSelectionValues(layer, start);
+                if (ViewModelMain.Current.ActiveSelection.SelectedPoints == null) //Move every pixel if none is selected
+                {
+                    SelectTool select = new SelectTool();
+                    _currentSelection = select.GetAllSelection();
+                    _updateViewModelSelection = false;
+                }
+                else
+                {
+                    _currentSelection = ViewModelMain.Current.ActiveSelection.SelectedPoints;
+                }
+                _startSelection = _currentSelection;
+                _startPixelColors = GetPixelsForSelection(layer, _startSelection);
+            }
+
             return MoveSelection(layer, mouseMove);
         }
 
         public BitmapPixelChanges MoveSelection(Layer layer, Coordinates[] mouseMove)
         {
-            Coordinates start = mouseMove[^1];
             Coordinates end = mouseMove[0];
 
-            if (_lastStartMousePos != start)
+            _currentSelection = TranslateSelection(end, out Coordinates[] previousSelection);
+            if (_updateViewModelSelection)
             {
-                ResetSelectionValues(layer, start);
+                ViewModelMain.Current.ActiveSelection = new Selection(_currentSelection);
             }
-
-            Coordinates[] previousSelection = TranslateSelection(end);
             ClearSelectedPixels(layer, previousSelection);
 
 
             _lastMouseMove = end;
             return BitmapPixelChanges.FromArrays(
-                        ViewModelMain.Current.ActiveSelection.SelectedPoints, _startPixelColors);
+                        _currentSelection, _startPixelColors);
         }
 
         private void ResetSelectionValues(Layer layer, Coordinates start)
         {
             _lastStartMousePos = start;
-            _startSelection = ViewModelMain.Current.ActiveSelection.SelectedPoints;
-            _startPixelColors = GetPixelsForSelection(layer, _startSelection);
             _lastMouseMove = start;
             _clearedPixels = false;
+            _updateViewModelSelection = true;
         }
 
-        private Coordinates[] TranslateSelection(Coordinates end)
+        private Coordinates[] TranslateSelection(Coordinates end, out Coordinates[] previousSelection)
         {
             Coordinates translation = ImageManipulation.Transform.GetTranslation(_lastMouseMove, end);
-            Coordinates[] previousSelection = ViewModelMain.Current.ActiveSelection.SelectedPoints.ToArray();
-            ViewModelMain.Current.ActiveSelection =
-                new Selection(ImageManipulation.Transform.Translate(previousSelection, translation));
-            return previousSelection;
+            previousSelection = _currentSelection.ToArray();            
+            return ImageManipulation.Transform.Translate(previousSelection, translation);
         }
 
         private void ClearSelectedPixels(Layer layer, Coordinates[] selection)

+ 5 - 0
PixiEditor/Models/Tools/Tools/RectangleTool.cs

@@ -55,6 +55,11 @@ namespace PixiEditor.Models.Tools.Tools
             return output.Distinct().ToArray();
         }
 
+        public Coordinates[] CreateRectangle(Coordinates start, Coordinates end, int thickness)
+        {
+            return CreateRectangle(new Coordinates[] { start, end }, thickness);
+        }
+
         private Coordinates[] CalculateRectanglePoints(DoubleCords coordinates)
         {
             List<Coordinates> finalCoordinates = new List<Coordinates>();

+ 34 - 3
PixiEditor/Models/Tools/Tools/SelectTool.cs

@@ -15,10 +15,41 @@ namespace PixiEditor.Models.Tools.Tools
 
         public override void Use(Coordinates[] pixels)
         {
-            RectangleTool rectangleTool = new RectangleTool();
-            List<Coordinates> selection = rectangleTool.CreateRectangle(pixels, 1).ToList();
-            selection.AddRange(rectangleTool.CalculateFillForRectangle(pixels[^1], pixels[0], 1));
+            Select(pixels);
+        }
+
+        private void Select(Coordinates[] pixels)
+        {
+            Coordinates[] selection = GetRectangleSelectionForPoints(pixels[^1], pixels[0]);
             ViewModelMain.Current.ActiveSelection = new DataHolders.Selection(selection.ToArray());
         }
+
+        public Coordinates[] GetRectangleSelectionForPoints(Coordinates start, Coordinates end)
+        {
+            RectangleTool rectangleTool = new RectangleTool();
+            List<Coordinates> selection = rectangleTool.CreateRectangle(start, end, 1).ToList();
+            selection.AddRange(rectangleTool.CalculateFillForRectangle(start, end, 1));
+            return selection.ToArray();
+        }
+
+        /// <summary>
+        /// Gets coordinates of every pixel in root layer
+        /// </summary>
+        /// <returns>Coordinates array of pixels</returns>
+        public Coordinates[] GetAllSelection()
+        {
+
+            return GetAllSelection(ViewModelMain.Current.BitmapManager.Layers[0]);
+        }
+
+        /// <summary>
+        /// Gets coordinates of every pixel in choosen layer
+        /// </summary>
+        /// <param name="layer"></param>
+        /// <returns>Coordinates array of pixels</returns>
+        public Coordinates[] GetAllSelection(Layer layer)
+        {
+            return GetRectangleSelectionForPoints(new Coordinates(0,0), new Coordinates(layer.Width - 1, layer.Height - 1));
+        }
     }
 }

+ 3 - 3
PixiEditor/ViewModels/ViewModelMain.cs

@@ -192,7 +192,8 @@ namespace PixiEditor.ViewModels
                     new Shortcut(Key.N, GenerateDrawAreaCommand, modifier: ModifierKeys.Control),
                     new Shortcut(Key.S, SaveFileCommand, "AsNew", ModifierKeys.Control | ModifierKeys.Shift),
                     new Shortcut(Key.D, DeselectCommand, modifier: ModifierKeys.Control),
-                    new Shortcut(Key.A, SelectAllCommand, modifier: ModifierKeys.Control)
+                    new Shortcut(Key.A, SelectAllCommand, modifier: ModifierKeys.Control),
+                    new Shortcut(Key.C, CopyCommand, modifier: ModifierKeys.Control)
                 }
             };
             UndoManager.SetMainRoot(this);
@@ -210,8 +211,7 @@ namespace PixiEditor.ViewModels
         public void SelectAll(object parameter)
         {
             SelectTool select = new SelectTool();
-            select.Use(new Coordinates[] {new Coordinates(0,0),
-                new Coordinates(BitmapManager.Layers[0].Width - 1, BitmapManager.Layers[0].Height - 1)});
+            select.Use(select.GetAllSelection());
         }
 
         private bool CanSelectAll(object property)

+ 1 - 1
PixiEditor/Views/MainWindow.xaml

@@ -70,7 +70,7 @@
                     <MenuItem Header="_Undo" InputGestureText="Ctrl+Z" Command="{Binding UndoCommand}"/>
                     <MenuItem Header="_Redo" InputGestureText="Ctrl+Y" Command="{Binding RedoCommand}"/>
                     <Separator/>
-                    <MenuItem Header="_Copy" Command="{Binding CopyCommand}"/>
+                    <MenuItem Header="_Copy" Command="{Binding CopyCommand}" InputGestureText="Ctrl+C"/>
                     <MenuItem Header="_Paste"/>
                 </MenuItem>
                 <MenuItem Header="_Select">