Browse Source

Selection tool wip

flabbet 5 years ago
parent
commit
e8c8e25812

BIN
PixiEditor/Images/SelectImage.png


+ 4 - 4
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -135,15 +135,15 @@ namespace PixiEditor.Models.Controllers
         private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
         private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
         {
         {
             if (Mouse.LeftButton == MouseButtonState.Pressed)
             if (Mouse.LeftButton == MouseButtonState.Pressed)
-            {
+            {               
                 if (IsOperationTool(SelectedTool))
                 if (IsOperationTool(SelectedTool))
                 {
                 {
-                    BitmapOperations.TriggerAction(e.NewPosition,
-                        MouseController.LastMouseMoveCoordinates.ToList(), (BitmapOperationTool)SelectedTool);
+                    BitmapOperations.ExecuteTool(e.NewPosition,
+                        MouseController.LastMouseMoveCoordinates, (BitmapOperationTool)SelectedTool);
                 }
                 }
                 else
                 else
                 {
                 {
-                    ReadonlyToolUtility.ExecuteTool((ReadonlyTool)SelectedTool);
+                    ReadonlyToolUtility.ExecuteTool(MouseController.LastMouseMoveCoordinates.ToArray(), (ReadonlyTool)SelectedTool);
                 }
                 }
             }            
             }            
             else if(Mouse.LeftButton == MouseButtonState.Released)
             else if(Mouse.LeftButton == MouseButtonState.Released)

+ 1 - 1
PixiEditor/Models/Controllers/BitmapOperationsUtility.cs

@@ -28,7 +28,7 @@ namespace PixiEditor.Models.Controllers
             Manager = manager;
             Manager = manager;
         }
         }
 
 
