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