Changeset 576
- Timestamp:
- 06/22/08 14:55:06 (5 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/tom-tweenlite/lib/com/modestmaps/core/Tile.as
r528 r576 20 20 this.row = row; 21 21 this.column = column; 22 23 this.name = column+":"+row+":"+zoom;24 22 25 23 // otherwise you'll get seams between tiles :( branches/tom-tweenlite/lib/com/modestmaps/core/TileGrid.as
r574 r576 13 13 import flash.events.IOErrorEvent; 14 14 import flash.events.MouseEvent; 15 import flash.geom.ColorTransform; 15 16 import flash.geom.Matrix; 16 17 import flash.geom.Point; … … 146 147 this.draggable = draggable; 147 148 this.provider = provider; 149 150 trace("initializing tilegrid for", map.name); 148 151 149 152 // from provider: … … 237 240 } 238 241 239 p ublicfunction onRender(event:Event=null):void240 { 241 if (!dirty ) {242 protected function onRender(event:Event=null):void 243 { 244 if (!dirty || !stage) { 242 245 return; 243 246 } … … 316 319 if (!tile) { 317 320 tile = map.createTile(col, row, currentZoom); 321 tile.name = key; 318 322 coord.row = tile.row; 319 323 coord.column = tile.column; … … 331 335 visibleTiles.push(tile); 332 336 337 var tileReady:Boolean = !(tile.alpha < 1 || layersNeeded[tile]); 333 338 var foundParent:Boolean = false; 334 339 var foundChildren:int = 0; … … 345 350 346 351 // if it still doesn't have enough images yet, or it's fading in, try a double size parent instead 347 if (MAX_PARENT_SEARCH > 0 && (tile.alpha < 1 || layersNeeded[tile])&& currentZoom > minZoom) {352 if (MAX_PARENT_SEARCH > 0 && !tileReady && currentZoom > minZoom) { 348 353 pkey = parentKey(col, row, currentZoom, currentZoom-1); 349 354 if (alreadySeen[pkey] is Tile) { … … 361 366 362 367 // if it doesn't have an image yet, see if we can make it from smaller images 363 if (!foundParent && MAX_CHILD_SEARCH > 0 && (tile.alpha < 1 || layersNeeded[tile])&& currentZoom < maxZoom) {368 if (!foundParent && MAX_CHILD_SEARCH > 0 && !tileReady && currentZoom < maxZoom) { 364 369 for (var czoom:int = currentZoom+1; czoom <= Math.min(maxZoom, currentZoom+MAX_CHILD_SEARCH); czoom++) { 365 370 var ckeys:Array = childKeys(col, row, currentZoom, czoom); … … 375 380 } 376 381 } // czoom 377 378 382 } 379 383 } 380 381 var startZoomSearch:int = currentZoom - 1;384 385 var startZoomSearch:int = currentZoom - 1; 382 386 383 387 if (currentZoom > previousZoom) { … … 385 389 startZoomSearch -= 1; 386 390 } 387 391 392 var endZoomSearch:int = Math.max(minZoom, currentZoom-MAX_PARENT_SEARCH); 393 394 var stillNeedsAnImage:Boolean = !foundParent && foundChildren < 4; 388 395 // if it still doesn't have an image yet, try more parent zooms 389 if ( !foundParent && foundChildren < 4 && MAX_PARENT_SEARCH > 1 && (tile.alpha < 1 || layersNeeded[tile])&& currentZoom > minZoom) {390 for (pzoom = startZoomSearch; pzoom >= Math.max(minZoom, currentZoom-MAX_PARENT_SEARCH); pzoom--) {396 if (stillNeedsAnImage && MAX_PARENT_SEARCH > 1 && tileReady && currentZoom > minZoom) { 397 for (pzoom = startZoomSearch; pzoom >= endZoomSearch; pzoom--) { 391 398 pkey = parentKey(col, row, currentZoom, pzoom); 392 399 if (alreadySeen[pkey] is Tile) { 393 400 ptile = ensureVisible(pkey); 394 401 ptile.alpha = 1; 395 foundParent = true;402 stillNeedsAnImage = false; 396 403 break; 397 404 } 398 405 } 399 } 400 401 if ( !foundParent && foundChildren < 4) {406 } 407 408 if (stillNeedsAnImage) { 402 409 blankCount++; 403 410 //trace("sorry, no parent known for", key); … … 406 413 } 407 414 } 408 409 var i:int; 410 411 // prune children that are not currently visible 412 // loop backwards so removal doesn't change i 413 for (i = well.numChildren-1; i >= 0; i--) { 415 416 // make absolutlely sure all our newly visible tiles are cached if they're done loading 417 // TODO: this should probably happen onLoadEnd when there are no URLs left 418 for each (tile in visibleTiles) { 419 if (recentlySeen.indexOf(tile) < 0 && !layersNeeded[tile]) { 420 recentlySeen.push(tile); 421 } 422 } 423 424 // prune tiles from the well if they shouldn't be there (not currently in visibleTiles) 425 // (loop backwards so removal doesn't change i) 426 for (var i:int = well.numChildren-1; i >= 0; i--) { 414 427 tile = well.getChildAt(i) as Tile; 415 428 if (visibleTiles.indexOf(tile) < 0) { 416 429 well.removeChild(tile); 417 430 tile.alpha = 0.0; 431 // if it's not in recentlySeen, that's because it wasn't done loading 432 // so we can give it back to the map now 433 if (recentlySeen.indexOf(tile) < 0) { 434 map.destroyTile(tile); 435 } 436 // otherwise, we'll keep it around for a while in case we want it 437 // back on the stage 418 438 } 419 439 } … … 422 442 // this means current is on top, +1 and -1 are next, then +2 and -2, etc. 423 443 visibleTiles.sort(distanceFromCurrentZoomCompare, Array.DESCENDING); 424 425 444 426 445 // for fixing positions when we're between zoom levels: … … 442 461 443 462 // if we're done loading this one, move it to the end of recently seen: 444 if ( layersNeeded[tile] == null) {463 if (!layersNeeded[tile]) { 445 464 var ri:int = recentlySeen.indexOf(tile.name); 446 465 if (ri >= 0) { 447 466 recentlySeen.splice(ri, 1); 448 467 } 468 //else { 469 // trace('adding something to recently seen') 470 //} 449 471 recentlySeen.push(tile.name); 450 472 } … … 454 476 // let's make sure we keep them around: 455 477 var maxRecentlySeen:int = Math.max(visibleTiles.length,MAX_TILES_TO_KEEP); 478 /* trace(); 479 trace('visibleTiles', visibleTiles.length); 480 trace('maxRecentlySeen', maxRecentlySeen); 481 trace('recentlySeen', recentlySeen.length); */ 456 482 457 483 // prune cache of already seen tiles if it's getting too big: … … 498 524 499 525 // process the queue 500 var coord:Coordinate = new Coordinate(0,0,0);501 526 while (openRequests.length < MAX_OPEN_REQUESTS && queue.length > 0) { 502 527 var tile:Tile = queue.shift() as Tile; 503 coord.row = tile.row;504 coord.column = tile.column;505 coord.zoom = tile.zoom;506 528 // if it's still on the stage: 507 529 if (tile.parent) { 508 // TODO: add urls to Tile? 509 var urls:Array = layersNeeded[tile] as Array; 510 if (urls && urls.length > 0) { 511 var url:* = urls.shift(); 512 var tileLoader:Loader = new Loader(); 513 tileLoader.name = tile.name; 514 try { 515 tileLoader.load((url is URLRequest) ? url : new URLRequest(url)); 516 tileLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadEnd, false, 0, true); 517 tileLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError, false, 0, true); 518 openRequests.push(tileLoader); 519 } 520 catch(error:Error) { 521 tile.paintError(); 522 } 523 } 524 } 530 loadNextURLForTile(tile); 531 } 532 // TODO: make sure that something else took care of calling map.destroyTile? 533 // TODO: should that something also pull the tile out of the queue? 525 534 } 526 535 … … 535 544 previousOpenRequests = openRequests.length; 536 545 546 } 547 548 private function loadNextURLForTile(tile:Tile):void 549 { 550 // TODO: add urls to Tile? 551 var urls:Array = layersNeeded[tile] as Array; 552 if (urls && urls.length > 0) { 553 var url:* = urls.shift(); 554 var tileLoader:Loader = new Loader(); 555 tileLoader.name = tile.name; 556 try { 557 tileLoader.load((url is URLRequest) ? url : new URLRequest(url)); 558 tileLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadEnd, false, 0, true); 559 tileLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError, false, 0, true); 560 openRequests.push(tileLoader); 561 } 562 catch(error:Error) { 563 tile.paintError(); 564 } 565 } 566 else if (urls && urls.length == 0) { 567 if (tile.zoom == currentZoom) { 568 tile.addedLoader(); 569 } 570 else { 571 tile.alpha = 1; 572 } 573 alreadySeen[tile.name] = tile; 574 575 delete layersNeeded[tile]; 576 } 537 577 } 538 578 … … 606 646 if (tile) { 607 647 tile.addChild(loader); 608 609 var urls:Array = layersNeeded[tile] as Array; 610 if (urls && urls.length) { 611 var url:String = urls.shift() as String; 612 var tileLoader:Loader = new Loader(); 613 tileLoader.name = tile.name; 614 tileLoader.load(new URLRequest(url)); 615 tileLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadEnd, false, 0, true); 616 tileLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError, false, 0, true); 617 openRequests.push(tileLoader); 618 } 619 else { 620 if (urls) { 621 delete layersNeeded[tile]; 622 } 623 if (tile.zoom == currentZoom) { 624 tile.addedLoader(); 625 } 626 else { 627 tile.alpha = 1; 628 } 629 alreadySeen[tile.name] = tile; 630 } 631 648 loadNextURLForTile(tile); 632 649 } 633 650 else {
