NodeGraph.js 1.4 KB

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