Node.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {Box} from "../Box";
  2. import {Vector2} from "../../math/Vector2";
  3. import {NodeHook} from "./NodeHook";
  4. /**
  5. * Node objects can be connected between them to create graphs.
  6. *
  7. * 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.
  8. *
  9. * This class implements node basic functionality, the logic to connected node and define inputs/outputs of the nodes.
  10. *
  11. * @class Node
  12. */
  13. function Node()
  14. {
  15. Box.call(this);
  16. this.draggable = true;
  17. /**
  18. * List of inputs of the node.
  19. *
  20. * @type {NodeHook[]}
  21. */
  22. this.inputs = [];
  23. /**
  24. * List of outputs of the node.
  25. *
  26. * @type {NodeHook[]}
  27. */
  28. this.outputs = [];
  29. }
  30. Node.prototype = Object.create(Box.prototype);
  31. /**
  32. * Add input to this node.
  33. *
  34. * @param type
  35. */
  36. Node.prototype.addInput = function(type)
  37. {
  38. var hook = new NodeHook(this, NodeHook.INPUT);
  39. hook.type = type;
  40. this.inputs.push(hook);
  41. this.add(hook);
  42. };
  43. /**
  44. * Add output hook to this node.
  45. *
  46. * @param type
  47. */
  48. Node.prototype.addOutput = function(type)
  49. {
  50. var hook = new NodeHook(this, NodeHook.OUTPUT);
  51. hook.type = type;
  52. this.outputs.push(hook);
  53. this.add(hook);
  54. };
  55. Node.prototype.draw = function(context, viewport, canvas)
  56. {
  57. var height = this.box.max.y - this.box.min.y;
  58. // Input hooks position
  59. var step = height / (this.inputs.length + 1);
  60. var start = this.box.min.y + step;
  61. for(var i = 0; i < this.inputs.length; i++)
  62. {
  63. this.inputs[i].position.set(this.box.min.x, start + step * i);
  64. }
  65. // Output hooks position
  66. var step = height / (this.outputs.length + 1);
  67. var start = this.box.min.y + step;
  68. for(var i = 0; i < this.outputs.length; i++)
  69. {
  70. this.outputs[i].position.set(this.box.max.x, start + step * i);
  71. }
  72. Box.prototype.draw.call(this, context, viewport, canvas);
  73. };
  74. export {Node};