Skip to content

Commit fcbbd50

Browse files
Drastically improved square edge pathfinding method
1 parent aa3ebaf commit fcbbd50

File tree

5 files changed

+78
-35
lines changed

5 files changed

+78
-35
lines changed

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

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,64 @@ import { Canvas, Position, Side } from "src/@types/Canvas"
22
import EdgePathfindingMethod, { EdgePath } from "./edge-pathfinding-method"
33
import SvgPathHelper from "src/utils/svg-path-helper"
44
import AdvancedCanvasPlugin from "src/main"
5+
import BBoxHelper from "src/utils/bbox-helper"
6+
import CanvasHelper from "src/utils/canvas-helper"
57

68
export default class EdgePathfindingSquare extends EdgePathfindingMethod {
7-
getPath(_plugin: AdvancedCanvasPlugin, _canvas: Canvas, fromPos: Position, fromSide: Side, toPos: Position, _toSide: Side, _isDragging: boolean): EdgePath {
9+
getPath(_plugin: AdvancedCanvasPlugin, _canvas: Canvas, fromPos: Position, fromSide: Side, toPos: Position, toSide: Side, _isDragging: boolean): EdgePath {
810
let pathArray: Position[] = []
9-
if (fromSide === 'bottom' || fromSide === 'top') {
10-
pathArray = [
11-
fromPos,
12-
{ x: fromPos.x, y: fromPos.y + (toPos.y - fromPos.y) / 2 },
13-
{ x: toPos.x, y: fromPos.y + (toPos.y - fromPos.y) / 2 },
14-
toPos
15-
]
11+
12+
if (fromSide === toSide) {
13+
// Same side -> Make a U
14+
const direction = BBoxHelper.direction(fromSide)
15+
16+
if (BBoxHelper.isHorizontal(fromSide)) {
17+
pathArray = [
18+
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 },
21+
toPos
22+
]
23+
} else {
24+
pathArray = [
25+
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 },
28+
toPos
29+
]
30+
}
31+
} else if (BBoxHelper.isHorizontal(fromSide) === BBoxHelper.isHorizontal(toSide)) {
32+
// Same axis, different side -> Make a Z
33+
if (BBoxHelper.isHorizontal(fromSide)) {
34+
pathArray = [
35+
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 },
38+
toPos
39+
]
40+
} else {
41+
pathArray = [
42+
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 },
45+
toPos
46+
]
47+
}
1648
} else {
17-
pathArray = [
18-
fromPos,
19-
{ x: fromPos.x + (toPos.x - fromPos.x) / 2, y: fromPos.y },
20-
{ x: fromPos.x + (toPos.x - fromPos.x) / 2, y: toPos.y },
21-
toPos
22-
]
49+
// Different axis -> Make a L
50+
if (BBoxHelper.isHorizontal(fromSide)) {
51+
pathArray = [
52+
fromPos,
53+
{ x: toPos.x, y: fromPos.y },
54+
toPos
55+
]
56+
} else {
57+
pathArray = [
58+
fromPos,
59+
{ x: fromPos.x, y: toPos.y },
60+
toPos
61+
]
62+
}
2363
}
2464

2565
return {

src/canvas-extensions/better-default-settings-canvas-extension.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CanvasEvent } from "src/core/events"
33
import SettingsManager from "src/settings"
44
import { FileSelectModal } from "src/utils/modal-helper"
55
import CanvasExtension from "../core/canvas-extension"
6+
import CanvasHelper from "src/utils/canvas-helper"
67

