Changeset 29170 in osm for applications/viewer/jmapviewer


Ignore:
Timestamp:
2013-01-05T11:21:15+01:00 (12 years ago)
Author:
the111
Message:

Fix #josm8328 - Add scroll wrap option. Default/current map options unaffected.

Location:
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Demo.java

    r27992 r29170  
    6363        setExtendedState(JFrame.MAXIMIZED_BOTH);
    6464        JPanel panel = new JPanel();
     65        JPanel panelTop = new JPanel();
     66        JPanel panelBottom = new JPanel();
    6567        JPanel helpPanel = new JPanel();
    6668
     
    7375        add(panel, BorderLayout.NORTH);
    7476        add(helpPanel, BorderLayout.SOUTH);
     77        panel.setLayout(new BorderLayout());
     78        panel.add(panelTop, BorderLayout.NORTH);
     79        panel.add(panelBottom, BorderLayout.SOUTH);
    7580        JLabel helpLabel = new JLabel("Use right mouse button to move,\n "
    7681                + "left double click or mouse wheel to zoom.");
     
    103108        });
    104109        map.setTileLoader((TileLoader) tileLoaderSelector.getSelectedItem());
    105         panel.add(tileSourceSelector);
    106         panel.add(tileLoaderSelector);
     110        panelTop.add(tileSourceSelector);
     111        panelTop.add(tileLoaderSelector);
    107112        final JCheckBox showMapMarker = new JCheckBox("Map markers visible");
    108113        showMapMarker.setSelected(map.getMapMarkersVisible());
     
    113118            }
    114119        });
    115         panel.add(showMapMarker);
     120        panelBottom.add(showMapMarker);
    116121        final JCheckBox showTileGrid = new JCheckBox("Tile grid visible");
    117122        showTileGrid.setSelected(map.isTileGridVisible());
     
    122127            }
    123128        });
    124         panel.add(showTileGrid);
     129        panelBottom.add(showTileGrid);
    125130        final JCheckBox showZoomControls = new JCheckBox("Show zoom controls");
    126131        showZoomControls.setSelected(map.getZoomContolsVisible());
     
    131136            }
    132137        });
    133         panel.add(showZoomControls);
    134         panel.add(button);
     138        panelBottom.add(showZoomControls);
     139        final JCheckBox scrollWrapEnabled = new JCheckBox("Scrollwrap enabled");
     140        scrollWrapEnabled.addActionListener(new ActionListener() {
     141            public void actionPerformed(ActionEvent e) {
     142                map.setScrollWrapEnabled(scrollWrapEnabled.isSelected());
     143            }
     144        });
     145        panelBottom.add(scrollWrapEnabled);
     146        panelBottom.add(button);
    135147
    136         panel.add(zoomLabel);
    137         panel.add(zoomValue);
    138         panel.add(mperpLabelName);
    139         panel.add(mperpLabelValue);
     148        panelTop.add(zoomLabel);
     149        panelTop.add(zoomValue);
     150        panelTop.add(mperpLabelName);
     151        panelTop.add(mperpLabelValue);
    140152
    141153        add(map, BorderLayout.CENTER);
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapViewer.java

    r29169 r29170  
    6363
    6464    protected boolean tileGridVisible;
     65    protected boolean scrollWrapEnabled;
    6566
    6667    protected TileController tileController;
     
    510511        int x_max = getWidth();
    511512        int y_max = getHeight();
     513       
     514        // calculate the length of the grid (number of squares per edge)
     515        int gridLength = 1 << zoom;
    512516
    513517        // paint the tiles in a spiral, starting from center of the map
     
    523527                    if (x_min <= posx && posx <= x_max && y_min <= posy && posy <= y_max) {
    524528                        // tile is visible
    525                         Tile tile = tileController.getTile(tilex, tiley, zoom);
     529                        Tile tile;
     530                        if (scrollWrapEnabled) {
     531                            // in case tilex is out of bounds, grab the tile to use for wrapping
     532                            int tilexWrap = (((tilex % gridLength) + gridLength) % gridLength);
     533                            tile = tileController.getTile(tilexWrap, tiley, zoom);
     534                        } else {
     535                            tile = tileController.getTile(tilex, tiley, zoom);
     536                        }
    526537                        if (tile != null) {
    527538                            tile.paint(g, posx, posy);
     
    543554        // outer border of the map
    544555        int mapSize = tilesize << zoom;
    545         g.drawRect(w2 - center.x, h2 - center.y, mapSize, mapSize);
     556        if (scrollWrapEnabled) {
     557            g.drawLine(0, h2 - center.y, getWidth(), h2 - center.y);
     558            g.drawLine(0, h2 - center.y + mapSize, getWidth(), h2 - center.y + mapSize);
     559        } else {
     560            g.drawRect(w2 - center.x, h2 - center.y, mapSize, mapSize);
     561        }
    546562
    547563        // g.drawString("Tiles in cache: " + tileCache.getTileCount(), 50, 20);
    548564
     565        // keep x-coordinates from growing without bound if scroll-wrap is enabled
     566        if (scrollWrapEnabled) {
     567            center.x = center.x % mapSize;
     568        }
     569       
    549570        if (mapPolygonsVisible && mapPolygonList != null) {
    550571            for (MapPolygon polygon : mapPolygonList) {
     
    573594    protected void paintMarker(Graphics g, MapMarker marker) {
    574595        Point p = getMapPosition(marker.getLat(), marker.getLon());
    575         if (p != null) {
     596        if (scrollWrapEnabled) {
     597            int tilesize = tileSource.getTileSize();
     598            int mapSize = tilesize << zoom;
     599            if (p == null) {
     600                p = getMapPosition(marker.getLat(), marker.getLon(), false);
     601            }
    576602            marker.paint(g, p);
     603            int xSave = p.x;
     604            int xWrap = xSave;
     605            // overscan of 15 allows up to 30-pixel markers to gracefully scroll off the edge of the panel
     606            while ((xWrap -= mapSize) >= -15) {
     607                p.x = xWrap;
     608                marker.paint(g, p);
     609            }
     610            xWrap = xSave;
     611            while ((xWrap += mapSize) <= getWidth() + 15) {
     612                p.x = xWrap;
     613                marker.paint(g, p);
     614            }
     615        } else {
     616            if (p != null) {
     617                marker.paint(g, p);
     618            }
    577619        }
    578620    }
     
    589631            if (pTopLeft != null && pBottomRight != null) {
    590632                rectangle.paint(g, pTopLeft, pBottomRight);
     633                if (scrollWrapEnabled) {
     634                    int tilesize = tileSource.getTileSize();
     635                    int mapSize = tilesize << zoom;
     636                    int xTopLeftSave = pTopLeft.x;
     637                    int xTopLeftWrap = xTopLeftSave;
     638                    int xBottomRightSave = pBottomRight.x;
     639                    int xBottomRightWrap = xBottomRightSave;
     640                    while ((xBottomRightWrap -= mapSize) >= 0) {
     641                        xTopLeftWrap -= mapSize;
     642                        pTopLeft.x = xTopLeftWrap;
     643                        pBottomRight.x = xBottomRightWrap;
     644                        rectangle.paint(g, pTopLeft, pBottomRight);
     645                    }
     646                    xTopLeftWrap = xTopLeftSave;
     647                    xBottomRightWrap = xBottomRightSave;
     648                    while ((xTopLeftWrap += mapSize) <= getWidth()) {
     649                        xBottomRightWrap += mapSize;
     650                        pTopLeft.x = xTopLeftWrap;
     651                        pBottomRight.x = xBottomRightWrap;
     652                        rectangle.paint(g, pTopLeft, pBottomRight);
     653                    }
     654                   
     655                }
    591656            }
    592657        }
     
    608673            }
    609674            polygon.paint(g, points);
     675            if (scrollWrapEnabled) {
     676                int tilesize = tileSource.getTileSize();
     677                int mapSize = tilesize << zoom;
     678                List<Point> pointsWrapped = new LinkedList<Point>(points);
     679                boolean keepWrapping = true;
     680                while (keepWrapping) {
     681                    for (Point p : pointsWrapped) {
     682                        p.x -= mapSize;
     683                        if (p.x < 0) {
     684                            keepWrapping = false;
     685                        }
     686                    }
     687                    polygon.paint(g, pointsWrapped);
     688                }
     689                pointsWrapped = new LinkedList<Point>(points);
     690                keepWrapping = true;
     691                while (keepWrapping) {
     692                    for (Point p : pointsWrapped) {
     693                        p.x += mapSize;
     694                        if (p.x > getWidth()) {
     695                            keepWrapping = false;
     696                        }
     697                    }
     698                    polygon.paint(g, pointsWrapped);
     699                }
     700            }
    610701        }
    611702    }
     
    867958        repaint();
    868959    }
     960   
     961    public boolean isScrollWrapEnabled() {
     962        return scrollWrapEnabled;
     963    }
     964   
     965    public void setScrollWrapEnabled(boolean scrollWrapEnabled) {
     966        this.scrollWrapEnabled = scrollWrapEnabled;
     967        repaint();
     968    }
    869969
    870970    /**
Note: See TracChangeset for help on using the changeset viewer.