PixelsOperation.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.ComponentModel.DataAnnotations.Schema;
  2. using System.Linq;
  3. using ChunkyImageLib.DataHolders;
  4. using PixiEditor.DrawingApi.Core.ColorsImpl;
  5. using PixiEditor.DrawingApi.Core.Numerics;
  6. using PixiEditor.DrawingApi.Core.Surface;
  7. using PixiEditor.DrawingApi.Core.Surface.PaintImpl;
  8. namespace ChunkyImageLib.Operations;
  9. internal class PixelsOperation : IMirroredDrawOperation
  10. {
  11. public bool IgnoreEmptyChunks => false;
  12. private readonly Point[] pixels;
  13. private readonly Color color;
  14. private readonly BlendMode blendMode;
  15. private readonly Paint paint;
  16. public PixelsOperation(IEnumerable<VecI> pixels, Color color, BlendMode blendMode)
  17. {
  18. this.pixels = pixels.Select(pixel => new Point(pixel.X, pixel.Y)).ToArray();
  19. this.color = color;
  20. this.blendMode = blendMode;
  21. paint = new Paint() { BlendMode = blendMode };
  22. }
  23. public void DrawOnChunk(Chunk targetChunk, VecI chunkPos)
  24. {
  25. // a hacky way to make the lines look slightly better on non full res chunks
  26. paint.Color = new Color(color.R, color.G, color.B, (byte)(color.A * targetChunk.Resolution.Multiplier()));
  27. DrawingSurface surf = targetChunk.Surface.DrawingSurface;
  28. surf.Canvas.Save();
  29. surf.Canvas.Scale((float)targetChunk.Resolution.Multiplier());
  30. surf.Canvas.Translate(-chunkPos * ChunkyImage.FullChunkSize);
  31. surf.Canvas.DrawPoints(PointMode.Points, pixels, paint);
  32. surf.Canvas.Restore();
  33. }
  34. public AffectedArea FindAffectedArea(VecI imageSize)
  35. {
  36. HashSet<VecI> affectedChunks = new HashSet<VecI>();
  37. RectI? affectedArea = null;
  38. foreach (var pixel in pixels)
  39. {
  40. affectedChunks.Add(OperationHelper.GetChunkPos(pixel, ChunkyImage.FullChunkSize));
  41. if (affectedArea is null)
  42. affectedArea = new RectI(pixel, VecI.One);
  43. else
  44. affectedArea = affectedArea.Value.Union(new RectI(pixel, VecI.One));
  45. }
  46. return new AffectedArea(affectedChunks, affectedArea);
  47. }
  48. public IDrawOperation AsMirrored(double? verAxisX, double? horAxisY)
  49. {
  50. var arr = pixels.Select(pixel => new VecI(
  51. verAxisX is not null ? (int)Math.Round(2 * (double)verAxisX - (int)pixel.X - 1) : (int)pixel.X,
  52. horAxisY is not null ? (int)Math.Round(2 * (double)horAxisY - (int)pixel.Y - 1) : (int)pixel.Y
  53. ));
  54. return new PixelsOperation(arr, color, blendMode);
  55. }
  56. public void Dispose()
  57. {
  58. paint.Dispose();
  59. }
  60. }