-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRotatedNodeResizeHandle.java
505 lines (431 loc) · 17.6 KB
/
RotatedNodeResizeHandle.java
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/****************************************************************************
**
** This demo file is part of yFiles for JavaFX 3.6.
**
** Copyright (c) 2000-2023 by yWorks GmbH, Vor dem Kreuzberg 28,
** 72070 Tuebingen, Germany. All rights reserved.
**
** yFiles demo files exhibit yFiles for JavaFX functionalities. Any redistribution
** of demo files in source code or binary form, with or without
** modification, is not permitted.
**
** Owners of a valid software license for a yFiles for JavaFX version that this
** demo is shipped with are allowed to use the demo source code as basis
** for their own yFiles for JavaFX powered applications. Use of such programs is
** governed by the rights and conditions as set out in the yFiles for JavaFX
** license agreement.
**
** THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
** NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
***************************************************************************/
package complete.rotatablenodes;
import com.yworks.yfiles.geometry.IOrientedRectangle;
import com.yworks.yfiles.geometry.IPoint;
import com.yworks.yfiles.geometry.OrientedRectangle;
import com.yworks.yfiles.geometry.PointD;
import com.yworks.yfiles.geometry.RectD;
import com.yworks.yfiles.geometry.SizeD;
import com.yworks.yfiles.graph.IGraph;
import com.yworks.yfiles.graph.INode;
import com.yworks.yfiles.graph.IPort;
import com.yworks.yfiles.graph.portlocationmodels.IPortLocationModel;
import com.yworks.yfiles.graph.portlocationmodels.IPortLocationModelParameter;
import com.yworks.yfiles.view.CanvasControl;
import com.yworks.yfiles.view.input.ClickEventArgs;
import com.yworks.yfiles.view.input.HandlePositions;
import com.yworks.yfiles.view.input.HandleTypes;
import com.yworks.yfiles.view.input.IHandle;
import com.yworks.yfiles.view.input.IInputMode;
import com.yworks.yfiles.view.input.IInputModeContext;
import com.yworks.yfiles.view.input.IReshapeHandler;
import com.yworks.yfiles.view.input.PortLocationModelParameterHandle;
import com.yworks.yfiles.view.input.SnapContext;
import javafx.scene.Cursor;
import java.util.ArrayList;
import java.util.List;
/**
* A node reshape handle that adjusts its position according to the node rotation.
*/
public class RotatedNodeResizeHandle implements IHandle, IPoint {
private static final double EIGHTH_FROM_CIRCLE = Math.PI * 0.25;
private final HandlePositions position;
private final INode node;
private final IReshapeHandler reshapeHandler;
private final boolean symmetricResize;
private final List<IHandle> portHandles;
private final OrientedRectangle initialLayout;
private PointD dummyLocation;
private SizeD dummySize;
private RectD initialRect;
RotatedNodeResizeHandle(HandlePositions position, INode node, IReshapeHandler reshapeHandler, boolean symmetricResize) {
this.position = position;
this.node = node;
this.reshapeHandler = reshapeHandler;
this.symmetricResize = symmetricResize;
portHandles = new ArrayList<IHandle>();
initialLayout = new OrientedRectangle(getNodeBasedOrientedRectangle());
}
/**
* Returns the node rotation information.
*/
private CachingOrientedRectangle getNodeBasedOrientedRectangle() {
if (node.getStyle() instanceof RotatableNodeStyleDecorator) {
return ((RotatableNodeStyleDecorator) node.getStyle()).getRotatedLayout(node);
} else {
return new CachingOrientedRectangle();
}
}
/**
* Sets the node rotation information.
*/
private RectD setNodeLocationAndSize(IInputModeContext inputModeContext, PointD anchor, SizeD size) {
IGraph graph = inputModeContext.getGraph();
if (graph == null) {
return RectD.EMPTY;
}
OrientedRectangle orientedRectangle = new OrientedRectangle(anchor.getX(), anchor.getY(),
size.getWidth(), size.getHeight(), initialLayout.getUpX(), initialLayout.getUpY());
PointD center = orientedRectangle.getCenter();
return RectD.fromCenter(center, size);
}
/**
* Returns whether or not the node is symmetrical resized.
*/
public boolean getSymmetricResize() {
return symmetricResize;
}
/**
* Returns the visualization of the handle. In this case a dot that rotates nicely.
*/
@Override
public HandleTypes getType() {
return HandleTypes.RESIZE;
}
@Override
public Cursor getCursor() {
CachingOrientedRectangle layout = getNodeBasedOrientedRectangle();
double angle = layout.getAngle();
Cursor[] cursors = new Cursor[] {
Cursor.N_RESIZE,
Cursor.NW_RESIZE,
Cursor.W_RESIZE,
Cursor.SW_RESIZE,
Cursor.S_RESIZE,
Cursor.SE_RESIZE,
Cursor.E_RESIZE,
Cursor.NE_RESIZE,
};
int index = 0;
if (position.equals(HandlePositions.NORTH)) {
index = 0;
} else if (position.equals(HandlePositions.NORTH_WEST)) {
index = 1;
} else if (position.equals(HandlePositions.WEST)) {
index = 2;
} else if (position.equals(HandlePositions.SOUTH_WEST)) {
index = 3;
} else if (position.equals(HandlePositions.SOUTH)) {
index = 4;
} else if (position.equals(HandlePositions.SOUTH_EAST)) {
index = 5;
} else if(position.equals(HandlePositions.EAST)) {
index = 6;
} else if (position.equals(HandlePositions.NORTH_EAST)) {
index = 7;
}
//Pick the right array index for the respective handle position
//Then shift the array position according to the rotation angle
index += (int) Math.round(angle / EIGHTH_FROM_CIRCLE);
index %= cursors.length;
if(index < 0) {
index += cursors.length;
}
return cursors[index];
}
@Override
public void handleClick(ClickEventArgs eventArgs) {
}
/**
* The location of this handle considering the node rotation.
*/
@Override
public IPoint getLocation() {
return getLocation(getNodeBasedOrientedRectangle(), position);
}
/**
* Stores the initial layout of the node in case the user cancels the resizing.
*/
@Override
public void initializeDrag(IInputModeContext inputModeContext) {
if (reshapeHandler != null) {
//if there is a reshape handler, initialize to ensure proper handling of the parent group node
reshapeHandler.initializeReshape(inputModeContext);
}
initialLayout.reshape(getNodeBasedOrientedRectangle());
dummyLocation = initialLayout.getAnchorLocation();
dummySize = initialLayout.getSize().toSizeD();
initialRect = node.getLayout().toRectD();
portHandles.clear();
DelegatingContext portContext = new DelegatingContext(inputModeContext);
for (IPort port : node.getPorts()) {
DummyPortLocationModelParameterHandle portHandle = new DummyPortLocationModelParameterHandle(port);
portHandle.initializeDrag(portContext);
portHandles.add(portHandle);
}
}
/**
* Adjusts the node location and size according to the new handle location.
*/
@Override
public void handleMove(IInputModeContext iInputModeContext, PointD originalLocation, PointD newLocation) {
//calculate how much the handle was moved
PointD upNormal = new PointD(-initialLayout.getUpY(), initialLayout.getUpX());
double deltaW = getWidthDelta(originalLocation, newLocation, upNormal);
PointD up = initialLayout.getUp();
double deltaH = getHeightDelta(originalLocation, newLocation, up);
//add one or two times delta to the width to expand the node right and left
dummySize = new SizeD(
initialLayout.getWidth() + deltaW * (symmetricResize ? 2 : 1),
initialLayout.getHeight() + deltaH * (symmetricResize ? 2 : 1));
//calculate the new location. Depending on our handle position, a different corner of the node should stay fixed
if (symmetricResize) {
double dx = upNormal.getX() * deltaW + up.getX() * deltaH;
double dy = upNormal.getY() * deltaW + up.getY() * deltaH;
dummyLocation = PointD.subtract(initialLayout.getAnchorLocation(), new PointD(dx, dy));
} else {
double w = dummySize.getWidth() - initialLayout.getWidth();
double h = dummySize.getHeight() - initialLayout.getHeight();
if (position.equals(HandlePositions.NORTH_WEST)) {
dummyLocation = PointD.subtract(initialLayout.getAnchorLocation(),
new PointD(-up.getY() * w, up.getX() * w));
}
if (position.equals(HandlePositions.SOUTH) || position.equals(HandlePositions.SOUTH_WEST) ||
position.equals(HandlePositions.WEST)) {
dummyLocation = PointD.subtract(initialLayout.getAnchorLocation(),
new PointD(up.getX() * h - up.getY() * w, up.getY() * h + up.getX() * w));
}
if (position.equals(HandlePositions.SOUTH_EAST)) {
dummyLocation = PointD.subtract(initialLayout.getAnchorLocation(),
new PointD(up.getX() * h, up.getY() * h));
}
if (position.equals(HandlePositions.NORTH) || position.equals(HandlePositions.NORTH_EAST) ||
position.equals(HandlePositions.EAST)) {
dummyLocation = initialLayout.getAnchorLocation();
}
}
RectD newLayout = setNodeLocationAndSize(iInputModeContext, dummyLocation, dummySize);
DelegatingContext portContext = new DelegatingContext(iInputModeContext);
for (IHandle portHandle : portHandles) {
portHandle.handleMove(portContext, dummyLocation, newLocation);
}
if (reshapeHandler != null) {
//if there is a reshape handler, ensure proper handling of a parent group node
reshapeHandler.handleReshape(iInputModeContext, initialRect, newLayout);
}
}
/**
* Returns the delta by which the width of the node was changed.
*/
private double getWidthDelta(PointD originalLocation, PointD newLocation, PointD vector) {
if(position.equals(HandlePositions.NORTH_WEST) || position.equals(HandlePositions.WEST) ||
position.equals(HandlePositions.SOUTH_WEST)) {
// calculate the total distance the handle has been moved in this drag gesture
// max with minus half the node size - because the node can't shrink below zero
return Math.max(vector.scalarProduct(PointD.subtract(originalLocation, newLocation)),
-initialLayout.getWidth() * (symmetricResize ? 0.5d : 1d));
}
if(position.equals(HandlePositions.NORTH_EAST) || position.equals(HandlePositions.EAST) ||
position.equals(HandlePositions.SOUTH_EAST)) {
return Math.max(vector.scalarProduct(PointD.subtract(newLocation, originalLocation)),
-initialLayout.getWidth() * (symmetricResize ? 0.5d : 1d));
}
//default return
return 0.0d;
}
/**
* Returns the delta by which the height of the node was changed.
*/
private double getHeightDelta(PointD originalLocation, PointD newLocation, PointD vector) {
if(position.equals(HandlePositions.NORTH_WEST) || position.equals(HandlePositions.NORTH) ||
position.equals(HandlePositions.NORTH_EAST)) {
return Math.max(vector.scalarProduct(PointD.subtract(newLocation, originalLocation)),
-initialLayout.getHeight() * (symmetricResize ? 0.5d : 1d));
}
if(position.equals(HandlePositions.SOUTH_WEST) || position.equals(HandlePositions.SOUTH) ||
position.equals(HandlePositions.SOUTH_EAST)) {
return Math.max(vector.scalarProduct(PointD.subtract(originalLocation, newLocation)),
-initialLayout.getHeight() * (symmetricResize ? 0.5d : 1d));
}
//default return
return 0.0d;
}
/**
* Restores the original node layout.
*/
@Override
public void cancelDrag(IInputModeContext iInputModeContext, PointD originalLocation) {
setNodeLocationAndSize(iInputModeContext, initialLayout.getAnchorLocation(), initialLayout.getSize().toSizeD());
DelegatingContext portContext = new DelegatingContext(iInputModeContext);
for(IHandle portHandle : portHandles) {
portHandle.cancelDrag(portContext, originalLocation);
}
portHandles.clear();
if(reshapeHandler != null) {
//if there is a reshape handler, ensure proper handling of a parent group node
reshapeHandler.cancelReshape(iInputModeContext, initialRect);
}
}
/**
* Applies the new node layout.
*/
@Override
public void dragFinished(IInputModeContext iInputModeContext, PointD originalLocation, PointD newLocation) {
RectD newLayout = setNodeLocationAndSize(iInputModeContext, dummyLocation, dummySize);
DelegatingContext portContext = new DelegatingContext(iInputModeContext);
for(IHandle portHandle : portHandles) {
portHandle.dragFinished(iInputModeContext, originalLocation, newLocation);
}
portHandles.clear();
if(reshapeHandler != null) {
//if there is a reshape handler, ensure proper handling of a parent group node
reshapeHandler.reshapeFinished(iInputModeContext, initialRect, newLayout);
}
}
/**
* Gets the location that is specified by the given ratios.
*/
private static PointD getLocation(IOrientedRectangle rectangle, double ratioWidth, double ratioHeight) {
double x1 = rectangle.getAnchorX();
double y1 = rectangle.getAnchorY();
double upX = rectangle.getUpX();
double upY = rectangle.getUpY();
double w = rectangle.getWidth() * ratioWidth;
double h = rectangle.getHeight() * ratioHeight;
double x2 = x1 + upX * h - upY * w;
double y2 = y1 + upY * h + upX * w;
return new PointD(x2, y2);
}
/**
* Returns the x-coordinate of the rotated bounds.
*/
@Override
public double getX() {
return getLocation(getNodeBasedOrientedRectangle(), position).x;
}
/**
* Returns the y-coordinate of the rotated bounds.
*/
@Override
public double getY() {
return getLocation(getNodeBasedOrientedRectangle(), position).y;
}
/**
* Returns the location of the specified position on the border of the oriented rectangle.
*/
private PointD getLocation(IOrientedRectangle layout, HandlePositions position) {
if(layout == null){
return node.getLayout().toPointD();
}
if(HandlePositions.NORTH_WEST.equals(position)){
return getLocation(layout, 0.0, 1.0);
} else if (HandlePositions.NORTH.equals(position)) {
return getLocation(layout, 0.5, 1.0);
} else if (HandlePositions.NORTH_EAST.equals(position)) {
return getLocation(layout, 1.0, 1.0);
} else if (HandlePositions.EAST.equals(position)) {
return getLocation(layout, 1.0, 0.5);
} else if (HandlePositions.SOUTH_EAST.equals(position)) {
return getLocation(layout, 1.0, 0.0);
} else if (HandlePositions.SOUTH.equals(position)) {
return getLocation(layout, 0.5, 0.0);
} else if (HandlePositions.SOUTH_WEST.equals(position)) {
return layout.getAnchorLocation();
} else if (HandlePositions.WEST.equals(position)) {
return getLocation(layout, 0.0, 0.5);
} else {
throw new IllegalArgumentException("position out of range");
}
}
/**
* A context that returns no snapContext in its lookup and delegates its other methods to an inner context.
*/
public static class DelegatingContext implements IInputModeContext {
private final IInputModeContext context;
/**
* Initialize a new instance.
*/
DelegatingContext(IInputModeContext context) {
this.context = context;
}
/**
* The wrapped context's zoom
*/
@Override
public double getZoom() {
return context.getZoom();
}
/**
* Returns the wrapped context's hit test radius.
*/
@Override
public double getHitTestRadius() {
return context.getHitTestRadius();
}
/**
* Returns the wrapped context's canvas component.
*/
@Override
public CanvasControl getCanvasControl() {
return context.getCanvasControl();
}
/**
* Returns the wrapped context's parent input mode.
*/
@Override
public IInputMode getParentInputMode() {
return context.getParentInputMode();
}
/**
* Delegates to the wrapped context's lookup but cancels the snap context.
*/
@Override
public <TLookup> TLookup lookup(Class<TLookup> aClass) {
return aClass == SnapContext.class ? null : context.lookup(aClass);
}
}
/**
* This port handle is used only to trigger the updates of the orthogonal edge editing facility of yFiles.
* <p>
* In yFiles, everything code related to updates of the orthogonal edge editing facility is internal. As a workaround,
* we explicitly call internal port handles from our custom node handles.
* </p>
*/
public static class DummyPortLocationModelParameterHandle extends PortLocationModelParameterHandle {
DummyPortLocationModelParameterHandle(IPort port) {
super(port);
}
/**
* Does nothing since we don't want to change the port location.
*/
@Override
protected void setParameter(IGraph graph, IPort port, IPortLocationModelParameter param) {
//do nothing
}
/**
* Returns the current port location since we don't want to change the port location.
*/
@Override
protected IPortLocationModelParameter getNewParameter(IPort port, IPortLocationModel model, PointD location) {
return port.getLocationParameter();
}
}
}