Skip to content

Commit

Permalink
more refinements to processing interactive map
Browse files Browse the repository at this point in the history
  • Loading branch information
tom committed Feb 18, 2008
1 parent cf6bdc0 commit 3b0b1d3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
58 changes: 24 additions & 34 deletions sketches/modest_maps_interactive/InteractiveMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class InteractiveMap implements PConstants {

}

/** draw the map on the given PApplet */
void draw() {

// remember smooth setting so it can be reset
Expand All @@ -82,8 +83,6 @@ void draw() {
float minY = p.screenY(0,0);
float maxX = p.screenX(TILE_WIDTH, TILE_HEIGHT);
float maxY = p.screenY(TILE_WIDTH, TILE_HEIGHT);
p.println(minX + " " + minY);
p.println(maxX + " " + maxY);

// what power of 2 are we at?
// 0 when scale is around 1, 1 when scale is around 2,
Expand All @@ -95,8 +94,6 @@ void draw() {
int cols = (int)p.pow(2,zoom);
int rows = (int)p.pow(2,zoom);

// p.println(cols + " " + rows);

// find the biggest box the screen would fit in:, aligned with the map:
float screenMinX = 0;
float screenMinY = 0;
Expand Down Expand Up @@ -162,8 +159,7 @@ void draw() {
// or if we have any of the children
if (!gotParent) {
Coordinate zoomed = coord.zoomBy(1).container();
Coordinate[] kids = new Coordinate[] {
zoomed, zoomed.right(), zoomed.down(), zoomed.right().down() };
Coordinate[] kids = { zoomed, zoomed.right(), zoomed.down(), zoomed.right().down() };
for (int i = 0; i < kids.length; i++) {
zoomed = kids[i];
// make sure we still have ints:
Expand Down Expand Up @@ -201,10 +197,7 @@ void draw() {

if (images.containsKey(coord)) {
PImage tile = (PImage)images.get(coord);
p.image(tile,coord.column*256,coord.row*256,256,256);
// if (p.frameCount % 100 == 0) {
// p.println(p.screenX(coord.column*256,coord.row*256) + ", " + p.screenY(coord.column*256,coord.row*256));
// }
p.image(tile,coord.column*TILE_WIDTH,coord.row*TILE_HEIGHT,TILE_WIDTH,TILE_HEIGHT);
if (recentImages.contains(tile)) {
recentImages.remove(tile);
}
Expand All @@ -216,28 +209,27 @@ void draw() {

p.popMatrix();

// println(pending.size() + " pending...");
// println(queue.size() + " in queue, pruning...");
queue.retainAll(visibleKeys); // stop fetching things we can't see
// println(queue.size() + " in queue");
// println();
// stop fetching things we can't see:
// (visibleKeys also has the parents and children, if needed, but that shouldn't matter)
queue.retainAll(visibleKeys);

// sort what's left by distance from center:
queueSorter.setCenter(new Coordinate( (minRow + maxRow) / 2.0f, (minCol + maxCol) / 2.0f, zoom));
// println("center: " + center);
Collections.sort(queue, queueSorter);

// load up to 4 more things:
processQueue();

// clear some images away if we have too many...
if (recentImages.size() > MAX_IMAGES_TO_KEEP) {
//println(recentImages.size() + " images in memory, removing...");
recentImages.subList(0, recentImages.size()-MAX_IMAGES_TO_KEEP).clear();
//println(recentImages.size() + " images in memory");
images.values().retainAll(recentImages);
}

if (smooth) p.smooth();
// restore smoothing, if needed
if (smooth) {
p.smooth();
}

}

Expand All @@ -251,17 +243,17 @@ Location getCenter() {
}

Coordinate getCenterCoordinate() {
float row = (float)(ty*sc/-256.0);
float column = (float)(tx*sc/-256.0);
float row = (float)(ty*sc/-TILE_WIDTH);
float column = (float)(tx*sc/-TILE_HEIGHT);
float zoom = zoomForScale((float)sc);
return new Coordinate(row, column, zoom);
}

void setCenter(Coordinate center) {
//println("setting center to " + center);
sc = p.pow(2.0f, center.zoom);
tx = -256.0*center.column/sc;
ty = -256.0*center.row/sc;
tx = -TILE_WIDTH*center.column/sc;
ty = -TILE_HEIGHT*center.row/sc;
}

void setCenter(Location location) {
Expand Down Expand Up @@ -311,9 +303,6 @@ void setMapProvider(AbstractMapProvider provider) {
pending.clear();
}

// TODO: move constructor args to match:
//float width, float height, boolean draggable, AbstractMapProvider provider, Location[] extent

Point2f locationPoint(Location location) {
PMatrix m = new PMatrix();
m.translate(width/2, height/2);
Expand All @@ -323,13 +312,17 @@ Point2f locationPoint(Location location) {
Coordinate coord = provider.locationCoordinate(location).zoomTo(0);
float[] out = new float[3];
m.mult3(new float[] {
coord.column*256.0f, coord.row*256.0f, 0 }
coord.column*TILE_WIDTH, coord.row*TILE_HEIGHT, 0 }
, out);

return new Point2f(out[0], out[1]);
}

Location pointLocation(Point2f point) {
return pointLocation(point.x, point.y);
}

Location pointLocation(float x, float y) {

// TODO: create this matrix once and keep it around for drawing and projecting
PMatrix m = new PMatrix();
Expand All @@ -344,11 +337,11 @@ Location pointLocation(Point2f point) {
, tl);
float br[] = new float[3];
m.mult3(new float[] {
256,256,0 }
TILE_WIDTH, TILE_HEIGHT, 0 }
, br);

float col = (point.x - tl[0]) / (br[0] - tl[0]);
float row = (point.y - tl[1]) / (br[1] - tl[1]);
float col = (x - tl[0]) / (br[0] - tl[0]);
float row = (y - tl[1]) / (br[1] - tl[1]);
Coordinate coord = new Coordinate(row, col, 0);

return provider.coordinateLocation(coord);
Expand Down Expand Up @@ -445,13 +438,10 @@ class TileLoader implements Runnable {
this.coord = coord;
}
public void run() {
p.println("loading " + coord);
String[] urls = provider.getTileUrls(coord);
// p.println("loading " + urls[0]);
PImage img = p.loadImage(urls[0], "unknown");
PImage img = p.loadImage(urls[0], "unknown"); // use unknown to let loadImage decide
if (img != null) {
for (int i = 1; i < urls.length; i++) {
// p.println("loading " + urls[i]);
PImage img2 = p.loadImage(urls[i], "unknown");
if (img2 != null) {
img.blend(img2, 0, 0, img.width, img.height, 0, 0, img.width, img.height, BLEND);
Expand Down
16 changes: 12 additions & 4 deletions sketches/modest_maps_interactive/modest_maps_interactive.pde
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PanButton up, down, left, right;
Button[] buttons;

void setup() {
size(screen.width, screen.height);
size(screen.width/2, screen.height/2);
smooth();

addMouseWheelListener(new java.awt.event.MouseWheelListener() {
Expand All @@ -23,7 +23,6 @@ void setup() {

out = new ZoomButton(5,5,14,14,false);
in = new ZoomButton(22,5,14,14,true);

up = new PanButton(14,25,14,14,UP);
down = new PanButton(14,57,14,14,DOWN);
left = new PanButton(5,41,14,14,LEFT);
Expand All @@ -32,6 +31,8 @@ void setup() {
buttons = new Button[] {
in, out, up, down, left, right };

textFont(createFont("Helvetica", 12), 12);

}

void draw() {
Expand Down Expand Up @@ -78,8 +79,15 @@ void draw() {
}
}

// println(map.getCenter());
// println(map.pointLocation(new Point2f(width/2, height/2)));
Location location = map.pointLocation(mouseX, mouseY);

fill(0);
noStroke();
rect(5, height-5-g.textSize, textWidth(location.toString()), g.textSize+textDescent());

fill(255);
textAlign(LEFT, BOTTOM);
text(location.toString(), 5, height-5);

}

Expand Down

0 comments on commit 3b0b1d3

Please sign in to comment.