-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAbstractZoomInvariantLabelStyle.java
240 lines (211 loc) · 8.94 KB
/
AbstractZoomInvariantLabelStyle.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
/****************************************************************************
**
** 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 style.zoominvariantlabelstyle;
import com.yworks.yfiles.geometry.IOrientedRectangle;
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.ILabel;
import com.yworks.yfiles.graph.SimpleLabel;
import com.yworks.yfiles.graph.labelmodels.FreeLabelModel;
import com.yworks.yfiles.graph.styles.AbstractLabelStyle;
import com.yworks.yfiles.graph.styles.DefaultLabelStyle;
import com.yworks.yfiles.graph.styles.ILabelStyle;
import com.yworks.yfiles.view.ICanvasContext;
import com.yworks.yfiles.view.IRenderContext;
import com.yworks.yfiles.view.ISelectionIndicatorInstaller;
import com.yworks.yfiles.view.IVisualCreator;
import com.yworks.yfiles.view.OrientedRectangleIndicatorInstaller;
import com.yworks.yfiles.view.VisualGroup;
import com.yworks.yfiles.view.input.IInputModeContext;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.transform.Affine;
import javafx.scene.transform.NonInvertibleTransformException;
import javafx.scene.transform.Transform;
/**
* A label style that may render labels at the same size regardless of the zoom level.
* The style is implemented as a wrapper for an existing label style.
*/
public abstract class AbstractZoomInvariantLabelStyle extends AbstractLabelStyle {
private ILabelStyle innerLabelStyle;
private final OrientedRectangle dummyLabelLayout;
private final OrientedRectangle dummyLabelBounds;
private final SimpleLabel dummyLabel;
protected AbstractZoomInvariantLabelStyle() {
DefaultLabelStyle labelStyle = new DefaultLabelStyle();
this.innerLabelStyle = labelStyle;
this.dummyLabelLayout = new OrientedRectangle();
this.dummyLabelBounds = new OrientedRectangle();
this.dummyLabel = new SimpleLabel(null, "", FreeLabelModel.INSTANCE.createDynamic(dummyLabelLayout));
}
/**
* Gets the wrapped label style used to request the {@link #getPreferredSize(ILabel) preferred size} and to render the
* label.
* @return The wrapped label style.
*/
public ILabelStyle getInnerLabelStyle() {
return innerLabelStyle;
}
/**
* Sets the wrapped label style used to request the {@link #getPreferredSize(ILabel) preferred size} and to render the
* label.
* @param labelStyle The wrapped label style.
*/
public void setInnerLabelStyle( ILabelStyle labelStyle ) {
this.innerLabelStyle = labelStyle;
}
/**
* Determines the scale factor for the given label and zoom level.
* @param label the current label which will be styled
* @param zoom the current zoom level
*/
protected abstract double getScaleForZoom( ILabel label, double zoom );
@Override
protected SizeD getPreferredSize( ILabel label ) {
return innerLabelStyle.getRenderer().getPreferredSize(label, innerLabelStyle);
}
@Override
protected Node createVisual( IRenderContext context, ILabel label ) {
// creates the container for the visual and sets a transform for view coordinates
VisualGroup container = new VisualGroup();
double scale = getScaleForZoom(label, context.getZoom());
updateDummyLabel(context, label, scale);
Affine transform = Transform.affine(
scale,
0,
0,
scale,
label.getLayout().getCenter().getX(),
label.getLayout().getCenter().getY()
);
setTransform(container, transform);
Affine inverse = null;
try {
inverse = transform.createInverse();
} catch (NonInvertibleTransformException e) {
e.printStackTrace();
}
IVisualCreator creator = innerLabelStyle.getRenderer().getVisualCreator(dummyLabel, innerLabelStyle);
// the wrapped style should always think it's rendering with the zoom factor
// induced through the style's zoom minimum and/or maximum zoom value
Node visual = creator.createVisual(new DummyContext(context, scale, inverse));
if (visual != null) {
// add the created visual to the container
container.add(visual);
}
return container;
}
@Override
protected Node updateVisual( IRenderContext context, Node oldVisual, ILabel label ) {
if (!(oldVisual instanceof VisualGroup) || ((VisualGroup) oldVisual).getChildren().isEmpty()) {
return createVisual(context, label);
}
VisualGroup container = (VisualGroup) oldVisual;
double scale = getScaleForZoom(label, context.getZoom());
updateDummyLabel(context, label, scale);
Affine transform = Transform.affine(
scale,
0,
0,
scale,
label.getLayout().getCenter().getX(),
label.getLayout().getCenter().getY()
);
setTransform(container, transform);
Affine inverse = null;
try {
inverse = transform.createInverse();
} catch (NonInvertibleTransformException e) {
e.printStackTrace();
}
// update the visual created by the inner style renderer
Node visual = container.getChildren().get(0);
IVisualCreator creator = innerLabelStyle.getRenderer().getVisualCreator(dummyLabel, innerLabelStyle);
Node updatedVisual = creator.updateVisual(new DummyContext(context, scale, transform), visual);
if (updatedVisual != visual) {
container.remove(visual);
if (updatedVisual != null) {
container.add(updatedVisual);
}
}
return container;
}
/**
* Updates the internal label to match the given original label.
*/
private void updateDummyLabel( ICanvasContext context, ILabel original, double scale ) {
dummyLabel.setOwner(original.getOwner());
dummyLabel.setStyle(original.getStyle());
dummyLabel.setTag(original.getTag());
dummyLabel.setText(original.getText());
IOrientedRectangle originalLayout = original.getLayout();
dummyLabelLayout.reshape(originalLayout);
dummyLabelLayout.setCenter(new PointD(0, 0));
dummyLabel.setPreferredSize(dummyLabelLayout.toSizeD());
dummyLabelBounds.reshape(originalLayout);
dummyLabelBounds.resize(originalLayout.getWidth() * scale, originalLayout.getHeight() * scale);
dummyLabelBounds.setCenter(originalLayout.getCenter());
}
private static void setTransform( Node group, Transform transform ) {
ObservableList<Transform> transforms = group.getTransforms();
transforms.clear();
transforms.add(transform);
}
@Override
protected RectD getBounds( ICanvasContext context, ILabel label ) {
return getUpdatedLabelBounds(context, label).getBounds();
}
@Override
protected boolean isVisible( ICanvasContext context, RectD clip, ILabel label ) {
return clip.intersects(getUpdatedLabelBounds(context, label), 0);
}
@Override
protected boolean isHit( IInputModeContext context, PointD location, ILabel label ) {
return getUpdatedLabelBounds(context, label).contains(location, 0.001);
}
@Override
protected boolean isInBox( IInputModeContext context, RectD box, ILabel label ) {
return box.intersects(getUpdatedLabelBounds(context, label), 0);
}
private OrientedRectangle getUpdatedLabelBounds(ICanvasContext context, ILabel label) {
double scale = getScaleForZoom(label, context.getZoom());
updateDummyLabel(context, label, scale);
return this.dummyLabelBounds;
}
@Override
protected Object lookup( ILabel label, Class type ) {
if (type == ISelectionIndicatorInstaller.class) {
return new OrientedRectangleIndicatorInstaller(dummyLabelBounds);
}
return super.lookup(label, type);
}
}