2
0

Node.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {NodeSocket} from "./NodeSocket";
  2. import {RoundedBox} from "../RoundedBox";
  3. /**
  4. * Node objects can be connected between them to create graphs.
  5. *
  6. * 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.
  7. *
  8. * This class implements node basic functionality, the logic to connect node and define inputs/outputs of the nodes.
  9. *
  10. * @class Node
  11. */
  12. function Node()
  13. {
  14. RoundedBox.call(this);
  15. this.draggable = true;
  16. /**
  17. * List of inputs of the node.
  18. *
  19. * @type {NodeSocket[]}
  20. */
  21. this.inputs = [];
  22. /**
  23. * List of outputs of the node.
  24. *
  25. * @type {NodeSocket[]}
  26. */
  27. this.outputs = [];
  28. }
  29. Node.prototype = Object.create(RoundedBox.prototype);
  30. /**
  31. * This method should be used for the node to register their socket inputs/outputs.
  32. *
  33. * It is called automatically after the node is added to the node graph to create sockets.
  34. */
  35. Node.prototype.registerSockets = null;
  36. /**
  37. * Add input to this node, can be connected to other nodes to receive data.
  38. *
  39. * @param {string} type Data type of the node socket.
  40. * @param {string} name Name of the node socket.
  41. * @return {NodeSocket} Node socket created for this node.
  42. */
  43. Node.prototype.addInput = function(type, name)
  44. {
  45. var socket = new NodeSocket(this, NodeSocket.INPUT, type, name);
  46. this.inputs.push(socket);
  47. this.parent.add(socket);
  48. return socket;
  49. };
  50. /**
  51. * Add output socket to this node, can be connected to other nodes to send data.
  52. *
  53. * @param {string} type Data type of the node socket.
  54. * @param {string} name Name of the node socket.
  55. * @return {NodeSocket} Node socket created for this node.
  56. */
  57. Node.prototype.addOutput = function(type, name)
  58. {
  59. var socket = new NodeSocket(this, NodeSocket.OUTPUT, type, name);
  60. this.outputs.push(socket);
  61. this.parent.add(socket);
  62. return socket;
  63. };
  64. /**
  65. * Get a output socket by its name. If there are multiple sockets with the same name only the first one found is returned.
  66. *
  67. * @param {string} name Name of the node socket to get.
  68. * @return {NodeSocket} Node socket if it was found, null otherwise.
  69. */
  70. Node.prototype.getOutput = function(name)
  71. {
  72. for(var i = 0; i < this.outputs.length; i++)
  73. {
  74. if(this.outputs[i].name === name)
  75. {
  76. return this.outputs[i];
  77. }
  78. }
  79. return null;
  80. };
  81. /**
  82. * Get a input socket by its name. If there are multiple sockets with the same name only the first one found is returned.
  83. *
  84. * @param {string} name Name of the node socket to get.
  85. * @return {NodeSocket} Node socket if it was found, null otherwise.
  86. */
  87. Node.prototype.getInput = function(name)
  88. {
  89. for(var i = 0; i < this.inputs.length; i++)
  90. {
  91. if(this.inputs[i].name === name)
  92. {
  93. return this.inputs[i];
  94. }
  95. }
  96. return null;
  97. };
  98. Node.prototype.destroy = function()
  99. {
  100. RoundedBox.prototype.destroy.call(this);
  101. for(var i = 0; i < this.inputs.length; i++)
  102. {
  103. this.inputs[i].destroy();
  104. }
  105. for(var i = 0; i < this.outputs.length; i++)
  106. {
  107. this.outputs[i].destroy();
  108. }
  109. };
  110. Node.prototype.onUpdate = function()
  111. {
  112. var height = this.box.max.y - this.box.min.y;
  113. // Input hooks position
  114. var step = height / (this.inputs.length + 1);
  115. var start = this.box.min.y + step;
  116. for(var i = 0; i < this.inputs.length; i++)
  117. {
  118. this.inputs[i].position.set(this.position.x + this.box.min.x, this.position.y + (start + step * i));
  119. }
  120. // Output hooks position
  121. step = height / (this.outputs.length + 1);
  122. start = this.box.min.y + step;
  123. for(var i = 0; i < this.outputs.length; i++)
  124. {
  125. this.outputs[i].position.set(this.position.x + this.box.max.x, this.position.y + (start + step * i));
  126. }
  127. };
  128. export {Node};