-        public void TriggerAction(Coordinates newPos, List<Coordinates> mouseMove, BitmapOperationTool tool)
+        public void ExecuteTool(Coordinates newPos, List<Coordinates> mouseMove, BitmapOperationTool tool)
         {
         {
             if (tool != null && tool.ToolType != ToolType.None)
             if (tool != null && tool.ToolType != ToolType.None)
             {
             {

+ 5 - 5
PixiEditor/Models/Controllers/ReadonlyToolUtility.cs

@@ -1,7 +1,7 @@
-using PixiEditor.Models.Tools;
-using System;
+using PixiEditor.Models.Layers;
+using PixiEditor.Models.Position;
+using PixiEditor.Models.Tools;
 using System.Collections.Generic;
 using System.Collections.Generic;
-using System.Text;
 
 
 namespace PixiEditor.Models.Controllers
 namespace PixiEditor.Models.Controllers
 {
 {
@@ -14,9 +14,9 @@ namespace PixiEditor.Models.Controllers
             Manager = manager;
             Manager = manager;
         }
         }
 
 
-        public void ExecuteTool(ReadonlyTool tool)
+        public void ExecuteTool(Coordinates[] mouseMove, ReadonlyTool tool)
         {            
         {            
-            tool.Use();
+            tool.Use(mouseMove);
         }
         }
 
 
     }
     }

+ 30 - 0
PixiEditor/Models/DataHolders/Selection.cs

@@ -0,0 +1,30 @@
+using PixiEditor.Models.Position;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows;
+
+namespace PixiEditor.Models.DataHolders
+{
+    public class Selection
+    {
+        public Coordinates[] SelectedPoints { get; set; } = null;
+        public int VisualCanvasTop => SelectedPoints != null ? SelectedPoints.Min(x => x.Y) : -1;
+        public int VisualCanvasLeft => SelectedPoints != null ? SelectedPoints.Min(x => x.X) : -1;
+        public int VisualWidth => SelectedPoints != null ? Math.Abs(SelectedPoints[0].X - SelectedPoints[^1].X) : 0;
+
+        public int VisualHeight => SelectedPoints != null ? Math.Abs(SelectedPoints[0].Y - SelectedPoints[^1].Y) : 0;
+        public Visibility Visibility => SelectedPoints != null ? Visibility.Visible : Visibility.Collapsed;
+
+        public Selection()
+        {
+
+        }
+
+        public Selection(Coordinates[] selectedPoints)
+        {
+            SelectedPoints = selectedPoints;
+        }
+    }
+}

+ 4 - 2
PixiEditor/Models/Tools/ReadonlyTool.cs

@@ -1,4 +1,6 @@
-using System;
+using PixiEditor.Models.Layers;
+using PixiEditor.Models.Position;
+using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Text;
 using System.Text;
 
 
@@ -6,6 +8,6 @@ namespace PixiEditor.Models.Tools
 {
 {
     public abstract class ReadonlyTool : Tool
     public abstract class ReadonlyTool : Tool
     {
     {
-        public abstract void Use();
+        public abstract void Use(Coordinates[] pixels);
     }
     }
 }
 }

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

@@ -8,6 +8,6 @@ namespace PixiEditor.Models.Tools
 {
 {
     public enum ToolType
     public enum ToolType
     {
     {
-        None, Pen, Bucket, Line, Circle, Rectangle, Earser, Brightness, ColorPicker
+        None, Select, Pen, Bucket, Line, Circle, Rectangle, Earser, Brightness, ColorPicker
     }
     }
 }
 }

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

@@ -15,9 +15,10 @@ namespace PixiEditor.Models.Tools.Tools
         public ColorPickerTool()
         public ColorPickerTool()
         {
         {
             HideHighlight = true;
             HideHighlight = true;
+            Tooltip = "Swaps primary color with selected on canvas.";
         }
         }
 
 
-        public override void Use()
+        public override void Use(Coordinates[] coordinates)
         {
         {
             using (var bitmap = new System.Drawing.Bitmap(1, 1))
             using (var bitmap = new System.Drawing.Bitmap(1, 1))
             {
             {

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

@@ -72,7 +72,7 @@ namespace PixiEditor.Models.Tools.Tools
             return finalCoordinates.ToArray();
             return finalCoordinates.ToArray();
         }
         }
 
 
-        private Coordinates[] CalculateFillForRectangle(Coordinates start, Coordinates end, int thickness)
+        public Coordinates[] CalculateFillForRectangle(Coordinates start, Coordinates end, int thickness)
         {
         {
             int offset = (int)Math.Ceiling(thickness / 2f);
             int offset = (int)Math.Ceiling(thickness / 2f);
             DoubleCords fixedCords = CalculateCoordinatesForShapeRotation(start, end);
             DoubleCords fixedCords = CalculateCoordinatesForShapeRotation(start, end);

+ 23 - 0
PixiEditor/Models/Tools/Tools/SelectTool.cs

@@ -0,0 +1,23 @@
+using PixiEditor.Models.Layers;
+using PixiEditor.Models.Position;
+using PixiEditor.ViewModels;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace PixiEditor.Models.Tools.Tools
+{
+    public class SelectTool : ReadonlyTool
+    {
+        public override ToolType ToolType => ToolType.Select;
+
+        public override void Use(Coordinates[] pixels)
+        {
+            RectangleTool rectangleTool = new RectangleTool();
+            List<Coordinates> selection = rectangleTool.CreateRectangle(pixels, 1).ToList();
+            selection.AddRange(rectangleTool.CalculateFillForRectangle(selection[0], selection[^1], 1));
+            ViewModelMain.Current.ActiveSelection = new DataHolders.Selection(selection.ToArray());
+        }
+    }
+}

+ 2 - 0
PixiEditor/PixiEditor.csproj

@@ -17,6 +17,7 @@
 
 
   <ItemGroup>
   <ItemGroup>
     <None Remove="Images\PixiEditorLogo.png" />
     <None Remove="Images\PixiEditorLogo.png" />
+    <None Remove="Images\SelectImage.png" />
     <None Remove="Images\SwapArrows.png" />
     <None Remove="Images\SwapArrows.png" />
     <None Include="..\icon.ico">
     <None Include="..\icon.ico">
       <Pack>True</Pack>
       <Pack>True</Pack>
@@ -54,6 +55,7 @@
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Resource>
     </Resource>
     <Resource Include="Images\RectangleImage.png" />
     <Resource Include="Images\RectangleImage.png" />
+    <Resource Include="Images\SelectImage.png" />
     <Resource Include="Images\SwapArrows.png" />
     <Resource Include="Images\SwapArrows.png" />
     <Resource Include="Images\transparentbg.png" />
     <Resource Include="Images\transparentbg.png" />
   </ItemGroup>
   </ItemGroup>

+ 17 - 7
PixiEditor/ViewModels/ViewModelMain.cs

@@ -6,8 +6,7 @@ using System.Collections.Generic;
 using System.Windows;
 using System.Windows;
 using System.Windows.Input;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media;
-using System.Windows.Media.Imaging;
-using PixiTools = PixiEditor.Models.Tools.Tools;
+using System.Windows.Media.Imaging;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.IO;
 using PixiEditor.Models.IO;
@@ -15,6 +14,7 @@ using PixiEditor.Models.Position;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
 using System.Linq;
 using System.Linq;
 using System.Collections.ObjectModel;
 using System.Collections.ObjectModel;
+using PixiEditor.Models.Tools.Tools;
 
 
 namespace PixiEditor.ViewModels
 namespace PixiEditor.ViewModels
 {
 {
@@ -122,13 +122,23 @@ namespace PixiEditor.ViewModels
             }
             }
         }
         }
 
 
-
-
         public BitmapManager BitmapManager { 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; }
-
+        private Selection _selection = new Selection();
+
+        public Selection ActiveSelection
+        {
+            get => _selection;
+            set
+            {
+                _selection = value;
+                RaisePropertyChanged("ActiveSelection");
+            }
+        }
+
+
         public ViewModelMain()
         public ViewModelMain()
         {
         {
             PixiFilesManager.InitializeTempDirectories();
             PixiFilesManager.InitializeTempDirectories();
@@ -154,8 +164,8 @@ namespace PixiEditor.ViewModels
             SwapColorsCommand = new RelayCommand(SwapColors);
             SwapColorsCommand = new RelayCommand(SwapColors);
             KeyDownCommand = new RelayCommand(KeyDown);
             KeyDownCommand = new RelayCommand(KeyDown);
             RenameLayerCommand = new RelayCommand(RenameLayer);
             RenameLayerCommand = new RelayCommand(RenameLayer);
-            ToolSet = new ObservableCollection<Tool> { new PixiTools.PenTool(), new PixiTools.FloodFill(), new PixiTools.LineTool(),
-            new PixiTools.CircleTool(), new PixiTools.RectangleTool(), new PixiTools.EarserTool(), new PixiTools.ColorPickerTool(), new PixiTools.BrightnessTool() };
+            ToolSet = new ObservableCollection<Tool> { new SelectTool(), new PenTool(), new FloodFill(), new LineTool(),
+            new CircleTool(), new RectangleTool(), new EarserTool(), new ColorPickerTool(), new BrightnessTool()};
             ShortcutController = new ShortcutController
             ShortcutController = new ShortcutController
             {
             {
                 Shortcuts = new List<Shortcut> { 
                 Shortcuts = new List<Shortcut> { 

+ 3 - 0
PixiEditor/Views/MainWindow.xaml

@@ -134,6 +134,9 @@
                                     </DataTemplate>
                                     </DataTemplate>
                                 </ItemsControl.ItemTemplate>
                                 </ItemsControl.ItemTemplate>
                             </ItemsControl>
                             </ItemsControl>
+                            <Rectangle Stroke="#7F67B8FF" Height="{Binding ActiveSelection.VisualHeight}" Width="{Binding ActiveSelection.VisualWidth}" 
+                                       Canvas.Left="{Binding ActiveSelection.VisualCanvasLeft}" Canvas.Top="{Binding ActiveSelection.VisualCanvasTop}" Visibility="{Binding ActiveSelection.Visibility}" 
+                                       Fill="Transparent" StrokeThickness="1" SnapsToDevicePixels="True"/>
                         </Canvas>
                         </Canvas>
                     </vws:MainDrawingPanel.Item>
                     </vws:MainDrawingPanel.Item>
                 </vws:MainDrawingPanel>
                 </vws:MainDrawingPanel>