NodeGraph.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections.Immutable;
  2. using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
  3. using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
  4. using PixiEditor.ChangeableDocument.Rendering;
  5. namespace PixiEditor.ChangeableDocument.Changeables.Graph;
  6. public class NodeGraph : IReadOnlyNodeGraph, IDisposable
  7. {
  8. private ImmutableList<IReadOnlyNode>? cachedExecutionList;
  9. private readonly List<Node> _nodes = new();
  10. public IReadOnlyCollection<Node> Nodes => _nodes;
  11. public Node? OutputNode => CustomOutputNode ?? Nodes.OfType<OutputNode>().FirstOrDefault();
  12. public Node? CustomOutputNode { get; set; }
  13. IReadOnlyCollection<IReadOnlyNode> IReadOnlyNodeGraph.AllNodes => Nodes;
  14. IReadOnlyNode IReadOnlyNodeGraph.OutputNode => OutputNode;
  15. public void AddNode(Node node)
  16. {
  17. if (Nodes.Contains(node))
  18. {
  19. return;
  20. }
  21. node.ConnectionsChanged += ResetCache;
  22. _nodes.Add(node);
  23. ResetCache();
  24. }
  25. public void RemoveNode(Node node)
  26. {
  27. if (!Nodes.Contains(node))
  28. {
  29. return;
  30. }
  31. node.ConnectionsChanged -= ResetCache;
  32. _nodes.Remove(node);
  33. ResetCache();
  34. }
  35. public Queue<IReadOnlyNode> CalculateExecutionQueue(IReadOnlyNode outputNode)
  36. {
  37. return new Queue<IReadOnlyNode>(CalculateExecutionQueueInternal(outputNode));
  38. }
  39. private ImmutableList<IReadOnlyNode> CalculateExecutionQueueInternal(IReadOnlyNode outputNode)
  40. {
  41. return cachedExecutionList ??= GraphUtils.CalculateExecutionQueue(outputNode).ToImmutableList();
  42. }
  43. void IReadOnlyNodeGraph.AddNode(IReadOnlyNode node) => AddNode((Node)node);
  44. void IReadOnlyNodeGraph.RemoveNode(IReadOnlyNode node) => RemoveNode((Node)node);
  45. public void Dispose()
  46. {
  47. foreach (var node in Nodes)
  48. {
  49. node.Dispose();
  50. }
  51. }
  52. public bool TryTraverse(Action<IReadOnlyNode> action)
  53. {
  54. if(OutputNode == null) return false;
  55. var queue = CalculateExecutionQueueInternal(OutputNode);
  56. foreach (var node in queue)
  57. {
  58. action(node);
  59. }
  60. return true;
  61. }
  62. public void Execute(RenderContext context)
  63. {
  64. if (OutputNode == null) return;
  65. if(!CanExecute()) return;
  66. var queue = CalculateExecutionQueueInternal(OutputNode);
  67. foreach (var node in queue)
  68. {
  69. if (node is Node typedNode)
  70. {
  71. if(typedNode.IsDisposed) continue;
  72. typedNode.ExecuteInternal(context);
  73. }
  74. else
  75. {
  76. node.Execute(context);
  77. }
  78. }
  79. }
  80. private bool CanExecute()
  81. {
  82. foreach (var node in Nodes)
  83. {
  84. if (node.IsDisposed)
  85. {
  86. return false;
  87. }
  88. }
  89. return true;
  90. }
  91. private void ResetCache()
  92. {
  93. cachedExecutionList = null;
  94. }
  95. public int GetCacheHash()
  96. {
  97. HashCode hash = new();
  98. foreach (var node in Nodes)
  99. {
  100. hash.Add(node.GetCacheHash());
  101. }
  102. return hash.ToHashCode();
  103. }
  104. }