Skip to content

Commit d3f31c8

Browse files
Fixed square edges not aligning with grid
1 parent fcbbd50 commit d3f31c8

File tree

5 files changed

+22
-19
lines changed

5 files changed

+22
-19
lines changed

src/canvas-extensions/advanced-styles/edge-pathfinding-methods/edge-pathfinding-method.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Canvas, Position, Side } from "src/@types/Canvas"
22
import AdvancedCanvasPlugin from "src/main"
33

44
export default abstract class EdgePathfindingMethod {
5-
abstract getPath(plugin: AdvancedCanvasPlugin, canvas: Canvas, fromPos: Position, fromSide: Side, toPos: Position, toSide: Side, isDragging: boolean): EdgePath | null
5+
abstract getPath(plugin: AdvancedCanvasPlugin, canvas: Canvas, fromPos: Position, fromBBoxSidePos: Position, fromSide: Side, toPos: Position, toBBoxSidePos: Position, toSide: Side, isDragging: boolean): EdgePath | null
66
}
77

88
export interface EdgePath {

src/canvas-extensions/advanced-styles/edge-pathfinding-methods/pathfinding-a-star.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Node {
3535
}
3636

3737
export default class EdgePathfindingAStar extends EdgePathfindingMethod {
38-
getPath(plugin: AdvancedCanvasPlugin, canvas: Canvas, fromPos: Position, fromSide: Side, toPos: Position, toSide: Side, isDragging: boolean): EdgePath | null {
38+
getPath(plugin: AdvancedCanvasPlugin, canvas: Canvas, fromPos: Position, _fromBBoxSidePos: Position, fromSide: Side, toPos: Position, _toBBoxSidePos: Position, toSide: Side, isDragging: boolean): EdgePath | null {
3939
if (isDragging && !plugin.settings.getSetting('edgeStylePathfinderPathLiveUpdate')) return null
4040

4141
const nodeBBoxes = [...canvas.nodes.values()]

src/canvas-extensions/advanced-styles/edge-pathfinding-methods/pathfinding-direct.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import SvgPathHelper from "src/utils/svg-path-helper"
44
import AdvancedCanvasPlugin from "src/main"
55

66
export default class EdgePathfindingDirect extends EdgePathfindingMethod {
7-
getPath(_plugin: AdvancedCanvasPlugin, _canvas: Canvas, fromPos: Position, _fromSide: Side, toPos: Position, _toSide: Side, _isDragging: boolean): EdgePath {
7+
getPath(_plugin: AdvancedCanvasPlugin, _canvas: Canvas, fromPos: Position, _fromBBoxSidePos: Position, _fromSide: Side, toPos: Position, _toBBoxSidePos: Position, _toSide: Side, _isDragging: boolean): EdgePath {
88
return {
99
svgPath: SvgPathHelper.pathArrayToSvgPath([fromPos, toPos], false),
1010
center: {

src/canvas-extensions/advanced-styles/edge-pathfinding-methods/pathfinding-square.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import BBoxHelper from "src/utils/bbox-helper"
66
import CanvasHelper from "src/utils/canvas-helper"
77

88
export default class EdgePathfindingSquare extends EdgePathfindingMethod {
9-
getPath(_plugin: AdvancedCanvasPlugin, _canvas: Canvas, fromPos: Position, fromSide: Side, toPos: Position, toSide: Side, _isDragging: boolean): EdgePath {
9+
getPath(_plugin: AdvancedCanvasPlugin, _canvas: Canvas, fromPos: Position, fromBBoxSidePos: Position, fromSide: Side, toPos: Position, toBBoxSidePos: Position, toSide: Side, _isDragging: boolean): EdgePath {
1010
let pathArray: Position[] = []
1111

1212
if (fromSide === toSide) {
@@ -16,15 +16,15 @@ export default class EdgePathfindingSquare extends EdgePathfindingMethod {
1616
if (BBoxHelper.isHorizontal(fromSide)) {
1717
pathArray = [
1818
fromPos,
19-
{ x: Math.max(fromPos.x, toPos.x) + direction * CanvasHelper.GRID_SIZE, y: fromPos.y },
20-
{ x: Math.max(fromPos.x, toPos.x) + direction * CanvasHelper.GRID_SIZE, y: toPos.y },
19+
{ x: Math.max(fromBBoxSidePos.x, toBBoxSidePos.x) + direction * CanvasHelper.GRID_SIZE, y: fromBBoxSidePos.y },
20+
{ x: Math.max(fromBBoxSidePos.x, toBBoxSidePos.x) + direction * CanvasHelper.GRID_SIZE, y: toBBoxSidePos.y },
2121
toPos
2222
]
2323
} else {
2424
pathArray = [
2525
fromPos,
26-
{ x: fromPos.x, y: Math.max(fromPos.y, toPos.y) + direction * CanvasHelper.GRID_SIZE },
27-
{ x: toPos.x, y: Math.max(fromPos.y, toPos.y) + direction * CanvasHelper.GRID_SIZE },
26+
{ x: fromBBoxSidePos.x, y: Math.max(fromBBoxSidePos.y, toBBoxSidePos.y) + direction * CanvasHelper.GRID_SIZE },
27+
{ x: toBBoxSidePos.x, y: Math.max(fromBBoxSidePos.y, toBBoxSidePos.y) + direction * CanvasHelper.GRID_SIZE },
2828
toPos
2929
]
3030
}
@@ -33,15 +33,15 @@ export default class EdgePathfindingSquare extends EdgePathfindingMethod {
3333
if (BBoxHelper.isHorizontal(fromSide)) {
3434
pathArray = [
3535
fromPos,
36-
{ x: fromPos.x + (toPos.x - fromPos.x) / 2, y: fromPos.y },
37-
{ x: fromPos.x + (toPos.x - fromPos.x) / 2, y: toPos.y },
36+
{ x: fromBBoxSidePos.x + (toBBoxSidePos.x - fromBBoxSidePos.x) / 2, y: fromBBoxSidePos.y },
37+
{ x: fromBBoxSidePos.x + (toBBoxSidePos.x - fromBBoxSidePos.x) / 2, y: toBBoxSidePos.y },
3838
toPos
3939
]
4040
} else {
4141
pathArray = [
4242
fromPos,
43-
{ x: fromPos.x, y: fromPos.y + (toPos.y - fromPos.y) / 2 },
44-
{ x: toPos.x, y: fromPos.y + (toPos.y - fromPos.y) / 2 },
43+
{ x: fromBBoxSidePos.x, y: fromBBoxSidePos.y + (toBBoxSidePos.y - fromBBoxSidePos.y) / 2 },
44+
{ x: toBBoxSidePos.x, y: fromBBoxSidePos.y + (toBBoxSidePos.y - fromBBoxSidePos.y) / 2 },
4545
toPos
4646
]
4747
}
@@ -50,13 +50,13 @@ export default class EdgePathfindingSquare extends EdgePathfindingMethod {
5050
if (BBoxHelper.isHorizontal(fromSide)) {
5151
pathArray = [
5252
fromPos,
53-
{ x: toPos.x, y: fromPos.y },
53+
{ x: toBBoxSidePos.x, y: fromBBoxSidePos.y },
5454
toPos
5555
]
5656
} else {
5757
pathArray = [
5858
fromPos,
59-
{ x: fromPos.x, y: toPos.y },
59+
{ x: fromBBoxSidePos.x, y: toBBoxSidePos.y },
6060
toPos
6161
]
6262
}
@@ -65,8 +65,8 @@ export default class EdgePathfindingSquare extends EdgePathfindingMethod {
6565
return {
6666
svgPath: SvgPathHelper.pathArrayToSvgPath(pathArray, false),
6767
center: {
68-
x: (fromPos.x + toPos.x) / 2,
69-
y: (fromPos.y + toPos.y) / 2
68+
x: (fromBBoxSidePos.x + toBBoxSidePos.x) / 2,
69+
y: (fromBBoxSidePos.y + toBBoxSidePos.y) / 2
7070
},
7171
rotateArrows: false
7272
}

src/canvas-extensions/advanced-styles/edge-styles.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,17 @@ export default class EdgeStylesExtension extends CanvasExtension {
114114
// Set pathfinding method
115115
const pathfindingMethod = edgeData.styleAttributes?.pathfindingMethod
116116
if (pathfindingMethod) {
117+
const fromBBoxSidePos = BBoxHelper.getCenterOfBBoxSide(edge.from.node.getBBox(), edge.from.side)
117118
const fromPos = edge.from.end === 'none' ?
118-
BBoxHelper.getCenterOfBBoxSide(edge.from.node.getBBox(), edge.from.side) :
119+
fromBBoxSidePos :
119120
edge.bezier.from
121+
122+
const toBBoxSidePos = BBoxHelper.getCenterOfBBoxSide(edge.to.node.getBBox(), edge.to.side)
120123
const toPos = edge.to.end === 'none' ?
121-
BBoxHelper.getCenterOfBBoxSide(edge.to.node.getBBox(), edge.to.side) :
124+
toBBoxSidePos :
122125
edge.bezier.to
123126

124-
const path = new EDGE_PATHFINDING_METHODS[pathfindingMethod]().getPath(this.plugin, canvas, fromPos, edge.from.side, toPos, edge.to.side, canvas.isDragging)
127+
const path = new EDGE_PATHFINDING_METHODS[pathfindingMethod]().getPath(this.plugin, canvas, fromPos, fromBBoxSidePos, edge.from.side, toPos, toBBoxSidePos, edge.to.side, canvas.isDragging)
125128
if (!path) return
126129

127130
edge.center = path.center

0 commit comments

Comments
 (0)