]> gerrit.simantics Code Review - simantics/district.git/blob
195472fcd2474b0b060de1df3b032c350c672c5e
[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     public 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     static final 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 static double startX;
63     private static double startY;
64     private static double endX;
65     private static double endY;
66
67     @Override
68     public void init() {
69     }
70
71     @Override
72     public void render(Graphics2D g2d) {
73         AffineTransform ot = null;
74         AffineTransform t = getTransform();
75         double scale = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_UNDER_SPATIAL_ROOT) : 1.0;
76         if (t != null && !t.isIdentity()) {
77             //ot = g2d.getTransform();
78             ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
79             if (ot == null)
80                 ot = g2d.getTransform();
81             g2d.transform(t);
82             if (scaleStroke) {
83                 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
84                 work.setTransform(ot);
85                 work.concatenate(t);
86                 scale = DistrictNetworkNodeUtils.getScale(work);
87             }
88         }
89
90         zoomLevel = (Integer) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
91
92         if (!hidden) {
93             Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
94             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
95     
96             Color oldColor = g2d.getColor();
97             BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
98     
99             BasicStroke bs = null;
100             if (scaleStroke) {
101                 bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale));
102             } else {
103                 bs = STROKE;
104             }
105
106             Path2D path = renderDetailed(zoomLevel) ? this.detailedPath : this.path;
107     
108             if (isSelected()) {
109                 g2d.setColor(SELECTION_COLOR);
110                 g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale)));
111                 g2d.draw(path);
112             }
113     
114             g2d.setColor(dynamicColor != null ? dynamicColor : color);
115             g2d.setStroke(bs);
116             g2d.draw(path);
117     
118             // Reset
119             g2d.setStroke(oldStroke);
120             g2d.setColor(oldColor);
121             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
122         }
123
124         Point2D centerP = getCenterPoint(zoomLevel);
125         for (INode nn : getNodes()) {
126             G2DNode g2dNode = (G2DNode) nn;
127             if (g2dNode instanceof SVGNode) {
128                 // If the node is hidden from the start, then the center point cannot be calculated yet.
129                 if (!isDefined(centerP))
130                     break;
131
132                 // Render SVG symbol
133                 double viewScaleRecip = scaleStroke ? 10 / scale : 10;
134                 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(centerP, NORMAL, symbolRect, viewScaleRecip);
135                 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
136                 g2dNode.setTransform(symbolTransform);
137             }
138             g2dNode.render(g2d);
139         }
140
141         if (ot != null)
142             g2d.setTransform(ot);
143     }
144
145     static boolean isDefined(Point2D p) {
146         return p != null && !(Double.isNaN(p.getX()) || Double.isNaN(p.getY()));
147     }
148
149     boolean renderDetailed(int zoomLevel) {
150         return zoomLevel > 13;
151     }
152
153     public float getStrokeWidth(AffineTransform tr, boolean selection) {
154         double scale = DistrictNetworkNodeUtils.getScale(tr);
155         float width = STROKE.getLineWidth() * getStrokeWidth(scale);
156         if (selection) width = width + (float) (2 * STROKE.getLineWidth() / scale);
157         return width;
158     }
159     
160     private float getStrokeWidth(double scale) {
161         if (scaleStroke) {
162             double str = stroke != null ? Math.abs(stroke) : 1.0;
163             float strokeWidth = (float) (str / scale);
164             return strokeWidth;
165         }
166         else {
167             return 1.f;
168         }
169     }
170
171     public Path2D getPath() {
172         return renderDetailed(zoomLevel) ? detailedPath : path;
173     }
174
175     public Point2D getCenterPoint(int zoomLevel) {
176         return renderDetailed(zoomLevel) ? detailedCenterPoint : centerPoint;
177     }
178
179     public Point2D getDirection(int zoomLevel) {
180         return renderDetailed(zoomLevel) ? detailedDirection : direction;
181     }
182
183     public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
184         startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
185         startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
186         endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
187         endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
188
189         if (result == null) {
190              result = new Path2D.Double();
191         } else {
192              result.reset();
193         }
194         result.moveTo(startX, startY);
195         if (detailed) {
196             double[] detailedGeometry = edge.getGeometry();
197             if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
198                 // ok, lets do this
199                 for (int i = 0; i < detailedGeometry.length; i += 2) {
200                     double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
201                     double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
202                     result.lineTo(x, y);
203                 }
204             }
205         }
206         result.lineTo(endX, endY);
207         return result;
208     }
209
210     private boolean isSelected() {
211         return NodeUtil.isSelected(this, 1);
212     }
213
214     @Override
215     public Rectangle2D getBoundsInLocal() {
216         return bounds;
217     }
218
219     private void updateBounds() {
220         Rectangle2D oldBounds = bounds;
221         if (oldBounds == null)
222             oldBounds = new Rectangle2D.Double();
223         bounds = calculateBounds(oldBounds);
224     }
225
226     /**
227      * Calculates conservative bounds of the node provided by
228      * <code>detailedPath</code>. These might be larger than the simplified bounds
229      * of <code>path</code> but the conservative bounds ensure that spatial
230      * optimizations work properly.
231      */
232     private Rectangle2D calculateBounds(Rectangle2D rect) {
233         if (detailedPath == null)
234             detailedPath = calculatePath(edge, detailedPath, true);
235         return detailedPath.getBounds2D();
236     }
237
238     public void setDNEdge(DistrictNetworkEdge edge) {
239         this.edge = edge;
240         path = calculatePath(edge, path, false);
241         detailedPath = calculatePath(edge, detailedPath, true);
242
243         centerPoint = new Point2D.Double();
244         detailedCenterPoint = new Point2D.Double();
245         direction = new Point2D.Double();
246         detailedDirection = new Point2D.Double();
247
248         DistrictNetworkNodeUtils.calculateCenterPointAndDirection(path, centerPoint, direction);
249         DistrictNetworkNodeUtils.calculateCenterPointAndDirection(detailedPath, detailedCenterPoint, detailedDirection);
250
251         updateBounds();
252     }
253
254     public void setColor(Color color) {
255         this.color = color;
256     }
257
258     public Color getColor() {
259         return color;
260     }
261
262     @PropertySetter(value = "stroke")
263     public void setStroke(Double stroke) {
264         this.stroke = stroke;
265     }
266
267     @PropertySetter(value = "dynamicColor")
268     public void setDynamicColor(Color color) {
269         this.dynamicColor = color;
270     }
271
272     @PropertySetter(value = "arrowLength")
273     public void setArrowLength(Double length) {
274         // find if there is a child deferred arrow node
275         DistrictNetworkEdgeArrayNode child = getOrCreateNode(DistrictNetworkEdgeArrayNode.NODE_KEY, DistrictNetworkEdgeArrayNode.class);
276         child.setEdgeNode(this);
277         child.setArrowLength(length);
278         //arrowLength = length;
279     }
280
281     @PropertySetter(value = "SVG")
282     public void setSVG(String value) {
283         for (INode nn : this.getNodes())
284             if (nn instanceof SVGNode)
285                 ((SVGNode)nn).setData(value);
286     }
287
288     @PropertySetter(value = "hidden")
289     public void setHidden(Boolean value) {
290         this.hidden = value;
291     }
292
293 }