-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathCurvedLinkReshapingTool.js
105 lines (104 loc) · 4.33 KB
/
CurvedLinkReshapingTool.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright 1998-2025 by Northwoods Software Corporation. All Rights Reserved.
*/
/*
* This is an extension and not part of the main GoJS library.
* The source code for this is at extensionsJSM/CurvedLinkReshapingTool.ts.
* Note that the API for this class may change with any version, even point releases.
* If you intend to use an extension in production, you should copy the code to your own source directory.
* Extensions can be found in the GoJS kit under the extensions or extensionsJSM folders.
* See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
*/
/**
* This CurvedLinkReshapingTool class allows for a {@link go.Link}'s path to be modified by the user
* via the dragging of a single tool handle at the middle of the link.
* Dragging the handle changes the value of {@link go.Link.curviness}.
*
* If you want to experiment with this extension, try the <a href="../../samples/CurvedLinkReshaping.html">Curved Link Reshaping</a> sample.
* @category Tool Extension
*/
class CurvedLinkReshapingTool extends go.LinkReshapingTool {
constructor(init) {
super();
this._originalCurviness = NaN;
if (init)
Object.assign(this, init);
}
/**
* @hidden @internal
*/
makeAdornment(pathshape) {
const link = pathshape.part;
if (link !== null && link.curve === go.Curve.Bezier && link.pointsCount === 4) {
const adornment = new go.Adornment();
adornment.type = go.Panel.Link;
const h = this.makeHandle(pathshape, 0);
if (h !== null) {
this.setReshapingBehavior(h, go.ReshapingBehavior.All);
h.cursor = 'move';
adornment.add(h);
}
adornment.category = this.name;
adornment.adornedObject = pathshape;
return adornment;
}
else {
return super.makeAdornment(pathshape);
}
}
/**
* Start reshaping, if {@link findToolHandleAt} finds a reshape handle at the mouse down point.
*
* If successful this sets {@link handle} to be the reshape handle that it finds
* and {@link adornedLink} to be the {@link go.Link} being routed.
* It also remembers the original link route (a list of Points) and curviness in case this tool is cancelled.
* And it starts a transaction.
*/
doActivate() {
super.doActivate();
if (this.adornedLink !== null)
this._originalCurviness = this.adornedLink.curviness;
}
/**
* Restore the link route to be the original points and curviness and stop this tool.
*/
doCancel() {
if (this.adornedLink !== null)
this.adornedLink.curviness = this._originalCurviness;
super.doCancel();
}
/**
* Change the route of the {@link adornedLink} by moving the point corresponding to the current
* {@link handle} to be at the given {@link go.Point}.
* This is called by {@link doMouseMove} and {@link doMouseUp} with the result of calling
* {@link computeReshape} to constrain the input point.
* @param newpt - the value of the call to {@link computeReshape}.
*/
reshape(newpt) {
const link = this.adornedLink;
if (link !== null && link.curve === go.Curve.Bezier && link.pointsCount === 4) {
const start = link.getPoint(0);
const end = link.getPoint(3);
const ang = start.directionPoint(end);
const mid = new go.Point((start.x + end.x) / 2, (start.y + end.y) / 2);
const a = new go.Point(9999, 0).rotate(ang + 90).add(mid);
const b = new go.Point(9999, 0).rotate(ang - 90).add(mid);
const q = newpt.copy().projectOntoLineSegmentPoint(a, b);
let curviness = Math.sqrt(mid.distanceSquaredPoint(q));
const port = link.fromPort;
if (port === link.toPort && port !== null) {
if (newpt.y < port.getDocumentPoint(go.Spot.Center).y)
curviness = -curviness;
}
else {
const diff = mid.directionPoint(q) - ang;
if ((diff > 0 && diff < 180) || diff < -180)
curviness = -curviness;
}
link.curviness = curviness;
}
else {
super.reshape(newpt);
}
}
}