NodeConnector.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {BezierCurve} from "../BezierCurve";
  2. import {Object2D} from "../../Object2D";
  3. import {NodeGraph} from "./NodeGraph";
  4. /**
  5. * Node connector is used to connect a output of a node to a input of another node.
  6. *
  7. * Some nodes outputs might support multiple connections having an output connected to multiple inputs.
  8. *
  9. * Data always goes from the output node to a input node.
  10. *
  11. * @class
  12. * @extends {BezierCurve}
  13. */
  14. function NodeConnector()
  15. {
  16. BezierCurve.call(this);
  17. /**
  18. * Width of the connector line.
  19. *
  20. * @type {number}
  21. */
  22. this.lineWidth = 2;
  23. /**
  24. * Origin output socket that is attached to a node.
  25. *
  26. * @type {NodeSocket}
  27. */
  28. this.outputSocket = null;
  29. /**
  30. * Destination input socket that is attached to a node.
  31. *
  32. * @type {NodeSocket}
  33. */
  34. this.inputSocket = null;
  35. }
  36. NodeConnector.prototype = Object.create(BezierCurve.prototype);
  37. NodeConnector.prototype.constructor = NodeConnector;
  38. NodeConnector.prototype.type = "NodeConnector";
  39. Object2D.register(NodeConnector, "NodeConnector");
  40. NodeConnector.prototype.destroy = function()
  41. {
  42. BezierCurve.prototype.destroy.call(this);
  43. if(this.outputSocket !== null)
  44. {
  45. this.outputSocket.removeConnector(this);
  46. this.outputSocket = null;
  47. }
  48. if(this.inputSocket !== null)
  49. {
  50. this.inputSocket.removeConnector(this);
  51. this.inputSocket = null;
  52. }
  53. };
  54. NodeConnector.prototype.onUpdate = function()
  55. {
  56. if(this.outputSocket !== null)
  57. {
  58. this.from.copy(this.outputSocket.position);
  59. }
  60. if(this.inputSocket !== null)
  61. {
  62. this.to.copy(this.inputSocket.position);
  63. }
  64. // Center control points
  65. this.fromCp.copy(this.from);
  66. this.fromCp.add(this.to);
  67. this.fromCp.multiplyScalar(0.5);
  68. this.toCp.copy(this.fromCp);
  69. var curvature = 0.5;
  70. // Check vertical/horizontal distances
  71. var yDistance = this.to.y - this.from.y;
  72. var xDistance = this.to.x - this.from.x;
  73. // Apply a offset to the control points
  74. if(Math.abs(xDistance) > Math.abs(yDistance))
  75. {
  76. this.toCp.x += xDistance * curvature;
  77. this.fromCp.x -= xDistance * curvature;
  78. }
  79. else
  80. {
  81. this.toCp.y += yDistance * curvature;
  82. this.fromCp.y -= yDistance * curvature;
  83. }
  84. };
  85. NodeConnector.prototype.serialize = function(recursive)
  86. {
  87. var data = BezierCurve.prototype.serialize.call(this, recursive);
  88. data.outputSocket = this.outputSocket !== null ? this.outputSocket.uuid : null;
  89. data.inputSocket = this.inputSocket !== null ? this.inputSocket.uuid : null;
  90. return data;
  91. };
  92. NodeConnector.prototype.parse = function(data, root)
  93. {
  94. BezierCurve.prototype.parse.call(this, data, root);
  95. if(data.outputSocket !== null)
  96. {
  97. this.outputSocket = root.getChildByUUID(data.outputSocket);
  98. }
  99. if(data.inputSocket !== null)
  100. {
  101. this.inputSocket = root.getChildByUUID(data.inputSocket);
  102. }
  103. };
  104. export {NodeConnector};