]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.maps/src/org/simantics/maps/sg/MapNode.java
Trying to get map background coloring to work
[simantics/district.git] / org.simantics.district.maps / src / org / simantics / maps / sg / MapNode.java
index 952553b0ec4cb263d75d9d2bd07524cc738f225a..f9c6abf9af2fee69d4871965ac006cec329c9d51 100644 (file)
@@ -66,6 +66,7 @@ public class MapNode extends G2DNode implements ITileListener  {
     private final int    VIEWBOX_QUIET_TIME = 500;
 
     protected Boolean enabled = true;
+    protected Color backgroundColor;
 
     enum TileState {
         NOT_LOADED,
@@ -222,11 +223,15 @@ public class MapNode extends G2DNode implements ITileListener  {
         this.enabled = enabled;
     }
 
+    @SyncField("backgroundColor")
+    public void setBackgroundColor(Color color) {
+        this.backgroundColor = color;
+    }
+
     @Override
     public void render(Graphics2D g2d) {
-        if (!enabled)
-            return;
 
+        
         AffineTransform ot = g2d.getTransform();
         g2d.transform(transform);
         
@@ -250,58 +255,68 @@ public class MapNode extends G2DNode implements ITileListener  {
         Rectangle2D b = (Rectangle2D)((Rectangle)g2d.getRenderingHint(G2DRenderingHints.KEY_CONTROL_BOUNDS)).getBounds2D(); // getClipBounds is not accurate enough, use original clipbounds and scale here
 
         Rectangle2D viewbox = new Rectangle2D.Double(offsetX/scaleX, offsetY/scaleY, b.getWidth()/sp.getWidth(), b.getHeight()/sp.getHeight()); //g.getClipBounds();
-        if(viewbox == null) return; // FIXME
-
-        double smallerViewboxDimension = viewbox.getWidth() < viewbox.getHeight() ? viewbox.getWidth() * MAP_SCALE : viewbox.getHeight() * MAP_SCALE;
-        int level = 0;
-        double tileSize = 360 * MAP_SCALE*2;
-        while (level < MAX_TILE_LEVEL) {
-            double ratio = smallerViewboxDimension / tileSize;
-            if (ratio >= 0.85) {
-                break;
+
+        if (enabled) {
+        
+            double smallerViewboxDimension = viewbox.getWidth() < viewbox.getHeight() ? viewbox.getWidth() * MAP_SCALE : viewbox.getHeight() * MAP_SCALE;
+            int level = 0;
+            double tileSize = 360 * MAP_SCALE*2;
+            while (level < MAX_TILE_LEVEL) {
+                double ratio = smallerViewboxDimension / tileSize;
+                if (ratio >= 0.85) {
+                    break;
+                }
+                tileSize *= 0.5;
+                level++;
             }
-            tileSize *= 0.5;
-            level++;
-        }
-        /*
-         *  To convert y-coordinates to map coordinates in ruler, use:
-         *    double val = (y-offsetY)/scaleY;
-         *    val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val))));
-         *    String str = formatValue(val);
-         */
-
-        double minx = Math.min(180, Math.max(viewbox.getMinX(), -180));
-        double maxx = Math.min(180, Math.max(viewbox.getMaxX(), -180));
-
-        double miny = Math.min(360, Math.max(viewbox.getMinY()+180, 0));
-        double maxy = Math.min(360, Math.max(viewbox.getMaxY()+180, 0));
-
-        g2d.setTransform(new AffineTransform());
-        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
-
-        int levels = (1 << level);
-
-        // http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
-        int left = (int)Math.floor( (minx + 180) / 360 * (1<<level) );
-        int right = (int)Math.floor( (maxx + 180) / 360 * (1<<level) );
-        int top = (int)Math.floor(miny / 360 * (1<<level));//  (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(miny)) + 1 / Math.cos(Math.toRadians(miny))) / Math.PI) / 2 * (1<<level) );
-        int bottom = (int)Math.floor(maxy / 360 * (1<<level));//  (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(maxy)) + 1 / Math.cos(Math.toRadians(maxy))) / Math.PI) / 2 * (1<<level) );
-
-        double tsx = 360 / (double)levels; // Size of tile on zoom level
-        for(int tx = left; tx <= right; tx++) {
-            if(tx < 0 || tx >= levels) continue;
-            for(int ty = top; ty <= bottom; ty++) {
-                if(ty < 0 || ty >= levels) continue;
-                TileKey tile = new TileKey(level, tx, ty);
-                double y = (double)ty - (double)levels/2; // In level 0 we have only one tile
-                paintTile(tileCache, g2d, tr, tile, tx*tsx-180, y*tsx, tsx);
+            /*
+             *  To convert y-coordinates to map coordinates in ruler, use:
+             *    double val = (y-offsetY)/scaleY;
+             *    val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val))));
+             *    String str = formatValue(val);
+             */
+    
+            double minx = Math.min(180, Math.max(viewbox.getMinX(), -180));
+            double maxx = Math.min(180, Math.max(viewbox.getMaxX(), -180));
+    
+            double miny = Math.min(360, Math.max(viewbox.getMinY()+180, 0));
+            double maxy = Math.min(360, Math.max(viewbox.getMaxY()+180, 0));
+    
+            g2d.setTransform(new AffineTransform());
+            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+    
+            int levels = (1 << level);
+    
+            // http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
+            int left = (int)Math.floor( (minx + 180) / 360 * (1<<level) );
+            int right = (int)Math.floor( (maxx + 180) / 360 * (1<<level) );
+            int top = (int)Math.floor(miny / 360 * (1<<level));//  (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(miny)) + 1 / Math.cos(Math.toRadians(miny))) / Math.PI) / 2 * (1<<level) );
+            int bottom = (int)Math.floor(maxy / 360 * (1<<level));//  (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(maxy)) + 1 / Math.cos(Math.toRadians(maxy))) / Math.PI) / 2 * (1<<level) );
+    
+            double tsx = 360 / (double)levels; // Size of tile on zoom level
+            for(int tx = left; tx <= right; tx++) {
+                if(tx < 0 || tx >= levels) continue;
+                for(int ty = top; ty <= bottom; ty++) {
+                    if(ty < 0 || ty >= levels) continue;
+                    TileKey tile = new TileKey(level, tx, ty);
+                    double y = (double)ty - (double)levels/2; // In level 0 we have only one tile
+                    paintTile(tileCache, g2d, tr, tile, tx*tsx-180, y*tsx, tsx);
+                }
+            }
+    //        g.dispose();
+    
+            // Check if transform has changed
+            transformChanged(tr, level);
+        } else {
+            if (backgroundColor != null) {
+                g2d.setTransform(new AffineTransform());
+                Rectangle2D controlBounds = (Rectangle2D) g2d.getRenderingHint(G2DRenderingHints.KEY_CONTROL_BOUNDS);
+                Color color = g2d.getColor();
+                g2d.setColor(backgroundColor);
+                g2d.fill(controlBounds);
+                g2d.setColor(color);
             }
         }
-//        g.dispose();
-
-        // Check if transform has changed
-        transformChanged(tr, level);
-        
         g2d.setTransform(ot);
     }