]> gerrit.simantics Code Review - simantics/district.git/blob
e724a68dc46b0063e1129ae9d31ba744083416fc
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Graphics2D;
6 import java.awt.RenderingHints;
7 import java.awt.geom.AffineTransform;
8 import java.awt.geom.Line2D;
9 import java.awt.geom.Path2D;
10 import java.awt.geom.Point2D;
11 import java.awt.geom.Rectangle2D;
12
13 import org.simantics.district.network.ModelledCRS;
14 import org.simantics.district.network.ui.DistrictNetworkEdge;
15 import org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElementFactory;
16 import org.simantics.scenegraph.INode;
17 import org.simantics.scenegraph.ISelectionPainterNode;
18 import org.simantics.scenegraph.g2d.G2DNode;
19 import org.simantics.scenegraph.g2d.G2DParentNode;
20 import org.simantics.scenegraph.g2d.G2DRenderingHints;
21 import org.simantics.scenegraph.g2d.nodes.SVGNode;
22 import org.simantics.scenegraph.utils.GeometryUtils;
23 import org.simantics.scenegraph.utils.NodeUtil;
24
25 public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelectionPainterNode {
26
27     private static final long serialVersionUID = 8049769475036519806L;
28
29     private static final BasicStroke STROKE           = new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
30     private static final Color       SELECTION_COLOR  = new Color(255, 0, 255, 96);
31
32     private DistrictNetworkEdge edge;
33     private Rectangle2D bounds;
34     private transient Path2D path;
35     private transient Path2D detailedPath;
36
37     private transient int zoomLevel = 0;
38
39     private static boolean scaleStroke = true;
40     private Color color;
41     private Double stroke;
42     private transient Color dynamicColor = null;
43
44     // Dimensions for shut-off valve symbol
45     private static final double left = -0.25;
46     private static final double top = -0.25;
47     private static final double width = 0.5;
48     private static final double height = 0.5;
49
50     private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
51
52     private transient Point2D centerPoint;
53     private transient Point2D detailedCenterPoint;
54     private transient Point2D direction;
55     private transient Point2D detailedDirection;
56
57     private transient Rectangle2D symbolRect;
58     private transient AffineTransform symbolTransform;
59
60     private boolean hidden = false;
61
62     private Double arrowLength;
63
64     private static double startX;
65     private static double startY;
66     private static double endX;
67     private static double endY;
68
69     private Path2D arrowPath;
70
71     @Override
72     public void init() {
73     }
74
75     @Override
76     public void render(Graphics2D g2d) {
77         AffineTransform ot = null;
78         AffineTransform t = getTransform();
79         double scale = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_UNDER_SPATIAL_ROOT) : 1.0;
80         if (t != null && !t.isIdentity()) {
81             //ot = g2d.getTransform();
82             ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
83             if (ot == null)
84                 ot = g2d.getTransform();
85             g2d.transform(t);
86             if (scaleStroke) {
87                 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
88                 work.setTransform(ot);
89                 work.concatenate(t);
90                 scale = DistrictNetworkNodeUtils.getScale(work);
91             }
92         }
93
94         zoomLevel = (Integer) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
95
96         if (!hidden) {
97             Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
98             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
99     
100             Color oldColor = g2d.getColor();
101             BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
102     
103             BasicStroke bs = null;
104             if (scaleStroke) {
105                 bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale));
106             } else {
107                 bs = STROKE;
108             }
109
110             Path2D path = renderDetailed(zoomLevel) ? this.detailedPath : this.path;
111     
112             if (isSelected()) {
113                 g2d.setColor(SELECTION_COLOR);
114                 g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale)));
115                 g2d.draw(path);
116             }
117     
118             g2d.setColor(dynamicColor != null ? dynamicColor : color);
119             g2d.setStroke(bs);
120             g2d.draw(path);
121     
122             // Draw arrow
123             if (arrowLength != null) {
124                 g2d.setColor(Color.BLACK);
125                 float lw = STROKE.getLineWidth() / (float)scale;
126                 g2d.setStroke(new BasicStroke(lw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
127
128                 double l = arrowLength;
129                 double w = 2 * (double) lw * Math.signum(l);
130                 if (Math.abs(w) > Math.abs(l)) w = l;
131                 double offset = 2 * (double) lw;
132
133                 Point2D centerPoint = getCenterPoint(zoomLevel);
134                 Point2D direction = getDirection(zoomLevel);
135                 double centerX = centerPoint.getX(), centerY = centerPoint.getY();
136                 double deltaX = direction.getX(), deltaY = direction.getY();
137
138                 // Ensure the line is always rendered on top of the edge
139                 // to prevent overlap with static info rendered below it.
140                 double odx = offset * deltaY;
141                 double ody = offset * deltaX;
142                 if (odx < 0) {
143                     odx = -odx;
144                     ody = -ody;
145                 }
146
147                 double x0 = centerX - l/2 * deltaX + ody;
148                 double y0 = centerY - l/2 * deltaY - odx;
149                 double x1 = centerX + (l/2 - w) * deltaX + ody;
150                 double y1 = centerY + (l/2 - w) * deltaY - odx;
151
152                 g2d.draw(new Line2D.Double(x0, y0, x1, y1));
153
154                 arrowPath = new Path2D.Double();
155                 arrowPath.moveTo(x1 + w * deltaX, y1 + w * deltaY);
156                 arrowPath.lineTo(x1 + w * deltaY, y1 - w * deltaX);
157                 arrowPath.lineTo(x1 - w * deltaY, y1 + w * deltaX);
158                 arrowPath.closePath();
159                 g2d.fill(arrowPath);
160             }
161
162             // Reset
163             g2d.setStroke(oldStroke);
164             g2d.setColor(oldColor);
165             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
166         }
167
168         Point2D centerP = getCenterPoint(zoomLevel);
169         for (INode nn : getNodes()) {
170             G2DNode g2dNode = (G2DNode) nn;
171             if (g2dNode instanceof SVGNode) {
172                 // If the node is hidden from the start, then the center point cannot be calculated yet.
173                 if (!isDefined(centerP))
174                     break;
175
176                 // Render SVG symbol
177                 double viewScaleRecip = scaleStroke ? 10 / scale : 10;
178                 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(centerP, NORMAL, symbolRect, viewScaleRecip);
179                 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
180                 g2dNode.setTransform(symbolTransform);
181             }
182             g2dNode.render(g2d);
183         }
184
185         if (ot != null)
186             g2d.setTransform(ot);
187     }
188
189     static boolean isDefined(Point2D p) {
190         return p != null && !(Double.isNaN(p.getX()) || Double.isNaN(p.getY()));
191     }
192
193     boolean renderDetailed(int zoomLevel) {
194         return zoomLevel > 13;
195     }
196
197     public float getStrokeWidth(AffineTransform tr, boolean selection) {
198         double scale = DistrictNetworkNodeUtils.getScale(tr);
199         float width = STROKE.getLineWidth() * getStrokeWidth(scale);
200         if (selection) width = width + (float) (2 * STROKE.getLineWidth() / scale);
201         return width;
202     }
203     
204     private float getStrokeWidth(double scale) {
205         if (scaleStroke) {
206             double str = stroke != null ? Math.abs(stroke) : 1.0;
207             float strokeWidth = (float) (str / scale);
208             return strokeWidth;
209         }
210         else {
211             return 1.f;
212         }
213     }
214
215     public Path2D getPath() {
216         return renderDetailed(zoomLevel) ? detailedPath : path;
217     }
218
219     public Point2D getCenterPoint(int zoomLevel) {
220         return renderDetailed(zoomLevel) ? detailedCenterPoint : centerPoint;
221     }
222
223     public Point2D getDirection(int zoomLevel) {
224         return renderDetailed(zoomLevel) ? detailedDirection : direction;
225     }
226
227     public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
228         startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
229         startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
230         endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
231         endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
232
233         if (result == null) {
234              result = new Path2D.Double();
235         } else {
236              result.reset();
237         }
238         result.moveTo(startX, startY);
239         if (detailed) {
240             double[] detailedGeometry = edge.getGeometry();
241             if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
242                 // ok, lets do this
243                 for (int i = 0; i < detailedGeometry.length; i += 2) {
244                     double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
245                     double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
246                     result.lineTo(x, y);
247                 }
248             }
249         }
250         result.lineTo(endX, endY);
251         return result;
252     }
253
254     private boolean isSelected() {
255         return NodeUtil.isSelected(this, 1);
256     }
257
258     @Override
259     public Rectangle2D getBoundsInLocal() {
260         return bounds;
261     }
262
263     private void updateBounds() {
264         Rectangle2D oldBounds = bounds;
265         if (oldBounds == null)
266             oldBounds = new Rectangle2D.Double();
267         bounds = calculateBounds(oldBounds);
268     }
269
270     /**
271      * Calculates conservative bounds of the node provided by
272      * <code>detailedPath</code>. These might be larger than the simplified bounds
273      * of <code>path</code> but the conservative bounds ensure that spatial
274      * optimizations work properly.
275      */
276     private Rectangle2D calculateBounds(Rectangle2D rect) {
277         if (detailedPath == null)
278             detailedPath = calculatePath(edge, detailedPath, true);
279         return detailedPath.getBounds2D();
280     }
281
282     public void setDNEdge(DistrictNetworkEdge edge) {
283         this.edge = edge;
284         path = calculatePath(edge, path, false);
285         detailedPath = calculatePath(edge, detailedPath, true);
286
287         centerPoint = new Point2D.Double();
288         detailedCenterPoint = new Point2D.Double();
289         direction = new Point2D.Double();
290         detailedDirection = new Point2D.Double();
291
292         DistrictNetworkNodeUtils.calculateCenterPointAndDirection(path, centerPoint, direction);
293         DistrictNetworkNodeUtils.calculateCenterPointAndDirection(detailedPath, detailedCenterPoint, detailedDirection);
294
295         updateBounds();
296     }
297
298     public void setColor(Color color) {
299         this.color = color;
300     }
301
302     public Color getColor() {
303         return color;
304     }
305
306     @PropertySetter(value = "stroke")
307     public void setStroke(Double stroke) {
308         this.stroke = stroke;
309     }
310
311     @PropertySetter(value = "dynamicColor")
312     public void setDynamicColor(Color color) {
313         this.dynamicColor = color;
314     }
315
316     @PropertySetter(value = "arrowLength")
317     public void setArrowLength(Double length) {
318         arrowLength = length;
319     }
320
321     @PropertySetter(value = "SVG")
322     public void setSVG(String value) {
323         for (INode nn : this.getNodes())
324             if (nn instanceof SVGNode)
325                 ((SVGNode)nn).setData(value);
326     }
327
328     @PropertySetter(value = "hidden")
329     public void setHidden(Boolean value) {
330         this.hidden = value;
331     }
332
333 }