DrawingSurfaceLineOperation.cs 2.1 KB

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