SelectTool.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using PixiEditor.Models.Controllers;
  7. using PixiEditor.Models.DataHolders;
  8. using PixiEditor.Models.Enums;
  9. using PixiEditor.Models.Position;
  10. using PixiEditor.Models.Tools.ToolSettings.Settings;
  11. using PixiEditor.Models.Tools.ToolSettings.Toolbars;
  12. using PixiEditor.ViewModels;
  13. namespace PixiEditor.Models.Tools.Tools
  14. {
  15. public class SelectTool : ReadonlyTool
  16. {
  17. public override ToolType ToolType => ToolType.Select;
  18. private Selection _oldSelection;
  19. public SelectionType SelectionType = SelectionType.Add;
  20. public SelectTool()
  21. {
  22. Tooltip = "Selects area. (M)";
  23. Toolbar = new SelectToolToolbar();
  24. }
  25. public override void OnRecordingLeftMouseDown(MouseEventArgs e)
  26. {
  27. Enum.TryParse((Toolbar.GetSetting<DropdownSetting>("Mode")?.Value as ComboBoxItem)?.Content as string, out SelectionType);
  28. _oldSelection = null;
  29. if (ViewModelMain.Current.ActiveSelection != null &&
  30. ViewModelMain.Current.ActiveSelection.SelectedPoints != null)
  31. _oldSelection = ViewModelMain.Current.ActiveSelection;
  32. }
  33. public override void OnStoppedRecordingMouseUp(MouseEventArgs e)
  34. {
  35. if (ViewModelMain.Current.ActiveSelection.SelectedPoints.Count() <= 1)
  36. {
  37. // If we have not selected multiple points, clear the selection
  38. ViewModelMain.Current.ActiveSelection.Clear();
  39. }
  40. UndoManager.AddUndoChange(new Change("ActiveSelection", _oldSelection,
  41. ViewModelMain.Current.ActiveSelection, "Select pixels"));
  42. }
  43. public override void Use(Coordinates[] pixels)
  44. {
  45. Select(pixels);
  46. }
  47. private void Select(Coordinates[] pixels)
  48. {
  49. IEnumerable<Coordinates> selection = GetRectangleSelectionForPoints(pixels[^1], pixels[0]);
  50. ViewModelMain.Current.ActiveSelection.SetSelection(selection, SelectionType);
  51. }
  52. public IEnumerable<Coordinates> GetRectangleSelectionForPoints(Coordinates start, Coordinates end)
  53. {
  54. RectangleTool rectangleTool = new RectangleTool();
  55. List<Coordinates> selection = rectangleTool.CreateRectangle(start, end, 1).ToList();
  56. selection.AddRange(rectangleTool.CalculateFillForRectangle(start, end, 1));
  57. return selection;
  58. }
  59. /// <summary>
  60. /// Gets coordinates of every pixel in root layer
  61. /// </summary>
  62. /// <returns>Coordinates array of pixels</returns>
  63. public IEnumerable<Coordinates> GetAllSelection()
  64. {
  65. return GetAllSelection(ViewModelMain.Current.BitmapManager.ActiveDocument);
  66. }
  67. /// <summary>
  68. /// Gets coordinates of every pixel in chosen document
  69. /// </summary>
  70. /// <param name="document"></param>
  71. /// <returns>Coordinates array of pixels</returns>
  72. public IEnumerable<Coordinates> GetAllSelection(Document document)
  73. {
  74. return GetRectangleSelectionForPoints(new Coordinates(0, 0), new Coordinates(document.Width - 1, document.Height - 1));
  75. }
  76. }
  77. }