ClearPathOperation.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using ChunkyImageLib.DataHolders;
  2. using PixiEditor.DrawingApi.Core.Numerics;
  3. using PixiEditor.DrawingApi.Core.Surface.Vector;
  4. namespace ChunkyImageLib.Operations;
  5. internal class ClearPathOperation : IMirroredDrawOperation
  6. {
  7. private VectorPath path;
  8. private RectI pathTightBounds;
  9. public bool IgnoreEmptyChunks => true;
  10. public ClearPathOperation(VectorPath path, RectI? pathTightBounds = null)
  11. {
  12. this.path = new VectorPath(path);
  13. this.pathTightBounds = (pathTightBounds ?? (RectI)path.TightBounds);
  14. }
  15. public void DrawOnChunk(Chunk chunk, VecI chunkPos)
  16. {
  17. chunk.Surface.DrawingSurface.Canvas.Save();
  18. using VectorPath transformedPath = new(path);
  19. float scale = (float)chunk.Resolution.Multiplier();
  20. VecD trans = -chunkPos * ChunkyImage.FullChunkSize * scale;
  21. transformedPath.Transform(Matrix3X3.CreateScaleTranslation(scale, scale, (float)trans.X, (float)trans.Y));
  22. chunk.Surface.DrawingSurface.Canvas.ClipPath(transformedPath);
  23. chunk.Surface.DrawingSurface.Canvas.Clear();
  24. chunk.Surface.DrawingSurface.Canvas.Restore();
  25. }
  26. public AffectedArea FindAffectedArea(VecI imageSize)
  27. {
  28. return new AffectedArea(OperationHelper.FindChunksTouchingRectangle(pathTightBounds, ChunkPool.FullChunkSize), pathTightBounds);
  29. }
  30. public void Dispose()
  31. {
  32. path.Dispose();
  33. }
  34. public IDrawOperation AsMirrored(double? verAxisX, double? horAxisY)
  35. {
  36. var matrix = Matrix3X3.CreateScale(verAxisX is not null ? -1 : 1, horAxisY is not null ? -1 : 1, (float?)verAxisX ?? 0, (float?)horAxisY ?? 0);
  37. using var copy = new VectorPath(path);
  38. copy.Transform(matrix);
  39. var newRect = pathTightBounds;
  40. if (verAxisX is not null)
  41. newRect = (RectI)newRect.ReflectX((double)verAxisX).Round();
  42. if (horAxisY is not null)
  43. newRect = (RectI)newRect.ReflectY((double)horAxisY).Round();
  44. return new ClearPathOperation(copy, newRect);
  45. }
  46. }