objects_node_Node.js.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: objects/node/Node.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: objects/node/Node.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>import {NodeSocket} from "./NodeSocket";
  20. import {RoundedBox} from "../RoundedBox";
  21. import {Object2D} from "../../Object2D";
  22. /**
  23. * Node objects can be connected between them to create graphs.
  24. *
  25. * Each node contains inputs, outputs and a set of attributes containing their state. Inputs can be connected to outputs of other nodes, and vice-versa.
  26. *
  27. * This class implements node basic functionality, the logic to connect node and define inputs/outputs of the nodes.
  28. *
  29. * @class
  30. * @extends {RoundedBox}
  31. */
  32. function Node()
  33. {
  34. RoundedBox.call(this);
  35. this.draggable = true;
  36. /**
  37. * List of inputs of the node.
  38. *
  39. * @type {NodeSocket[]}
  40. */
  41. this.inputs = [];
  42. /**
  43. * List of outputs of the node.
  44. *
  45. * @type {NodeSocket[]}
  46. */
  47. this.outputs = [];
  48. }
  49. Node.prototype = Object.create(RoundedBox.prototype);
  50. Node.prototype.constructor = Node;
  51. Node.prototype.type = "Node";
  52. Object2D.register(Node, "Node");
  53. /**
  54. * This method should be used for the node to register their socket inputs/outputs.
  55. *
  56. * It is called automatically after the node is added to the node graph to create sockets.
  57. */
  58. Node.prototype.registerSockets = null;
  59. /**
  60. * Add input to this node, can be connected to other nodes to receive data.
  61. *
  62. * @param {string} type Data type of the node socket.
  63. * @param {string} name Name of the node socket.
  64. * @return {NodeSocket} Node socket created for this node.
  65. */
  66. Node.prototype.addInput = function(type, name)
  67. {
  68. var socket = new NodeSocket(this, NodeSocket.INPUT, type, name);
  69. this.inputs.push(socket);
  70. this.parent.add(socket);
  71. return socket;
  72. };
  73. /**
  74. * Add output socket to this node, can be connected to other nodes to send data.
  75. *
  76. * @param {string} type Data type of the node socket.
  77. * @param {string} name Name of the node socket.
  78. * @return {NodeSocket} Node socket created for this node.
  79. */
  80. Node.prototype.addOutput = function(type, name)
  81. {
  82. var socket = new NodeSocket(this, NodeSocket.OUTPUT, type, name);
  83. this.outputs.push(socket);
  84. this.parent.add(socket);
  85. return socket;
  86. };
  87. /**
  88. * Get a output socket by its name. If there are multiple sockets with the same name only the first one found is returned.
  89. *
  90. * @param {string} name Name of the node socket to get.
  91. * @return {NodeSocket} Node socket if it was found, null otherwise.
  92. */
  93. Node.prototype.getOutput = function(name)
  94. {
  95. for(var i = 0; i &lt; this.outputs.length; i++)
  96. {
  97. if(this.outputs[i].name === name)
  98. {
  99. return this.outputs[i];
  100. }
  101. }
  102. return null;
  103. };
  104. /**
  105. * Get a input socket by its name. If there are multiple sockets with the same name only the first one found is returned.
  106. *
  107. * @param {string} name Name of the node socket to get.
  108. * @return {NodeSocket} Node socket if it was found, null otherwise.
  109. */
  110. Node.prototype.getInput = function(name)
  111. {
  112. for(var i = 0; i &lt; this.inputs.length; i++)
  113. {
  114. if(this.inputs[i].name === name)
  115. {
  116. return this.inputs[i];
  117. }
  118. }
  119. return null;
  120. };
  121. Node.prototype.destroy = function()
  122. {
  123. RoundedBox.prototype.destroy.call(this);
  124. for(var i = 0; i &lt; this.inputs.length; i++)
  125. {
  126. this.inputs[i].destroy();
  127. }
  128. for(var i = 0; i &lt; this.outputs.length; i++)
  129. {
  130. this.outputs[i].destroy();
  131. }
  132. };
  133. Node.prototype.onUpdate = function()
  134. {
  135. var height = this.box.max.y - this.box.min.y;
  136. // Input hooks position
  137. var step = height / (this.inputs.length + 1);
  138. var start = this.box.min.y + step;
  139. for(var i = 0; i &lt; this.inputs.length; i++)
  140. {
  141. this.inputs[i].position.set(this.position.x + this.box.min.x, this.position.y + (start + step * i));
  142. }
  143. // Output hooks position
  144. step = height / (this.outputs.length + 1);
  145. start = this.box.min.y + step;
  146. for(var i = 0; i &lt; this.outputs.length; i++)
  147. {
  148. this.outputs[i].position.set(this.position.x + this.box.max.x, this.position.y + (start + step * i));
  149. }
  150. };
  151. Node.prototype.serialize = function(recursive)
  152. {
  153. var data = RoundedBox.prototype.serialize.call(this, recursive);
  154. data.inputs = [];
  155. for(var i = 0; i &lt; this.inputs.length; i++)
  156. {
  157. data.inputs.push(this.inputs[i].uuid);
  158. }
  159. data.outputs = [];
  160. for(var i = 0; i &lt; this.outputs.length; i++)
  161. {
  162. data.outputs.push(this.outputs[i].uuid);
  163. }
  164. return data;
  165. };
  166. Node.prototype.parse = function(data, root)
  167. {
  168. RoundedBox.prototype.parse.call(this, data, root);
  169. for(var i = 0; i &lt; data.inputs.length; i++)
  170. {
  171. this.inputs.push(root.getChildByUUID(data.inputs[i]));
  172. }
  173. for(var i = 0; i &lt; data.outputs.length; i++)
  174. {
  175. this.outputs.push(root.getChildByUUID(data.outputs[i]));
  176. }
  177. };
  178. export {Node};
  179. </code></pre>
  180. </article>
  181. </section>
  182. </div>
  183. <nav>
  184. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AnimationTimer.html">AnimationTimer</a></li><li><a href="BarGraph.html">BarGraph</a></li><li><a href="BezierCurve.html">BezierCurve</a></li><li><a href="Box.html">Box</a></li><li><a href="Box2.html">Box2</a></li><li><a href="BoxMask.html">BoxMask</a></li><li><a href="Circle.html">Circle</a></li><li><a href="ColorStyle.html">ColorStyle</a></li><li><a href="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</a></li><li><a href="FileUtils.html">FileUtils</a></li><li><a href="Gauge.html">Gauge</a></li><li><a href="GradientColorStop.html">GradientColorStop</a></li><li><a href="GradientStyle.html">GradientStyle</a></li><li><a href="Graph.html">Graph</a></li><li><a href="Helpers.html">Helpers</a></li><li><a href="Image.html">Image</a></li><li><a href="Key.html">Key</a></li><li><a href="Line.html">Line</a></li><li><a href="LinearGradientStyle.html">LinearGradientStyle</a></li><li><a href="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="MultiLineText.html">MultiLineText</a></li><li><a href="Node.html">Node</a></li><li><a href="NodeConnector.html">NodeConnector</a></li><li><a href="NodeGraph.html">NodeGraph</a></li><li><a href="NodeSocket.html">NodeSocket</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Path.html">Path</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="PatternStyle.html">PatternStyle</a></li><li><a href="PieChart.html">PieChart</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="QuadraticCurve.html">QuadraticCurve</a></li><li><a href="RadialGradientStyle.html">RadialGradientStyle</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="RoundedBox.html">RoundedBox</a></li><li><a href="ScatterGraph.html">ScatterGraph</a></li><li><a href="Style.html">Style</a></li><li><a href="Text.html">Text</a></li><li><a href="UUID.html">UUID</a></li><li><a href="Vector2.html">Vector2</a></li><li><a href="Viewport.html">Viewport</a></li><li><a href="ViewportControls.html">ViewportControls</a></li></ul><h3>Global</h3><ul><li><a href="global.html#writeFile">writeFile</a></li></ul>
  185. </nav>
  186. <br class="clear">
  187. <footer>
  188. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Sat Sep 17 2022 14:24:36 GMT+0100 (Hora de verão da Europa Ocidental)
  189. </footer>
  190. <script> prettyPrint(); </script>
  191. <script src="scripts/linenumber.js"> </script>
  192. </body>
  193. </html>