Changeset 9537 in osm for applications/editors/josm/plugins
- Timestamp:
- 2008-08-07T15:33:50+02:00 (16 years ago)
- Location:
- applications/editors/josm/plugins/slippy_map_chooser
- Files:
-
- 4 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/slippy_map_chooser
-
Property svn:ignore
set to
build
-
Property svn:ignore
set to
-
applications/editors/josm/plugins/slippy_map_chooser/build.xml
r8698 r9537 47 47 <target name="compile" depends="init"> 48 48 <echo message="creating ${plugin.jar}"/> 49 <javac srcdir="src " classpath="${josm}" destdir="build"/>49 <javac srcdir="src;src_jmv" classpath="${josm}" destdir="build" debug="true"/> 50 50 </target> 51 51 -
applications/editors/josm/plugins/slippy_map_chooser/src/OsmMapControl.java
r7343 r9537 2 2 // License: GPL. Copyright 2007 by Tim Haussmann 3 3 4 import java.awt.Cursor;5 4 import java.awt.Point; 6 import java.awt.event.ActionEvent;7 5 import java.awt.event.KeyEvent; 8 6 import java.awt.event.MouseAdapter; 9 7 import java.awt.event.MouseEvent; 8 import java.awt.event.MouseListener; 10 9 import java.awt.event.MouseMotionListener; 11 import java.awt.event.MouseWheelEvent;12 import java.awt.event.MouseWheelListener;13 10 14 import javax.swing.AbstractAction;15 11 import javax.swing.JComponent; 16 12 import javax.swing.JPanel; 17 13 import javax.swing.KeyStroke; 18 import static org.openstreetmap.josm.tools.I18n.tr;19 14 20 15 /** 21 * This class controls the user input by listening to mouse and key events. Currently implemented is:22 * - zooming in and out with scrollwheel23 * - zooming in and centering by double clicking24 * - selecting an area by clicking and draggingthe mouse16 * This class controls the user input by listening to mouse and key events. 17 * Currently implemented is: - zooming in and out with scrollwheel - zooming in 18 * and centering by double clicking - selecting an area by clicking and dragging 19 * the mouse 25 20 * 26 21 * @author Tim Haussmann 27 22 */ 28 public class OsmMapControl extends MouseAdapter implements MouseMotionListener, Mouse WheelListener {23 public class OsmMapControl extends MouseAdapter implements MouseMotionListener, MouseListener { 29 24 30 31 //positions while moving the map 32 private int iLastX = 0; 33 private int iLastY = 0; 34 35 //start and end point of selection rectangle 25 // start and end point of selection rectangle 36 26 private Point iStartSelectionPoint; 37 27 private Point iEndSelectionPoint; 38 28 39 // the SlippyMapChooserComponent29 // the SlippyMapChooserComponent 40 30 private final SlippyMapChooser iSlippyMapChooser; 41 42 //The old cursor when we changed it to movement cursor.43 private Cursor iOldCursor;44 31 45 private boolean isMovementInPlace = false;46 47 32 private SizeButton iSizeButton = null; 48 33 49 50 private final class ZoomerAction extends AbstractAction {51 private final String action;52 public ZoomerAction(String action) {53 this.action = action;54 }55 public void actionPerformed(ActionEvent e) {56 if (action.equals(".") || action.equals(",")) {57 Point mouse = iSlippyMapChooser.getMousePosition();58 if (mouse == null)59 mouse = new Point((int)iSlippyMapChooser.getBounds().getCenterX(), (int)iSlippyMapChooser.getBounds().getCenterY());60 MouseWheelEvent we = new MouseWheelEvent(iSlippyMapChooser, e.getID(), e.getWhen(), e.getModifiers(), mouse.x, mouse.y, 0, false, MouseWheelEvent.WHEEL_UNIT_SCROLL, 1, action.equals(",") ? -1 : 1);61 mouseWheelMoved(we);62 } else {63 if (action.equals(tr("left")))64 iSlippyMapChooser.moveMap(-30, 0);65 else if (action.equals("right"))66 iSlippyMapChooser.moveMap(30, 0);67 else if (action.equals("up"))68 iSlippyMapChooser.moveMap(0, -30);69 else if (action.equals("down"))70 iSlippyMapChooser.moveMap(0, 30);71 }72 }73 }74 75 76 77 34 /** 78 35 * Create a new OsmMapControl … … 82 39 iSlippyMapChooser.addMouseListener(this); 83 40 iSlippyMapChooser.addMouseMotionListener(this); 84 iSlippyMapChooser.addMouseWheelListener(this); 85 86 String[] n = {",",".","up","right","down","left"}; 87 int[] k = {KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_UP, KeyEvent.VK_RIGHT, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT}; 41 42 String[] n = { ",", ".", "up", "right", "down", "left" }; 43 int[] k = 44 { KeyEvent.VK_COMMA, KeyEvent.VK_PERIOD, KeyEvent.VK_UP, KeyEvent.VK_RIGHT, 45 KeyEvent.VK_DOWN, KeyEvent.VK_LEFT }; 88 46 89 47 if (contentPane != null) { 90 48 for (int i = 0; i < n.length; ++i) { 91 contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(k[i], KeyEvent.CTRL_DOWN_MASK), "MapMover.Zoomer."+n[i]); 92 contentPane.getActionMap().put("MapMover.Zoomer."+n[i], new ZoomerAction(n[i])); 49 contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( 50 KeyStroke.getKeyStroke(k[i], KeyEvent.CTRL_DOWN_MASK), 51 "MapMover.Zoomer." + n[i]); 93 52 } 94 53 } 95 96 54 iSizeButton = sizeButton; 97 55 } 98 56 99 57 /** 100 * If the right mouse button is pressed and moved while holding, move the map.101 * If the left mouse button is pressed start the selection rectangle and stop when left mouse button is released58 * Start drawing the selection rectangle if it was the 1st button (left 59 * button) 102 60 */ 103 public void mouseDragged(MouseEvent e) { 104 int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK; 105 //Moving the map 106 if ((e.getModifiersEx() & (MouseEvent.BUTTON3_DOWN_MASK | offMask)) == MouseEvent.BUTTON3_DOWN_MASK) { 107 108 startMovement(e); 109 110 //calc the moved distance since the last drag event 111 int distX = e.getX()-iLastX; 112 int distY = e.getY()-iLastY; 113 114 //change the offset for the origin of the map 115 iSlippyMapChooser.moveMap(-distX, -distY); 116 117 //update the last position of the mouse for the next dragging event 118 iLastX = e.getX(); 119 iLastY = e.getY(); 120 } 121 //selecting 122 else if((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == MouseEvent.BUTTON1_DOWN_MASK){ 123 iEndSelectionPoint = e.getPoint(); 124 iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint()); 125 } 126 //end moving of the map 127 else 128 endMovement(); 129 } 130 131 /** 132 * Start moving the map, if it was the 3rd button (right button). 133 * Start drawing the selection rectangle if it was the 1st button (left button) 134 */ 135 @Override public void mousePressed(MouseEvent e) { 136 int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK; 137 if (e.getButton() == MouseEvent.BUTTON3 && (e.getModifiersEx() & offMask) == 0) 138 startMovement(e); 139 else if (e.getButton() == MouseEvent.BUTTON1){ 140 if(!iSizeButton.hit(e.getPoint())){ 61 @Override 62 public void mousePressed(MouseEvent e) { 63 if (e.getButton() == MouseEvent.BUTTON1) { 64 if (!iSizeButton.hit(e.getPoint())) { 141 65 iStartSelectionPoint = e.getPoint(); 142 i SlippyMapChooser.setSelection(iStartSelectionPoint, iEndSelectionPoint);66 iEndSelectionPoint = e.getPoint(); 143 67 } 144 68 } 145 69 } 146 147 148 /**149 * When dragging the map change the cursor back to it's pre-move cursor.150 * If a double-click occurs center and zoom the map on the clicked location.151 */152 @Override public void mouseReleased(MouseEvent e) {153 if (e.getButton() == MouseEvent.BUTTON3)154 endMovement();155 else if (e.getButton() == MouseEvent.BUTTON1){156 if(iSizeButton.hit(e.getPoint())){157 iSizeButton.toggle();158 iSlippyMapChooser.resizeSlippyMap();159 }else{160 if(e.getClickCount() == 1){161 iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint());162 163 //reset the selections start and end164 iEndSelectionPoint = null;165 iStartSelectionPoint = null;166 }167 else if(e.getClickCount() == 2){168 iSlippyMapChooser.zoomIn(e.getPoint());169 iSlippyMapChooser.centerOnScreenPoint(e.getPoint());170 }171 }172 }173 }174 70 175 /** 176 * Start movement by setting a new cursor and remember the current mouse 177 * position. 178 */ 179 private void startMovement(MouseEvent e) { 180 if (isMovementInPlace) 181 return; 182 isMovementInPlace = true; 183 iLastX = e.getX(); 184 iLastY = e.getY(); 185 iOldCursor = iSlippyMapChooser.getCursor(); 186 iSlippyMapChooser.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 187 } 188 189 /** 190 * End the movement. Setting back the cursor and clear the movement variables 191 */ 192 private void endMovement() { 193 if (!isMovementInPlace) 194 return; 195 isMovementInPlace = false; 196 if (iOldCursor != null) 197 iSlippyMapChooser.setCursor(iOldCursor); 198 else 199 iSlippyMapChooser.setCursor(Cursor.getDefaultCursor()); 200 iOldCursor = null; 201 } 202 203 /** 204 * Zoom the map in and out. 205 * @param e The wheel event. 206 */ 207 public void mouseWheelMoved(MouseWheelEvent e) { 208 int rot = e.getWheelRotation(); 209 //scroll wheel rotated away from user 210 if(rot < 0){ 211 iSlippyMapChooser.zoomIn(e.getPoint()); 212 } 213 //scroll wheel rotated towards the user 214 else if(rot > 0){ 215 iSlippyMapChooser.zoomOut(e.getPoint()); 71 @Override 72 public void mouseDragged(MouseEvent e) { 73 if (iStartSelectionPoint != null) { 74 iEndSelectionPoint = e.getPoint(); 75 iSlippyMapChooser.setSelection(iStartSelectionPoint, iEndSelectionPoint); 216 76 } 217 77 } 218 78 219 79 /** 220 * Does nothing. Only to satisfy MouseMotionListener 80 * When dragging the map change the cursor back to it's pre-move cursor. If 81 * a double-click occurs center and zoom the map on the clicked location. 221 82 */ 222 public void mouseMoved(MouseEvent e) {} 83 @Override 84 public void mouseReleased(MouseEvent e) { 85 if (e.getButton() == MouseEvent.BUTTON1) { 86 if (iSizeButton.hit(e.getPoint())) { 87 iSizeButton.toggle(); 88 iSlippyMapChooser.resizeSlippyMap(); 89 } else { 90 if (e.getClickCount() == 1) { 91 iSlippyMapChooser.setSelection(iStartSelectionPoint, e.getPoint()); 92 93 // reset the selections start and end 94 iEndSelectionPoint = null; 95 iStartSelectionPoint = null; 96 } 97 } 98 } 99 } 100 223 101 } -
applications/editors/josm/plugins/slippy_map_chooser/src/SlippyMapChooser.java
r9159 r9537 1 1 // This code has been adapted and copied from code that has been written by Immanuel Scholz and others for JOSM. 2 2 // License: GPL. Copyright 2007 by Tim Haussmann 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 3 5 4 6 import java.awt.BorderLayout; … … 9 11 import java.awt.Point; 10 12 import java.awt.Toolkit; 11 import java. util.Enumeration;12 13 import javax.swing.JComponent; 13 import java.awt.geom.Point2D; 14 import java.util.Vector; 15 14 16 import javax.swing.JLabel; 15 17 import javax.swing.JPanel; 16 18 17 import org.openstreetmap.josm.data.coor.LatLon; 19 import org.openstreetmap.gui.jmapviewer.JMapViewer; 20 import org.openstreetmap.gui.jmapviewer.MapMarkerDot; 21 import org.openstreetmap.gui.jmapviewer.OsmMercator; 22 import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker; 18 23 import org.openstreetmap.josm.gui.download.DownloadDialog; 19 24 import org.openstreetmap.josm.gui.download.DownloadSelection; 20 import static org.openstreetmap.josm.tools.I18n.tr;21 25 22 26 /** 23 * JComponent that displays the slippy map tiles 27 * JComponent that displays the slippy map tiles 28 * 24 29 * @author Tim Haussmann 25 * 30 * 26 31 */ 27 public class SlippyMapChooser extends JComponent implements DownloadSelection{ 28 29 private static final int MAX_ZOOMLEVEL = 20; 30 private static final int MIN_ZOOMLEVEL = 1; 31 32 private TileDB iTileDB; 33 32 public class SlippyMapChooser extends JMapViewer implements DownloadSelection { 33 34 34 private DownloadDialog iGui; 35 36 //the upper left and lower right corners of the selection rectangle 35 36 // upper left and lower right corners of the selection rectangle (x/y on 37 // ZOOM_MAX) 37 38 Point iSelectionRectStart; 38 39 Point iSelectionRectEnd; 39 40 //Offsets for x and y (i.e. to center the first tile) 41 int iOffsetX = 0; 42 int iOffsetY = 0; 43 44 //the zoom level of the currently shown tiles 45 static int iZoomlevel = 3; 46 47 private boolean iFirstPaint = true; 48 private LatLon iFirstPaintCenter = new LatLon(51,7); 49 40 50 41 private SizeButton iSizeButton = new SizeButton(); 51 52 // standard dimension42 43 // standard dimension 53 44 private Dimension iDownloadDialogDimension; 54 // screen size45 // screen size 55 46 private Dimension iScreenSize; 56 57 private LatLon iScreenCenterBeforeResize; 58 private LatLon iSelectionStartBeforeResize; 59 private LatLon iSelectionEndBeforeResize; 60 private boolean isJustResized = false; 61 62 private int iVisibleTilesX = 2; 63 private int iVisibleTilesY = 3; 64 47 65 48 /** 66 49 * Create the chooser component. 67 50 */ 68 public SlippyMapChooser() { 69 70 //create the tile db 71 iTileDB = new TileDB(this); 72 73 74 75 setMinimumSize(new Dimension(350, 350/2)); 51 public SlippyMapChooser() { 52 super(); 53 setZoomContolsVisible(false); 54 setMapMarkerVisible(false); 55 setMinimumSize(new Dimension(350, 350 / 2)); 76 56 } 77 57 … … 81 61 temp.setLayout(new BorderLayout()); 82 62 temp.add(this, BorderLayout.CENTER); 83 temp.add(new JLabel((tr("Zoom: Mousewheel or double click. Move map: Hold right mousebutton and move mouse. Select: Click."))), BorderLayout.SOUTH); 63 temp.add(new JLabel((tr("Zoom: Mousewheel or double click. " 64 + "Move map: Hold right mousebutton and move mouse. Select: Click."))), 65 BorderLayout.SOUTH); 84 66 iGui.tabpane.add(temp, tr("Slippy map")); 85 86 new OsmMapControl(this,temp, iSizeButton); 67 68 new OsmMapControl(this, temp, iSizeButton); 69 boundingBoxChanged(gui); 70 } 71 72 protected Point getTopLeftCoordinates() { 73 return new Point(center.x - (getWidth() / 2), center.y - (getHeight() / 2)); 74 } 75 76 /** 77 * Draw the map. 78 */ 79 @Override 80 public void paint(Graphics g) { 81 try { 82 super.paint(g); 83 84 // draw selection rectangle 85 if (iSelectionRectStart != null && iSelectionRectEnd != null) { 86 87 int zoomDiff = MAX_ZOOM - zoom; 88 Point tlc = getTopLeftCoordinates(); 89 int x_min = (iSelectionRectStart.x >> zoomDiff) - tlc.x; 90 int y_min = (iSelectionRectStart.y >> zoomDiff) - tlc.y; 91 int x_max = (iSelectionRectEnd.x >> zoomDiff) - tlc.x; 92 int y_max = (iSelectionRectEnd.y >> zoomDiff) - tlc.y; 93 94 int w = x_max - x_min; 95 int h = y_max - y_min; 96 g.setColor(new Color(0.9f, 0.7f, 0.7f, 0.6f)); 97 g.fillRect(x_min, y_min, w, h); 98 99 g.setColor(Color.BLACK); 100 g.drawRect(x_min, y_min, w, h); 101 102 } 103 104 iSizeButton.paint(g); 105 } catch (Exception e) { 106 e.printStackTrace(); 107 } 108 } 109 110 public void boundingBoxChanged(DownloadDialog gui) { 111 112 // test if a bounding box has been set set 113 if (gui.minlat == 0.0 && gui.minlon == 0.0 && gui.maxlat == 0.0 && gui.maxlon == 0.0) 114 return; 115 116 int y1 = OsmMercator.LatToY(gui.minlat, MAX_ZOOM); 117 int y2 = OsmMercator.LatToY(gui.maxlat, MAX_ZOOM); 118 int x1 = OsmMercator.LonToX(gui.minlon, MAX_ZOOM); 119 int x2 = OsmMercator.LonToX(gui.maxlon, MAX_ZOOM); 120 121 iSelectionRectStart = new Point(Math.min(x1, x2), Math.min(y1, y2)); 122 iSelectionRectEnd = new Point(Math.max(x1, x2), Math.max(y1, y2)); 123 124 // calc the screen coordinates for the new selection rectangle 125 MapMarkerDot xmin_ymin = new MapMarkerDot(gui.minlat, gui.minlon); 126 MapMarkerDot xmax_ymax = new MapMarkerDot(gui.maxlat, gui.maxlon); 127 128 Vector<MapMarker> marker = new Vector<MapMarker>(2); 129 marker.add(xmin_ymin); 130 marker.add(xmax_ymax); 131 setMapMarkerList(marker); 132 setDisplayToFitMapMarkers(); 133 zoomOut(); 134 } 135 136 /** 137 * Callback for the OsmMapControl. (Re-)Sets the start and end point of the 138 * selection rectangle. 139 * 140 * @param aStart 141 * @param aEnd 142 */ 143 public void setSelection(Point aStart, Point aEnd) { 144 if (aStart == null || aEnd == null) 145 return; 146 Point p_max = new Point(Math.max(aEnd.x, aStart.x), Math.max(aEnd.y, aStart.y)); 147 Point p_min = new Point(Math.min(aEnd.x, aStart.x), Math.min(aEnd.y, aStart.y)); 148 149 Point tlc = getTopLeftCoordinates(); 150 int zoomDiff = MAX_ZOOM - zoom; 151 Point pEnd = new Point(p_max.x + tlc.x, p_max.y + tlc.y); 152 Point pStart = new Point(p_min.x + tlc.x, p_min.y + tlc.y); 153 154 pEnd.x <<= zoomDiff; 155 pEnd.y <<= zoomDiff; 156 pStart.x <<= zoomDiff; 157 pStart.y <<= zoomDiff; 158 159 iSelectionRectStart = pStart; 160 iSelectionRectEnd = pEnd; 161 162 Point2D.Double l1 = getPosition(p_max); 163 Point2D.Double l2 = getPosition(p_min); 164 iGui.minlat = Math.min(l2.x, l1.x); 165 iGui.minlon = Math.min(l1.y, l2.y); 166 iGui.maxlat = Math.max(l2.x, l1.x); 167 iGui.maxlon = Math.max(l1.y, l2.y); 168 169 iGui.boundingBoxChanged(this); 87 170 repaint(); 88 } 89 90 /** 91 * Performs resizing of the DownloadDialog in order to enlarge or shrink the map. 92 */ 93 public void resizeSlippyMap(){ 94 if(iScreenSize == null){ 95 Component c = iGui.getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent(); 96 //remember the initial set screen dimensions 171 } 172 173 /** 174 * Performs resizing of the DownloadDialog in order to enlarge or shrink the 175 * map. 176 */ 177 public void resizeSlippyMap() { 178 if (iScreenSize == null) { 179 Component c = 180 iGui.getParent().getParent().getParent().getParent().getParent().getParent() 181 .getParent().getParent().getParent(); 182 // remember the initial set screen dimensions 97 183 iDownloadDialogDimension = c.getSize(); 98 // retrive the size of the display184 // retrive the size of the display 99 185 iScreenSize = Toolkit.getDefaultToolkit().getScreenSize(); 100 186 } 101 102 //remember the screen center (we want to have the same center after resizing) 103 iScreenCenterBeforeResize = getLatLonOfScreenPoint(new Point(getWidth()/2, getHeight()/2)); 104 105 //remember lat/lon of the selection rectangle 106 if(iSelectionRectEnd != null && iSelectionRectStart != null){ 107 iSelectionStartBeforeResize = getLatLonOfScreenPoint(iSelectionRectStart); 108 iSelectionEndBeforeResize = getLatLonOfScreenPoint(iSelectionRectEnd); 109 } 110 111 //resize 112 Component co = iGui.getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent(); 187 188 // resize 189 Component co = 190 iGui.getParent().getParent().getParent().getParent().getParent().getParent() 191 .getParent().getParent().getParent(); 113 192 Dimension currentDimension = co.getSize(); 114 115 //enlarge 116 if(currentDimension.equals(iDownloadDialogDimension)){ 117 //make the each dimension 90% of the absolute display size and center the DownloadDialog 118 int w = iScreenSize.width*90/100; 119 int h = iScreenSize.height*90/100; 120 co.setBounds((iScreenSize.width-w)/2, (iScreenSize.height-h)/2, w, h); 121 } 122 //shrink 123 else{ 124 //set the size back to the initial dimensions and center the DownloadDialog 193 194 // enlarge 195 if (currentDimension.equals(iDownloadDialogDimension)) { 196 // make the each dimension 90% of the absolute display size and 197 // center the DownloadDialog 198 int w = iScreenSize.width * 90 / 100; 199 int h = iScreenSize.height * 90 / 100; 200 co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h); 201 } 202 // shrink 203 else { 204 // set the size back to the initial dimensions and center the 205 // DownloadDialog 125 206 int w = iDownloadDialogDimension.width; 126 207 int h = iDownloadDialogDimension.height; 127 co.setBounds((iScreenSize.width-w)/2, (iScreenSize.height-h)/2, w, h); 128 } 129 130 //the new dimension are 'available' after (or while) the next repaint 131 isJustResized = true; 132 208 co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h); 209 } 133 210 repaint(); 134 211 } 135 212 136 /**137 * Draw the map.138 */139 @Override public void paint(Graphics g) {140 141 if(iFirstPaint){142 //calculate numbers of visible tiles143 calcVisibleTiles();144 145 //save selection146 LatLon selStart = null;147 LatLon selEnd = null;148 if(iSelectionRectEnd != null && iSelectionRectStart != null){149 selStart = getLatLonOfScreenPoint(iSelectionRectStart);150 selEnd = getLatLonOfScreenPoint(iSelectionRectEnd);151 }152 centerOnLatLon(iFirstPaintCenter);153 //restore selection154 if(selStart != null && selEnd != null){155 iSelectionRectStart = getScreenPointForLatLon(selStart);156 iSelectionRectEnd = getScreenPointForLatLon(selEnd);157 }158 159 loadVisibleTiles();160 iFirstPaint = false;161 repaint();162 }163 164 if(isJustResized){165 centerOnLatLon(iScreenCenterBeforeResize);166 //restore selection167 if(iSelectionEndBeforeResize != null && iSelectionStartBeforeResize != null){168 iSelectionRectStart = getScreenPointForLatLon(iSelectionStartBeforeResize);169 iSelectionRectEnd = getScreenPointForLatLon(iSelectionEndBeforeResize);170 }171 172 //calculate numbers of visible tiles173 calcVisibleTiles();174 175 loadVisibleTiles();176 isJustResized = false;177 }178 179 //translate origin to draw map tiles in 'map space'180 g.translate(iOffsetX, iOffsetY);181 182 //draw tiles of the current zoomlevel183 for(int y=0; y<iVisibleTilesY; y++){184 for(int x=0; x<iVisibleTilesX; x++){185 OsmTile t = iTileDB.get(OsmTile.key(iZoomlevel, (-iOffsetX + x*OsmTile.WIDTH)/OsmTile.WIDTH, ((-iOffsetY+ y*OsmTile.HEIGHT)/OsmTile.HEIGHT)));186 if(t != null){187 t.paint(g,iTileDB);188 }189 }190 }191 192 //translate origin back193 g.translate(-iOffsetX, -iOffsetY);194 195 //draw selection rectangle196 if(iSelectionRectStart != null && iSelectionRectEnd != null){197 198 g.setColor(Color.black);199 g.drawRect(iSelectionRectStart.x, iSelectionRectStart.y, iSelectionRectEnd.x -iSelectionRectStart.x, iSelectionRectEnd.y -iSelectionRectStart.y);200 201 g.setColor(new Color(0.9f,0.7f,0.7f,0.6f));202 g.fillRect(iSelectionRectStart.x+1, iSelectionRectStart.y+1, iSelectionRectEnd.x -iSelectionRectStart.x-1, iSelectionRectEnd.y -iSelectionRectStart.y-1);203 }204 205 iSizeButton.paint(g);206 207 208 if(SlippyMapChooserPlugin.DEBUG_MODE){209 g.setColor(Color.black);210 g.drawString("Free Memory: " + Runtime.getRuntime().freeMemory()/1024 + "/" + Runtime.getRuntime().totalMemory()/1024 + "kB" , 5, 50);211 g.drawString("Tiles in DB: " + iTileDB.getCachedTilesSize(), 5, 65);212 g.drawString("Loading Queue Size: " + iTileDB.getLoadingQueueSize(), 5, 80);213 214 }215 }216 217 218 public void boundingBoxChanged(DownloadDialog gui) {219 220 //calc the screen coordinates for the new selection rectangle221 int x1 = OsmMercator.LonToX(gui.minlon, iZoomlevel);222 int x2 = OsmMercator.LonToX(gui.maxlon, iZoomlevel);223 int y1 = OsmMercator.LatToY(gui.minlat, iZoomlevel);224 int y2 = OsmMercator.LatToY(gui.maxlat, iZoomlevel);225 226 iSelectionRectStart = new Point(Math.min(x1, x2)+iOffsetX, Math.min(y1, y2)+iOffsetY);227 iSelectionRectEnd = new Point(Math.max(x1, x2)+iOffsetX, Math.max(y1, y2)+iOffsetY);228 229 //calc zoom level230 double dLat = Math.abs(gui.maxlat - gui.minlat);231 double dLon = Math.abs(gui.maxlon - gui.minlon);232 int zoomLat = (int) (Math.log(90 / dLat)/Math.log(2));233 int zoomLon = (int) (Math.log(90 / dLon)/Math.log(2));234 iZoomlevel = Math.max(zoomLat, zoomLon);235 236 //center on the rectangle237 if(gui.minlat != 0 && gui.maxlat != 0 && gui.minlon != 0 && gui.maxlat != 0){238 iFirstPaintCenter = new LatLon((gui.minlat + gui.maxlat)/2, (gui.minlon + gui.maxlon)/2);239 iFirstPaint = true;240 }241 repaint();242 }243 244 /**245 * Loads all tiles that are visible246 */247 void loadVisibleTiles(){248 for(int y=iVisibleTilesY-1; y>=0; y--){249 for(int x=0; x<iVisibleTilesX; x++){250 if(y > 0 && y < iVisibleTilesX-2){251 iTileDB.loadTile(iZoomlevel, (-iOffsetX + x*OsmTile.WIDTH)/OsmTile.WIDTH,((-iOffsetY+ y*OsmTile.HEIGHT)/OsmTile.HEIGHT), TileDB.PRIO_HIGH );252 }else{253 iTileDB.loadTile(iZoomlevel, (-iOffsetX + x*OsmTile.WIDTH)/OsmTile.WIDTH,((-iOffsetY+ y*OsmTile.HEIGHT)/OsmTile.HEIGHT), TileDB.PRIO_LOW );254 }255 }256 }257 }258 259 /**260 * Callback for the OsmMapControl. (Re-)Sets the start and end point of the selection rectangle.261 * @param aStart262 * @param aEnd263 */264 void setSelection(Point aStart, Point aEnd){265 if(aStart == null || aEnd == null)266 return;267 iSelectionRectEnd = new Point(Math.max(aEnd.x , aStart.x ) ,Math.max(aEnd.y, aStart.y ));268 iSelectionRectStart = new Point(Math.min(aEnd.x , aStart.x ) ,Math.min(aEnd.y , aStart.y ));269 270 LatLon l1 = getLatLonOfScreenPoint(aStart);271 LatLon l2 = getLatLonOfScreenPoint(aEnd);272 273 iGui.minlat = Math.min(l1.lat(), l2.lat());274 iGui.minlon = Math.min(l1.lon(), l2.lon());275 iGui.maxlat = Math.max(l1.lat(), l2.lat());276 iGui.maxlon = Math.max(l1.lon(), l2.lon());277 278 iGui.boundingBoxChanged(this);279 280 repaint();281 }282 283 /**284 * Callback for OsmMapControll. Moves the map and the selection rectangle.285 * @param x number of pixels to move along the x-axis (longitude)286 * @param y number of pixels to move along the y axis (latitude)287 */288 void moveMap(int x, int y) {289 int moveX = x;290 int moveY = y;291 292 int tempOffsetX = iOffsetX - x;293 int tempOffsetY = iOffsetY - y;294 295 296 int maxPixels = OsmMercator.getMaxPixels(iZoomlevel);297 298 if(moveX != 0){299 //deactivate dragging if the map is smaller than the DownloadDialog300 if(maxPixels < getWidth()){301 //center map horizontally302 iOffsetX = (getWidth()-maxPixels)/2;303 moveX = 0;304 }else{305 //don't allow the map to hide outside the JComponent drawing area306 if(tempOffsetX > 30){307 if(moveX < 0){308 moveX = 0;309 iOffsetX = 30;310 }311 }else if(-tempOffsetX > maxPixels + 30 - getWidth()){312 if(moveX > 0){313 moveX = 0;314 iOffsetX = -(maxPixels + 30 - getWidth());315 }316 }317 }318 }319 320 if(moveY != 0){321 //deactivate dragging if the map is smaller than the DownloadDialog322 if(maxPixels < getHeight()){323 //center map vertically324 iOffsetY = (getHeight()-maxPixels)/2;325 moveY = 0;326 }else{327 //don't allow the map to hide outside the JComponent drawing area328 if(tempOffsetY > 30){329 if(moveY < 0){330 moveY = 0;331 iOffsetY = 30;332 }333 }else if(-tempOffsetY > maxPixels + 30 - getHeight()){334 if(moveY > 0){335 moveY = 0;336 iOffsetY = -(maxPixels + 30 - getHeight());337 }338 }339 }340 }341 342 343 344 345 346 //execute the movement347 iOffsetX -= moveX;348 iOffsetY -= moveY;349 350 //also move the selection rect351 if(iSelectionRectEnd != null && iSelectionRectStart != null){352 iSelectionRectEnd.x -= moveX;353 iSelectionRectEnd.y -= moveY;354 iSelectionRectStart.x -= moveX;355 iSelectionRectStart.y -= moveY;356 }357 358 loadVisibleTiles();359 360 repaint();361 }362 363 /**364 * Zoom in one level365 * Callback for OsmMapControl. Zoom out one level366 */367 void zoomIn(Point curPos){368 369 //cache center of screen and the selection rectangle370 LatLon l = getLatLonOfScreenPoint(curPos);371 LatLon selStart = null;372 LatLon selEnd = null;373 if(iSelectionRectEnd != null && iSelectionRectStart != null){374 selStart = getLatLonOfScreenPoint(iSelectionRectStart);375 selEnd = getLatLonOfScreenPoint(iSelectionRectEnd);376 }377 378 //increment zoom level379 iZoomlevel += 1;380 if(iZoomlevel > MAX_ZOOMLEVEL){381 iZoomlevel = MAX_ZOOMLEVEL;382 return;383 }384 385 setLatLonAtPoint(l, curPos);386 387 //restore selection388 if(selStart != null && selEnd != null){389 iSelectionRectStart = getScreenPointForLatLon(selStart);390 iSelectionRectEnd = getScreenPointForLatLon(selEnd);391 }392 393 loadVisibleTiles();394 395 centerMap();396 397 repaint();398 }399 400 /**401 * Zoom out one level.402 * Callback for OsmMapControl.403 */404 void zoomOut(Point curPos){405 //cache center of screen and the selction rect406 LatLon l = getLatLonOfScreenPoint(curPos);407 LatLon selStart = null;408 LatLon selEnd = null;409 if(iSelectionRectEnd != null && iSelectionRectStart != null){410 selStart = getLatLonOfScreenPoint(iSelectionRectStart);411 selEnd = getLatLonOfScreenPoint(iSelectionRectEnd);412 }413 414 //decrement zoom level415 iZoomlevel -= 1;416 if(iZoomlevel < MIN_ZOOMLEVEL){417 iZoomlevel = MIN_ZOOMLEVEL;418 return;419 }420 421 setLatLonAtPoint(l, curPos);422 423 //restore selection424 if(selStart != null && selEnd != null){425 iSelectionRectStart = getScreenPointForLatLon(selStart);426 iSelectionRectEnd = getScreenPointForLatLon(selEnd);427 }428 429 loadVisibleTiles();430 431 centerMap();432 433 repaint();434 }435 436 /**437 * Calculates the latitude and longitude of a Point given in map space438 * @param aPoint in pixels on the map439 * @return the LatLon of the given Point440 */441 private LatLon getLatLonOfPoint(Point aPoint){442 return new LatLon(OsmMercator.YToLat(aPoint.y, iZoomlevel),OsmMercator.XToLon(aPoint.x, iZoomlevel));443 }444 445 /**446 * Returns the map coordinates for a LatLon447 * @param aLatLon448 * @return449 */450 private Point getPointForLatLon(LatLon aLatLon){451 Point p = new Point();452 p.y = OsmMercator.LatToY(aLatLon.lat(), iZoomlevel);453 p.x = OsmMercator.LonToX(aLatLon.lon(), iZoomlevel);454 return p;455 }456 457 /**458 * Calculates the latitude and longitude of a Point given in screen space (in the JComponent)459 * @param aPoint in Pixels on the screen (the JComponent)460 * @return the LatLon of the given Point461 */462 LatLon getLatLonOfScreenPoint(Point aPoint){463 Point mapCoordinates = new Point(aPoint);464 mapCoordinates.x -= iOffsetX;465 mapCoordinates.y -= iOffsetY;466 return getLatLonOfPoint(mapCoordinates);467 }468 469 /**470 * Calculates the screen coordinates of a LatLon.471 * @param aLatLon472 * @return the coordinates as Point in the JComponent473 */474 Point getScreenPointForLatLon(LatLon aLatLon){475 Point p = getPointForLatLon(aLatLon);476 p.x += iOffsetX;477 p.y += iOffsetY;478 return p;479 }480 481 482 /**483 * Centers the map on a Point484 * @param aPoint in screen coordinates (JComponent)485 */486 void centerOnScreenPoint(Point aPoint){487 moveMap(aPoint.x - getWidth()/2, aPoint.y-getHeight()/2);488 }489 490 /**491 * Centers the map on the location given by LatLon492 * @param aLatLon the location to center on493 */494 private void centerOnLatLon(LatLon aLatLon){495 setLatLonAtPoint(aLatLon, new Point(getWidth()/2,getHeight()/2));496 }497 498 /**499 * Moves the map that the specified latLon is shown at the point on screen500 * given501 * @param aLatLon a position502 * @param p a point on the screen503 */504 private void setLatLonAtPoint(LatLon aLatLon, Point p){505 int x = OsmMercator.LonToX(aLatLon.lon(), iZoomlevel);506 int y = OsmMercator.LatToY(aLatLon.lat(), iZoomlevel);507 iOffsetX = - x + p.x;508 iOffsetY = - y + p.y;509 repaint();510 }511 512 /**513 * Caclulates the visible tiles for each dimension514 */515 private void calcVisibleTiles(){516 if(iGui != null){517 iVisibleTilesX = iGui.getWidth()/256 + 2;518 iVisibleTilesY = iGui.getHeight()/256 + 2;519 }520 }521 522 /**523 * Centers the map after zooming. I.e. when the map is smaller than the524 * component it is shown in.525 */526 private void centerMap(){527 528 int maxPixels = OsmMercator.getMaxPixels(iZoomlevel);529 530 if(maxPixels < getWidth()){531 //center map horizontally532 iOffsetX = (getWidth()-maxPixels)/2;533 }534 if(maxPixels < getHeight()){535 //center map vertically536 iOffsetY = (getHeight()-maxPixels)/2;537 }538 }539 213 }
Note:
See TracChangeset
for help on using the changeset viewer.