| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package wmsturbochallenge;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.Graphics;
|
|---|
| 5 | import java.awt.Graphics2D;
|
|---|
| 6 | import java.awt.Point;
|
|---|
| 7 | import java.awt.image.BufferedImage;
|
|---|
| 8 |
|
|---|
| 9 | import org.openstreetmap.josm.data.ProjectionBounds;
|
|---|
| 10 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 11 | import org.openstreetmap.josm.gui.MapView;
|
|---|
| 12 | import org.openstreetmap.josm.gui.layer.MainLayerManager;
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Implements a fake MapView that we can pass to WMSLayer's .paint,
|
|---|
| 16 | * this will give us two things:
|
|---|
| 17 | * # We'll be able to tell WMSLayer.paint() what area we want it
|
|---|
| 18 | * to download (it ignores the "bounds" parameter) and override
|
|---|
| 19 | * isVisible and friends as needed, and
|
|---|
| 20 | * # We'll receive notifications from Grabber when we need to
|
|---|
| 21 | * repaint (and call WMSLayer's .paint again) because the
|
|---|
| 22 | * Grabber downloaded some or all of the tiles that we asked
|
|---|
| 23 | * WMSLayer for and WMSLayer created the Grabber passing it
|
|---|
| 24 | * our MapView. Otherwise we wouldn't be able to tell when
|
|---|
| 25 | * this happened and could only guess.
|
|---|
| 26 | * @author Andrzej Zaborowski
|
|---|
| 27 | */
|
|---|
| 28 | class FakeMapView extends MapView {
|
|---|
| 29 | public ProjectionBounds view_bounds;
|
|---|
| 30 | public MapView parent;
|
|---|
| 31 |
|
|---|
| 32 | public Graphics2D graphics;
|
|---|
| 33 | public BufferedImage ground_image;
|
|---|
| 34 | public int ground_width = -1;
|
|---|
| 35 | public int ground_height = -1;
|
|---|
| 36 | public double scale;
|
|---|
| 37 | public double max_east_west;
|
|---|
| 38 |
|
|---|
| 39 | FakeMapView(MapView parent, double scale) {
|
|---|
| 40 | // TODO: MapView constructor contains registering listeners and other code, that probably shouldn't be called in fake map view
|
|---|
| 41 | super(new MainLayerManager(), null);
|
|---|
| 42 | this.parent = parent;
|
|---|
| 43 | this.scale = scale;
|
|---|
| 44 |
|
|---|
| 45 | ProjectionBounds parent_bounds = parent.getProjectionBounds();
|
|---|
| 46 | max_east_west =
|
|---|
| 47 | parent_bounds.maxEast - parent_bounds.minEast;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | public void setProjectionBounds(ProjectionBounds bounds) {
|
|---|
| 51 | view_bounds = bounds;
|
|---|
| 52 |
|
|---|
| 53 | if (bounds.maxEast - bounds.minEast > max_east_west) {
|
|---|
| 54 | max_east_west = bounds.maxEast - bounds.minEast;
|
|---|
| 55 |
|
|---|
| 56 | /* We need to set the parent MapView's bounds (i.e.
|
|---|
| 57 | * zoom level) to the same as ours max possible
|
|---|
| 58 | * bounds to avoid WMSLayer thinking we're zoomed
|
|---|
| 59 | * out more than we are or it'll pop up an annoying
|
|---|
| 60 | * "requested area is too large" popup.
|
|---|
| 61 | */
|
|---|
| 62 | EastNorth parent_center = parent.getCenter();
|
|---|
| 63 | parent.zoomTo(new ProjectionBounds(
|
|---|
| 64 | new EastNorth(
|
|---|
| 65 | parent_center.east() -
|
|---|
| 66 | max_east_west / 2,
|
|---|
| 67 | parent_center.north()),
|
|---|
| 68 | new EastNorth(
|
|---|
| 69 | parent_center.east() +
|
|---|
| 70 | max_east_west / 2,
|
|---|
| 71 | parent_center.north())));
|
|---|
| 72 |
|
|---|
| 73 | /* Request again because NavigatableContent adds
|
|---|
| 74 | * a border just to be sure.
|
|---|
| 75 | */
|
|---|
| 76 | ProjectionBounds new_bounds =
|
|---|
| 77 | parent.getProjectionBounds();
|
|---|
| 78 | max_east_west =
|
|---|
| 79 | new_bounds.maxEast - new_bounds.minEast;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | Point vmin = getPoint(bounds.getMin());
|
|---|
| 83 | Point vmax = getPoint(bounds.getMax());
|
|---|
| 84 | int w = vmax.x + 1;
|
|---|
| 85 | int h = vmin.y + 1;
|
|---|
| 86 |
|
|---|
| 87 | if (w <= ground_width && h <= ground_height) {
|
|---|
| 88 | graphics.setClip(0, 0, w, h);
|
|---|
| 89 | return;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | if (w > ground_width)
|
|---|
| 93 | ground_width = w;
|
|---|
| 94 | if (h > ground_height)
|
|---|
| 95 | ground_height = h;
|
|---|
| 96 |
|
|---|
| 97 | ground_image = new BufferedImage(ground_width,
|
|---|
| 98 | ground_height,
|
|---|
| 99 | BufferedImage.TYPE_INT_RGB);
|
|---|
| 100 | graphics = ground_image.createGraphics();
|
|---|
| 101 | graphics.setClip(0, 0, w, h);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | @Override
|
|---|
| 105 | public ProjectionBounds getProjectionBounds() {
|
|---|
| 106 | return view_bounds;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | @Override
|
|---|
| 110 | public Point getPoint(EastNorth p) {
|
|---|
| 111 | double x = p.east() - view_bounds.minEast;
|
|---|
| 112 | double y = view_bounds.maxNorth - p.north();
|
|---|
| 113 | x /= this.scale;
|
|---|
| 114 | y /= this.scale;
|
|---|
| 115 |
|
|---|
| 116 | return new Point((int) x, (int) y);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | @Override
|
|---|
| 120 | public EastNorth getEastNorth(int x, int y) {
|
|---|
| 121 | return new EastNorth(
|
|---|
| 122 | view_bounds.minEast + x * this.scale,
|
|---|
| 123 | view_bounds.minNorth - y * this.scale);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | public boolean isVisible(int x, int y) {
|
|---|
| 127 | return true;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | @Override
|
|---|
| 131 | public Graphics getGraphics() {
|
|---|
| 132 | return graphics;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | @Override
|
|---|
| 136 | public void repaint() {
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|