Node.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {Box} from "../Box";
  2. import {NodeSocket} from "./NodeSocket";
  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 connected node and define inputs/outputs of the nodes.
  9. *
  10. * @class Node
  11. */
  12. function Node()
  13. {
  14. Box.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(Box.prototype);
  30. /**
  31. * Add input to this node.
  32. *
  33. * @param type
  34. */
  35. Node.prototype.addInput = function(type)
  36. {
  37. var hook = new NodeSocket(this, NodeSocket.INPUT);
  38. hook.type = type;
  39. this.inputs.push(hook);
  40. this.parent.add(hook);
  41. };
  42. /**
  43. * Add output hook to this node.
  44. *
  45. * @param type
  46. */
  47. Node.prototype.addOutput = function(type)
  48. {
  49. var hook = new NodeSocket(this, NodeSocket.OUTPUT);
  50. hook.type = type;
  51. this.outputs.push(hook);
  52. this.parent.add(hook);
  53. };
  54. Node.prototype.onUpdate = function()
  55. {
  56. var height = this.box.max.y - this.box.min.y;
  57. // Input hooks position
  58. var step = height / (this.inputs.length + 1);
  59. var start = this.box.min.y + step;
  60. for(var i = 0; i < this.inputs.length; i++)
  61. {
  62. this.inputs[i].position.set(this.position.x + this.box.min.x, this.position.y + (start + step * i));
  63. }
  64. // Output hooks position
  65. step = height / (this.outputs.length + 1);
  66. start = this.box.min.y + step;
  67. for(var i = 0; i < this.outputs.length; i++)
  68. {
  69. this.outputs[i].position.set(this.position.x + this.box.max.x, this.position.y + (start + step * i));
  70. }
  71. };
  72. export {Node};