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