]> gerrit.simantics Code Review - simantics/district.git/blob
1c677316a26c167751150d21d290fb33fe53cbbb
[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                 double x0 = centerX - l/2 * deltaX + offset * deltaY;
139                 double y0 = centerY - l/2 * deltaY - offset * deltaX;
140                 double x1 = centerX + (l/2 - w) * deltaX + offset * deltaY;
141                 double y1 = centerY + (l/2 - w) * deltaY - offset * deltaX;
142
143                 g2d.draw(new Line2D.Double(x0, y0, x1, y1));
144
145                 arrowPath = new Path2D.Double();
146                 arrowPath.moveTo(x1 + w * deltaX, y1 + w * deltaY);
147                 arrowPath.lineTo(x1 + w * deltaY, y1 - w * deltaX);
148                 arrowPath.lineTo(x1 - w * deltaY, y1 + w * deltaX);
149                 arrowPath.closePath();
150                 g2d.fill(arrowPath);
151             }
152
153             // Reset
154             g2d.setStroke(oldStroke);
155             g2d.setColor(oldColor);
156             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
157         }
158
159         Point2D centerP = getCenterPoint(zoomLevel);
160         for (INode nn : getNodes()) {
161             G2DNode g2dNode = (G2DNode) nn;
162             if (g2dNode instanceof SVGNode) {
163                 // If the node is hidden from the start, then the center point cannot be calculated yet.
164                 if (!isDefined(centerP))
165                     break;
166
167                 // Render SVG symbol
168                 double viewScaleRecip = scaleStroke ? 10 / scale : 10;
169                 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(centerP, NORMAL, symbolRect, viewScaleRecip);
170                 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
171                 g2dNode.setTransform(symbolTransform);
172             }
173             g2dNode.render(g2d);
174         }
175
176         if (ot != null)
177             g2d.setTransform(ot);
178     }
179
180     static boolean isDefined(Point2D p) {
181         return p != null && !(Double.isNaN(p.getX()) || Double.isNaN(p.getY()));
182     }
183
184     boolean renderDetailed(int zoomLevel) {
185         return zoomLevel > 13;
186     }
187
188     public float getStrokeWidth(AffineTransform tr, boolean selection) {
189         double scale = DistrictNetworkNodeUtils.getScale(tr);
190         float width = STROKE.getLineWidth() * getStrokeWidth(scale);
191         if (selection) width = width + (float) (2 * STROKE.getLineWidth() / scale);
192         return width;
193     }
194     
195     private float getStrokeWidth(double scale) {
196         if (scaleStroke) {
197             double str = stroke != null ? Math.abs(stroke) : 1.0;
198             float strokeWidth = (float) (str / scale);
199             return strokeWidth;
200         }
201         else {
202             return 1.f;
203         }
204     }
205
206     public Path2D getPath() {
207         return renderDetailed(zoomLevel) ? detailedPath : path;
208     }
209
210     public Point2D getCenterPoint(int zoomLevel) {
211         return renderDetailed(zoomLevel) ? detailedCenterPoint : centerPoint;
212     }
213
214     public Point2D getDirection(int zoomLevel) {
215         return renderDetailed(zoomLevel) ? detailedDirection : direction;
216     }
217
218     public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
219         startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
220         startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
221         endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
222         endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
223
224         if (result == null) {
225              result = new Path2D.Double();
226         } else {
227              result.reset();
228         }
229         result.moveTo(startX, startY);
230         if (detailed) {
231             double[] detailedGeometry = edge.getGeometry();
232             if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
233                 // ok, lets do this
234                 for (int i = 0; i < detailedGeometry.length; i += 2) {
235                     double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
236                     double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
237                     result.lineTo(x, y);
238                 }
239             }
240         }
241         result.lineTo(endX, endY);
242         return result;
243     }
244
245     private boolean isSelected() {
246         return NodeUtil.isSelected(this, 1);
247     }
248
249     @Override
250     public Rectangle2D getBoundsInLocal() {
251         return bounds;
252     }
253
254     private void updateBounds() {
255         Rectangle2D oldBounds = bounds;
256         if (oldBounds == null)
257             oldBounds = new Rectangle2D.Double();
258         bounds = calculateBounds(oldBounds);
259     }
260
261     /**
262      * Calculates conservative bounds of the node provided by
263      * <code>detailedPath</code>. These might be larger than the simplified bounds
264      * of <code>path</code> but the conservative bounds ensure that spatial
265      * optimizations work properly.
266      */
267     private Rectangle2D calculateBounds(Rectangle2D rect) {
268         if (detailedPath == null)
269             detailedPath = calculatePath(edge, detailedPath, true);
270         return detailedPath.getBounds2D();
271     }
272
273     public void setDNEdge(DistrictNetworkEdge edge) {
274         this.edge = edge;
275         path = calculatePath(edge, path, false);
276         detailedPath = calculatePath(edge, detailedPath, true);
277
278         centerPoint = new Point2D.Double();
279         detailedCenterPoint = new Point2D.Double();
280         direction = new Point2D.Double();
281         detailedDirection = new Point2D.Double();
282
283         DistrictNetworkNodeUtils.calculateCenterPointAndDirection(path, centerPoint, direction);
284         DistrictNetworkNodeUtils.calculateCenterPointAndDirection(detailedPath, detailedCenterPoint, detailedDirection);
285
286         updateBounds();
287     }
288
289     public void setColor(Color color) {
290         this.color = color;
291     }
292
293     public Color getColor() {
294         return color;
295     }
296
297     @PropertySetter(value = "stroke")
298     public void setStroke(Double stroke) {
299         this.stroke = stroke;
300     }
301
302     @PropertySetter(value = "dynamicColor")
303     public void setDynamicColor(Color color) {
304         this.dynamicColor = color;
305     }
306
307     @PropertySetter(value = "arrowLength")
308     public void setArrowLength(Double length) {
309         arrowLength = length;
310     }
311
312     @PropertySetter(value = "SVG")
313     public void setSVG(String value) {
314         for (INode nn : this.getNodes())
315             if (nn instanceof SVGNode)
316                 ((SVGNode)nn).setData(value);
317     }
318
319     @PropertySetter(value = "hidden")
320     public void setHidden(Boolean value) {
321         this.hidden = value;
322     }
323
324 }