1 package org.simantics.maps.sg;
3 import java.awt.AlphaComposite;
4 import java.awt.BasicStroke;
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;
14 import org.simantics.g2d.participant.MouseUtil;
15 import org.simantics.g2d.participant.MouseUtil.MouseInfo;
16 import org.simantics.maps.MapScalingTransform;
17 import org.simantics.scenegraph.g2d.G2DNode;
18 import org.simantics.scenegraph.utils.DPIUtil;
20 public class MapLocationZoomInfoNode extends G2DNode {
22 private static final long serialVersionUID = 7994492218791569147L;
24 private static final Color GRAY = new Color(100, 100, 100);
26 protected boolean enabled = true;
28 private MouseUtil util;
31 public void render(Graphics2D g2d) {
35 AffineTransform ot = g2d.getTransform();
36 Color originalColor = g2d.getColor();
37 g2d.transform(transform);
39 g2d.setTransform(new AffineTransform());
40 // do the rendering magic
42 Font rulerFont = new Font("Tahoma", Font.PLAIN, DPIUtil.upscale(9));
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));
48 Rectangle2D bounds = g2d.getClipBounds();
52 int zoomLevel = MapScalingTransform.zoomLevel(ot);
53 MouseInfo mouseInfo = util.getMouseInfo(0);
57 if (mouseInfo != null && mouseInfo.canvasPosition != null) {
58 Point2D canvasPosition = mouseInfo.canvasPosition;
59 double cx = canvasPosition.getX();
60 double cy = canvasPosition.getY();
62 startLat = yToLatitude(-cy / transform.getScaleY());
63 startLon = xToLongitude(cx / transform.getScaleX());
69 String str = "X: " + formatValue(startLon, MAX_DIGITS) + ", Y: " + formatValue(startLat, MAX_DIGITS) + ", Z: " + zoomLevel;
70 g2d.setFont(rulerFont);
71 FontMetrics fm = g2d.getFontMetrics();
72 Rectangle2D r = fm.getStringBounds(str, g2d);
74 double pixels = r.getWidth() + 10;
75 double scaleRight = bounds.getMaxX() - 20;
76 double newScaleLeft = scaleRight - pixels;
77 double y = bounds.getMaxY() - 65;
78 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
81 Rectangle2D vertical = new Rectangle2D.Double(newScaleLeft, y, pixels, 20);
85 g2d.setFont(rulerFont);
88 g2d.setColor(Color.BLACK);
89 g2d.drawString(str, (int)newScaleLeft + 5, (int)y + 15);
91 g2d.setColor(originalColor);
96 public Rectangle2D getBoundsInLocal() {
100 public boolean isEnabled() {
104 public void setEnabled(boolean enabled) {
105 this.enabled = enabled;
108 public void setMouseUtil(MouseUtil util) {
112 private static final transient int MAX_DIGITS = 7;
113 private static final transient double EPSILON = 0.01;
114 private static final transient double TRIM_THRESHOLD_MAX_VALUE = Math.pow(10, 4);
115 private static final transient String[] SI_UNIT_LARGE_PREFIXES = {
116 "k", "M", "G", "T", "P", "E", "Z", "Y"
119 private static String formatValue(double value, int maxDigits) {
120 int magnitude = (int) Math.round(Math.log10(value));
121 //System.out.println("magnitude: " + magnitude + ", " + value);
122 int allowedDecimals = maxDigits;
123 allowedDecimals -= Math.abs(magnitude);
124 if (allowedDecimals < 0)
127 String valueStr = String.format(Locale.US, "%." + allowedDecimals + "f", value);
128 if (allowedDecimals > 0) {
129 for (int trunc = valueStr.length() - 1; trunc > 0; --trunc) {
130 char ch = valueStr.charAt(trunc);
132 valueStr = valueStr.substring(0, trunc);
135 if (valueStr.charAt(trunc) != '0') {
136 valueStr = valueStr.substring(0, trunc + 1);
140 if (Math.abs(value) + EPSILON > TRIM_THRESHOLD_MAX_VALUE) {
141 // Cut anything beyond a possible decimal dot out since they
142 // should not show anyway. This is a complete hack that tries to
143 // circumvent floating-point inaccuracy problems.
144 int dotIndex = valueStr.lastIndexOf('.');
146 valueStr = valueStr.substring(0, dotIndex);
151 double trimValue = value;
152 if (Math.abs(value)+EPSILON >= TRIM_THRESHOLD_MAX_VALUE) {
153 for (int i = 0; Math.abs(trimValue)+EPSILON >= TRIM_THRESHOLD_MAX_VALUE; ++i) {
154 double trim = trimValue / 1000;
155 if (Math.abs(trim)-EPSILON < TRIM_THRESHOLD_MAX_VALUE) {
156 valueStr = valueStr.substring(0, valueStr.length() - (i + 1) * 3);
157 valueStr += SI_UNIT_LARGE_PREFIXES[i];
164 if (valueStr.equals("-0"))
170 // TODO: these only work with Spherical Mercator
171 private static double xToLongitude(double x) {
175 private static double yToLatitude(double y) {
176 double rad = Math.toRadians(y);
177 double sinh = Math.sinh(rad);
178 double atan = Math.atan(sinh);
179 double finald = Math.toDegrees(atan);