Node.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using System.Diagnostics;
  2. using System.Reflection;
  3. using PixiEditor.ChangeableDocument.Changeables.Animations;
  4. using PixiEditor.ChangeableDocument.Changeables.Graph.Context;
  5. using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
  6. using PixiEditor.ChangeableDocument.Changeables.Interfaces;
  7. using PixiEditor.ChangeableDocument.Rendering;
  8. using PixiEditor.Common;
  9. using PixiEditor.DrawingApi.Core;
  10. using PixiEditor.DrawingApi.Core.ColorsImpl;
  11. using PixiEditor.DrawingApi.Core.Shaders;
  12. using PixiEditor.Numerics;
  13. namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
  14. [DebuggerDisplay("Type = {GetType().Name}")]
  15. public abstract class Node : IReadOnlyNode, IDisposable
  16. {
  17. private string displayName;
  18. private List<InputProperty> inputs = new();
  19. private List<OutputProperty> outputs = new();
  20. protected List<KeyFrameData> keyFrames = new();
  21. public Guid Id { get; internal set; } = Guid.NewGuid();
  22. public IReadOnlyList<InputProperty> InputProperties => inputs;
  23. public IReadOnlyList<OutputProperty> OutputProperties => outputs;
  24. public IReadOnlyList<KeyFrameData> KeyFrames => keyFrames;
  25. IReadOnlyList<IInputProperty> IReadOnlyNode.InputProperties => inputs;
  26. IReadOnlyList<IOutputProperty> IReadOnlyNode.OutputProperties => outputs;
  27. IReadOnlyList<IReadOnlyKeyFrameData> IReadOnlyNode.KeyFrames => keyFrames;
  28. public VecD Position { get; set; }
  29. public virtual string DisplayName
  30. {
  31. get => displayName;
  32. set => displayName = value;
  33. }
  34. protected virtual bool ExecuteOnlyOnCacheChange => false;
  35. private bool _isDisposed;
  36. private Dictionary<int, Texture> _managedTextures = new();
  37. public void Execute(RenderContext context)
  38. {
  39. ExecuteInternal(context);
  40. }
  41. internal void ExecuteInternal(RenderContext context)
  42. {
  43. if (_isDisposed) throw new ObjectDisposedException("Node was disposed before execution.");
  44. if (ExecuteOnlyOnCacheChange && !CacheChanged(context))
  45. {
  46. return;
  47. }
  48. OnExecute(context);
  49. if (ExecuteOnlyOnCacheChange)
  50. {
  51. UpdateCache(context);
  52. }
  53. }
  54. protected abstract void OnExecute(RenderContext context);
  55. protected virtual bool CacheChanged(RenderContext context)
  56. {
  57. return inputs.Any(x => x.CacheChanged);
  58. }
  59. protected virtual void UpdateCache(RenderContext context)
  60. {
  61. foreach (var input in inputs)
  62. {
  63. input.UpdateCache();
  64. }
  65. }
  66. protected Texture RequestTexture(int id, VecI size, bool clear = true)
  67. {
  68. if (_managedTextures.TryGetValue(id, out var texture))
  69. {
  70. if (texture.Size != size || texture.IsDisposed)
  71. {
  72. texture.Dispose();
  73. texture = new Texture(size);
  74. _managedTextures[id] = texture;
  75. return texture;
  76. }
  77. if (clear)
  78. {
  79. texture.DrawingSurface.Canvas.Clear(Colors.Transparent);
  80. }
  81. return texture;
  82. }
  83. _managedTextures[id] = new Texture(size);
  84. return _managedTextures[id];
  85. }
  86. public void TraverseBackwards(Func<IReadOnlyNode, bool> action)
  87. {
  88. var visited = new HashSet<IReadOnlyNode>();
  89. var queueNodes = new Queue<IReadOnlyNode>();
  90. queueNodes.Enqueue(this);
  91. while (queueNodes.Count > 0)
  92. {
  93. var node = queueNodes.Dequeue();
  94. if (!visited.Add(node))
  95. {
  96. continue;
  97. }
  98. if (!action(node))
  99. {
  100. return;
  101. }
  102. foreach (var inputProperty in node.InputProperties)
  103. {
  104. if (inputProperty.Connection != null)
  105. {
  106. queueNodes.Enqueue(inputProperty.Connection.Node);
  107. }
  108. }
  109. }
  110. }
  111. public void TraverseForwards(Func<IReadOnlyNode, bool> action)
  112. {
  113. var visited = new HashSet<IReadOnlyNode>();
  114. var queueNodes = new Queue<IReadOnlyNode>();
  115. queueNodes.Enqueue(this);
  116. while (queueNodes.Count > 0)
  117. {
  118. var node = queueNodes.Dequeue();
  119. if (!visited.Add(node))
  120. {
  121. continue;
  122. }
  123. if (!action(node))
  124. {
  125. return;
  126. }
  127. foreach (var outputProperty in node.OutputProperties)
  128. {
  129. foreach (var connection in outputProperty.Connections)
  130. {
  131. if (connection.Connection != null)
  132. {
  133. queueNodes.Enqueue(connection.Node);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. public void RemoveKeyFrame(Guid keyFrameId)
  140. {
  141. keyFrames.RemoveAll(x => x.KeyFrameGuid == keyFrameId);
  142. }
  143. public void SetKeyFrameLength(Guid id, int startFrame, int duration)
  144. {
  145. KeyFrameData frame = keyFrames.FirstOrDefault(x => x.KeyFrameGuid == id);
  146. if (frame is not null)
  147. {
  148. frame.StartFrame = startFrame;
  149. frame.Duration = duration;
  150. }
  151. }
  152. public void SetKeyFrameVisibility(Guid id, bool isVisible)
  153. {
  154. KeyFrameData frame = keyFrames.FirstOrDefault(x => x.KeyFrameGuid == id);
  155. if (frame is not null)
  156. {
  157. frame.IsVisible = isVisible;
  158. }
  159. }
  160. public void AddFrame(Guid id, KeyFrameData value)
  161. {
  162. if (keyFrames.Any(x => x.KeyFrameGuid == id))
  163. {
  164. throw new InvalidOperationException("Key frame with this id already exists.");
  165. }
  166. keyFrames.Add(value);
  167. }
  168. protected FuncInputProperty<T> CreateFuncInput<T>(string propName, string displayName, T defaultValue)
  169. {
  170. var property = new FuncInputProperty<T>(this, propName, displayName, defaultValue);
  171. if (InputProperties.Any(x => x.InternalPropertyName == propName))
  172. {
  173. throw new InvalidOperationException($"Input with name {propName} already exists.");
  174. }
  175. inputs.Add(property);
  176. return property;
  177. }
  178. protected InputProperty<T> CreateInput<T>(string propName, string displayName, T defaultValue)
  179. {
  180. var property = new InputProperty<T>(this, propName, displayName, defaultValue);
  181. if (InputProperties.Any(x => x.InternalPropertyName == propName))
  182. {
  183. throw new InvalidOperationException($"Input with name {propName} already exists.");
  184. }
  185. inputs.Add(property);
  186. return property;
  187. }
  188. protected FuncOutputProperty<T> CreateFuncOutput<T>(string propName, string displayName,
  189. Func<FuncContext, T> defaultFunc)
  190. {
  191. var property = new FuncOutputProperty<T>(this, propName, displayName, defaultFunc);
  192. outputs.Add(property);
  193. return property;
  194. }
  195. protected OutputProperty<T> CreateOutput<T>(string propName, string displayName, T defaultValue)
  196. {
  197. var property = new OutputProperty<T>(this, propName, displayName, defaultValue);
  198. outputs.Add(property);
  199. return property;
  200. }
  201. protected void AddOutputProperty(OutputProperty property)
  202. {
  203. outputs.Add(property);
  204. }
  205. protected void AddInputProperty(InputProperty property)
  206. {
  207. if (InputProperties.Any(x => x.InternalPropertyName == property.InternalPropertyName))
  208. {
  209. throw new InvalidOperationException($"Input with name {property.InternalPropertyName} already exists.");
  210. }
  211. inputs.Add(property);
  212. }
  213. public virtual void Dispose()
  214. {
  215. _isDisposed = true;
  216. DisconnectAll();
  217. foreach (var input in inputs)
  218. {
  219. if (input is { Connection: null, NonOverridenValue: IDisposable disposable })
  220. {
  221. disposable.Dispose();
  222. input.NonOverridenValue = default;
  223. }
  224. }
  225. foreach (var output in outputs)
  226. {
  227. if (output.Connections.Count == 0 && output.Value is IDisposable disposable)
  228. {
  229. disposable.Dispose();
  230. output.Value = default;
  231. }
  232. }
  233. if (keyFrames is not null)
  234. {
  235. foreach (var keyFrame in keyFrames)
  236. {
  237. keyFrame.Dispose();
  238. }
  239. }
  240. foreach (var texture in _managedTextures)
  241. {
  242. texture.Value.Dispose();
  243. }
  244. }
  245. public void DisconnectAll()
  246. {
  247. foreach (var input in inputs)
  248. {
  249. input.Connection?.DisconnectFrom(input);
  250. }
  251. foreach (var output in outputs)
  252. {
  253. var connections = output.Connections.ToArray();
  254. for (var i = 0; i < connections.Length; i++)
  255. {
  256. var conn = connections[i];
  257. output.DisconnectFrom(conn);
  258. }
  259. }
  260. }
  261. public string GetNodeTypeUniqueName()
  262. {
  263. NodeInfoAttribute? attribute = GetType().GetCustomAttribute<NodeInfoAttribute>();
  264. if (attribute is null)
  265. {
  266. throw new InvalidOperationException("Node does not have NodeInfo attribute.");
  267. }
  268. return attribute.UniqueName;
  269. }
  270. public abstract Node CreateCopy();
  271. public Node Clone()
  272. {
  273. var clone = CreateCopy();
  274. clone.DisplayName = DisplayName;
  275. clone.Id = Guid.NewGuid();
  276. clone.Position = Position;
  277. for (var i = 0; i < clone.inputs.Count; i++)
  278. {
  279. var cloneInput = inputs[i];
  280. var newInput = cloneInput.Clone(clone);
  281. clone.inputs[i].NonOverridenValue = newInput.NonOverridenValue;
  282. }
  283. for (var i = 0; i < clone.outputs.Count; i++)
  284. {
  285. var cloneOutput = outputs[i];
  286. var newOutput = cloneOutput.Clone(clone);
  287. clone.outputs[i].Value = newOutput.Value;
  288. }
  289. foreach (var keyFrame in keyFrames)
  290. {
  291. KeyFrameData newKeyFrame = new KeyFrameData(keyFrame.KeyFrameGuid, keyFrame.StartFrame, keyFrame.Duration,
  292. keyFrame.AffectedElement)
  293. {
  294. IsVisible = keyFrame.IsVisible,
  295. Duration = keyFrame.Duration,
  296. Data = keyFrame.Data is ICloneable cloneable ? cloneable.Clone() : keyFrame.Data
  297. };
  298. clone.keyFrames.Add(newKeyFrame);
  299. }
  300. return clone;
  301. }
  302. public InputProperty? GetInputProperty(string inputProperty)
  303. {
  304. return inputs.FirstOrDefault(x => x.InternalPropertyName == inputProperty);
  305. }
  306. public OutputProperty? GetOutputProperty(string outputProperty)
  307. {
  308. return outputs.FirstOrDefault(x => x.InternalPropertyName == outputProperty);
  309. }
  310. public bool HasInputProperty(string propertyName)
  311. {
  312. return inputs.Any(x => x.InternalPropertyName == propertyName);
  313. }
  314. public bool HasOutputProperty(string propertyName)
  315. {
  316. return outputs.Any(x => x.InternalPropertyName == propertyName);
  317. }
  318. IInputProperty? IReadOnlyNode.GetInputProperty(string inputProperty)
  319. {
  320. return GetInputProperty(inputProperty);
  321. }
  322. IOutputProperty? IReadOnlyNode.GetOutputProperty(string outputProperty)
  323. {
  324. return GetOutputProperty(outputProperty);
  325. }
  326. public virtual void SerializeAdditionalData(Dictionary<string, object> additionalData)
  327. {
  328. }
  329. internal virtual OneOf<None, IChangeInfo, List<IChangeInfo>> DeserializeAdditionalData(IReadOnlyDocument target,
  330. IReadOnlyDictionary<string, object> data)
  331. {
  332. return new None();
  333. }
  334. }