BresenhamLineOperation.cs 2.4 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 BresenhamLineOperation : IMirroredDrawOperation
  8. {
  9. public bool IgnoreEmptyChunks => false;
  10. private readonly VecI from;
  11. private readonly VecI to;
  12. private readonly Color color;
  13. private readonly BlendMode blendMode;
  14. private readonly Point[] points;
  15. private Paint paint;
  16. public BresenhamLineOperation(VecI from, VecI to, Color color, BlendMode blendMode)
  17. {
  18. this.from = from;
  19. this.to = to;
  20. this.color = color;
  21. this.blendMode = blendMode;
  22. paint = new Paint() { BlendMode = blendMode };
  23. points = BresenhamLineHelper.GetBresenhamLine(from, to);
  24. }
  25. public void DrawOnChunk(Chunk chunk, VecI chunkPos)
  26. {
  27. // a hacky way to make the lines look slightly better on non full res chunks
  28. paint.Color = new Color(color.R, color.G, color.B, (byte)(color.A * chunk.Resolution.Multiplier()));
  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.DrawPoints(PointMode.Points, points, paint);
  34. surf.Canvas.Restore();
  35. }
  36. public AffectedArea FindAffectedArea(VecI imageSize)
  37. {
  38. RectI bounds = RectI.FromTwoPixels(from, to);
  39. return new AffectedArea(OperationHelper.FindChunksTouchingRectangle(bounds, ChunkyImage.FullChunkSize), bounds);
  40. }
  41. public IDrawOperation AsMirrored(double? verAxisX, double? horAxisY)
  42. {
  43. RectI newFrom = new RectI(from, new VecI(1));
  44. RectI newTo = new RectI(to, new VecI(1));
  45. if (verAxisX is not null)
  46. {
  47. newFrom = (RectI)newFrom.ReflectX((double)verAxisX).Round();
  48. newTo = (RectI)newTo.ReflectX((double)verAxisX).Round();
  49. }
  50. if (horAxisY is not null)
  51. {
  52. newFrom = (RectI)newFrom.ReflectY((double)horAxisY).Round();
  53. newTo = (RectI)newTo.ReflectY((double)horAxisY).Round();
  54. }
  55. return new BresenhamLineOperation(newFrom.Pos, newTo.Pos, color, blendMode);
  56. }
  57. public void Dispose()
  58. {
  59. paint.Dispose();
  60. }
  61. }