SelectRectangle_UpdateableChange.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using PixiEditor.ChangeableDocument.Enums;
  2. using SkiaSharp;
  3. namespace PixiEditor.ChangeableDocument.Changes.Selection;
  4. internal class SelectRectangle_UpdateableChange : UpdateableChange
  5. {
  6. private SKPath? originalPath;
  7. private RectI rect;
  8. private readonly SelectionMode mode;
  9. [GenerateUpdateableChangeActions]
  10. public SelectRectangle_UpdateableChange(RectI rect, SelectionMode mode)
  11. {
  12. this.rect = rect;
  13. this.mode = mode;
  14. }
  15. public override OneOf<Success, Error> InitializeAndValidate(Document target)
  16. {
  17. originalPath = new SKPath(target.Selection.SelectionPath);
  18. return new Success();
  19. }
  20. [UpdateChangeMethod]
  21. public void Update(RectI rect)
  22. {
  23. this.rect = rect;
  24. }
  25. private Selection_ChangeInfo CommonApply(Document target)
  26. {
  27. using var rectPath = new SKPath() { FillType = SKPathFillType.EvenOdd };
  28. rectPath.MoveTo(rect.TopLeft);
  29. rectPath.LineTo(rect.TopRight);
  30. rectPath.LineTo(rect.BottomRight);
  31. rectPath.LineTo(rect.BottomLeft);
  32. rectPath.Close();
  33. var toDispose = target.Selection.SelectionPath;
  34. if (mode == SelectionMode.New)
  35. target.Selection.SelectionPath = new(rectPath);
  36. else
  37. target.Selection.SelectionPath = originalPath!.Op(rectPath, mode.ToSKPathOp());
  38. toDispose.Dispose();
  39. return new Selection_ChangeInfo(new SKPath(target.Selection.SelectionPath));
  40. }
  41. public override OneOf<None, IChangeInfo, List<IChangeInfo>> ApplyTemporarily(Document target)
  42. {
  43. return CommonApply(target);
  44. }
  45. public override OneOf<None, IChangeInfo, List<IChangeInfo>> Apply(Document target, bool firstApply, out bool ignoreInUndo)
  46. {
  47. var changes = CommonApply(target);
  48. ignoreInUndo = false;
  49. return changes;
  50. }
  51. public override OneOf<None, IChangeInfo, List<IChangeInfo>> Revert(Document target)
  52. {
  53. (var toDispose, target.Selection.SelectionPath) = (target.Selection.SelectionPath, new SKPath(originalPath));
  54. toDispose.Dispose();
  55. return new Selection_ChangeInfo(new SKPath(target.Selection.SelectionPath));
  56. }
  57. public override void Dispose()
  58. {
  59. originalPath?.Dispose();
  60. }
  61. }