NodeGraph.js 1.3 KB

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