NodeGraph.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. NodeGraph.prototype.type = "NodeGraph";
  20. /**
  21. * Create and add a new node of specific node type to the graph.
  22. *
  23. * Automatically finds an empty space as close as possible to other nodes to add this new node.
  24. *
  25. * @param {Node} node Node object to be added.
  26. * @return {Node} Node created (already added to the graph).
  27. */
  28. NodeGraph.prototype.addNode = function(node)
  29. {
  30. // Check available position on screen.
  31. var x = 0, y = 0;
  32. for(var i = 0; i < this.children.length; i++)
  33. {
  34. if(this.children[i].position.x > x)
  35. {
  36. x = this.children[i].position.x;
  37. }
  38. if(this.children[i].position.y > y)
  39. {
  40. y = this.children[i].position.y;
  41. }
  42. }
  43. // Create and add new node
  44. node.position.set(x + 200, y / 2.0);
  45. this.add(node);
  46. if(node.registerSockets !== null)
  47. {
  48. node.registerSockets();
  49. }
  50. return node;
  51. };
  52. export {NodeGraph};