NodeGraph.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import {Object2D} from "../../Object2D";
  2. import {Node} from "./Node";
  3. /**
  4. * Node graph object should be used as a container for node elements.
  5. *
  6. * The node graph object specifies how the nodes are processed, each individual node can store and process data, the node graph specified how this information is processed.
  7. *
  8. * All node elements are stored as children of the node graph.
  9. *
  10. * @class
  11. * @extends {Object2D}
  12. */
  13. function NodeGraph()
  14. {
  15. Object2D.call(this);
  16. }
  17. NodeGraph.prototype = Object.create(Object2D.prototype);
  18. NodeGraph.prototype.constructor = NodeGraph;
  19. /**
  20. * Create and add a new node of specific node type to the graph.
  21. *
  22. * Automatically finds an empty space as close as possible to other nodes to add this new node.
  23. *
  24. * @param {Node} node Node object to be added.
  25. * @return {Node} Node created (already added to the graph).
  26. */
  27. NodeGraph.prototype.addNode = function(node)
  28. {
  29. // Check available position on screen.
  30. var x = 0, y = 0;
  31. for(var i = 0; i < this.children.length; i++)
  32. {
  33. if(this.children[i].position.x > x)
  34. {
  35. x = this.children[i].position.x;
  36. }
  37. if(this.children[i].position.y > y)
  38. {
  39. y = this.children[i].position.y;
  40. }
  41. }
  42. // Create and add new node
  43. node.position.set(x + 200, y / 2.0);
  44. this.add(node);
  45. if(node.registerSockets !== null)
  46. {
  47. node.registerSockets();
  48. }
  49. return node;
  50. };
  51. export {NodeGraph};