NodeViewModel.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System.Collections.ObjectModel;
  2. using Avalonia;
  3. using ChunkyImageLib;
  4. using CommunityToolkit.Mvvm.ComponentModel;
  5. using PixiEditor.AvaloniaUI.Models.DocumentModels;
  6. using PixiEditor.AvaloniaUI.Models.Handlers;
  7. using PixiEditor.AvaloniaUI.Models.Structures;
  8. using PixiEditor.AvaloniaUI.ViewModels.Document;
  9. using PixiEditor.ChangeableDocument.Actions.Generated;
  10. using PixiEditor.ChangeableDocument.Changeables.Interfaces;
  11. using PixiEditor.Numerics;
  12. namespace PixiEditor.AvaloniaUI.ViewModels.Nodes;
  13. internal class NodeViewModel : ObservableObject, INodeHandler
  14. {
  15. private string nodeName;
  16. private VecD position;
  17. private ObservableRangeCollection<INodePropertyHandler> inputs = new();
  18. private ObservableRangeCollection<INodePropertyHandler> outputs = new();
  19. private Surface resultPreview;
  20. private bool isSelected;
  21. protected Guid id;
  22. public Guid Id
  23. {
  24. get => id;
  25. init => id = value;
  26. }
  27. public string NodeName
  28. {
  29. get => nodeName;
  30. set => SetProperty(ref nodeName, value);
  31. }
  32. public VecD PositionBindable
  33. {
  34. get => position;
  35. set
  36. {
  37. if (!Document.UpdateableChangeActive)
  38. {
  39. Internals.ActionAccumulator.AddFinishedActions(
  40. new NodePosition_Action(Id, value),
  41. new EndNodePosition_Action());
  42. }
  43. }
  44. }
  45. public ObservableRangeCollection<INodePropertyHandler> Inputs
  46. {
  47. get => inputs;
  48. set => SetProperty(ref inputs, value);
  49. }
  50. public ObservableRangeCollection<INodePropertyHandler> Outputs
  51. {
  52. get => outputs;
  53. set => SetProperty(ref outputs, value);
  54. }
  55. public Surface ResultPreview
  56. {
  57. get => resultPreview;
  58. set => SetProperty(ref resultPreview, value);
  59. }
  60. public bool IsSelected
  61. {
  62. get => isSelected;
  63. set => SetProperty(ref isSelected, value);
  64. }
  65. internal DocumentViewModel Document { get; init; }
  66. internal DocumentInternalParts Internals { get; init; }
  67. public NodeViewModel()
  68. {
  69. }
  70. public NodeViewModel(string nodeName, Guid id, VecD position, DocumentViewModel document, DocumentInternalParts internals)
  71. {
  72. this.nodeName = nodeName;
  73. this.id = id;
  74. this.position = position;
  75. Document = document;
  76. Internals = internals;
  77. }
  78. public void SetPosition(VecD newPosition)
  79. {
  80. position = newPosition;
  81. OnPropertyChanged(nameof(PositionBindable));
  82. }
  83. public void TraverseBackwards(Func<INodeHandler, bool> func)
  84. {
  85. var visited = new HashSet<INodeHandler>();
  86. var queueNodes = new Queue<INodeHandler>();
  87. queueNodes.Enqueue(this);
  88. while (queueNodes.Count > 0)
  89. {
  90. var node = queueNodes.Dequeue();
  91. if (!visited.Add(node))
  92. {
  93. continue;
  94. }
  95. if (!func(node))
  96. {
  97. return;
  98. }
  99. foreach (var inputProperty in node.Inputs)
  100. {
  101. if (inputProperty.ConnectedOutput != null)
  102. {
  103. queueNodes.Enqueue(inputProperty.ConnectedOutput.Node);
  104. }
  105. }
  106. }
  107. }
  108. public void TraverseForwards(Func<INodeHandler, bool> func)
  109. {
  110. var visited = new HashSet<INodeHandler>();
  111. var queueNodes = new Queue<INodeHandler>();
  112. queueNodes.Enqueue(this);
  113. while (queueNodes.Count > 0)
  114. {
  115. var node = queueNodes.Dequeue();
  116. if (!visited.Add(node))
  117. {
  118. continue;
  119. }
  120. if (!func(node))
  121. {
  122. return;
  123. }
  124. foreach (var outputProperty in node.Outputs)
  125. {
  126. foreach (var connection in outputProperty.ConnectedInputs)
  127. {
  128. queueNodes.Enqueue(connection.Node);
  129. }
  130. }
  131. }
  132. }
  133. public NodePropertyViewModel FindInputProperty(string propName)
  134. {
  135. return Inputs.FirstOrDefault(x => x.PropertyName == propName) as NodePropertyViewModel;
  136. }
  137. public NodePropertyViewModel FindOutputProperty(string propName)
  138. {
  139. return Outputs.FirstOrDefault(x => x.PropertyName == propName) as NodePropertyViewModel;
  140. }
  141. }