From 4b107e772c20292a50472052676fca41973bab71 Mon Sep 17 00:00:00 2001 From: STEINNI Date: Mon, 2 Mar 2026 20:45:55 +0000 Subject: [PATCH] graflow: refacto & facto of buildSegment --- bzGraflow.js | 69 +++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 39 deletions(-) diff --git a/bzGraflow.js b/bzGraflow.js index fccb011..b87b54f 100644 --- a/bzGraflow.js +++ b/bzGraflow.js @@ -459,6 +459,24 @@ class BZgraflow extends Buildoz{ return({ x: x - r.left, y: y - r.top }) } + emitWireSegment(x1, y1, c1x, c1y, c2x, c2y, x2, y2, wireType, startAxis, loop=false){ + if(loop) wireType = 'bezier' // loops only use bezier to look good + + if(wireType === 'bezier'){ + return(`C ${c1x} ${c1y}, ${c2x} ${c2y}, ${x2} ${y2}`) + } + if(wireType === 'straight'){ + return(`L ${c1x} ${c1y} L ${c2x} ${c2y} L ${x2} ${y2}`) + } + if(wireType === 'ortho'){ + if(startAxis === 'v'){ + return(`V ${(c1y + c2y) / 2} H ${c2x} V ${y2}`) + } + return(`H ${(c1x + c2x) / 2} V ${c2y} H ${x2}`) + } + return('') + } + linkNodes(idNode1, idPort1, idNode2, idPort2) { const tension = parseInt(this.getBZAttribute('tension')) || 60 const wireType = this.getBZAttribute('wiretype') || 'bezier' @@ -494,23 +512,10 @@ class BZgraflow extends Buildoz{ c2y -= 1*tension } } - - if((wireType === 'bezier') || loop) { // loops only use bezier to look good - return(`M ${x1} ${y1} C ${c1x} ${c1y}, ${c2x} ${c2y}, ${x2} ${y2}`) - } - if((wireType === 'straight') && (!loop)) { - return(`M ${x1} ${y1} L ${c1x} ${c1y} L ${c2x} ${c2y} L ${x2} ${y2}`) - } - if((wireType === 'ortho') && (!loop)) { - let path = `M ${x1} ${y1} ` - if(['n', 's'].includes(port1.direction)) { - path += `V ${(c1y+c2y)/2} H ${c2x} V ${y2}` - } else { - path += `H ${(c1x+c2x)/2} V ${c2y} H ${x2}` - } - return(path) - } - return('') + const startAxis = ['n', 's'].includes(port1.direction) ? 'v' : 'h' + const seg = this.emitWireSegment(x1, y1, c1x, c1y, c2x, c2y, x2, y2, wireType, startAxis, loop) + if(!seg) return('') + return(`M ${x1} ${y1} ${seg}`) } linkInterNodes(idNode1, idPort1, idNode2, idPort2, interNodes, orientation='horizontal') { const tension = parseInt(this.getBZAttribute('tension')) || 60 @@ -528,34 +533,20 @@ class BZgraflow extends Buildoz{ let c1x, c1y, c2x, c2y if(orientation1=='horizontal') { c1x = Math.floor(x1 + tension) - c1y = y1 + c1y = y1 } else { - c1x = x1 + c1x = x1 c1y = Math.floor(y1 + tension) - } + } if(orientation2=='horizontal') { c2x = Math.floor(x2 - tension) - c2y = y2 + c2y = y2 } else { - c2x = x2 - c2y = Math.floor(y2 - tension) + c2x = x2 + c2y = Math.floor(y2 - tension) } - if(wireType === 'bezier') { - return(`C ${c1x} ${c1y}, ${c2x} ${c2y}, ${x2} ${y2}`) - } - if(wireType === 'straight') { - return(`L ${c1x} ${c1y} L ${c2x} ${c2y} L ${x2} ${y2}`) - } - if(wireType === 'ortho') { - let path = `M ${x1} ${y1} ` - if(['n', 's'].includes(port1.direction)) { - path += `V ${(c1y+c2y)/2} H ${c2x} V ${y2}` - } else { - path += `H ${(c1x+c2x)/2} V ${c2y} H ${x2}` - } - return(path) - } - return(``) + const startAxis = (orientation1=='vertical') ? 'v' : 'h' + return(this.emitWireSegment(x1, y1, c1x, c1y, c2x, c2y, x2, y2, wireType, startAxis, false)) } // Start/end points in SVG coords (works for both bezier and line)