-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLabelResizeHandle.java
290 lines (258 loc) · 9.94 KB
/
LabelResizeHandle.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
/****************************************************************************
**
** 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.labelhandleprovider;
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.SizeD;
import com.yworks.yfiles.graph.IGraph;
import com.yworks.yfiles.graph.ILabel;
import com.yworks.yfiles.graph.labelmodels.ILabelModel;
import com.yworks.yfiles.graph.labelmodels.ILabelModelParameter;
import com.yworks.yfiles.graph.labelmodels.ILabelModelParameterFinder;
import com.yworks.yfiles.view.CanvasControl;
import com.yworks.yfiles.view.ICanvasObject;
import com.yworks.yfiles.view.OrientedRectangleIndicatorInstaller;
import com.yworks.yfiles.view.input.ClickEventArgs;
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;
/**
* Custom {@link IHandle} implementation for resizing labels.
*/
public class LabelResizeHandle implements IHandle {
private final ILabel label;
private final boolean symmetricResize;
private IPoint location;
private boolean emulate;
private SizeD dummyPreferredSize;
private PointD dummyLocation;
private ICanvasObject sizeIndicator;
/**
* Initializes a new {@code LabelResizeHandle} instance for the given label.
* @param symmetricResize if {@code true} resizing in east-west direction is
* symmetric.
*/
public LabelResizeHandle( ILabel label, boolean symmetricResize ) {
this.label = label;
this.symmetricResize = symmetricResize;
}
/**
* Returns the handle's type.
*/
@Override
public HandleTypes getType() {
return HandleTypes.RESIZE;
}
/**
* Returns the cursor that should be displayed while this handle
* implementation is active.
*/
@Override
public Cursor getCursor() {
return Cursor.H_RESIZE;
}
/**
* Returns the handle's location.
*/
@Override
public IPoint getLocation() {
if (location == null) {
location = new LabelResizeHandleLivePoint();
}
return location;
}
/**
* Initializes the handle's state right before a resize operation.
* Invoked right before the handle is dragged.
*/
@Override
public void initializeDrag( IInputModeContext context ) {
dummyPreferredSize = label.getPreferredSize();
dummyLocation = label.getLayout().getAnchorLocation();
// start using the calculated dummy bounds
emulate = true;
CanvasControl canvasControl = context.getCanvasControl();
if (canvasControl != null) {
// add a ghost visualization for the resized label bounds
LabelResizeIndicatorInstaller installer = new LabelResizeIndicatorInstaller();
sizeIndicator = installer.addCanvasObject(
canvasControl.getCanvasContext(),
canvasControl.getSelectionGroup(),
this);
}
}
/**
* Calculates the new label size for the current handle position.
* Invoked when the handle is dragged.
*/
@Override
public void handleMove( IInputModeContext context, PointD originalLocation, PointD newLocation ) {
IOrientedRectangle layout = label.getLayout();
// the normal (orthogonal) vector of the 'up' vector
PointD upNormal = new PointD(-layout.getUpY(), layout.getUpX());
// calculate the total distance the handle has been moved in this drag gesture
double delta = upNormal.scalarProduct(PointD.subtract(newLocation, originalLocation));
// prevent shrinking the label to a size less than 0
delta = Math.max(delta, -layout.getWidth() * (symmetricResize ? 0.5 : 1));
// add one or two times delta to the width to expand the label right and left
double newWidth = Math.max(0, layout.getWidth() + delta * (symmetricResize ? 2 : 1));
dummyPreferredSize = new SizeD(newWidth, dummyPreferredSize.getHeight());
// calculate the new location
dummyLocation =
symmetricResize
? PointD.subtract(layout.getAnchorLocation(), new PointD(upNormal.getX() * delta, upNormal.getY() * delta))
: layout.getAnchorLocation();
}
/**
* Resizes the handle's associated label.
* Invoked when the drag gesture has finished.
*/
@Override
public void dragFinished( IInputModeContext context, PointD originalLocation, PointD newLocation ) {
IGraph graph = context.getGraph();
if (graph != null) {
// assign the new size
graph.setLabelPreferredSize(label, dummyPreferredSize);
// update the label's layout parameter for its new size
// this ensures that the resize rectangle which acts as user feedback is
// in sync with the actual bounds assigned to the label
ILabelModel model = label.getLayoutParameter().getModel();
ILabelModelParameterFinder finder = model.lookup(ILabelModelParameterFinder.class);
if (finder != null) {
ILabelModelParameter param = finder.findBestParameter(label, model, getCurrentLabelLayout());
graph.setLabelLayoutParameter(label, param);
}
}
reset();
}
/**
* Cancels the resize operation and resets the handle's state.
* Invoked when the drag gesture is canceled.
*/
@Override
public void cancelDrag( IInputModeContext context, PointD originalLocation ) {
reset();
}
@Override
public void handleClick(ClickEventArgs eventArgs) {
}
private void reset() {
// use normal label bounds if drag gesture is over.
emulate = false;
// remove the visual size indicator
if (sizeIndicator != null) {
sizeIndicator.remove();
sizeIndicator = null;
}
}
/**
* Returns the current label layout.
*/
public OrientedRectangle getCurrentLabelLayout() {
IOrientedRectangle labelLayout = label.getLayout();
if (emulate) {
return new OrientedRectangle(
dummyLocation.getX(),
dummyLocation.getY(),
dummyPreferredSize.getWidth(),
dummyPreferredSize.getHeight(),
labelLayout.getUpX(),
labelLayout.getUpY());
} else {
return new OrientedRectangle(
labelLayout.getAnchorX(),
labelLayout.getAnchorY(),
label.getPreferredSize().getWidth(),
label.getPreferredSize().getHeight(),
labelLayout.getUpX(),
labelLayout.getUpY());
}
}
/**
* Represents the location of the resize handle.
*/
private class LabelResizeHandleLivePoint implements IPoint{
/**
* Returns the handle's x-coordinate.
*/
@Override
public double getX() {
IOrientedRectangle layout = label.getLayout();
SizeD preferredSize = emulate
? dummyPreferredSize
: label.getPreferredSize();
PointD anchor = emulate
? dummyLocation
: layout.getAnchorLocation();
PointD up = layout.getUp();
// the resize handle is always placed in the center of the label's right border, i.e.
// the resize handle location in the coordinate system of the label's oriented bounds is (1, 0.5)
// transform the label-local coordinates to regular graph coordinates
return anchor.getX()
- up.getY() * preferredSize.getWidth()
+ up.getX() * preferredSize.getHeight() * 0.5;
}
/**
* Returns the handle's y-coordinate.
*/
@Override
public double getY() {
IOrientedRectangle layout = label.getLayout();
SizeD preferredSize = emulate
? dummyPreferredSize
: label.getPreferredSize();
PointD anchor = emulate
? dummyLocation
: layout.getAnchorLocation();
PointD up = layout.getUp();
// the resize handle is always placed in the center of the label's right border, i.e.
// the resize handle location in the coordinate system of the label's oriented bounds is (1, 0.5)
// transform the label-local coordinates to regular graph coordinates
return anchor.getY()
+ up.getX() * preferredSize.getWidth()
+ up.getY() * preferredSize.getHeight() * 0.5;
}
}
/**
* Visualizes the oriented bounds of the label that is resized.
*/
private class LabelResizeIndicatorInstaller extends OrientedRectangleIndicatorInstaller {
/**
* Returns the oriented bounds of the label that is resized.
*/
@Override
protected IOrientedRectangle getRectangle( Object item ) {
return getCurrentLabelLayout();
}
}
}