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