forked from NorthwoodsSoftware/GoJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometryReshapingTool.js
More file actions
351 lines (323 loc) · 11 KB
/
Copy pathGeometryReshapingTool.js
File metadata and controls
351 lines (323 loc) · 11 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"use strict";
/*
* Copyright (C) 1998-2018 by Northwoods Software Corporation. All Rights Reserved.
*/
/**
* @constructor
* @extends Tool
* @class
* This GeometryReshapingTool class allows for a Shape's Geometry to be modified by the user
* via the dragging of tool handles.
* This does not handle Links, whose routes should be reshaped by the LinkReshapingTool.
* The {@link #reshapeObjectName} needs to identify the named {@link Shape} within the
* selected {@link Part}.
* If the shape cannot be found or if its {@link Shape#geometry} is not of type {@link Geometry#Path},
* this will not show any GeometryReshaping {@link Adornment}.
* At the current time this tool does not support adding or removing {@link PathSegment}s to the Geometry.
*/
function GeometryReshapingTool() {
go.Tool.call(this);
this.name = "GeometryReshaping";
var h = new go.Shape();
h.figure = "Diamond";
h.desiredSize = new go.Size(7, 7);
h.fill = "lightblue";
h.stroke = "dodgerblue";
h.cursor = "move";
/** @type {GraphObject} */
this._handleArchetype = h;
/** @type {string} */
this._reshapeObjectName = 'SHAPE'; //??? can't add Part.reshapeObjectName property
// there's no Part.reshapeAdornmentTemplate either
// internal state
/** @type {GraphObject} */
this._handle = null;
/** @type {Shape} */
this._adornedShape = null;
/** @type {Geometry} */
this._originalGeometry = null; // in case the tool is cancelled and the UndoManager is not enabled
}
go.Diagram.inherit(GeometryReshapingTool, go.Tool);
/*
* A small GraphObject used as a reshape handle for each segment.
* The default GraphObject is a small blue diamond.
* @name GeometryReshapingTool#handleArchetype
* @function.
* @return {GraphObject}
*/
Object.defineProperty(GeometryReshapingTool.prototype, "handleArchetype", {
get: function() { return this._handleArchetype; },
set: function(val) { this._handleArchetype = value; }
});
/*
* The name of the GraphObject to be reshaped.
* @name GeometryReshapingTool#reshapeObjectName
* @function.
* @return {string}
*/
Object.defineProperty(GeometryReshapingTool.prototype, "reshapeObjectName", {
get: function() { return this._reshapeObjectName; },
set: function(val) { this._reshapeObjectName = value; }
});
/*
* This read-only property returns the {@link GraphObject} that is the tool handle being dragged by the user.
* This will be contained by an {@link Adornment} whose category is "GeometryReshaping".
* Its {@link Adornment#adornedObject} is the same as the {@link #adornedShape}.
* @name GeometryReshapingTool#handle
* @function.
* @return {GraphObject}
*/
Object.defineProperty(GeometryReshapingTool.prototype, "handle", {
get: function() { return this._handle; }
});
/*
* Gets the {@link Shape} that is being reshaped.
* This must be contained within the selected Part.
* @name GeometryReshapingTool#adornedShape
* @function.
* @return {Shape}
*/
Object.defineProperty(GeometryReshapingTool.prototype, "adornedShape", {
get: function() { return this._adornedShape; }
});
/*
* This read-only property remembers the original value for {@link Shape#geometry},
* so that it can be restored if this tool is cancelled.
* @name GeometryReshapingTool#originalGeometry
* @function.
* @return {Geometry}
*/
Object.defineProperty(GeometryReshapingTool.prototype, "originalGeometry", {
get: function() { return this._originalGeometry; }
});
/**
* Show an {@link Adornment} with a reshape handle at each point of the geometry.
* Don't show anything if {@link #reshapeObjectName} doesn't identify a {@link Shape}
* that has a {@link Shape#geometry} of type {@link Geometry#Path}.
* @this {GeometryReshapingTool}
* @param {Part} part the part.
*/
GeometryReshapingTool.prototype.updateAdornments = function(part) {
if (part === null || part instanceof go.Link) return; // this tool never applies to Links
if (part.isSelected && !this.diagram.isReadOnly) {
var selelt = part.findObject(this.reshapeObjectName);
if (selelt instanceof go.Shape && selelt.actualBounds.isReal() && selelt.isVisibleObject() &&
part.canReshape() && part.actualBounds.isReal() && part.isVisible() &&
selelt.geometry.type === go.Geometry.Path) {
var adornment = part.findAdornment(this.name);
if (adornment === null) {
adornment = this.makeAdornment(selelt);
}
if (adornment !== null) {
// update the position/alignment of each handle
var geo = selelt.geometry;
var b = geo.bounds;
// update the size of the adornment
adornment.findObject("BODY").desiredSize = b.size;
adornment.elements.each(function(h) {
if (h._typ === undefined) return;
var fig = geo.figures.elt(h._fig);
var seg = fig.segments.elt(h._seg);
var x = 0;
var y = 0;
switch (h._typ) {
case 0: x = fig.startX; y = fig.startY; break;
case 1: x = seg.endX; y = seg.endY; break;
case 2: x = seg.point1X; y = seg.point1Y; break;
case 3: x = seg.point2X; y = seg.point2Y; break;
}
var bw = b.width;
if (bw === 0) bw = 0.001;
var bh = b.height;
if (bh === 0) bh = 0.001;
h.alignment = new go.Spot(Math.max(0, Math.min((x - b.x) / bw, 1)),
Math.max(0, Math.min((y - b.y) / bh, 1)));
});
part.addAdornment(this.name, adornment);
adornment.location = selelt.getDocumentPoint(go.Spot.TopLeft);
adornment.angle = selelt.getDocumentAngle();
return;
}
}
}
part.removeAdornment(this.name);
};
/*
* @this {GeometryReshapingTool}
*/
GeometryReshapingTool.prototype.makeAdornment = function(selelt) {
var adornment = new go.Adornment();
adornment.type = go.Panel.Spot;
adornment.locationObjectName = "BODY";
adornment.locationSpot = new go.Spot(0, 0, -selelt.strokeWidth / 2, -selelt.strokeWidth / 2);
var h = new go.Shape();
h.name = "BODY";
h.fill = null;
h.stroke = null;
h.strokeWidth = 0;
adornment.add(h);
var geo = selelt.geometry;
// requires Path Geometry, checked above in updateAdornments
for (var f = 0; f < geo.figures.count; f++) {
var fig = geo.figures.elt(f);
for (var g = 0; g < fig.segments.count; g++) {
var seg = fig.segments.elt(g);
var h;
if (g === 0) {
h = this.makeHandle(selelt, fig, seg);
if (h !== null) {
h._typ = 0;
h._fig = f;
h._seg = g;
adornment.add(h);
}
}
h = this.makeHandle(selelt, fig, seg);
if (h !== null) {
h._typ = 1;
h._fig = f;
h._seg = g;
adornment.add(h);
}
if (seg.type === go.PathSegment.QuadraticBezier || seg.type === go.PathSegment.Bezier) {
h = this.makeHandle(selelt, fig, seg);
if (h !== null) {
h._typ = 2;
h._fig = f;
h._seg = g;
adornment.add(h);
}
if (seg.type === go.PathSegment.Bezier) {
h = this.makeHandle(selelt, fig, seg);
if (h !== null) {
h._typ = 3;
h._fig = f;
h._seg = g;
adornment.add(h);
}
}
}
}
}
adornment.category = this.name;
adornment.adornedObject = selelt;
return adornment;
};
/*
* @this {GeometryReshapingTool}
*/
GeometryReshapingTool.prototype.makeHandle = function(selelt, fig, seg) {
var h = this.handleArchetype;
if (h === null) return null;
return h.copy();
};
/*
* @this {GeometryReshapingTool}
*/
GeometryReshapingTool.prototype.canStart = function() {
if (!this.isEnabled) return false;
var diagram = this.diagram;
if (diagram === null || diagram.isReadOnly) return false;
if (!diagram.allowReshape) return false;
if (!diagram.lastInput.left) return false;
var h = this.findToolHandleAt(diagram.firstInput.documentPoint, this.name);
return (h !== null);
};
/**
* @this {GeometryReshapingTool}
*/
GeometryReshapingTool.prototype.doActivate = function() {
var diagram = this.diagram;
if (diagram === null) return;
this._handle = this.findToolHandleAt(diagram.firstInput.documentPoint, this.name);
if (this._handle === null) return;
var shape = this._handle.part.adornedObject;
if (!shape) return;
this._adornedShape = shape;
diagram.isMouseCaptured = true;
this.startTransaction(this.name);
this._originalGeometry = shape.geometry;
this.isActive = true;
};
/**
* @this {GeometryReshapingTool}
*/
GeometryReshapingTool.prototype.doDeactivate = function() {
this.stopTransaction();
this._handle = null;
this._adornedShape = null;
var diagram = this.diagram;
if (diagram !== null) diagram.isMouseCaptured = false;
this.isActive = false;
};
/**
* @this {GeometryReshapingTool}
*/
GeometryReshapingTool.prototype.doCancel = function() {
var shape = this._adornedShape;
if (shape !== null) {
// explicitly restore the original route, in case !UndoManager.isEnabled
shape.geometry = this._originalGeometry;
}
this.stopTool();
};
/**
* @this {GeometryReshapingTool}
*/
GeometryReshapingTool.prototype.doMouseMove = function() {
var diagram = this.diagram;
if (this.isActive && diagram !== null) {
var newpt = this.computeReshape(diagram.lastInput.documentPoint);
this.reshape(newpt);
}
};
/**
* @this {GeometryReshapingTool}
*/
GeometryReshapingTool.prototype.doMouseUp = function() {
var diagram = this.diagram;
if (this.isActive && diagram !== null) {
var newpt = this.computeReshape(diagram.lastInput.documentPoint);
this.reshape(newpt);
this.transactionResult = this.name; // success
}
this.stopTool();
};
/**
* @expose
* @this {GeometryReshapingTool}
* @param {Point} newPoint the value of the call to {@link #computeReshape}.
*/
GeometryReshapingTool.prototype.reshape = function(newPoint) {
var shape = this.adornedShape;
var locpt = shape.getLocalPoint(newPoint);
var geo = shape.geometry.copy();
shape.desiredSize = new go.Size(NaN, NaN); // set the desiredSize once we've gotten our Geometry so we don't clobber
var type = this.handle._typ;
if (type === undefined) return;
var fig = geo.figures.elt(this.handle._fig);
var seg = fig.segments.elt(this.handle._seg);
switch (type) {
case 0: fig.startX = locpt.x; fig.startY = locpt.y; break;
case 1: seg.endX = locpt.x; seg.endY = locpt.y; break;
case 2: seg.point1X = locpt.x; seg.point1Y = locpt.y; break;
case 3: seg.point2X = locpt.x; seg.point2Y = locpt.y; break;
}
var offset = geo.normalize(); // avoid any negative coordinates in the geometry
shape.geometry = geo; // modify the Shape
var part = shape.part; // move the Part holding the Shape
if (!part.locationSpot.equals(go.Spot.Center)) { // but only if the locationSpot isn't Center
part.move(part.position.copy().subtract(offset));
}
this.updateAdornments(part); // update any Adornments of the Part
this.diagram.maybeUpdate(); // force more frequent drawing for smoother looking behavior
};
/**
* @expose
* @this {GeometryReshapingTool}
* @param {Point} p the point where the handle is being dragged.
* @return {Point}
*/
GeometryReshapingTool.prototype.computeReshape = function(p) {
return p; // no constraints on the points
};