]> gerrit.simantics Code Review - simantics/district.git/blob
a28abada5b88f5f1f0c08365d418fa9f55ad7994
[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.geom.Line2D;
7 import java.awt.geom.Path2D;
8 import java.awt.geom.Point2D;
9 import java.awt.geom.Rectangle2D;
10
11 import org.simantics.district.network.ui.styles.DistrictNetworkStaticInfoStyle;
12 import org.simantics.scenegraph.ParentNode;
13 import org.simantics.scenegraph.g2d.G2DNode;
14 import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
15 import org.simantics.scenegraph.utils.NodeUtil;
16 import org.simantics.utils.datastructures.Pair;
17
18 public class DistrictNetworkEdgeArrayNode extends G2DNode implements DeferredNode {
19
20     private static final long serialVersionUID = -8377444630205663419L;
21
22     public static final String NODE_KEY = "DISTRICT_NETWORK_EDGE_ARRAY";
23
24     private static final String RENDERER_ID = "edgeArrayDeferredRenderer";
25
26     private DistrictNetworkEdgeNode edgeNode = null;
27     private Double arrowLength;
28     private Path2D arrowPath;
29
30     @Override
31     public void render(Graphics2D g2d) {
32         ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(this, RTreeNode.class);
33         DeferredRenderingNode deferred = root != null ? (DeferredRenderingNode) root.getNode(RENDERER_ID) : null;
34         if (deferred != null)
35             deferred.deferNode(g2d.getTransform(), this);
36         else
37             renderDeferred(g2d);
38     }
39
40     @Override
41     public void renderDeferred(Graphics2D g2d) {
42
43         // Draw arrow
44         if (arrowLength != null) {
45             double scale = DistrictNetworkEdgeNode.scaleStroke ? (Double) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_SCALE_UNDER_SPATIAL_ROOT) : 1.0;
46             g2d.setColor(Color.BLACK);
47             float lw = DistrictNetworkEdgeNode.STROKE.getLineWidth() / (float)scale;
48             g2d.setStroke(new BasicStroke(lw, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
49
50             double l = arrowLength;
51             double w = 2 * (double) lw * Math.signum(l);
52             if (Math.abs(w) > Math.abs(l)) w = l;
53             double offset = 2 * (double) lw;
54
55             int zoomLevel = (Integer) g2d.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
56             
57             Point2D centerPoint = edgeNode.getCenterPoint(zoomLevel);
58             Point2D direction = edgeNode.getDirection(zoomLevel);
59             double centerX = centerPoint.getX(), centerY = centerPoint.getY();
60             double deltaX = direction.getX(), deltaY = direction.getY();
61
62             // Ensure the line is always rendered on top of the edge
63             // to prevent overlap with static info rendered below it.
64             double odx = offset * deltaY;
65             double ody = offset * deltaX;
66             if (odx < 0) {
67                 odx = -odx;
68                 ody = -ody;
69             }
70
71             double x0 = centerX - l/2 * deltaX + ody;
72             double y0 = centerY - l/2 * deltaY - odx;
73             double x1 = centerX + (l/2 - w) * deltaX + ody;
74             double y1 = centerY + (l/2 - w) * deltaY - odx;
75
76             g2d.draw(new Line2D.Double(x0, y0, x1, y1));
77
78             arrowPath = new Path2D.Double();
79             arrowPath.moveTo(x1 + w * deltaX, y1 + w * deltaY);
80             arrowPath.lineTo(x1 + w * deltaY, y1 - w * deltaX);
81             arrowPath.lineTo(x1 - w * deltaY, y1 + w * deltaX);
82             arrowPath.closePath();
83             g2d.fill(arrowPath);
84         }
85     }
86
87     @Override
88     public Rectangle2D getBoundsInLocal() {
89         return null;
90     }
91     
92     public void setEdgeNode(DistrictNetworkEdgeNode edgeNode) {
93         this.edgeNode = edgeNode;
94     }
95
96     public void setArrowLength(Double length) {
97         this.arrowLength = length;
98     }
99
100     public static Pair<String, Class<DeferredRenderingNode>> renderer() {
101         return Pair.make(RENDERER_ID, DeferredRenderingNode.class);
102     }
103
104 }