Changeset 496

Show
Ignore:
Timestamp:
02/18/08 15:52:55 (9 months ago)
Author:
tom
Message:

more refinements to processing interactive map

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/processing/sketches/modest_maps_interactive/InteractiveMap.java

    r495 r496  
    6363  } 
    6464 
     65  /** draw the map on the given PApplet */ 
    6566  void draw() { 
    6667 
     
    8384    float maxX = p.screenX(TILE_WIDTH, TILE_HEIGHT); 
    8485    float maxY = p.screenY(TILE_WIDTH, TILE_HEIGHT); 
    85     p.println(minX + " " + minY); 
    86     p.println(maxX + " " + maxY); 
    8786 
    8887    // what power of 2 are we at? 
     
    9594    int cols = (int)p.pow(2,zoom); 
    9695    int rows = (int)p.pow(2,zoom); 
    97  
    98 //    p.println(cols + " " + rows); 
    9996 
    10097    // find the biggest box the screen would fit in:, aligned with the map: 
     
    163160          if (!gotParent) { 
    164161            Coordinate zoomed = coord.zoomBy(1).container(); 
    165             Coordinate[] kids = new Coordinate[] {  
    166               zoomed, zoomed.right(), zoomed.down(), zoomed.right().down()                         };  
     162            Coordinate[] kids = { zoomed, zoomed.right(), zoomed.down(), zoomed.right().down() };  
    167163            for (int i = 0; i < kids.length; i++) { 
    168164              zoomed = kids[i]; 
     
    202198        if (images.containsKey(coord)) { 
    203199          PImage tile = (PImage)images.get(coord); 
    204           p.image(tile,coord.column*256,coord.row*256,256,256); 
    205 //          if (p.frameCount % 100 == 0) { 
    206 //            p.println(p.screenX(coord.column*256,coord.row*256) + ", " + p.screenY(coord.column*256,coord.row*256)); 
    207 //          } 
     200          p.image(tile,coord.column*TILE_WIDTH,coord.row*TILE_HEIGHT,TILE_WIDTH,TILE_HEIGHT); 
    208201          if (recentImages.contains(tile)) { 
    209202            recentImages.remove(tile); 
     
    217210    p.popMatrix(); 
    218211 
    219     //  println(pending.size() + " pending..."); 
    220     //  println(queue.size() + " in queue, pruning..."); 
    221     queue.retainAll(visibleKeys); // stop fetching things we can't see 
    222     //  println(queue.size() + " in queue"); 
    223     //  println(); 
     212    // stop fetching things we can't see: 
     213    // (visibleKeys also has the parents and children, if needed, but that shouldn't matter) 
     214    queue.retainAll(visibleKeys); 
    224215 
    225216    // sort what's left by distance from center: 
    226217    queueSorter.setCenter(new Coordinate( (minRow + maxRow) / 2.0f, (minCol + maxCol) / 2.0f, zoom)); 
    227     //    println("center: " + center); 
    228218    Collections.sort(queue, queueSorter); 
    229219 
     
    231221    processQueue(); 
    232222 
     223    // clear some images away if we have too many... 
    233224    if (recentImages.size() > MAX_IMAGES_TO_KEEP) { 
    234       //println(recentImages.size() + " images in memory, removing..."); 
    235225      recentImages.subList(0, recentImages.size()-MAX_IMAGES_TO_KEEP).clear(); 
    236       //println(recentImages.size() + " images in memory"); 
    237226      images.values().retainAll(recentImages); 
    238227    } 
    239228 
    240     if (smooth) p.smooth(); 
     229    // restore smoothing, if needed 
     230    if (smooth) { 
     231      p.smooth(); 
     232    } 
    241233 
    242234  }  
     
    252244 
    253245  Coordinate getCenterCoordinate() { 
    254     float row = (float)(ty*sc/-256.0); 
    255     float column = (float)(tx*sc/-256.0); 
     246    float row = (float)(ty*sc/-TILE_WIDTH); 
     247    float column = (float)(tx*sc/-TILE_HEIGHT); 
    256248    float zoom = zoomForScale((float)sc); 
    257249    return new Coordinate(row, column, zoom);  
     
    261253    //println("setting center to " + center); 
    262254    sc = p.pow(2.0f, center.zoom); 
    263     tx = -256.0*center.column/sc; 
    264     ty = -256.0*center.row/sc; 
     255    tx = -TILE_WIDTH*center.column/sc; 
     256    ty = -TILE_HEIGHT*center.row/sc; 
    265257  } 
    266258 
     
    312304  } 
    313305 
    314   // TODO: move constructor args to match: 
    315   //float width, float height, boolean draggable, AbstractMapProvider provider, Location[] extent 
    316  
    317306  Point2f locationPoint(Location location) { 
    318307    PMatrix m = new PMatrix(); 
     
    324313    float[] out = new float[3]; 
    325314    m.mult3(new float[] { 
    326       coord.column*256.0f, coord.row*256.0f, 0    } 
     315      coord.column*TILE_WIDTH, coord.row*TILE_HEIGHT, 0    } 
    327316    , out); 
    328317 
     
    331320 
    332321  Location pointLocation(Point2f point) { 
     322    return pointLocation(point.x, point.y);  
     323  } 
     324 
     325  Location pointLocation(float x, float y) { 
    333326 
    334327    // TODO: create this matrix once and keep it around for drawing and projecting 
     
    345338    float br[] = new float[3];     
    346339    m.mult3(new float[] {  
    347       256,256,0     } 
     340      TILE_WIDTH, TILE_HEIGHT, 0     } 
    348341    , br); 
    349342 
    350     float col = (point.x - tl[0]) / (br[0] - tl[0]); 
    351     float row = (point.y - tl[1]) / (br[1] - tl[1]); 
     343    float col = (x - tl[0]) / (br[0] - tl[0]); 
     344    float row = (y - tl[1]) / (br[1] - tl[1]); 
    352345    Coordinate coord = new Coordinate(row, col, 0); 
    353346 
     
    446439    } 
    447440    public void run() { 
    448       p.println("loading " + coord); 
    449441      String[] urls = provider.getTileUrls(coord); 
    450       //      p.println("loading " + urls[0]); 
    451       PImage img = p.loadImage(urls[0], "unknown"); 
     442      PImage img = p.loadImage(urls[0], "unknown"); // use unknown to let loadImage decide 
    452443      if (img != null) { 
    453444        for (int i = 1; i < urls.length; i++) { 
    454           //          p.println("loading " + urls[i]); 
    455445          PImage img2 = p.loadImage(urls[i], "unknown"); 
    456446          if (img2 != null) { 
  • trunk/processing/sketches/modest_maps_interactive/modest_maps_interactive.pde

    r495 r496  
    77 
    88void setup() { 
    9   size(screen.width, screen.height); 
     9  size(screen.width/2, screen.height/2); 
    1010  smooth(); 
    1111 
     
    2424  out = new ZoomButton(5,5,14,14,false); 
    2525  in = new ZoomButton(22,5,14,14,true); 
    26  
    2726  up = new PanButton(14,25,14,14,UP); 
    2827  down = new PanButton(14,57,14,14,DOWN); 
     
    3231  buttons = new Button[] {  
    3332    in, out, up, down, left, right     }; 
     33 
     34  textFont(createFont("Helvetica", 12), 12); 
    3435 
    3536} 
     
    7980  } 
    8081 
    81   //  println(map.getCenter());   
    82   //  println(map.pointLocation(new Point2f(width/2, height/2))); 
     82  Location location = map.pointLocation(mouseX, mouseY); 
     83   
     84  fill(0); 
     85  noStroke(); 
     86  rect(5, height-5-g.textSize, textWidth(location.toString()), g.textSize+textDescent()); 
     87   
     88  fill(255); 
     89  textAlign(LEFT, BOTTOM); 
     90  text(location.toString(), 5, height-5); 
    8391 
    8492}