]> gerrit.simantics Code Review - simantics/district.git/blob
21f3d741e77d81755376faa7047b3762266d0a92
[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         if (scaleStroke) {
81             double scale = GeometryUtils.getScale(g2d.getTransform());
82             scale = Math.max(10000, Math.min(scale, 50000));
83             double str = stroke != null ? Math.abs(stroke) : 1.0;
84             bs = GeometryUtils.scaleStroke(STROKE, (float) (str / 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.scaleStroke(bs, 4f));
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             g2d.setStroke(new BasicStroke(bs.getLineWidth(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
105             
106             double l = arrowLength;
107             double w = 2 * (double) bs.getLineWidth() * Math.signum(l);
108             if (Math.abs(w) > Math.abs(l)) w = l;
109             double offset = 2 * (double) bs.getLineWidth();
110             
111             double centerX = (startX + endX) / 2, centerY = (startY + endY) / 2;
112             double deltaX = endX - startX, deltaY = endY - startY;
113             double length = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
114             deltaX /= length;
115             deltaY /= length;
116             
117             double x0 = centerX - l/2 * deltaX + offset * deltaY;
118             double y0 = centerY - l/2 * deltaY - offset * deltaX;
119             double x1 = centerX + (l/2 - w) * deltaX + offset * deltaY;
120             double y1 = centerY + (l/2 - w) * deltaY - offset * deltaX;
121             
122             g2d.draw(new Line2D.Double(x0, y0, x1, y1));
123             
124             Path2D path = new Path2D.Double();
125             path.moveTo(x1 + w * deltaX, y1 + w * deltaY);
126             path.lineTo(x1 + w * deltaY, y1 - w * deltaX);
127             path.lineTo(x1 - w * deltaY, y1 + w * deltaX);
128             path.closePath();
129             g2d.fill(path);
130         }
131         
132         // Reset
133         g2d.setStroke(oldStroke);
134         g2d.setColor(oldColor);
135         
136         // Render SVG symbol
137         double viewScaleRecip = 10;
138         if (scaleStroke) {
139             double scale = GeometryUtils.getScale(g2d.getTransform());
140             scale = Math.max(10000, Math.min(scale, 50000));
141             viewScaleRecip /= scale;
142         }
143         
144         Point2D p = getCenterPoint();
145         symbolRect = DistrictNetworkNodeUtils.calculateDrawnGeometry(p, NORMAL, symbolRect, viewScaleRecip);
146         symbolTransform = DistrictNetworkNodeUtils.getTransformToRectangle(symbolRect, symbolTransform);
147         
148         for (INode nn : getNodes()) {
149             G2DNode g2dNode = (G2DNode)nn;
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     private Point2D getCenterPoint() {
161         if (centerPoint == null)
162             centerPoint = new Point2D.Double();
163         
164         Rectangle2D bounds = path.getBounds2D();
165         centerPoint.setLocation(bounds.getCenterX(), bounds.getCenterY());
166         return centerPoint;
167     }
168
169 //    public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) {
170 //        // Convert to screen coordinates
171 //        double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
172 //        double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
173 //        double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
174 //        double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
175 //
176 //        if (result == null)
177 //            result = new Line2D.Double();
178 //        result.setLine(startX, startY, endX, endY);
179 //        return result;
180 //    }
181
182     public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
183         startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
184         startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY());
185         endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
186         endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());
187
188         if (result == null) {
189              result = new Path2D.Double();
190         } else {
191              result.reset();
192         }
193         result.moveTo(startX, startY);
194         if (detailed) {
195             double[] detailedGeometry = edge.getGeometry();
196             if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
197                 // ok, lets do this
198                 
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     private Rectangle2D calculateBounds(Rectangle2D rect) {
227         return calculatePath(edge, null, false).getBounds2D();
228     }
229
230     public void setDNEdge(DistrictNetworkEdge edge) {
231         this.edge = edge;
232         updateBounds();
233     }
234
235     public void setColor(Color color) {
236         this.color = color;
237     }
238
239     public Color getColor() {
240         return color;
241     }
242
243     @PropertySetter(value = "stroke")
244     public void setStroke(Double stroke) {
245         this.stroke = stroke;
246     }
247
248     @PropertySetter(value = "dynamicColor")
249     public void setDynamicColor(Color color) {
250         this.dynamicColor = color;
251     }
252     
253     @PropertySetter(value = "arrowLength")
254     public void setArroLength(Double length) {
255         arrowLength = length;
256     }
257
258     @PropertySetter(value = "SVG")
259     public void setSVG(String value) {
260         for (INode nn : this.getNodes())
261             if (nn instanceof SVGNode)
262                 ((SVGNode)nn).setData(value);
263     }
264     
265 }