2
0

TransformSelectionPath_UpdateableChange.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using ChunkyImageLib.Operations;
  2. using SkiaSharp;
  3. namespace PixiEditor.ChangeableDocument.Changes.Selection;
  4. internal class TransformSelectionPath_UpdateableChange : UpdateableChange
  5. {
  6. private SKPath? originalPath;
  7. private ShapeCorners originalCorners;
  8. private ShapeCorners newCorners;
  9. [GenerateUpdateableChangeActions]
  10. public TransformSelectionPath_UpdateableChange(ShapeCorners corners)
  11. {
  12. this.newCorners = corners;
  13. }
  14. [UpdateChangeMethod]
  15. public void Update(ShapeCorners corners)
  16. {
  17. this.newCorners = corners;
  18. }
  19. public override OneOf<Success, Error> InitializeAndValidate(Document target)
  20. {
  21. if (target.Selection.SelectionPath.IsEmpty)
  22. return new Error();
  23. originalPath = new(target.Selection.SelectionPath);
  24. var bounds = originalPath.TightBounds;
  25. originalCorners = new(bounds);
  26. return new Success();
  27. }
  28. private Selection_ChangeInfo CommonApply(Document target)
  29. {
  30. SKPath newPath = new(originalPath);
  31. var matrix = SKMatrix.CreateTranslation((float)-originalCorners.TopLeft.X, (float)-originalCorners.TopLeft.Y).PostConcat(
  32. OperationHelper.CreateMatrixFromPoints(newCorners, originalCorners.RectSize));
  33. newPath.Transform(matrix);
  34. var toDispose = target.Selection.SelectionPath;
  35. target.Selection.SelectionPath = newPath;
  36. toDispose.Dispose();
  37. return new Selection_ChangeInfo(new SKPath(target.Selection.SelectionPath));
  38. }
  39. public override OneOf<None, IChangeInfo, List<IChangeInfo>> Apply(Document target, out bool ignoreInUndo)
  40. {
  41. ignoreInUndo = false;
  42. return CommonApply(target);
  43. }
  44. public override OneOf<None, IChangeInfo, List<IChangeInfo>> ApplyTemporarily(Document target)
  45. {
  46. return CommonApply(target);
  47. }
  48. public override OneOf<None, IChangeInfo, List<IChangeInfo>> Revert(Document target)
  49. {
  50. var toDispose = target.Selection.SelectionPath;
  51. target.Selection.SelectionPath = new SKPath(originalPath);
  52. toDispose.Dispose();
  53. return new Selection_ChangeInfo(new SKPath(target.Selection.SelectionPath));
  54. }
  55. public override void Dispose()
  56. {
  57. originalPath?.Dispose();
  58. }
  59. }