PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/research/timeline/parser/plugins/ValueTimelineParser/ValueTimelineParserNode.cs

https://bitbucket.org/microdee/iris
C# | 292 lines | 213 code | 56 blank | 23 comment | 21 complexity | 90f0cfea36d8f6cd7a85dd2f3d45aaf7 MD5 | raw file
  1. #region usings
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.ComponentModel.Composition;
  6. using VVVV.PluginInterfaces.V1;
  7. using VVVV.PluginInterfaces.V2;
  8. using VVVV.PluginInterfaces.V2.Graph;
  9. using VVVV.Utils.VColor;
  10. using VVVV.Utils.VMath;
  11. using VVVV.Core;
  12. using VVVV.Core.Logging;
  13. #endregion usings
  14. namespace VVVV.Nodes
  15. {
  16. #region PluginInfo
  17. [PluginInfo(Name = "TimelineParser", Category = "Value", Help = "Basic template with one value in/out", Tags = "", AutoEvaluate = true)]
  18. #endregion PluginInfo
  19. public class ValueTimelineParserNode : IDisposable, IPluginEvaluate
  20. {
  21. #region fields & pins
  22. //fields
  23. private bool FDisposed = false;
  24. private INode2 FPatch;
  25. private bool firstFrame = true;
  26. private string nodePath;
  27. //collections
  28. private Dictionary<string, Track> tracks = new Dictionary<string, Track>();
  29. //pins
  30. [Input("Node Path", IsSingle = true)]
  31. IDiffSpread<string> FNodePath;
  32. [Input("Initialize", IsBang = true, IsSingle = true)]
  33. ISpread<bool> FInit;
  34. [Input("Update", IsBang = true, IsSingle = true)]
  35. ISpread<bool> FUpdate;
  36. [Output("Track Count")]
  37. ISpread<int> FTrackCount;
  38. [Output("Track Name")]
  39. ISpread<string> FTrackName;
  40. [Output("Keyframe Count")]
  41. ISpread<int> FKeyframeCount;
  42. [Output("Keyframe Value")]
  43. ISpread<double> FKeyframeValue;
  44. [Output("Keyframe Time")]
  45. ISpread<double> FKeyframeTime;
  46. [Import()]
  47. ILogger FLogger;
  48. [Import()]
  49. IHDEHost FHDEHost;
  50. [Import()]
  51. IPluginHost FPluginHost;
  52. #endregion fields & pins
  53. #region constructor/destructor
  54. public ValueTimelineParserNode()
  55. {
  56. }
  57. ~ValueTimelineParserNode()
  58. {
  59. Dispose(false);
  60. }
  61. public void Dispose()
  62. {
  63. Dispose(true);
  64. }
  65. protected void Dispose(bool disposing)
  66. {
  67. // Check to see if Dispose has already been called.
  68. if (!FDisposed) {
  69. if (disposing) {
  70. // Dispose managed resources.
  71. // FPatch.Added -= NodeAddedCB;
  72. // FPatch.Removed -= NodeRemovedCB;
  73. //unregister all ioboxes
  74. //foreach (var node in FPatch)
  75. //if (node.ID == nodeID)
  76. //RemoveNodes(node);
  77. }
  78. // Release unmanaged resources. If disposing is false,
  79. // only the following code is executed.
  80. }
  81. FDisposed = true;
  82. }
  83. #endregion constructor/destructor
  84. private void AddTracks()
  85. {
  86. foreach (var timeline in FPatch)
  87. {
  88. if (timeline.Name.Contains("IRIS.Timeline"))
  89. {
  90. foreach (var track in timeline)
  91. {
  92. if (track.Name.Contains("IRIS.Track") && !tracks.ContainsKey(track.ID.ToString()))
  93. {
  94. Track newTrack = new Track(track.Name,0);
  95. tracks.Add(track.ID.ToString(), newTrack);
  96. }
  97. foreach (var keyframe in track)
  98. {
  99. if (keyframe.Name.Contains("IRIS.Keyframe") && tracks.ContainsKey(track.ID.ToString()))
  100. {
  101. // parse time & value out of keyframe io boxes here
  102. Keyframe newKeyframe = new Keyframe(Convert.ToDouble(keyframe.FindPin("Time")[0].Trim('|')), Convert.ToDouble(keyframe.FindPin("Value")[0].Trim('|')),keyframe.ID.ToString() );
  103. //Keyframe newKeyframe = new Keyframe("hier","huhu");
  104. tracks[track.ID.ToString()].keyframes.Add(keyframe.ID.ToString(), newKeyframe);
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. private void UpdateTimeline()
  112. {
  113. foreach (var timeline in FPatch)
  114. {
  115. if (timeline.Name.Contains("IRIS.Timeline"))
  116. {
  117. foreach (var track in timeline)
  118. {
  119. foreach (var currentTrack in tracks)
  120. {
  121. if (!tracks.ContainsKey(track.ID.ToString()))
  122. {
  123. Track newTrack = new Track(track.Name,0);
  124. tracks.Add(track.ID.ToString(), newTrack);
  125. }
  126. //else
  127. //tracks.Remove(track.ID.ToString());
  128. }
  129. }
  130. }
  131. }
  132. }
  133. private void UpdateTracks()
  134. {
  135. foreach (var timeline in FPatch)
  136. {
  137. if (timeline.Name.Contains("IRIS.Timeline"))
  138. {
  139. foreach (var track in timeline)
  140. {
  141. }
  142. }
  143. }
  144. }
  145. private void UpdateKeyframeValues(Keyframe kf)
  146. {
  147. foreach (var timeline in FPatch)
  148. {
  149. if (timeline.Name.Contains("IRIS.Timeline"))
  150. {
  151. foreach (var track in timeline)
  152. {
  153. foreach (var keyframe in track)
  154. {
  155. if (keyframe.ID.ToString() == kf.key)
  156. {
  157. // parse time & value out of keyframe io boxes here
  158. kf.val = Convert.ToDouble(keyframe.FindPin("Value")[0].Trim('|'));
  159. kf.time = Convert.ToDouble(keyframe.FindPin("Time")[0].Trim('|'));
  160. FLogger.Log (LogType.Debug, "hier");
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. //called when data for any output pin is requested
  168. public void Evaluate(int SpreadMax)
  169. {
  170. //update can be forced
  171. if (FInit[0] || (FUpdate[0] && firstFrame))
  172. {
  173. if (!firstFrame)
  174. tracks.Clear();
  175. nodePath = FNodePath[0];
  176. var node = FHDEHost.GetNodeFromPath(nodePath);
  177. FPatch = node.Parent;
  178. AddTracks();
  179. firstFrame=false;
  180. }
  181. //calc Output Slicecounts & write values into output-pins
  182. FTrackCount.SliceCount = 1;
  183. FKeyframeCount.SliceCount = tracks.Count;
  184. FTrackName.SliceCount = tracks.Count;
  185. FTrackCount[0] = tracks.Count;
  186. int maxKeyFrameCount = 0;
  187. int i = 0;
  188. int j = 0;
  189. if (FInit[0] || FUpdate[0])
  190. {
  191. foreach (var track in tracks)
  192. {
  193. FKeyframeCount[i] = track.Value.keyframes.Count;
  194. FTrackName[i] = track.Value.name;
  195. maxKeyFrameCount += track.Value.keyframes.Count;
  196. FKeyframeValue.SliceCount = maxKeyFrameCount;
  197. FKeyframeTime.SliceCount = maxKeyFrameCount;
  198. foreach (var keyframe in track.Value.keyframes)
  199. {
  200. if (FInit[0] || FUpdate[0])
  201. UpdateKeyframeValues(keyframe.Value);
  202. FKeyframeValue[j] = keyframe.Value.val;
  203. FKeyframeTime[j]= keyframe.Value.time;
  204. j++;
  205. }
  206. i++;
  207. }
  208. //UpdateTimeline();
  209. }
  210. //FLogger.Log(LogType.Debug, "hi tty!");
  211. }
  212. }
  213. public class Track
  214. {
  215. public Dictionary<string, Keyframe> keyframes = new Dictionary<string, Keyframe>();
  216. public string name {get;set;}
  217. public int type {get;set;}
  218. public Track (string _name, int _type)
  219. {
  220. name = _name;
  221. type = _type;
  222. }
  223. }
  224. public class Keyframe
  225. {
  226. public double time {get;set;}
  227. public double val {get;set;}
  228. public string key {get;set;}
  229. public Keyframe (double _time, double _value, string _key)
  230. {
  231. time = _time;
  232. val = _value;
  233. key = _key;
  234. }
  235. }
  236. }