-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPortReshapeHandle.java
258 lines (238 loc) · 11.3 KB
/
PortReshapeHandle.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
/****************************************************************************
**
** 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 input.reshapehandleprovider;
import com.yworks.yfiles.geometry.IPoint;
import com.yworks.yfiles.geometry.PointD;
import com.yworks.yfiles.geometry.SizeD;
import com.yworks.yfiles.graph.IPort;
import com.yworks.yfiles.graph.styles.NodeStylePortStyleAdapter;
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.IInputModeContext;
import javafx.scene.Cursor;
/**
* An {@link IHandle} implementation for an {@link IPort} using a {@link NodeStylePortStyleAdapter}
* that can be used to reshape the {@link NodeStylePortStyleAdapter#getRenderSize() render size}.
*/
public class PortReshapeHandle implements IHandle {
private final IInputModeContext context;
private final IPort port;
private final NodeStylePortStyleAdapter adapter;
private final HandlePositions position;
// the initial RenderSize used to reset the size on Cancel
private SizeD initialRenderSize;
private double margins;
private SizeD minimumSize;
/**
* Gets the margins the handle is placed form the port visualization bounds.
*
* @return
*/
public double getMargins() {
return margins;
}
/**
* Sets the margins the handle is placed form the port visualization bounds.
*
* @param margins The margins to set.
*/
public void setMargins(double margins) {
this.margins = margins;
}
/**
* Gets the minimum size the {@link NodeStylePortStyleAdapter#getRenderSize() render size} may have.
*
* @return
*/
public SizeD getMinimumSize() {
return minimumSize;
}
/**
* Sets the minimum size the {@link NodeStylePortStyleAdapter#getRenderSize() render size} may have.
*
* @param minimumSize
*/
public void setMinimumSize(SizeD minimumSize) {
this.minimumSize = minimumSize;
}
/**
* Creates a new instance for port and its adapter.
*
* @param context The context of the reshape gesture.
* @param port The port whose visualization shall be resized.
* @param adapter The adapter whose render size shall be changed.
* @param position The position of the handle.
*/
public PortReshapeHandle(IInputModeContext context, IPort port, NodeStylePortStyleAdapter adapter, HandlePositions position) {
this.context = context;
this.port = port;
this.adapter = adapter;
this.position = position;
this.margins = 4;
}
/**
* Returns the current location of the handle.
*/
@Override
public IPoint getLocation() {
return calculateLocation();
}
/**
* Calculates the location of the handle considering the {@link IPort#getLocation() port location},
* {@link NodeStylePortStyleAdapter#getRenderSize() render size} and {@link PortReshapeHandle#getMargins() margins}.
*/
private PointD calculateLocation() {
PointD portLocation = port.getLocation();
double handleX = portLocation.getX();
double handleY = portLocation.getY();
double marginsInViewCoordinates = getMargins() / context.getZoom();
if (position == HandlePositions.NORTH_WEST || position == HandlePositions.WEST || position == HandlePositions.SOUTH_WEST) {
handleX -= adapter.getRenderSize().getWidth() / 2.0 + marginsInViewCoordinates;
} else if (position == HandlePositions.NORTH_EAST || position == HandlePositions.EAST || position == HandlePositions.SOUTH_EAST) {
handleX += adapter.getRenderSize().getWidth() / 2.0 + marginsInViewCoordinates;
}
if (position == HandlePositions.NORTH_WEST || position == HandlePositions.NORTH || position == HandlePositions.NORTH_EAST) {
handleY -= adapter.getRenderSize().getHeight() / 2.0 + marginsInViewCoordinates;
} else if (position == HandlePositions.SOUTH_WEST || position == HandlePositions.SOUTH || position == HandlePositions.SOUTH_EAST) {
handleY += adapter.getRenderSize().getHeight() / 2.0 + marginsInViewCoordinates;
}
return new PointD(handleX, handleY);
}
/**
* Stores the initial {@link NodeStylePortStyleAdapter#getRenderSize() render size}.
*
* @param context The context to retrieve information about the drag from.
*/
@Override
public void initializeDrag(IInputModeContext context) {
initialRenderSize = adapter.getRenderSize();
}
/**
* Calculates and applies the new {@link NodeStylePortStyleAdapter#getRenderSize() render size}.
*
* @param context The context to retrieve information about the drag from.
* @param originalLocation The value of the {@link #getLocation() Location} property at the time of {@link #initializeDrag(IInputModeContext)}.
* @param newLocation The coordinates in the world coordinate system that the client wants the handle to be at.
*/
@Override
public void handleMove(IInputModeContext context, PointD originalLocation, PointD newLocation) {
// calculate the size delta implied by the originalLocation and newLocation
SizeD delta = calculateDelta(originalLocation, newLocation);
// calculate and apply the new render size
adapter.setRenderSize(calculateNewSize(delta));
}
private SizeD calculateDelta(PointD originalLocation, PointD newLocation) {
// calculate the delta the mouse has been moved since InitializeDrag
PointD mouseDelta = PointD.subtract(newLocation, originalLocation);
// depending on the handle position this mouse delta shall increase or decrease the render size
if (HandlePositions.NORTH_WEST.equals(position)) {
return new SizeD(-2 * mouseDelta.getX(), -2 * mouseDelta.getY());
} else if (HandlePositions.NORTH.equals(position)) {
return new SizeD(0, -2 * mouseDelta.getY());
} else if (HandlePositions.NORTH_EAST.equals(position)) {
return new SizeD(2 * mouseDelta.getX(), -2 * mouseDelta.getY());
} else if (HandlePositions.WEST.equals(position)) {
return new SizeD(-2 * mouseDelta.getX(), 0);
} else if (HandlePositions.EAST.equals(position)) {
return new SizeD(2 * mouseDelta.getX(), 0);
} else if (HandlePositions.SOUTH_WEST.equals(position)) {
return new SizeD(-2 * mouseDelta.getX(), 2 * mouseDelta.getY());
} else if (HandlePositions.SOUTH.equals(position)) {
return new SizeD(0, 2 * mouseDelta.getY());
} else if (HandlePositions.SOUTH_EAST.equals(position)) {
return new SizeD(2 * mouseDelta.getX(), 2 * mouseDelta.getY());
}
return SizeD.EMPTY;
}
private SizeD calculateNewSize(SizeD delta) {
double newWidth = Math.max(getMinimumSize().getWidth(), initialRenderSize.getWidth() + delta.getWidth());
double newHeight = Math.max(getMinimumSize().getHeight(), initialRenderSize.getHeight() + delta.getHeight());
return new SizeD(newWidth, newHeight);
}
/**
* Resets {@link NodeStylePortStyleAdapter#getRenderSize() render size} to its initial value.
*
* @param context The context to retrieve information about the drag from.
* @param originalLocation The value of the coordinate of the {@link #getLocation() Location} property at the
* time of {@link #initializeDrag(IInputModeContext) initializeDrag}.
*/
@Override
public void cancelDrag(IInputModeContext context, PointD originalLocation) {
adapter.setRenderSize(initialRenderSize);
}
/**
* Calculates and applies the final {@link NodeStylePortStyleAdapter#getRenderSize() render size}.
*
* @param context The context to retrieve information about the drag from.
* @param originalLocation The value of the {@link #getLocation() Location} property at the time of
* {@link #initializeDrag(IInputModeContext) initializeDrag}.
* @param newLocation The coordinates in the world coordinate system that the client wants the handle to be at. Depending on the
* implementation the {@link #getLocation() Location} may or may not be modified to reflect the new value. This is the same
* value as delivered in the last invocation of {@link #handleMove(IInputModeContext, PointD, PointD) handleMove}.
*/
@Override
public void dragFinished(IInputModeContext context, PointD originalLocation, PointD newLocation) {
SizeD delta = calculateDelta(originalLocation, newLocation);
adapter.setRenderSize(calculateNewSize(delta));
}
/**
* Returns {@link HandleTypes#RESIZE}.
*/
@Override
public HandleTypes getType() {
return HandleTypes.RESIZE;
}
@Override
public Cursor getCursor() {
if (HandlePositions.SOUTH.equals(position)) {
return Cursor.S_RESIZE;
} else if (HandlePositions.NORTH.equals(position)) {
return Cursor.N_RESIZE;
} else if (HandlePositions.EAST.equals(position)) {
return Cursor.E_RESIZE;
} else if (HandlePositions.WEST.equals(position)) {
return Cursor.W_RESIZE;
} else if (HandlePositions.NORTH_WEST.equals(position)) {
return Cursor.NW_RESIZE;
} else if (HandlePositions.SOUTH_EAST.equals(position)) {
return Cursor.SE_RESIZE;
} else if (HandlePositions.NORTH_EAST.equals(position)) {
return Cursor.NE_RESIZE;
} else if (HandlePositions.SOUTH_WEST.equals(position)) {
return Cursor.SW_RESIZE;
}
return Cursor.DEFAULT;
}
@Override
public void handleClick(ClickEventArgs eventArgs) {
}
}