78
export default class BetterDefaultSettingsCanvasExtension extends CanvasExtension {
89
isEnabled() { return true }
@@ -61,8 +62,8 @@ export default class BetterDefaultSettingsCanvasExtension extends CanvasExtensi
6162
const file = await new FileSelectModal(this.plugin.app, undefined, true).awaitInput()
6263

6364
if (this.plugin.settings.getSetting('alignDoubleClickedNodeToGrid')) pos = {
64-
x: Math.round((pos.x - (canvas.config.defaultFileNodeDimensions.width / 2)) / 20) * 20 + (canvas.config.defaultFileNodeDimensions.width / 2),
65-
y: Math.round((pos.y - (canvas.config.defaultFileNodeDimensions.height / 2)) / 20) * 20 + (canvas.config.defaultFileNodeDimensions.height / 2)
65+
x: Math.round((pos.x - (canvas.config.defaultFileNodeDimensions.width / 2)) / CanvasHelper.GRID_SIZE) * CanvasHelper.GRID_SIZE + (canvas.config.defaultFileNodeDimensions.width / 2),
66+
y: Math.round((pos.y - (canvas.config.defaultFileNodeDimensions.height / 2)) / CanvasHelper.GRID_SIZE) * CanvasHelper.GRID_SIZE + (canvas.config.defaultFileNodeDimensions.height / 2)
6667
}
6768

6869
canvas.createFileNode({
@@ -74,8 +75,8 @@ export default class BetterDefaultSettingsCanvasExtension extends CanvasExtensi
7475
break
7576
default:
7677
if (this.plugin.settings.getSetting('alignDoubleClickedNodeToGrid')) pos = {
77-
x: Math.round((pos.x - (canvas.config.defaultTextNodeDimensions.width / 2)) / 20) * 20 + (canvas.config.defaultTextNodeDimensions.width / 2),
78-
y: Math.round((pos.y - (canvas.config.defaultTextNodeDimensions.height / 2)) / 20) * 20 + (canvas.config.defaultTextNodeDimensions.height / 2)
78+
x: Math.round((pos.x - (canvas.config.defaultTextNodeDimensions.width / 2)) / CanvasHelper.GRID_SIZE) * CanvasHelper.GRID_SIZE + (canvas.config.defaultTextNodeDimensions.width / 2),
79+
y: Math.round((pos.y - (canvas.config.defaultTextNodeDimensions.height / 2)) / CanvasHelper.GRID_SIZE) * CanvasHelper.GRID_SIZE + (canvas.config.defaultTextNodeDimensions.height / 2)
7980
}
8081

8182
canvas.createTextNode({

src/canvas-extensions/collapsible-groups-canvas-extension.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { BBox, Canvas, CanvasData, CanvasNode } from "src/@types/Canvas"
33
import { CanvasEvent } from "src/core/events"
44
import BBoxHelper from "src/utils/bbox-helper"
55
import CanvasExtension from "../core/canvas-extension"
6+
import CanvasHelper from "src/utils/canvas-helper"
67

78
const COLLAPSE_BUTTON_ID = 'group-collapse-button'
89

@@ -87,9 +88,9 @@ export default class CollapsibleGroupsCanvasExtension extends CanvasExtension {
8788
data.nodes.forEach((groupNodeData) => {
8889
if (!groupNodeData.isCollapsed) return
8990

90-
const groupNodeBBox = BBoxHelper.bboxFromNodeData(groupNodeData)
91+
const groupNodeBBox = CanvasHelper.getBBox([groupNodeData])
9192
const containedNodesData = data.nodes.filter((nodeData) =>
92-
nodeData.id !== groupNodeData.id && BBoxHelper.insideBBox(BBoxHelper.bboxFromNodeData(nodeData), groupNodeBBox, false)
93+
nodeData.id !== groupNodeData.id && BBoxHelper.insideBBox(CanvasHelper.getBBox([nodeData]), groupNodeBBox, false)
9394
)
9495
const containedEdgesData = data.edges.filter((edgeData) => {
9596
return containedNodesData.some((nodeData) => nodeData.id === edgeData.fromNode) ||

src/utils/bbox-helper.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,11 @@ export default class BBoxHelper {
9191
}
9292
}
9393

94-
static bboxFromNodeData(nodeData: CanvasNodeData): BBox {
95-
return {
96-
minX: nodeData.x,
97-
minY: nodeData.y,
98-
maxX: nodeData.x + nodeData.width,
99-
maxY: nodeData.y + nodeData.height
100-
}
94+
static isHorizontal(side: Side): boolean {
95+
return side === 'left' || side === 'right'
96+
}
97+
98+
static direction(side: Side): number {
99+
return (side === 'right' || side === 'bottom') ? 1 : -1
101100
}
102101
}

src/utils/canvas-helper.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ export interface MenuOption {
1212
}
1313

1414
export default class CanvasHelper {
15-
static canvasCommand(plugin: AdvancedCanvasPlugin, check: (canvas: Canvas) => boolean, run: (canvas: Canvas) => void): (checking: boolean) => boolean {
16-
return (checking: boolean) => {
17-
const canvas = plugin.getCurrentCanvas()
18-
if (checking) return canvas !== null && check(canvas)
15+
static readonly GRID_SIZE = 20
1916

20-
if (canvas) run(canvas)
17+
static canvasCommand(plugin: AdvancedCanvasPlugin, check: (canvas: Canvas) => boolean, run: (canvas: Canvas) => void): (checking: boolean) => boolean {
18+
return (checking: boolean) => {
19+
const canvas = plugin.getCurrentCanvas()
20+
if (checking) return canvas !== null && check(canvas)
2121

22-
return true
22+
if (canvas) run(canvas)
23+
24+
return true
25+
}
2326
}
24-
}
2527

2628
static createQuickSettingsButton(menuOption: MenuOption): HTMLElement {
2729
const quickSetting = document.createElement('div')
@@ -128,7 +130,7 @@ static canvasCommand(plugin: AdvancedCanvasPlugin, check: (canvas: Canvas) => bo
128130
}
129131
}
130132

131-
static getBBox(canvasNodes: (CanvasNode|CanvasNodeData)[]) {
133+
static getBBox(canvasNodes: (CanvasNode | CanvasNodeData)[]) {
132134
let minX = Infinity
133135
let minY = Infinity
134136
let maxX = -Infinity

0 commit comments

Comments
 (0)