DocumentEvaluator.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
  2. using PixiEditor.Numerics;
  3. namespace PixiEditor.ChangeableDocument.Rendering;
  4. public static class DocumentEvaluator
  5. {
  6. public static OneOf<Chunk, EmptyChunk> RenderChunk(VecI chunkPos, ChunkResolution resolution,
  7. IReadOnlyNodeGraph graph, int frame, RectI? globalClippingRect = null)
  8. {
  9. using RenderingContext context = new();
  10. try
  11. {
  12. RectI? transformedClippingRect = TransformClipRect(globalClippingRect, resolution, chunkPos);
  13. ChunkyImage? evaluated = graph.Execute(frame);
  14. if (evaluated is null)
  15. {
  16. return new EmptyChunk();
  17. }
  18. Chunk chunk = Chunk.Create(resolution);
  19. chunk.Surface.DrawingSurface.Canvas.Save();
  20. chunk.Surface.DrawingSurface.Canvas.Clear();
  21. if (transformedClippingRect is not null)
  22. {
  23. chunk.Surface.DrawingSurface.Canvas.ClipRect((RectD)transformedClippingRect);
  24. }
  25. evaluated.DrawMostUpToDateChunkOn(chunkPos, resolution, chunk.Surface.DrawingSurface, VecI.Zero,
  26. context.ReplacingPaintWithOpacity);
  27. chunk.Surface.DrawingSurface.Canvas.Restore();
  28. return chunk;
  29. }
  30. catch (ObjectDisposedException)
  31. {
  32. return new EmptyChunk();
  33. }
  34. }
  35. private static RectI? TransformClipRect(RectI? globalClippingRect, ChunkResolution resolution, VecI chunkPos)
  36. {
  37. if (globalClippingRect is not RectI rect)
  38. return null;
  39. double multiplier = resolution.Multiplier();
  40. VecI pixelChunkPos = chunkPos * (int)(ChunkyImage.FullChunkSize * multiplier);
  41. return (RectI?)rect.Scale(multiplier).Translate(-pixelChunkPos).RoundOutwards();
  42. }
  43. }