From 92049b4cd56d82456081d2bb4e73279a93822900 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Sun, 18 Sep 2022 09:00:36 +0000 Subject: [PATCH] can't connect nodes that are already connected --- static/diagram.js | 25 +++++++++++++++++++------ static/diagram.ts | 16 ++++++++++++++-- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/static/diagram.js b/static/diagram.js index a8e9620..9c75074 100644 --- a/static/diagram.js +++ b/static/diagram.js @@ -190,17 +190,26 @@ var Diagrams = /** @class */ (function () { continue; } if (node.pointInInputCircle(this.worldX, this.worldY)) { - this.addConnection(this.makingConnectionNode, node); + var connectionExists = false; + for (var _b = 0, _c = this.connections; _b < _c.length; _b++) { + var _d = _c[_b], output = _d[0], input = _d[1]; + if (this.makingConnectionNode.id == output.id && node.id == input.id) { + connectionExists = true; + } + } + if (!connectionExists) { + this.addConnection(this.makingConnectionNode, node); + } } } this.makingConnectionNode = null; } - for (var _b = 0, _c = this.connections; _b < _c.length; _b++) { - var _d = _c[_b], output = _d[0], input = _d[1]; - var _e = output.getOutputCircleXY(), outputX = _e[0], outputY = _e[1]; + for (var _e = 0, _f = this.connections; _e < _f.length; _e++) { + var _g = _f[_e], output = _g[0], input = _g[1]; + var _h = output.getOutputCircleXY(), outputX = _h[0], outputY = _h[1]; outputX += this.cameraX; outputY += this.cameraY; - var _f = input.getInputCircleXY(), inputX = _f[0], inputY = _f[1]; + var _j = input.getInputCircleXY(), inputX = _j[0], inputY = _j[1]; inputX += this.cameraX; inputY += this.cameraY; var dX = Math.abs(outputX - inputX); @@ -217,7 +226,8 @@ var Diagrams = /** @class */ (function () { var halfway = getBezierXY(0.5, outputX, outputY, cp1x, cp1y, cp2x, cp2y, inputX, inputY); var mouseOnHalfway = Math.pow(this.mouseX - halfway.x, 2) + Math.pow(this.mouseY - halfway.y, 2) <= 10 * 10; if (mouseOnHalfway) { - this.connections.splice(this.connections.indexOf([output, input]), 1); + this.removeConnection(output, input); + break; } } this.draw(); @@ -311,6 +321,9 @@ var Diagrams = /** @class */ (function () { Diagrams.prototype.addConnection = function (A, B) { this.connections.push([A, B]); }; + Diagrams.prototype.removeConnection = function (A, B) { + this.connections.splice(this.connections.indexOf([A, B]), 1); + }; Diagrams.prototype.drawDiagramNode = function (node) { }; Diagrams.prototype.fillParent = function () { diff --git a/static/diagram.ts b/static/diagram.ts index c87e669..35549a6 100644 --- a/static/diagram.ts +++ b/static/diagram.ts @@ -226,7 +226,15 @@ class Diagrams { continue; } if(node.pointInInputCircle(this.worldX, this.worldY)){ - this.addConnection(this.makingConnectionNode, node); + let connectionExists = false; + for (let [output, input] of this.connections) { + if (this.makingConnectionNode.id == output.id && node.id == input.id){ + connectionExists = true; + } + } + if (!connectionExists){ + this.addConnection(this.makingConnectionNode, node); + } } } this.makingConnectionNode = null; @@ -260,7 +268,8 @@ class Diagrams { let halfway = getBezierXY(0.5, outputX, outputY, cp1x, cp1y, cp2x, cp2y, inputX, inputY) let mouseOnHalfway = Math.pow(this.mouseX - halfway.x, 2) + Math.pow(this.mouseY - halfway.y, 2) <= 10*10 if (mouseOnHalfway){ - this.connections.splice(this.connections.indexOf([output, input]), 1) + this.removeConnection(output, input); + break; } } @@ -376,6 +385,9 @@ class Diagrams { addConnection(A: DiagramNode, B: DiagramNode){ this.connections.push([A, B]); } + removeConnection(A: DiagramNode, B: DiagramNode){ + this.connections.splice(this.connections.indexOf([A, B]), 1); + } drawDiagramNode(node: DiagramNode){