]> gerrit.simantics Code Review - simantics/district.git/blob
00fe2184e49dcc53ba77fa922e426b1e4888562c
[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
36     private static boolean scaleStroke = true;
37     private Color color;
38     private Double stroke;
39     private transient Color dynamicColor = null;
40
41     // Dimensions for shut-off valve symbol
42     private static final double left = -0.25;
43     private static final double top = -0.25;
44     private static final double width = 0.5;
45     private static final double height = 0.5;
46
47     private static final Rectangle2D NORMAL = new Rectangle2D.Double(left, top, width, height);
48
49     private transient Point2D centerPoint;
50     private transient Rectangle2D symbolRect;
51     private transient AffineTransform symbolTransform;
52
53     private boolean hidden = false;
54     
55     private Double arrowLength;
56
57     private static double startX;
58     private static double startY;
59     private static double endX;
60     private static double endY;
61
62     @Override
63     public void init() {
64     }
65
66     @Override
67     public void render(Graphics2D g2d) {
68         AffineTransform ot = null;
69         AffineTransform t = getTransform();
70         double scale = scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_UNDER_SPATIAL_ROOT) : 1.0;
71         if (t != null && !t.isIdentity()) {
72             //ot = g2d.getTransform();
73             ot = (AffineTransform) g2d.getRenderingHint(G2DRenderingHints.KEY_TRANSFORM_UNDER_SPATIAL_ROOT);
74             g2d.transform(t);
75             if (scaleStroke) {
76                 AffineTransform work = DistrictNetworkNodeUtils.sharedTransform.get();
77                 work.setTransform(ot);
78                 work.concatenate(t);
79                 scale = DistrictNetworkNodeUtils.getScale(work);
80             }
81         }
82
83         if (!hidden) {
84             Object aaHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
85             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
86     
87             Color oldColor = g2d.getColor();
88             BasicStroke oldStroke = (BasicStroke) g2d.getStroke();
89     
90             BasicStroke bs = null;
91             if (scaleStroke) {
92                 bs = GeometryUtils.scaleStroke(STROKE, getStrokeWidth(scale));
93             } else {
94                 bs = STROKE;
95             }
96
97             int zoomLevel = (Integer) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
98             path = calculatePath(edge, path, zoomLevel > 15);
99     
100             if (isSelected()) {
101                 g2d.setColor(SELECTION_COLOR);
102                 g2d.setStroke(GeometryUtils.scaleAndOffsetStrokeWidth(bs, 1.f, (float)(2 * STROKE.getLineWidth() / scale)));
103                 g2d.draw(path);
104             }
105     
106             g2d.setColor(dynamicColor != null ? dynamicColor : color);
107             g2d.setStroke(bs);
108             g2d.draw(path);
109     
110             // Draw arrow
111             if (arrowLength != null) {
112                 g2d.setColor(Color.BLACK);
113                 float lw = STROKE.getLineWidth() / (float)scale;
114                 g2d.setStroke(new BasicStroke(lw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
115                 
116                 double l = arrowLength;
117                 double w = 2 * (double) lw * Math.signum(l);
118                 if (Math.abs(w) > Math.abs(l)) w = l;
119                 double offset = 2 * (double) lw;
120                 
121                 double centerX = (startX + endX) / 2, centerY = (startY + endY) / 2;
122                 double deltaX = endX - startX, deltaY = endY - startY;
123                 double length = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
124                 deltaX /= length;
125                 deltaY /= length;
126                 
127                 double x0 = centerX - l/2 * deltaX + offset * deltaY;
128                 double y0 = centerY - l/2 * deltaY - offset * deltaX;
129                 double x1 = centerX + (l/2 - w) * deltaX + offset * deltaY;
130                 double y1 = centerY + (l/2 - w) * deltaY - offset * deltaX;
131                 
132                 g2d.draw(new Line2D.Double(x0, y0, x1, y1));
133                 
134                 Path2D path = new Path2D.Double();
135                 path.moveTo(x1 + w * deltaX, y1 + w * deltaY);
136                 path.lineTo(x1 + w * deltaY, y1 - w * deltaX);
137                 path.lineTo(x1 - w * deltaY, y1 + w * deltaX);
138                 path.closePath();
139                 g2d.fill(path);
140             }
141             
142             // Reset
143             g2d.setStroke(oldStroke);
144             g2d.setColor(oldColor);
145             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
146         }
147
148         for (INode nn : getNodes()) {
149             G2DNode g2dNode = (G2DNode)nn;
150             if (g2dNode instanceof SVGNode) {
151                 // Render SVG symbol
152                 double viewScaleRecip = 10;
153                 if (scaleStroke) {
154                     viewScaleRecip /= scale;
155                 }
156
157                 // If the node is hidden from the start, then the center point cannot be calculated yet.
158                 Point2D p = getCenterPoint();
159                 if (p == null)
160                     break;
161
162                 symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip);
163                 symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
164
165                 g2dNode.setTransform(symbolTransform);
166             }
167             g2dNode.render(g2d);
168         }
169
170         if (ot != null)
171             g2d.setTransform(ot);
172     }
173
174     public float getStrokeWidth(AffineTransform tr, boolean selection) {
175         double scale = DistrictNetworkNodeUtils.getScale(tr);
176         float width = STROKE.getLineWidth() * getStrokeWidth(scale);
177         if (selection) width = width + (float) (2 * STROKE.getLineWidth() / scale);
178         return width;
179     }
180     
181     private float getStrokeWidth(double scale) {
182         if (scaleStroke) {
183             double str = stroke != null ? Math.abs(stroke) : 1.0;
184             float strokeWidth = (float) (str / scale);
185             return strokeWidth;
186         }
187         else {
188             return 1.f;
189         }
190     }
191
192     public Path2D getPath() {
193         return path;
194     }
195     
196     private Point2D getCenterPoint() {
197         if (path == null)
198             return null;
199         if (centerPoint == null)
200             centerPoint = new Point2D.Double();
201         Rectangle2D bounds = path.getBounds2D();
202         centerPoint.setLocation(bounds.getCenterX(), bounds.getCenterY());
203         return centerPoint;
204     }
205
206 //    public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) {
207 //        // Convert to screen coordinates
208 //        double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
209 //        double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
210 //        double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
211 //        double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
212 //
213 //        if (result == null)
214 //            result = new Line2D.Double();
215 //        result.setLine(startX, startY, endX, endY);
216 //        return result;
217 //    }
218
219     public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
220         startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
221         startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
222         endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
223         endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
224
225         if (result == null) {
226              result = new Path2D.Double();
227         } else {
228              result.reset();
229         }
230         result.moveTo(startX, startY);
231         if (detailed) {
232             double[] detailedGeometry = edge.getGeometry();
233             if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
234                 // ok, lets do this
235                 
236                 for (int i = 0; i < detailedGeometry.length; i += 2) {
237                     double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
238                     double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
239                     result.lineTo(x, y);
240                 }
241             }
242         }
243         result.lineTo(endX, endY);
244         return result;
245     }
246
247     private boolean isSelected() {
248         return NodeUtil.isSelected(this, 1);
249     }
250
251     @Override
252     public Rectangle2D getBoundsInLocal() {
253         return bounds;
254     }
255
256     private void updateBounds() {
257         Rectangle2D oldBounds = bounds;
258         if (oldBounds == null)
259             oldBounds = new Rectangle2D.Double();
260         bounds = calculateBounds(oldBounds);
261     }
262
263     private Rectangle2D calculateBounds(Rectangle2D rect) {
264         return calculatePath(edge, null, false).getBounds2D();
265     }
266
267     public void setDNEdge(DistrictNetworkEdge edge) {
268         this.edge = edge;
269         updateBounds();
270     }
271
272     public void setColor(Color color) {
273         this.color = color;
274     }
275
276     public Color getColor() {
277         return color;
278     }
279
280     @PropertySetter(value = "stroke")
281     public void setStroke(Double stroke) {
282         this.stroke = stroke;
283     }
284
285     @PropertySetter(value = "dynamicColor")
286     public void setDynamicColor(Color color) {
287         this.dynamicColor = color;
288     }
289     
290     @PropertySetter(value = "arrowLength")
291     public void setArrowLength(Double length) {
292         arrowLength = length;
293     }
294
295     @PropertySetter(value = "SVG")
296     public void setSVG(String value) {
297         for (INode nn : this.getNodes())
298             if (nn instanceof SVGNode)
299                 ((SVGNode)nn).setData(value);
300     }
301     
302
303     @PropertySetter(value = "hidden")
304     public void setHidden(Boolean value) {
305         this.hidden = value;
306     }
307 }