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