DrawingSurfaceLineOperation.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using ChunkyImageLib.DataHolders;
  2. using PixiEditor.DrawingApi.Core.ColorsImpl;
  3. using PixiEditor.DrawingApi.Core.Numerics;
  4. using PixiEditor.DrawingApi.Core.Surface;
  5. using PixiEditor.DrawingApi.Core.Surface.PaintImpl;
  6. namespace ChunkyImageLib.Operations;
  7. internal class DrawingSurfaceLineOperation : IDrawOperation
  8. {
  9. public bool IgnoreEmptyChunks => false;
  10. private Paint paint;
  11. private readonly VecI from;
  12. private readonly VecI to;
  13. public DrawingSurfaceLineOperation(VecI from, VecI to, StrokeCap strokeCap, float strokeWidth, Color color, BlendMode blendMode)
  14. {
  15. paint = new()
  16. {
  17. StrokeCap = strokeCap,
  18. StrokeWidth = strokeWidth,
  19. Color = color,
  20. Style = PaintStyle.Stroke,
  21. BlendMode = blendMode,
  22. };
  23. this.from = from;
  24. this.to = to;
  25. }
  26. public void DrawOnChunk(Chunk chunk, VecI chunkPos)
  27. {
  28. paint.IsAntiAliased = chunk.Resolution != ChunkResolution.Full;
  29. var surf = chunk.Surface.DrawingSurface;
  30. surf.Canvas.Save();
  31. surf.Canvas.Scale((float)chunk.Resolution.Multiplier());
  32. surf.Canvas.Translate(-chunkPos * ChunkyImage.FullChunkSize);
  33. surf.Canvas.DrawLine(from, to, paint);
  34. surf.Canvas.Restore();
  35. }
  36. public HashSet<VecI> FindAffectedChunks(VecI imageSize)
  37. {
  38. RectI bounds = RectI.FromTwoPoints(from, to).Inflate((int)Math.Ceiling(paint.StrokeWidth));
  39. return OperationHelper.FindChunksTouchingRectangle(bounds, ChunkyImage.FullChunkSize);
  40. }
  41. public IDrawOperation AsMirrored(int? verAxisX, int? horAxisY)
  42. {
  43. VecI newFrom = from;
  44. VecI newTo = to;
  45. if (verAxisX is not null)
  46. {
  47. newFrom = newFrom.ReflectX((int)verAxisX);
  48. newTo = newTo.ReflectX((int)verAxisX);
  49. }
  50. if (horAxisY is not null)
  51. {
  52. newFrom = newFrom.ReflectY((int)horAxisY);
  53. newTo = newTo.ReflectY((int)horAxisY);
  54. }
  55. return new DrawingSurfaceLineOperation(newFrom, newTo, paint.StrokeCap, paint.StrokeWidth, paint.Color, paint.BlendMode);
  56. }
  57. public void Dispose()
  58. {
  59. paint.Dispose();
  60. }
  61. }