PixelOperation.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 PixelOperation : IMirroredDrawOperation
  8. {
  9. public bool IgnoreEmptyChunks => false;
  10. private readonly VecI pixel;
  11. private readonly Color color;
  12. private readonly BlendMode blendMode;
  13. private readonly Paint paint;
  14. public PixelOperation(VecI pixel, Color color, BlendMode blendMode)
  15. {
  16. this.pixel = pixel;
  17. this.color = color;
  18. this.blendMode = blendMode;
  19. paint = new Paint() { BlendMode = blendMode };
  20. }
  21. public void DrawOnChunk(Chunk chunk, VecI chunkPos)
  22. {
  23. // a hacky way to make the lines look slightly better on non full res chunks
  24. paint.Color = new Color(color.R, color.G, color.B, (byte)(color.A * chunk.Resolution.Multiplier()));
  25. DrawingSurface surf = chunk.Surface.DrawingSurface;
  26. surf.Canvas.Save();
  27. surf.Canvas.Scale((float)chunk.Resolution.Multiplier());
  28. surf.Canvas.Translate(-chunkPos * ChunkyImage.FullChunkSize);
  29. surf.Canvas.DrawPoint(pixel, paint);
  30. surf.Canvas.Restore();
  31. }
  32. public AffectedArea FindAffectedArea(VecI imageSize)
  33. {
  34. return new AffectedArea(new HashSet<VecI>() { OperationHelper.GetChunkPos(pixel, ChunkyImage.FullChunkSize) }, new RectI(pixel, VecI.One));
  35. }
  36. public IDrawOperation AsMirrored(double? verAxisX, double? horAxisY)
  37. {
  38. RectI pixelRect = new RectI(pixel, new VecI(1, 1));
  39. if (verAxisX is not null)
  40. pixelRect = (RectI)pixelRect.ReflectX((double)verAxisX).Round();
  41. if (horAxisY is not null)
  42. pixelRect = (RectI)pixelRect.ReflectY((double)horAxisY).Round();
  43. return new PixelOperation(pixelRect.Pos, color, blendMode);
  44. }
  45. public void Dispose()
  46. {
  47. paint.Dispose();
  48. }
  49. }