]> gerrit.simantics Code Review - simantics/district.git/blob
523f7e805cc8c5cd73e6ebbf8d0f935df2f4e0bb
[simantics/district.git] /
1 package org.simantics.district.network.ui.nodes;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.FontMetrics;
6 import java.awt.Graphics2D;
7 import java.awt.RenderingHints;
8 import java.awt.geom.AffineTransform;
9 import java.awt.geom.Point2D;
10 import java.awt.geom.Rectangle2D;
11
12 import org.simantics.district.network.ui.styles.DistrictNetworkStaticInfoStyle;
13 import org.simantics.scenegraph.ParentNode;
14 import org.simantics.scenegraph.g2d.G2DNode;
15 import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
16 import org.simantics.scenegraph.utils.DPIUtil;
17 import org.simantics.scenegraph.utils.NodeUtil;
18
19 public class DistrictNetworkStaticInfoNode extends G2DNode implements DeferredNode {
20
21         private static final long serialVersionUID = -1723122278582600873L;
22
23         private static final Font FONT = new Font(Font.SANS_SERIF, Font.PLAIN, DPIUtil.upscale(10));
24
25         public static final String NODE_KEY = "DISTRICT_NETWORK_STATIC_INFO";
26
27         String info = null;
28         Point2D origin = new Point2D.Double();
29         Point2D direction = new Point2D.Double();
30
31         private DistrictNetworkEdgeNode edgeNode = null;
32
33         private int prevZoomLevel = -1;
34
35         @Override
36         public void render(Graphics2D g) {
37                 ParentNode<?> root = (ParentNode<?>) NodeUtil.getNearestParentOfType(this, RTreeNode.class);
38                 DeferredRenderingNode deferred = root != null ? (DeferredRenderingNode) root.getNode(DistrictNetworkStaticInfoStyle.STATIC_INFO_DEFERRED) : null;
39                 if (deferred != null)
40                         deferred.deferNode(g.getTransform(), this);
41                 else
42                         renderDeferred(g);
43         }
44
45         @Override
46         public void renderDeferred(Graphics2D g) {
47                 if (info == null || "".equals(info))
48                         return;
49                 int zoomLevel = (Integer) g.getRenderingHint(DistrictRenderingHints.KEY_VIEW_ZOOM_LEVEL);
50                 if (zoomLevel < 17)
51                         return;
52
53                 AffineTransform oldTransform = g.getTransform();
54
55                 Font oldFont = g.getFont();
56                 Color oldColor = g.getColor();
57                 Object oldAA = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
58                 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
59
60                 if (edgeNode != null && zoomLevel != prevZoomLevel) {
61                         origin.setLocation(edgeNode.getCenterPoint(zoomLevel));
62                         Point2D dir = edgeNode.getDirection(zoomLevel);
63                         double s = dir.getX() >= 0.0 ? 1.0 : -1.0;
64                         direction.setLocation(s * dir.getX(), s * dir.getY());
65                 }
66                 prevZoomLevel = zoomLevel;
67
68                 doRender(g);
69
70                 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAA);
71                 g.setTransform(oldTransform);
72                 g.setColor(oldColor);
73                 g.setFont(oldFont);
74         }
75
76         private void doRender(Graphics2D g) {
77                 g.translate(origin.getX(), origin.getY());
78                 double scale = 1.0 / g.getTransform().getScaleX();
79                 g.scale(scale, scale);
80
81                 g.setFont(FONT);
82                 FontMetrics fm = g.getFontMetrics();
83                 int width = fm.stringWidth(info);
84                 int ascent = fm.getMaxAscent();
85
86                 g.transform(AffineTransform.getRotateInstance(direction.getX(), direction.getY()));
87                 g.translate(0, ascent+2);
88
89 //              int height = fm.getHeight();
90 //              g.setColor(Color.WHITE);
91 //              g.fillRect(-width/2 - 5, -ascent-1, width+10, height+2);
92                 g.setColor(Color.BLACK);
93 //              g.drawRect(-width/2 - 5, -ascent-1, width+10, height+2);
94
95                 g.drawString(info, -width/2, 0);
96         }
97
98         @Override
99         public Rectangle2D getBoundsInLocal() {
100                 return null;
101         }
102
103         public void setLocation(Point2D origin, Point2D direction) {
104                 this.origin.setLocation(origin);
105                 this.direction.setLocation(direction);
106         }
107
108         public void setInfo(String info) {
109                 this.info = info;
110         }
111
112         public void setEdgeNode(DistrictNetworkEdgeNode n) {
113                 this.edgeNode = n;
114                 // Ensure that origin/location are recalculated
115                 prevZoomLevel = -1;
116         }
117 }