Selection.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using PixiEditor.Helpers;
  2. using PixiEditor.Models.Enums;
  3. using PixiEditor.Models.Layers;
  4. using PixiEditor.Models.Position;
  5. using SkiaSharp;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Diagnostics;
  9. using System.Linq;
  10. namespace PixiEditor.Models.DataHolders
  11. {
  12. [DebuggerDisplay("{SelectedPoints.Count} selected Pixels")]
  13. public class Selection : NotifyableObject
  14. {
  15. private readonly SKColor selectionBlue;
  16. private Layer selectionLayer;
  17. public Selection(Coordinates[] selectedPoints)
  18. {
  19. SelectedPoints = new ObservableCollection<Coordinates>(selectedPoints);
  20. SelectionLayer = new Layer("_selectionLayer");
  21. selectionBlue = new SKColor(142, 202, 255, 127);
  22. }
  23. public ObservableCollection<Coordinates> SelectedPoints { get; private set; }
  24. public Layer SelectionLayer
  25. {
  26. get => selectionLayer;
  27. set
  28. {
  29. selectionLayer = value;
  30. RaisePropertyChanged(nameof(SelectionLayer));
  31. }
  32. }
  33. public void SetSelection(IEnumerable<Coordinates> selection, SelectionType mode)
  34. {
  35. SKColor selectionColor = selectionBlue;
  36. switch (mode)
  37. {
  38. case SelectionType.New:
  39. SelectedPoints = new ObservableCollection<Coordinates>(selection);
  40. SelectionLayer.Clear();
  41. break;
  42. case SelectionType.Add:
  43. SelectedPoints = new ObservableCollection<Coordinates>(SelectedPoints.Concat(selection).Distinct());
  44. break;
  45. case SelectionType.Subtract:
  46. SelectedPoints = new ObservableCollection<Coordinates>(SelectedPoints.Except(selection));
  47. selectionColor = SKColors.Transparent;
  48. break;
  49. }
  50. SelectionLayer.SetPixels(BitmapPixelChanges.FromSingleColoredArray(selection, selectionColor));
  51. }
  52. public void Clear()
  53. {
  54. SelectionLayer.Clear();
  55. SelectedPoints.Clear();
  56. }
  57. }
  58. }