1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | import * as go from '../release/go-module.js';
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export class SpiralLayout extends go.Layout {
|
25 | private _radius: number = NaN;
|
26 | private _spacing: number = 10;
|
27 | private _clockwise: boolean = true;
|
28 |
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 | get radius(): number { return this._radius; }
|
35 | set radius(val: number) {
|
36 | if (typeof val !== 'number') throw new Error('new value ofr SpiralLayout.radius must be a number, not ' + val);
|
37 | if (this._radius !== val) {
|
38 | this._radius = val;
|
39 | this.invalidateLayout();
|
40 | }
|
41 | }
|
42 |
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 | get spacing(): number { return this._spacing; }
|
49 | set spacing(val: number) {
|
50 | if (typeof val !== 'number') throw new Error('new value for SpiralLayout.spacing must be a number, not: ' + val);
|
51 | if (this._spacing !== val) {
|
52 | this._spacing = val;
|
53 | this.invalidateLayout();
|
54 | }
|
55 | }
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 | get clockwise(): boolean { return this._clockwise; }
|
63 | set clockwise(val: boolean) {
|
64 | if (typeof val !== 'boolean') throw new Error('new value for SpiralLayout.clockwise must be a boolean, not: ' + val);
|
65 | if (this._clockwise !== val) {
|
66 | this._clockwise = val;
|
67 | this.invalidateLayout();
|
68 | }
|
69 | }
|
70 |
|
71 | |
72 |
|
73 |
|
74 | public cloneProtected(copy: this): void {
|
75 | super.cloneProtected(copy);
|
76 | copy._radius = this._radius;
|
77 | copy._spacing = this._spacing;
|
78 | copy._clockwise = this._clockwise;
|
79 | }
|
80 |
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | public doLayout(coll: go.Diagram | go.Group | go.Iterable<go.Part>): void {
|
88 | if (this.network === null) {
|
89 | this.network = this.makeNetwork(coll);
|
90 | }
|
91 | this.arrangementOrigin = this.initialOrigin(this.arrangementOrigin);
|
92 | const originx = this.arrangementOrigin.x;
|
93 | const originy = this.arrangementOrigin.y;
|
94 | let root = null;
|
95 |
|
96 | const it = this.network.vertexes.iterator;
|
97 | while (it.next()) {
|
98 | const v = it.value;
|
99 | if (root === null) root = v;
|
100 | if (v.sourceEdges.count === 0) {
|
101 | root = v;
|
102 | break;
|
103 | }
|
104 | }
|
105 |
|
106 | if (root === null) {
|
107 | this.network = null;
|
108 | return;
|
109 | }
|
110 |
|
111 | const space = this.spacing;
|
112 | const cw = (this.clockwise ? 1 : -1);
|
113 | let rad = this.radius;
|
114 | if (rad <= 0 || isNaN(rad) || !isFinite(rad)) rad = this.diameter(root) / 4;
|
115 |
|
116 |
|
117 | let angle = cw * Math.PI;
|
118 | root.centerX = originx;
|
119 | root.centerY = originy;
|
120 |
|
121 | let edge = root.destinationEdges.first();
|
122 |
|
123 | const link = (edge !== null ? edge.link : null);
|
124 | if (link !== null) link.curviness = cw * rad;
|
125 |
|
126 |
|
127 | let vert = (edge !== null ? edge.toVertex : null);
|
128 | while (vert !== null) {
|
129 |
|
130 | const cos = Math.cos(angle);
|
131 | const sin = Math.sin(angle);
|
132 | let x = rad * (cos + angle * sin);
|
133 | let y = rad * (sin - angle * cos);
|
134 |
|
135 | if (link !== null && vert.node instanceof go.Group && link.toNode !== null && link.toNode !== vert.node) {
|
136 | const offset = link.toNode.location.copy().subtract(vert.node.location);
|
137 | x -= offset.x;
|
138 | y -= offset.y;
|
139 | }
|
140 | vert.centerX = x + originx;
|
141 | vert.centerY = y + originy;
|
142 |
|
143 | const nextedge = vert.destinationEdges.first();
|
144 | const nextvert = (nextedge !== null ? nextedge.toVertex : null);
|
145 | if (nextvert !== null) {
|
146 |
|
147 | if (this.isRouting && nextedge !== null && nextedge.link !== null) {
|
148 | if (!isNaN(nextedge.link.curviness)) {
|
149 | const c = nextedge.link.curviness;
|
150 | nextedge.link.curviness = cw * Math.abs(c);
|
151 | }
|
152 | }
|
153 |
|
154 |
|
155 | const dia = this.diameter(vert) / 2 + this.diameter(nextvert) / 2;
|
156 | angle += cw * Math.atan((dia + space) / Math.sqrt(x * x + y * y));
|
157 | }
|
158 | edge = nextedge;
|
159 | vert = nextvert;
|
160 | }
|
161 |
|
162 | this.updateParts();
|
163 | this.network = null;
|
164 | }
|
165 |
|
166 | |
167 |
|
168 |
|
169 | public diameter(v: go.LayoutVertex): number {
|
170 | if (!v) return 0;
|
171 | const b = v.bounds;
|
172 | return Math.sqrt(b.width * b.width + b.height * b.height);
|
173 | }
|
174 |
|
175 | }
|