FolderNode.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using PixiEditor.ChangeableDocument.Changeables.Animations;
  2. using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
  3. using PixiEditor.ChangeableDocument.Rendering;
  4. using PixiEditor.DrawingApi.Core;
  5. using PixiEditor.DrawingApi.Core.ColorsImpl;
  6. using PixiEditor.Numerics;
  7. namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
  8. [NodeInfo("Folder")]
  9. public class FolderNode : StructureNode, IReadOnlyFolderNode
  10. {
  11. public InputProperty<Texture?> Content { get; }
  12. public FolderNode()
  13. {
  14. Content = CreateInput<Texture?>("Content", "CONTENT", null);
  15. }
  16. public override Node CreateCopy() => new FolderNode { MemberName = MemberName };
  17. protected override Texture? OnExecute(RenderingContext context)
  18. {
  19. if(Background.Value == null && Content.Value == null)
  20. {
  21. Output.Value = null;
  22. return null;
  23. }
  24. if (!IsVisible.Value || Opacity.Value <= 0 || IsEmptyMask())
  25. {
  26. Output.Value = Background.Value;
  27. return Output.Value;
  28. }
  29. blendPaint.Color = new Color(255, 255, 255, 255);
  30. blendPaint.BlendMode = DrawingApi.Core.Surfaces.BlendMode.Src;
  31. VecI size = Content.Value?.Size ?? Background.Value?.Size ?? VecI.Zero;
  32. var outputWorkingSurface = RequestTexture(0, size);
  33. var filterlessWorkingSurface = RequestTexture(1, size);
  34. if (Background.Value != null)
  35. {
  36. DrawBackground(filterlessWorkingSurface, context);
  37. blendPaint.BlendMode = RenderingContext.GetDrawingBlendMode(BlendMode.Value);
  38. }
  39. if (Content.Value != null)
  40. {
  41. blendPaint.Color = blendPaint.Color.WithAlpha((byte)Math.Round(Opacity.Value * 255));
  42. DrawSurface(filterlessWorkingSurface, Content.Value, context, null);
  43. }
  44. FilterlessOutput.Value = filterlessWorkingSurface;
  45. if (!HasOperations())
  46. {
  47. if (Background.Value != null)
  48. {
  49. blendPaint.Color = new Color(255, 255, 255, 255);
  50. blendPaint.BlendMode = DrawingApi.Core.Surfaces.BlendMode.Src;
  51. DrawBackground(outputWorkingSurface, context);
  52. blendPaint.BlendMode = RenderingContext.GetDrawingBlendMode(BlendMode.Value);
  53. }
  54. if (Content.Value != null)
  55. {
  56. blendPaint.Color = blendPaint.Color.WithAlpha((byte)Math.Round(Opacity.Value * 255));
  57. DrawSurface(outputWorkingSurface, Content.Value, context, Filters.Value);
  58. }
  59. Output.Value = outputWorkingSurface;
  60. return Output.Value;
  61. }
  62. if (Content.Value != null)
  63. {
  64. DrawSurface(outputWorkingSurface, Content.Value, context, Filters.Value);
  65. ApplyMaskIfPresent(outputWorkingSurface, context);
  66. }
  67. if (Background.Value != null)
  68. {
  69. Texture tempSurface = RequestTexture(2, outputWorkingSurface.Size);
  70. DrawBackground(tempSurface, context);
  71. ApplyRasterClip(outputWorkingSurface, tempSurface);
  72. blendPaint.Color = blendPaint.Color.WithAlpha((byte)Math.Round(Opacity.Value * 255));
  73. blendPaint.BlendMode = RenderingContext.GetDrawingBlendMode(BlendMode.Value);
  74. tempSurface.DrawingSurface.Canvas.DrawSurface(outputWorkingSurface.DrawingSurface, 0, 0, blendPaint);
  75. Output.Value = tempSurface;
  76. return tempSurface;
  77. }
  78. Output.Value = outputWorkingSurface;
  79. return Output.Value;
  80. }
  81. public override RectI? GetTightBounds(KeyFrameTime frameTime)
  82. {
  83. RectI bounds = new RectI();
  84. if(Content.Connection != null)
  85. {
  86. Content.Connection.Node.TraverseBackwards((n) =>
  87. {
  88. if (n is ImageLayerNode imageLayerNode)
  89. {
  90. RectI? imageBounds = imageLayerNode.GetTightBounds(frameTime);
  91. if (imageBounds != null)
  92. {
  93. bounds = bounds.Union(imageBounds.Value);
  94. }
  95. }
  96. return true;
  97. });
  98. return bounds;
  99. }
  100. return RectI.Create(0, 0, Content.Value?.Size.X ?? 0, Content.Value?.Size.Y ?? 0);
  101. }
  102. public HashSet<Guid> GetLayerNodeGuids()
  103. {
  104. HashSet<Guid> guids = new();
  105. Content.Connection?.Node.TraverseBackwards((n) =>
  106. {
  107. if (n is ImageLayerNode imageLayerNode)
  108. {
  109. guids.Add(imageLayerNode.Id);
  110. }
  111. return true;
  112. });
  113. return guids;
  114. }
  115. /// <summary>
  116. /// Creates a clone of the folder, its mask and all of its children
  117. /// </summary>
  118. /*internal override Folder Clone()
  119. {
  120. var builder = ImmutableList<StructureMember>.Empty.ToBuilder();
  121. for (var i = 0; i < Children.Count; i++)
  122. {
  123. var child = Children[i];
  124. builder.Add(child.Clone());
  125. }
  126. return new Folder
  127. {
  128. GuidValue = GuidValue,
  129. IsVisible = IsVisible,
  130. Name = Name,
  131. Opacity = Opacity,
  132. Children = builder.ToImmutable(),
  133. Mask = Mask?.CloneFromCommitted(),
  134. BlendMode = BlendMode,
  135. ClipToMemberBelow = ClipToMemberBelow,
  136. MaskIsVisible = MaskIsVisible
  137. };
  138. }*/
  139. }