NodeGraphViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. using System.Collections.ObjectModel;
  2. using System.Reflection;
  3. using Avalonia.Input;
  4. using PixiEditor.Models.Commands.Attributes.Commands;
  5. using PixiEditor.ChangeableDocument.Actions;
  6. using PixiEditor.ChangeableDocument.Actions.Generated;
  7. using PixiEditor.ChangeableDocument.Changeables.Graph;
  8. using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
  9. using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
  10. using PixiEditor.ChangeableDocument.ChangeInfos;
  11. using PixiEditor.ChangeableDocument.ChangeInfos.NodeGraph;
  12. using PixiEditor.Models.DocumentModels;
  13. using PixiEditor.Models.Handlers;
  14. using Drawie.Numerics;
  15. using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Workspace;
  16. using PixiEditor.ViewModels.Nodes;
  17. namespace PixiEditor.ViewModels.Document;
  18. internal class NodeGraphViewModel : ViewModelBase, INodeGraphHandler, IDisposable
  19. {
  20. public DocumentViewModel DocumentViewModel { get; }
  21. public ObservableCollection<INodeHandler> AllNodes { get; } = new();
  22. public ObservableCollection<NodeConnectionViewModel> Connections { get; } = new();
  23. public ObservableCollection<NodeFrameViewModelBase> Frames { get; } = new();
  24. public ObservableCollection<string> AvailableRenderOutputs { get; } = new();
  25. public StructureTree StructureTree { get; } = new();
  26. public INodeHandler? OutputNode { get; private set; }
  27. private DocumentInternalParts Internals { get; }
  28. public NodeGraphViewModel(DocumentViewModel documentViewModel, DocumentInternalParts internals)
  29. {
  30. DocumentViewModel = documentViewModel;
  31. Internals = internals;
  32. }
  33. public void AddNode(INodeHandler node)
  34. {
  35. if (OutputNode == null &&
  36. node.InternalName == typeof(OutputNode).GetCustomAttribute<NodeInfoAttribute>().UniqueName)
  37. {
  38. OutputNode = node;
  39. }
  40. AllNodes.Add(node);
  41. StructureTree.Update(this);
  42. UpdateAvailableRenderOutputs();
  43. }
  44. public void RemoveNode(Guid nodeId)
  45. {
  46. var node = AllNodes.FirstOrDefault(x => x.Id == nodeId);
  47. if (node != null)
  48. {
  49. AllNodes.Remove(node);
  50. }
  51. StructureTree.Update(this);
  52. UpdateAvailableRenderOutputs();
  53. }
  54. public void AddFrame(Guid frameId, IEnumerable<Guid> nodes)
  55. {
  56. var frame = new NodeFrameViewModel(frameId, AllNodes.Where(x => nodes.Contains(x.Id)));
  57. Frames.Add(frame);
  58. }
  59. public void AddZone(Guid frameId, string internalName, Guid startId, Guid endId)
  60. {
  61. var start = AllNodes.First(x => x.Id == startId);
  62. var end = AllNodes.First(x => x.Id == endId);
  63. var zone = new NodeZoneViewModel(frameId, internalName, start, end);
  64. Frames.Add(zone);
  65. }
  66. public void RemoveFrame(Guid guid)
  67. {
  68. var frame = Frames.FirstOrDefault(x => x.Id == guid);
  69. if (frame == null) return;
  70. Frames.Remove(frame);
  71. }
  72. public void SetConnection(NodeConnectionViewModel connection)
  73. {
  74. var existingInputConnection = Connections.FirstOrDefault(x => x.InputProperty == connection.InputProperty);
  75. if (existingInputConnection != null)
  76. {
  77. Connections.Remove(existingInputConnection);
  78. existingInputConnection.InputProperty.ConnectedOutput = null;
  79. existingInputConnection.OutputProperty.ConnectedInputs.Remove(existingInputConnection.InputProperty);
  80. }
  81. connection.InputProperty.ConnectedOutput = connection.OutputProperty;
  82. connection.OutputProperty.ConnectedInputs.Add(connection.InputProperty);
  83. Connections.Add(connection);
  84. StructureTree.Update(this);
  85. }
  86. public void RemoveConnection(Guid nodeId, string property)
  87. {
  88. var connection = Connections.FirstOrDefault(x =>
  89. x.InputProperty.Node.Id == nodeId && x.InputProperty.PropertyName == property);
  90. if (connection != null)
  91. {
  92. connection.InputProperty.ConnectedOutput = null;
  93. connection.OutputProperty.ConnectedInputs.Remove(connection.InputProperty);
  94. Connections.Remove(connection);
  95. }
  96. var node = AllNodes.FirstOrDefault(x => x.Id == nodeId);
  97. if (node != null)
  98. {
  99. var input = node.Inputs.FirstOrDefault(x => x.PropertyName == property);
  100. if (input != null)
  101. {
  102. input.ConnectedOutput = null;
  103. }
  104. }
  105. StructureTree.Update(this);
  106. }
  107. public void RemoveConnections(Guid nodeId)
  108. {
  109. var connections = Connections
  110. .Where(x => x.InputProperty.Node.Id == nodeId || x.OutputProperty.Node.Id == nodeId).ToList();
  111. foreach (var connection in connections)
  112. {
  113. connection.InputProperty.ConnectedOutput = null;
  114. connection.OutputProperty.ConnectedInputs.Remove(connection.InputProperty);
  115. Connections.Remove(connection);
  116. }
  117. StructureTree.Update(this);
  118. }
  119. public bool TryTraverse(Func<INodeHandler, bool> func)
  120. {
  121. if (OutputNode == null) return false;
  122. var queue = CalculateExecutionQueue(OutputNode);
  123. while (queue.Count > 0)
  124. {
  125. var node = queue.Dequeue();
  126. func(node);
  127. }
  128. return true;
  129. }
  130. private Queue<INodeHandler> CalculateExecutionQueue(INodeHandler outputNode)
  131. {
  132. var finalQueue = new HashSet<INodeHandler>();
  133. var queueNodes = new Queue<INodeHandler>();
  134. queueNodes.Enqueue(outputNode);
  135. while (queueNodes.Count > 0)
  136. {
  137. var node = queueNodes.Dequeue();
  138. if (finalQueue.Contains(node))
  139. {
  140. continue;
  141. }
  142. bool canAdd = true;
  143. foreach (var input in node.Inputs)
  144. {
  145. if (input.ConnectedOutput == null)
  146. {
  147. continue;
  148. }
  149. if (finalQueue.Contains(input.ConnectedOutput.Node))
  150. {
  151. continue;
  152. }
  153. canAdd = false;
  154. if (finalQueue.Contains(input.ConnectedOutput.Node))
  155. {
  156. finalQueue.Remove(input.ConnectedOutput.Node);
  157. finalQueue.Add(input.ConnectedOutput.Node);
  158. }
  159. if (!queueNodes.Contains(input.ConnectedOutput.Node))
  160. {
  161. queueNodes.Enqueue(input.ConnectedOutput.Node);
  162. }
  163. }
  164. if (canAdd)
  165. {
  166. finalQueue.Add(node);
  167. }
  168. else
  169. {
  170. queueNodes.Enqueue(node);
  171. }
  172. }
  173. return new Queue<INodeHandler>(finalQueue);
  174. }
  175. public void SetNodePositions(List<INodeHandler> node, VecD startPos)
  176. {
  177. Guid[] nodeIds = node.Select(x => x.Id).ToArray();
  178. Internals.ActionAccumulator.AddActions(new NodePosition_Action(nodeIds, startPos));
  179. }
  180. public void UpdatePropertyValue(INodeHandler node, string property, object? value)
  181. {
  182. Internals.ActionAccumulator.AddFinishedActions(new UpdatePropertyValue_Action(node.Id, property, value));
  183. }
  184. public void GetComputedPropertyValue(INodePropertyHandler property)
  185. {
  186. Internals.ActionAccumulator.AddFinishedActions(new GetComputedPropertyValue_Action(property.Node.Id, property.PropertyName, property.IsInput));
  187. }
  188. public void EndChangeNodePosition()
  189. {
  190. Internals.ActionAccumulator.AddFinishedActions(new EndNodePosition_Action());
  191. }
  192. public void CreateNode(Type nodeType, VecD pos = default)
  193. {
  194. IAction change;
  195. PairNodeAttribute? pairAttribute = nodeType.GetCustomAttribute<PairNodeAttribute>(true);
  196. List<IAction> changes = new();
  197. if (pairAttribute != null)
  198. {
  199. Guid startId = Guid.NewGuid();
  200. Guid endId = Guid.NewGuid();
  201. changes.Add(new CreateNodePair_Action(startId, endId, nodeType));
  202. if (pos != default)
  203. {
  204. changes.Add(new NodePosition_Action([startId], pos));
  205. changes.Add(new EndNodePosition_Action());
  206. changes.Add(new NodePosition_Action([endId], new VecD(pos.X + 400, pos.Y)));
  207. changes.Add(new EndNodePosition_Action());
  208. }
  209. }
  210. else
  211. {
  212. Guid nodeId = Guid.NewGuid();
  213. changes.Add(new CreateNode_Action(nodeType, nodeId, Guid.Empty));
  214. if (pos != default)
  215. {
  216. changes.Add(new NodePosition_Action([nodeId], pos));
  217. changes.Add(new EndNodePosition_Action());
  218. }
  219. }
  220. Internals.ActionAccumulator.AddFinishedActions(changes.ToArray());
  221. }
  222. public void RemoveNodes(Guid[] selectedNodes)
  223. {
  224. List<IAction> actions = new();
  225. for (int i = 0; i < selectedNodes.Length; i++)
  226. {
  227. actions.Add(new DeleteNode_Action(selectedNodes[i]));
  228. }
  229. Internals.ActionAccumulator.AddFinishedActions(actions.ToArray());
  230. }
  231. // TODO: Remove this
  232. public void CreateNodeFrameAroundEverything()
  233. {
  234. CreateNodeFrame(AllNodes);
  235. }
  236. public void CreateNodeFrame(IEnumerable<INodeHandler> nodes)
  237. {
  238. Internals.ActionAccumulator.AddFinishedActions(new CreateNodeFrame_Action(Guid.NewGuid(),
  239. nodes.Select(x => x.Id)));
  240. }
  241. public void ConnectProperties(INodePropertyHandler? start, INodePropertyHandler? end)
  242. {
  243. if (start == null && end == null) return;
  244. INodeHandler inputNode = null, outputNode = null;
  245. string inputProperty = null, outputProperty = null;
  246. var input = start?.IsInput == true ? start : end;
  247. var output = start?.IsInput == false ? start : end;
  248. if (input == null && output != null)
  249. {
  250. input = output.ConnectedInputs?.FirstOrDefault();
  251. output = null;
  252. }
  253. if (input != null)
  254. {
  255. inputNode = input.Node;
  256. inputProperty = input.PropertyName;
  257. }
  258. if (output != null)
  259. {
  260. outputNode = output.Node;
  261. outputProperty = output.PropertyName;
  262. }
  263. if (input == null) return;
  264. IAction action = input != null && output != null
  265. ? new ConnectProperties_Action(inputNode.Id, outputNode.Id, inputProperty, outputProperty)
  266. : new DisconnectProperty_Action(inputNode.Id, inputProperty);
  267. Internals.ActionAccumulator.AddFinishedActions(action);
  268. }
  269. public void UpdateAvailableRenderOutputs()
  270. {
  271. List<string> outputs = new();
  272. foreach (var node in AllNodes)
  273. {
  274. if (node.InternalName == typeof(CustomOutputNode).GetCustomAttribute<NodeInfoAttribute>().UniqueName)
  275. {
  276. var nameInput =
  277. node.Inputs.FirstOrDefault(x => x.PropertyName == CustomOutputNode.OutputNamePropertyName);
  278. if (nameInput is { Value: string name } && !string.IsNullOrEmpty(name))
  279. {
  280. if(outputs.Contains(name)) continue;
  281. outputs.Add(name);
  282. }
  283. }
  284. else if (node.InternalName == typeof(OutputNode).GetCustomAttribute<NodeInfoAttribute>().UniqueName)
  285. {
  286. outputs.Insert(0, "DEFAULT");
  287. }
  288. }
  289. RemoveExcessiveRenderOutputs(outputs);
  290. AddMissingRenderOutputs(outputs);
  291. }
  292. private void RemoveExcessiveRenderOutputs(List<string> outputs)
  293. {
  294. for (int i = AvailableRenderOutputs.Count - 1; i >= 0; i--)
  295. {
  296. if (!outputs.Contains(AvailableRenderOutputs[i]))
  297. {
  298. AvailableRenderOutputs.RemoveAt(i);
  299. }
  300. }
  301. }
  302. private void AddMissingRenderOutputs(List<string> outputs)
  303. {
  304. foreach (var output in outputs)
  305. {
  306. if (!AvailableRenderOutputs.Contains(output))
  307. {
  308. AvailableRenderOutputs.Add(output);
  309. }
  310. }
  311. }
  312. public void Dispose()
  313. {
  314. foreach (var node in AllNodes)
  315. {
  316. node.Dispose();
  317. }
  318. }
  319. }