| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.Container;
|
|---|
| 5 | import java.awt.Point;
|
|---|
| 6 | import java.awt.geom.AffineTransform;
|
|---|
| 7 | import java.awt.geom.Point2D;
|
|---|
| 8 | import java.awt.geom.Point2D.Double;
|
|---|
| 9 |
|
|---|
| 10 | import javax.swing.JComponent;
|
|---|
| 11 |
|
|---|
| 12 | import org.openstreetmap.josm.Main;
|
|---|
| 13 | import org.openstreetmap.josm.data.Bounds;
|
|---|
| 14 | import org.openstreetmap.josm.data.ProjectionBounds;
|
|---|
| 15 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 16 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 17 | import org.openstreetmap.josm.data.projection.Projection;
|
|---|
| 18 | import org.openstreetmap.josm.gui.download.DownloadDialog;
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * This class represents a state of the {@link MapView}.
|
|---|
| 22 | * @author Michael Zangl
|
|---|
| 23 | * @since 10343
|
|---|
| 24 | */
|
|---|
| 25 | public final class MapViewState {
|
|---|
| 26 |
|
|---|
| 27 | private final Projection projection;
|
|---|
| 28 |
|
|---|
| 29 | private final int viewWidth;
|
|---|
| 30 | private final int viewHeight;
|
|---|
| 31 |
|
|---|
| 32 | private final double scale;
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Top left {@link EastNorth} coordinate of the view.
|
|---|
| 36 | */
|
|---|
| 37 | private final EastNorth topLeft;
|
|---|
| 38 |
|
|---|
| 39 | private final Point topLeftOnScreen;
|
|---|
| 40 | private final Point topLeftInWindow;
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Create a new {@link MapViewState}
|
|---|
| 44 | * @param projection The projection to use.
|
|---|
| 45 | * @param viewWidth The view width
|
|---|
| 46 | * @param viewHeight The view height
|
|---|
| 47 | * @param scale The scale to use
|
|---|
| 48 | * @param topLeft The top left corner in east/north space.
|
|---|
| 49 | */
|
|---|
| 50 | private MapViewState(Projection projection, int viewWidth, int viewHeight, double scale, EastNorth topLeft) {
|
|---|
| 51 | this.projection = projection;
|
|---|
| 52 | this.scale = scale;
|
|---|
| 53 | this.topLeft = topLeft;
|
|---|
| 54 |
|
|---|
| 55 | this.viewWidth = viewWidth;
|
|---|
| 56 | this.viewHeight = viewHeight;
|
|---|
| 57 | topLeftInWindow = new Point(0, 0);
|
|---|
| 58 | topLeftOnScreen = new Point(0, 0);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | private MapViewState(EastNorth topLeft, MapViewState mapViewState) {
|
|---|
| 62 | this.projection = mapViewState.projection;
|
|---|
| 63 | this.scale = mapViewState.scale;
|
|---|
| 64 | this.topLeft = topLeft;
|
|---|
| 65 |
|
|---|
| 66 | viewWidth = mapViewState.viewWidth;
|
|---|
| 67 | viewHeight = mapViewState.viewHeight;
|
|---|
| 68 | topLeftInWindow = mapViewState.topLeftInWindow;
|
|---|
| 69 | topLeftOnScreen = mapViewState.topLeftOnScreen;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | private MapViewState(double scale, MapViewState mapViewState) {
|
|---|
| 73 | this.projection = mapViewState.projection;
|
|---|
| 74 | this.scale = scale;
|
|---|
| 75 | this.topLeft = mapViewState.topLeft;
|
|---|
| 76 |
|
|---|
| 77 | viewWidth = mapViewState.viewWidth;
|
|---|
| 78 | viewHeight = mapViewState.viewHeight;
|
|---|
| 79 | topLeftInWindow = mapViewState.topLeftInWindow;
|
|---|
| 80 | topLeftOnScreen = mapViewState.topLeftOnScreen;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | private MapViewState(JComponent position, MapViewState mapViewState) {
|
|---|
| 84 | this.projection = mapViewState.projection;
|
|---|
| 85 | this.scale = mapViewState.scale;
|
|---|
| 86 | this.topLeft = mapViewState.topLeft;
|
|---|
| 87 |
|
|---|
| 88 | viewWidth = position.getWidth();
|
|---|
| 89 | viewHeight = position.getHeight();
|
|---|
| 90 | topLeftInWindow = new Point();
|
|---|
| 91 | // better than using swing utils, since this allows us to use the mehtod if no screen is present.
|
|---|
| 92 | Container component = position;
|
|---|
| 93 | while (component != null) {
|
|---|
| 94 | topLeftInWindow.x += component.getX();
|
|---|
| 95 | topLeftInWindow.y += component.getY();
|
|---|
| 96 | component = component.getParent();
|
|---|
| 97 | }
|
|---|
| 98 | topLeftOnScreen = position.getLocationOnScreen();
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * The scale in east/north units per pixel.
|
|---|
| 103 | * @return The scale.
|
|---|
| 104 | */
|
|---|
| 105 | public double getScale() {
|
|---|
| 106 | return scale;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * Gets the MapViewPoint representation for a position in view coordinates.
|
|---|
| 111 | * @param x The x coordinate inside the view.
|
|---|
| 112 | * @param y The y coordinate inside the view.
|
|---|
| 113 | * @return The MapViewPoint.
|
|---|
| 114 | */
|
|---|
| 115 | public MapViewPoint getForView(double x, double y) {
|
|---|
| 116 | return new MapViewViewPoint(x, y);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /**
|
|---|
| 120 | * Gets the {@link MapViewPoint} for the given {@link EastNorth} coordinate.
|
|---|
| 121 | * @param eastNorth the position.
|
|---|
| 122 | * @return The point for that position.
|
|---|
| 123 | */
|
|---|
| 124 | public MapViewPoint getPointFor(EastNorth eastNorth) {
|
|---|
| 125 | return new MapViewEastNorthPoint(eastNorth);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Gets a rectangle representing the whole view area.
|
|---|
| 130 | * @return The rectangle.
|
|---|
| 131 | */
|
|---|
| 132 | public MapViewRectangle getViewArea() {
|
|---|
| 133 | return getForView(0, 0).rectTo(getForView(viewWidth, viewHeight));
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Gets the center of the view.
|
|---|
| 138 | * @return The center position.
|
|---|
| 139 | */
|
|---|
| 140 | public MapViewPoint getCenter() {
|
|---|
| 141 | return getForView(viewWidth / 2.0, viewHeight / 2.0);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /**
|
|---|
| 145 | * Gets the width of the view on the Screen;
|
|---|
| 146 | * @return The width of the view component in screen pixel.
|
|---|
| 147 | */
|
|---|
| 148 | public double getViewWidth() {
|
|---|
| 149 | return viewWidth;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /**
|
|---|
| 153 | * Gets the height of the view on the Screen;
|
|---|
| 154 | * @return The height of the view component in screen pixel.
|
|---|
| 155 | */
|
|---|
| 156 | public double getViewHeight() {
|
|---|
| 157 | return viewHeight;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /**
|
|---|
| 161 | * Gets the current projection used for the MapView.
|
|---|
| 162 | * @return The projection.
|
|---|
| 163 | */
|
|---|
| 164 | public Projection getProjection() {
|
|---|
| 165 | return projection;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| 169 | * Creates an affine transform that is used to convert the east/north coordinates to view coordinates.
|
|---|
| 170 | * @return The affine transform. It should not be changed.
|
|---|
| 171 | * @since 10375
|
|---|
| 172 | */
|
|---|
| 173 | public AffineTransform getAffineTransform() {
|
|---|
| 174 | return new AffineTransform(1.0 / scale, 0.0, 0.0, -1.0 / scale, -topLeft.east() / scale,
|
|---|
| 175 | topLeft.north() / scale);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | /**
|
|---|
| 179 | * Creates a new state that is the same as the current state except for that it is using a new center.
|
|---|
| 180 | * @param newCenter The new center coordinate.
|
|---|
| 181 | * @return The new state.
|
|---|
| 182 | * @since 10375
|
|---|
| 183 | */
|
|---|
| 184 | public MapViewState usingCenter(EastNorth newCenter) {
|
|---|
| 185 | return movedTo(getCenter(), newCenter);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | /**
|
|---|
| 189 | * @param mapViewPoint The reference point.
|
|---|
| 190 | * @param newEastNorthThere The east/north coordinate that should be there.
|
|---|
| 191 | * @return The new state.
|
|---|
| 192 | * @since 10375
|
|---|
| 193 | */
|
|---|
| 194 | public MapViewState movedTo(MapViewPoint mapViewPoint, EastNorth newEastNorthThere) {
|
|---|
| 195 | EastNorth delta = newEastNorthThere.subtract(mapViewPoint.getEastNorth());
|
|---|
| 196 | if (delta.distanceSq(0, 0) < .000001) {
|
|---|
| 197 | return this;
|
|---|
| 198 | } else {
|
|---|
| 199 | return new MapViewState(topLeft.add(delta), this);
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /**
|
|---|
| 204 | * Creates a new state that is the same as the current state except for that it is using a new scale.
|
|---|
| 205 | * @param newScale The new scale to use.
|
|---|
| 206 | * @return The new state.
|
|---|
| 207 | * @since 10375
|
|---|
| 208 | */
|
|---|
| 209 | public MapViewState usingScale(double newScale) {
|
|---|
| 210 | return new MapViewState(newScale, this);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /**
|
|---|
| 214 | * Creates a new state that is the same as the current state except for that it is using the location of the given component.
|
|---|
| 215 | * <p>
|
|---|
| 216 | * The view is moved so that the center is the same as the old center.
|
|---|
| 217 | * @param positon The new location to use.
|
|---|
| 218 | * @return The new state.
|
|---|
| 219 | * @since 10375
|
|---|
| 220 | */
|
|---|
| 221 | public MapViewState usingLocation(JComponent positon) {
|
|---|
| 222 | EastNorth center = this.getCenter().getEastNorth();
|
|---|
| 223 | return new MapViewState(positon, this).usingCenter(center);
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /**
|
|---|
| 227 | * Create the default {@link MapViewState} object for the given map view. The screen position won't be set so that this method can be used
|
|---|
| 228 | * before the view was added to the hirarchy.
|
|---|
| 229 | * @param width The view width
|
|---|
| 230 | * @param height The view height
|
|---|
| 231 | * @return The state
|
|---|
| 232 | * @since 10375
|
|---|
| 233 | */
|
|---|
| 234 | public static MapViewState createDefaultState(int width, int height) {
|
|---|
| 235 | Projection projection = Main.getProjection();
|
|---|
| 236 | double scale = projection.getDefaultZoomInPPD();
|
|---|
| 237 | MapViewState state = new MapViewState(projection, width, height, scale, new EastNorth(0, 0));
|
|---|
| 238 | EastNorth center = calculateDefaultCenter();
|
|---|
| 239 | return state.movedTo(state.getCenter(), center);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | private static EastNorth calculateDefaultCenter() {
|
|---|
| 243 | Bounds b = DownloadDialog.getSavedDownloadBounds();
|
|---|
| 244 | if (b == null) {
|
|---|
| 245 | b = Main.getProjection().getWorldBoundsLatLon();
|
|---|
| 246 | }
|
|---|
| 247 | return Main.getProjection().latlon2eastNorth(b.getCenter());
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | /**
|
|---|
| 251 | * A class representing a point in the map view. It allows to convert between the different coordinate systems.
|
|---|
| 252 | * @author Michael Zangl
|
|---|
| 253 | */
|
|---|
| 254 | public abstract class MapViewPoint {
|
|---|
| 255 |
|
|---|
| 256 | /**
|
|---|
| 257 | * Get this point in view coordinates.
|
|---|
| 258 | * @return The point in view coordinates.
|
|---|
| 259 | */
|
|---|
| 260 | public Point2D getInView() {
|
|---|
| 261 | return new Point2D.Double(getInViewX(), getInViewY());
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | protected abstract double getInViewX();
|
|---|
| 265 |
|
|---|
| 266 | protected abstract double getInViewY();
|
|---|
| 267 |
|
|---|
| 268 | /**
|
|---|
| 269 | * Convert this point to window coordinates.
|
|---|
| 270 | * @return The point in window coordinates.
|
|---|
| 271 | */
|
|---|
| 272 | public Point2D getInWindow() {
|
|---|
| 273 | return getUsingCorner(topLeftInWindow);
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | /**
|
|---|
| 277 | * Convert this point to screen coordinates.
|
|---|
| 278 | * @return The point in screen coordinates.
|
|---|
| 279 | */
|
|---|
| 280 | public Point2D getOnScreen() {
|
|---|
| 281 | return getUsingCorner(topLeftOnScreen);
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | private Double getUsingCorner(Point corner) {
|
|---|
| 285 | return new Point2D.Double(corner.getX() + getInViewX(), corner.getY() + getInViewY());
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | /**
|
|---|
| 289 | * Gets the {@link EastNorth} coordinate of this point.
|
|---|
| 290 | * @return The east/north coordinate.
|
|---|
| 291 | */
|
|---|
| 292 | public EastNorth getEastNorth() {
|
|---|
| 293 | return new EastNorth(topLeft.east() + getInViewX() * scale, topLeft.north() - getInViewY() * scale);
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | /**
|
|---|
| 297 | * Create a rectangle from this to the other point.
|
|---|
| 298 | * @param other The other point. Needs to be of the same {@link MapViewState}
|
|---|
| 299 | * @return A rectangle.
|
|---|
| 300 | */
|
|---|
| 301 | public MapViewRectangle rectTo(MapViewPoint other) {
|
|---|
| 302 | return new MapViewRectangle(this, other);
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | /**
|
|---|
| 306 | * Gets the current position in LatLon coordinates according to the current projection.
|
|---|
| 307 | * @return The positon as LatLon.
|
|---|
| 308 | */
|
|---|
| 309 | public LatLon getLatLon() {
|
|---|
| 310 | return projection.eastNorth2latlon(getEastNorth());
|
|---|
| 311 | }
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | private class MapViewViewPoint extends MapViewPoint {
|
|---|
| 315 | private final double x;
|
|---|
| 316 | private final double y;
|
|---|
| 317 |
|
|---|
| 318 | MapViewViewPoint(double x, double y) {
|
|---|
| 319 | this.x = x;
|
|---|
| 320 | this.y = y;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | @Override
|
|---|
| 324 | protected double getInViewX() {
|
|---|
| 325 | return x;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | @Override
|
|---|
| 329 | protected double getInViewY() {
|
|---|
| 330 | return y;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | @Override
|
|---|
| 334 | public String toString() {
|
|---|
| 335 | return "MapViewViewPoint [x=" + x + ", y=" + y + "]";
|
|---|
| 336 | }
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | private class MapViewEastNorthPoint extends MapViewPoint {
|
|---|
| 340 |
|
|---|
| 341 | private final EastNorth eastNorth;
|
|---|
| 342 |
|
|---|
| 343 | MapViewEastNorthPoint(EastNorth eastNorth) {
|
|---|
| 344 | this.eastNorth = eastNorth;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | @Override
|
|---|
| 348 | protected double getInViewX() {
|
|---|
| 349 | return (eastNorth.east() - topLeft.east()) / scale;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | @Override
|
|---|
| 353 | protected double getInViewY() {
|
|---|
| 354 | return (topLeft.north() - eastNorth.north()) / scale;
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | @Override
|
|---|
| 358 | public EastNorth getEastNorth() {
|
|---|
| 359 | return eastNorth;
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | @Override
|
|---|
| 363 | public String toString() {
|
|---|
| 364 | return "MapViewEastNorthPoint [eastNorth=" + eastNorth + "]";
|
|---|
| 365 | }
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | /**
|
|---|
| 369 | * A rectangle on the MapView. It is rectangular in screen / EastNorth space.
|
|---|
| 370 | * @author Michael Zangl
|
|---|
| 371 | */
|
|---|
| 372 | public class MapViewRectangle {
|
|---|
| 373 | private final MapViewPoint p1;
|
|---|
| 374 | private final MapViewPoint p2;
|
|---|
| 375 |
|
|---|
| 376 | /**
|
|---|
| 377 | * Create a new MapViewRectangle
|
|---|
| 378 | * @param p1 The first point to use
|
|---|
| 379 | * @param p2 The second point to use.
|
|---|
| 380 | */
|
|---|
| 381 | MapViewRectangle(MapViewPoint p1, MapViewPoint p2) {
|
|---|
| 382 | this.p1 = p1;
|
|---|
| 383 | this.p2 = p2;
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | /**
|
|---|
| 387 | * Gets the projection bounds for this rectangle.
|
|---|
| 388 | * @return The projection bounds.
|
|---|
| 389 | */
|
|---|
| 390 | public ProjectionBounds getProjectionBounds() {
|
|---|
| 391 | ProjectionBounds b = new ProjectionBounds(p1.getEastNorth());
|
|---|
| 392 | b.extend(p2.getEastNorth());
|
|---|
| 393 | return b;
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | /**
|
|---|
| 397 | * Gets a rough estimate of the bounds by assuming lat/lon are parallel to x/y.
|
|---|
| 398 | * @return The bounds computed by converting the corners of this rectangle.
|
|---|
| 399 | */
|
|---|
| 400 | public Bounds getCornerBounds() {
|
|---|
| 401 | Bounds b = new Bounds(p1.getLatLon());
|
|---|
| 402 | b.extend(p2.getLatLon());
|
|---|
| 403 | return b;
|
|---|
| 404 | }
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | }
|
|---|