flowGraphConnection.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { Tools } from "../Misc/tools.js";
  2. import { RandomGUID } from "../Misc/guid.js";
  3. /**
  4. * @experimental
  5. * The type of a connection point - inpput or output.
  6. */
  7. export var FlowGraphConnectionType;
  8. (function (FlowGraphConnectionType) {
  9. FlowGraphConnectionType[FlowGraphConnectionType["Input"] = 0] = "Input";
  10. FlowGraphConnectionType[FlowGraphConnectionType["Output"] = 1] = "Output";
  11. })(FlowGraphConnectionType || (FlowGraphConnectionType = {}));
  12. /**
  13. * @experimental
  14. * The base connection class.
  15. */
  16. export class FlowGraphConnection {
  17. constructor(name, _connectionType,
  18. /* @internal */ _ownerBlock) {
  19. this._ownerBlock = _ownerBlock;
  20. /** @internal */
  21. this._connectedPoint = [];
  22. /**
  23. * A uniquely identifying string for the connection.
  24. */
  25. this.uniqueId = RandomGUID();
  26. /**
  27. * Used for parsing connections.
  28. * @internal
  29. */
  30. // disable warning as this is used for parsing
  31. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  32. this.connectedPointIds = [];
  33. this.name = name;
  34. this._connectionType = _connectionType;
  35. }
  36. /**
  37. * The type of the connection
  38. */
  39. get connectionType() {
  40. return this._connectionType;
  41. }
  42. /**
  43. * @internal
  44. * Override this to indicate if a point can connect to more than one point.
  45. */
  46. _isSingularConnection() {
  47. return true;
  48. }
  49. /**
  50. * Returns if a point is connected to any other point.
  51. * @returns boolean indicating if the point is connected.
  52. */
  53. isConnected() {
  54. return this._connectedPoint.length > 0;
  55. }
  56. /**
  57. * Connects two connections together.
  58. * @param point the connection to connect to.
  59. */
  60. connectTo(point) {
  61. if (this._connectionType === point._connectionType) {
  62. throw new Error(`Cannot connect two points of type ${this.connectionType}`);
  63. }
  64. if ((this._isSingularConnection() && this._connectedPoint.length > 0) || (point._isSingularConnection() && point._connectedPoint.length > 0)) {
  65. throw new Error("Max number of connections for point reached");
  66. }
  67. this._connectedPoint.push(point);
  68. point._connectedPoint.push(this);
  69. }
  70. /**
  71. * Saves the connection to a JSON object.
  72. * @param serializationObject the object to serialize to.
  73. */
  74. serialize(serializationObject = {}) {
  75. serializationObject.uniqueId = this.uniqueId;
  76. serializationObject.name = this.name;
  77. serializationObject._connectionType = this._connectionType;
  78. serializationObject.connectedPointIds = [];
  79. serializationObject.className = this.getClassName();
  80. for (const point of this._connectedPoint) {
  81. serializationObject.connectedPointIds.push(point.uniqueId);
  82. }
  83. }
  84. /**
  85. * @returns class name of the connection.
  86. */
  87. getClassName() {
  88. return "FGConnection";
  89. }
  90. /**
  91. * Deserialize from a object into this
  92. * @param serializationObject the object to deserialize from.
  93. */
  94. deserialize(serializationObject) {
  95. this.uniqueId = serializationObject.uniqueId;
  96. this.name = serializationObject.name;
  97. this._connectionType = serializationObject._connectionType;
  98. this.connectedPointIds = serializationObject.connectedPointIds;
  99. }
  100. /**
  101. * Parses a connection from an object
  102. * @param serializationObject the object to parse from.
  103. * @param ownerBlock the block that owns the connection.
  104. * @returns the parsed connection.
  105. */
  106. static Parse(serializationObject = {}, ownerBlock) {
  107. const type = Tools.Instantiate(serializationObject.className);
  108. const connection = new type(serializationObject.name, serializationObject._connectionType, ownerBlock);
  109. connection.deserialize(serializationObject);
  110. return connection;
  111. }
  112. }
  113. //# sourceMappingURL=flowGraphConnection.js.map