]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.maps/src/org/simantics/maps/sg/MapLocationZoomInfoNode.java
Possibility to show elevation while hoovering on map
[simantics/district.git] / org.simantics.district.maps / src / org / simantics / maps / sg / MapLocationZoomInfoNode.java
1 package org.simantics.maps.sg;
2
3 import java.awt.AlphaComposite;
4 import java.awt.BasicStroke;
5 import java.awt.Color;
6 import java.awt.Font;
7 import java.awt.FontMetrics;
8 import java.awt.Graphics2D;
9 import java.awt.geom.AffineTransform;
10 import java.awt.geom.Point2D;
11 import java.awt.geom.Rectangle2D;
12 import java.util.Locale;
13
14 import org.simantics.g2d.participant.MouseUtil;
15 import org.simantics.g2d.participant.MouseUtil.MouseInfo;
16 import org.simantics.maps.elevation.server.SingletonTiffTileInterface;
17 import org.simantics.scenegraph.g2d.G2DNode;
18 import org.simantics.scenegraph.utils.DPIUtil;
19
20 public class MapLocationZoomInfoNode extends G2DNode {
21
22     private static final long serialVersionUID = 7994492218791569147L;
23
24     private static final Color GRAY              = new Color(100, 100, 100);
25
26     protected boolean enabled = true;
27
28     private MouseUtil util;
29     
30     @Override
31     public void render(Graphics2D g2d) {
32         if (!enabled)
33             return;
34         
35         AffineTransform ot = g2d.getTransform();
36         Color originalColor = g2d.getColor();
37         g2d.transform(transform);
38         
39         g2d.setTransform(new AffineTransform());
40         // do the rendering magic
41         
42         Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
43         
44         //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
45         g2d.setStroke(new BasicStroke(1));
46         g2d.setColor(new Color(0.9f, 0.9f, 0.9f, 0.75f));
47         
48         Rectangle2D bounds = g2d.getClipBounds();
49         if (bounds == null)
50             return; // FIXME
51
52         MouseInfo mouseInfo = util.getMouseInfo(0);
53         
54         double startLat;
55         double startLon;
56         if (mouseInfo != null && mouseInfo.canvasPosition != null) {
57             Point2D canvasPosition = mouseInfo.canvasPosition;
58             double cx = canvasPosition.getX();
59             double cy = canvasPosition.getY();
60             
61             startLat = yToLatitude(-cy / transform.getScaleY());
62             startLon = xToLongitude(cx / transform.getScaleX());
63         } else {
64             startLat = 0;
65             startLon = 0;
66         }
67         Number zoomLevel = SingletonTiffTileInterface.lookup(startLat, startLon);
68         String str = "X: " + formatValue(startLon, MAX_DIGITS) + ", Y: " + formatValue(startLat, MAX_DIGITS) + ", Z: " + zoomLevel;
69         g2d.setFont(rulerFont);
70         FontMetrics fm = g2d.getFontMetrics();
71         Rectangle2D r = fm.getStringBounds(str, g2d);
72
73         double pixels = r.getWidth() + 10;
74         double scaleRight = bounds.getMaxX() - 20;
75         double newScaleLeft = scaleRight - pixels;
76         double y = bounds.getMaxY() - 65;
77         g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
78         
79         
80         Rectangle2D vertical = new Rectangle2D.Double(newScaleLeft, y, pixels, 20);
81         g2d.fill(vertical);
82         
83         g2d.setColor(GRAY);
84         g2d.setFont(rulerFont);
85         
86         
87         g2d.setColor(Color.BLACK);
88         g2d.drawString(str, (int)newScaleLeft + 5, (int)y + 15);
89         
90         g2d.setColor(originalColor);
91         g2d.setTransform(ot);
92     }
93
94     @Override
95     public Rectangle2D getBoundsInLocal() {
96         return null;
97     }
98
99     public boolean isEnabled() {
100         return enabled;
101     }
102
103     public void setEnabled(boolean enabled) {
104         this.enabled = enabled;
105     }
106
107     public void setMouseUtil(MouseUtil util) {
108         this.util = util;
109     }
110
111     private static final transient int    MAX_DIGITS = 7;
112     private static final transient double EPSILON    = 0.01;
113     private static final transient double TRIM_THRESHOLD_MAX_VALUE = Math.pow(10, 4);
114     private static final transient String[] SI_UNIT_LARGE_PREFIXES = {
115         "k", "M", "G", "T", "P", "E", "Z", "Y"
116     };
117
118     private static String formatValue(double value, int maxDigits) {
119         int magnitude = (int) Math.round(Math.log10(value));
120         //System.out.println("magnitude: " + magnitude + ", " + value);
121         int allowedDecimals = maxDigits;
122         allowedDecimals -= Math.abs(magnitude);
123         if (allowedDecimals < 0)
124             allowedDecimals = 0;
125
126         String valueStr = String.format(Locale.US, "%." + allowedDecimals + "f", value);
127         if (allowedDecimals > 0) {
128             for (int trunc = valueStr.length() - 1; trunc > 0; --trunc) {
129                 char ch = valueStr.charAt(trunc);
130                 if (ch == '.') {
131                     valueStr = valueStr.substring(0, trunc);
132                     break;
133                 }
134                 if (valueStr.charAt(trunc) != '0') {
135                     valueStr = valueStr.substring(0, trunc + 1);
136                     break;
137                 }
138             }
139             if (Math.abs(value) + EPSILON > TRIM_THRESHOLD_MAX_VALUE) {
140                 // Cut anything beyond a possible decimal dot out since they
141                 // should not show anyway. This is a complete hack that tries to
142                 // circumvent floating-point inaccuracy problems.
143                 int dotIndex = valueStr.lastIndexOf('.');
144                 if (dotIndex > -1) {
145                     valueStr = valueStr.substring(0, dotIndex);
146                 }
147             }
148         }
149
150         double trimValue = value;
151         if (Math.abs(value)+EPSILON >= TRIM_THRESHOLD_MAX_VALUE) {
152             for (int i = 0; Math.abs(trimValue)+EPSILON >= TRIM_THRESHOLD_MAX_VALUE; ++i) {
153                 double trim = trimValue / 1000;
154                 if (Math.abs(trim)-EPSILON < TRIM_THRESHOLD_MAX_VALUE) {
155                     valueStr = valueStr.substring(0, valueStr.length() - (i + 1) * 3);
156                     valueStr += SI_UNIT_LARGE_PREFIXES[i];
157                     break;
158                 }
159                 trimValue = trim;
160             }
161         }
162
163         if (valueStr.equals("-0"))
164             valueStr = "0";
165
166         return valueStr;
167     }
168
169     // TODO: these only work with Spherical Mercator
170     private static double xToLongitude(double x) {
171         return x;
172     }
173
174     private static double yToLatitude(double y) {
175         double rad = Math.toRadians(y);
176         double sinh = Math.sinh(rad);
177         double atan = Math.atan(sinh);
178         double finald = Math.toDegrees(atan);
179         return finald;
180     }
181 }