VectorLayerNode.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using ChunkyImageLib.Operations;
  2. using PixiEditor.ChangeableDocument.Changeables.Animations;
  3. using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
  4. using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Shapes.Data;
  5. using PixiEditor.ChangeableDocument.Changeables.Interfaces;
  6. using PixiEditor.ChangeableDocument.ChangeInfos.Vectors;
  7. using PixiEditor.ChangeableDocument.Rendering;
  8. using PixiEditor.DrawingApi.Core;
  9. using PixiEditor.DrawingApi.Core.Numerics;
  10. using PixiEditor.DrawingApi.Core.Surfaces;
  11. using PixiEditor.DrawingApi.Core.Surfaces.PaintImpl;
  12. using PixiEditor.Numerics;
  13. namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
  14. [NodeInfo("VectorLayer")]
  15. public class VectorLayerNode : LayerNode, ITransformableObject, IReadOnlyVectorNode, IRasterizable
  16. {
  17. public Matrix3X3 TransformationMatrix
  18. {
  19. get => ShapeData?.TransformationMatrix ?? Matrix3X3.Identity;
  20. set
  21. {
  22. if (ShapeData == null)
  23. {
  24. return;
  25. }
  26. ShapeData.TransformationMatrix = value;
  27. }
  28. }
  29. public ShapeVectorData? ShapeData { get; set; }
  30. IReadOnlyShapeVectorData IReadOnlyVectorNode.ShapeData => ShapeData;
  31. protected override bool AffectedByChunkResolution => true;
  32. private int lastCacheHash;
  33. protected override void OnExecute(RenderContext context)
  34. {
  35. base.OnExecute(context);
  36. Output.Value = context.TargetSurface;
  37. }
  38. public override VecD ScenePosition => ShapeData?.TransformedAABB.TopLeft ?? VecD.Zero;
  39. public override VecD SceneSize => ShapeData?.TransformedAABB.Size ?? VecD.Zero;
  40. public override void Render(SceneObjectRenderContext sceneContext)
  41. {
  42. Rasterize(sceneContext.TargetSurface, ChunkResolution.Full, blendPaint);
  43. }
  44. protected override VecI GetTargetSize(RenderContext ctx)
  45. {
  46. return ctx.DocumentSize;
  47. }
  48. protected override void DrawWithoutFilters(SceneObjectRenderContext ctx, DrawingSurface workingSurface, bool shouldClear,
  49. Paint paint)
  50. {
  51. if (ShapeData == null)
  52. {
  53. return;
  54. }
  55. if (shouldClear)
  56. {
  57. workingSurface.Canvas.Clear();
  58. }
  59. Rasterize(workingSurface, ctx.ChunkResolution, paint);
  60. }
  61. protected override void DrawWithFilters(SceneObjectRenderContext ctx, DrawingSurface workingSurface, bool shouldClear, Paint paint)
  62. {
  63. if (ShapeData == null)
  64. {
  65. return;
  66. }
  67. if (shouldClear)
  68. {
  69. workingSurface.Canvas.Clear();
  70. }
  71. Rasterize(workingSurface, ctx.ChunkResolution, paint);
  72. }
  73. public override bool RenderPreview(Texture renderOn, VecI chunk, ChunkResolution resolution, int frame)
  74. {
  75. if (ShapeData == null)
  76. {
  77. return false;
  78. }
  79. using var paint = new Paint();
  80. VecI tightBoundsSize = (VecI)ShapeData.TransformedAABB.Size;
  81. VecI translation = new VecI(
  82. (int)Math.Max(ShapeData.TransformedAABB.TopLeft.X, 0),
  83. (int)Math.Max(ShapeData.TransformedAABB.TopLeft.Y, 0));
  84. VecI size = tightBoundsSize + translation;
  85. if (size.X == 0 || size.Y == 0)
  86. {
  87. return false;
  88. }
  89. using Texture toRasterizeOn = new(size);
  90. int save = toRasterizeOn.DrawingSurface.Canvas.Save();
  91. Matrix3X3 matrix = ShapeData.TransformationMatrix;
  92. Rasterize(toRasterizeOn.DrawingSurface, resolution, paint);
  93. renderOn.DrawingSurface.Canvas.DrawSurface(toRasterizeOn.DrawingSurface, 0, 0, paint);
  94. toRasterizeOn.DrawingSurface.Canvas.RestoreToCount(save);
  95. return true;
  96. }
  97. public override void SerializeAdditionalData(Dictionary<string, object> additionalData)
  98. {
  99. base.SerializeAdditionalData(additionalData);
  100. additionalData["ShapeData"] = ShapeData;
  101. }
  102. internal override OneOf<None, IChangeInfo, List<IChangeInfo>> DeserializeAdditionalData(IReadOnlyDocument target,
  103. IReadOnlyDictionary<string, object> data)
  104. {
  105. base.DeserializeAdditionalData(target, data);
  106. ShapeData = (ShapeVectorData)data["ShapeData"];
  107. var affected = new AffectedArea(OperationHelper.FindChunksTouchingRectangle(
  108. (RectI)ShapeData.TransformedAABB, ChunkyImage.FullChunkSize));
  109. return new VectorShape_ChangeInfo(Id, affected);
  110. }
  111. protected override bool CacheChanged(RenderContext context)
  112. {
  113. return base.CacheChanged(context) || (ShapeData?.GetCacheHash() ?? -1) != lastCacheHash;
  114. }
  115. protected override void UpdateCache(RenderContext context)
  116. {
  117. base.UpdateCache(context);
  118. lastCacheHash = ShapeData?.GetCacheHash() ?? -1;
  119. }
  120. public override RectD? GetTightBounds(KeyFrameTime frameTime)
  121. {
  122. return ShapeData?.TransformedAABB ?? null;
  123. }
  124. public override ShapeCorners GetTransformationCorners(KeyFrameTime frameTime)
  125. {
  126. return ShapeData?.TransformationCorners ?? new ShapeCorners();
  127. }
  128. public void Rasterize(DrawingSurface surface, ChunkResolution resolution, Paint paint)
  129. {
  130. ShapeData?.RasterizeTransformed(surface, resolution, paint);
  131. }
  132. public override Node CreateCopy()
  133. {
  134. return new VectorLayerNode() { ShapeData = (ShapeVectorData?)ShapeData?.Clone(), };
  135. }
  136. }