objects_node_Node.js.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /**
  22. * Node objects can be connected between them to create graphs.
  23. *
  24. * 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.
  25. *
  26. * This class implements node basic functionality, the logic to connect node and define inputs/outputs of the nodes.
  27. *
  28. * @class Node
  29. */
  30. function Node()
  31. {
  32. RoundedBox.call(this);
  33. this.draggable = true;
  34. /**
  35. * List of inputs of the node.
  36. *
  37. * @type {NodeSocket[]}
  38. */
  39. this.inputs = [];
  40. /**
  41. * List of outputs of the node.
  42. *
  43. * @type {NodeSocket[]}
  44. */
  45. this.outputs = [];
  46. }
  47. Node.prototype = Object.create(RoundedBox.prototype);
  48. /**
  49. * This method should be used for the node to register their socket inputs/outputs.
  50. *
  51. * It is called automatically after the node is added to the node graph to create sockets.
  52. */
  53. Node.prototype.registerSockets = null;
  54. /**
  55. * Add input to this node, can be connected to other nodes to receive data.
  56. *
  57. * @param {string} type Data type of the node socket.
  58. * @param {string} name Name of the node socket.
  59. * @return {NodeSocket} Node socket created for this node.
  60. */
  61. Node.prototype.addInput = function(type, name)
  62. {
  63. var socket = new NodeSocket(this, NodeSocket.INPUT, type, name);
  64. this.inputs.push(socket);
  65. this.parent.add(socket);
  66. return socket;
  67. };
  68. /**
  69. * Add output socket to this node, can be connected to other nodes to send data.
  70. *
  71. * @param {string} type Data type of the node socket.
  72. * @param {string} name Name of the node socket.
  73. * @return {NodeSocket} Node socket created for this node.
  74. */
  75. Node.prototype.addOutput = function(type, name)
  76. {
  77. var socket = new NodeSocket(this, NodeSocket.OUTPUT, type, name);
  78. this.outputs.push(socket);
  79. this.parent.add(socket);
  80. return socket;
  81. };
  82. /**
  83. * Get a output socket by its name. If there are multiple sockets with the same name only the first one found is returned.
  84. *
  85. * @param {string} name Name of the node socket to get.
  86. * @return {NodeSocket} Node socket if it was found, null otherwise.
  87. */
  88. Node.prototype.getOutput = function(name)
  89. {
  90. for(var i = 0; i &lt; this.outputs.length; i++)
  91. {
  92. if(this.outputs[i].name === name)
  93. {
  94. return this.outputs[i];
  95. }
  96. }
  97. return null;
  98. };
  99. /**
  100. * Get a input socket by its name. If there are multiple sockets with the same name only the first one found is returned.
  101. *
  102. * @param {string} name Name of the node socket to get.
  103. * @return {NodeSocket} Node socket if it was found, null otherwise.
  104. */
  105. Node.prototype.getInput = function(name)
  106. {
  107. for(var i = 0; i &lt; this.inputs.length; i++)
  108. {
  109. if(this.inputs[i].name === name)
  110. {
  111. return this.inputs[i];
  112. }
  113. }
  114. return null;
  115. };
  116. Node.prototype.destroy = function()
  117. {
  118. RoundedBox.prototype.destroy.call(this);
  119. for(var i = 0; i &lt; this.inputs.length; i++)
  120. {
  121. this.inputs[i].destroy();
  122. }
  123. for(var i = 0; i &lt; this.outputs.length; i++)
  124. {
  125. this.outputs[i].destroy();
  126. }
  127. };
  128. Node.prototype.onUpdate = function()
  129. {
  130. var height = this.box.max.y - this.box.min.y;
  131. // Input hooks position
  132. var step = height / (this.inputs.length + 1);
  133. var start = this.box.min.y + step;
  134. for(var i = 0; i &lt; this.inputs.length; i++)
  135. {
  136. this.inputs[i].position.set(this.position.x + this.box.min.x, this.position.y + (start + step * i));
  137. }
  138. // Output hooks position
  139. step = height / (this.outputs.length + 1);
  140. start = this.box.min.y + step;
  141. for(var i = 0; i &lt; this.outputs.length; i++)
  142. {
  143. this.outputs[i].position.set(this.position.x + this.box.max.x, this.position.y + (start + step * i));
  144. }
  145. };
  146. export {Node};
  147. </code></pre>
  148. </article>
  149. </section>
  150. </div>
  151. <nav>
  152. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><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="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</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="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="Pattern.html">Pattern</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="QuadraticCurve.html">QuadraticCurve</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="RoundedBox.html">RoundedBox</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#connector">connector</a></li><li><a href="global.html#direction">direction</a></li><li><a href="global.html#inputs">inputs</a></li><li><a href="global.html#inputSocket">inputSocket</a></li><li><a href="global.html#name">name</a></li><li><a href="global.html#node">node</a></li><li><a href="global.html#outputs">outputs</a></li><li><a href="global.html#outputSocket">outputSocket</a></li><li><a href="global.html#text">text</a></li><li><a href="global.html#type">type</a></li></ul>
  153. </nav>
  154. <br class="clear">
  155. <footer>
  156. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Thu May 14 2020 18:02:06 GMT+0100 (Western European Summer Time)
  157. </footer>
  158. <script> prettyPrint(); </script>
  159. <script src="scripts/linenumber.js"> </script>
  160. </body>
  161. </html>