2
0

NodeHook.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {Circle} from "../Circle";
  2. import {Node} from "./Node";
  3. import {NodeConnector} from "./NodeConnector";
  4. import {Object2D} from "../../Object2D";
  5. /**
  6. * Represents a node hook point. Is attached to the node element and represented visually.
  7. *
  8. * Can be used as a node input, output or as a bidirectional connection.
  9. *
  10. * @class NodeHook
  11. * @param {Node} node Node of this hook.
  12. * @param {number} direction Direction of the hook.
  13. */
  14. function NodeHook(node, direction)
  15. {
  16. Circle.call(this);
  17. this.draggable = true;
  18. this.radius = 6;
  19. this.layer = 1;
  20. /**
  21. * Name of the hook presented to the user.
  22. */
  23. this.name = "";
  24. /**
  25. * Type of hook. Hooks of the same type can be connected.
  26. *
  27. * @type {string}
  28. */
  29. this.type = "";
  30. /**
  31. * If set true the hook can be connected to multiple hooks.
  32. *
  33. * @type {boolean}
  34. */
  35. this.multiple = false;
  36. /**
  37. * Direction of the node hook.
  38. *
  39. * @type {number}
  40. */
  41. this.direction = direction;
  42. /**
  43. * Node where this input element in attached.
  44. *
  45. * @type {Node}
  46. */
  47. this.node = node;
  48. /**
  49. * Node connector used to connect this hook to another node hook.
  50. *
  51. * @type {NodeConnector}
  52. */
  53. this.connector = null;
  54. }
  55. /**
  56. * Input hook can only be connected to an output.
  57. *
  58. * Is used to read data from the output.
  59. *
  60. * @type {number}
  61. */
  62. NodeHook.INPUT = 1;
  63. /**
  64. * Output hook can only be connected to an input.
  65. *
  66. * Writes data to the output.
  67. *
  68. * @type {number}
  69. */
  70. NodeHook.OUTPUT = 2;
  71. /**
  72. * Bidirectional hook can be connected to any type of hook.
  73. *
  74. * Can be used to write and read from inputs and/or outputs.
  75. *
  76. * @type {number}
  77. */
  78. NodeHook.BIDIRECTIONAL = 3;
  79. NodeHook.prototype = Object.create(Circle.prototype);
  80. NodeHook.prototype.onButtonDown = function()
  81. {
  82. if(this.connector === null)
  83. {
  84. // TODO <REMOVE THIS>
  85. console.log("Create a new connector.")
  86. var connector = new NodeConnector();
  87. if(this.direction === NodeHook.INPUT)
  88. {
  89. connector.inputHook = this;
  90. }
  91. else if(this.direction === NodeHook.OUTPUT)
  92. {
  93. connector.outputHook = this;
  94. }
  95. this.connector = connector;
  96. this.parent.add(connector);
  97. }
  98. };
  99. NodeHook.prototype.onPointerDrag = function(pointer, viewport, delta)
  100. {
  101. if(this.connector !== null)
  102. {
  103. var position = viewport.inverseMatrix.transformPoint(pointer.position);
  104. if(this.direction === NodeHook.INPUT)
  105. {
  106. this.connector.from.copy(position);
  107. }
  108. else if(this.direction === NodeHook.OUTPUT)
  109. {
  110. this.connector.to.copy(position);
  111. }
  112. // TODO <REMOVE THIS>
  113. console.log("Dragging around");
  114. }
  115. };
  116. NodeHook.prototype.onPointerDragEnd = function(pointer, viewport)
  117. {
  118. if(this.connector !== null)
  119. {
  120. //
  121. // TODO <REMOVE THIS>
  122. console.log("Finished drag.");
  123. }
  124. };
  125. export {NodeHook};