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