Changeset 580

Show
Ignore:
Timestamp:
06/26/08 16:37:47 (5 months ago)
Author:
tom
Message:

being so bold as to check in tilegrid bounds enforcing for the tweening branch, but optionally for now, default off

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/tom-tweenlite/lib/com/modestmaps/core/TileGrid.as

    r578 r580  
    1313        import flash.events.IOErrorEvent; 
    1414        import flash.events.MouseEvent; 
    15         import flash.geom.ColorTransform; 
    1615        import flash.geom.Matrix; 
    1716        import flash.geom.Point; 
     
    4140 
    4241                // pan, zoom and rotate 
    43                 protected var tx:Number = -TILE_WIDTH/2, ty:Number = -TILE_HEIGHT/2; 
    44                 protected var sc:Number = 1; 
     42                private var _panX:Number = -TILE_WIDTH/2; 
     43                private var _panY:Number = -TILE_HEIGHT/2; 
     44                private var _scale:Number = 1; 
    4545                 
    4646                protected var worldMatrix:Matrix; 
     
    127127                // 0 or 1, really: 2 will load *lots* of extra tiles 
    128128                public static var TILE_BUFFER:int = 0; 
     129 
     130                // set this to true to enable enforcing of map bounds from the map provider's limits 
     131                public static var ENFORCE_BOUNDS:Boolean = false; 
    129132                 
    130133                public var mapWidth:Number; 
     
    174177 
    175178                        worldMatrix = new Matrix(); 
    176                         worldMatrix.translate(tx,tx); 
    177                         worldMatrix.scale(sc,sc); 
     179                        worldMatrix.translate(_panX,_panX); 
     180                        worldMatrix.scale(_scale,_scale); 
    178181                        worldMatrix.translate(mapWidth/2, mapHeight/2); 
    179182                         
     
    220223                                } 
    221224                                 
    222                                 debugField.text = "tx: " + tx.toFixed(3) + " ty: " + ty.toFixed(3) + " sc: " + sc.toFixed(4) 
     225                                debugField.text = "tx: " + _panX.toFixed(3) + " ty: " + _panY.toFixed(3) + " sc: " + _scale.toFixed(4) 
    223226                                                + "\nfps: " + fps.toFixed(0) 
    224227                                                + "\ncurrent child count: " + well.numChildren 
     
    693696                { 
    694697                        var mousePoint:Point = globalToLocal(new Point(event.stageX, event.stageY)); 
    695                         tx += (mousePoint.x - pmouse.x) / sc
    696                         ty += (mousePoint.y - pmouse.y) / sc
     698                        _panX += (mousePoint.x - pmouse.x) / _scale
     699                        _panY += (mousePoint.y - pmouse.y) / _scale
    697700                        pmouse = mousePoint; 
    698701                        calculateMatrix(); 
    699                         var origin:Point = startPan || new Point(tx,ty); 
    700                         dispatchEvent(new MapEvent(MapEvent.PANNED, new Point(-(origin.x-tx)*sc, -(origin.y-ty)*sc))); 
     702                        var origin:Point = startPan || new Point(_panX,_panY); 
     703                        dispatchEvent(new MapEvent(MapEvent.PANNED, new Point(-(origin.x-_panX)*_scale, -(origin.y-_panY)*_scale))); 
    701704                        event.updateAfterEvent(); 
    702705                }        
     
    706709                        // first, simply do what was asked: 
    707710                        worldMatrix.identity(); 
    708                         worldMatrix.translate(tx,ty); 
    709                         worldMatrix.scale(sc,sc); 
     711                        worldMatrix.translate(_panX,_panY); 
     712                        worldMatrix.scale(_scale,_scale); 
    710713                        worldMatrix.translate(mapWidth/2, mapHeight/2); 
    711714                         
    712                         // TODO: later, is this a good place to enforce map bounds? 
    713                                                  
    714                         // last, nullify things that will be recalculated as needed 
     715                        // nullify things that will be recalculated as needed 
    715716                        _invertedMatrix = null; 
    716717                        _topLeftCoordinate = null; 
    717718                        _bottomRightCoordinate = null; 
     719 
     720                        // enforce map bounds? 
     721                        if (enforceBounds()) { 
     722                                // try again! 
     723                                worldMatrix.identity(); 
     724                                worldMatrix.translate(_panX,_panY); 
     725                                worldMatrix.scale(_scale,_scale); 
     726                                worldMatrix.translate(mapWidth/2, mapHeight/2);                          
     727 
     728                                // nullify things that will be recalculated as needed 
     729                                _invertedMatrix = null; 
     730                                _topLeftCoordinate = null; 
     731                                _bottomRightCoordinate = null; 
     732                        } 
    718733                         
    719734                        // and request a redraw: 
     
    741756                        if (!_topLeftCoordinate) { 
    742757                                var tl:Point = invertedMatrix.transformPoint(new Point()); 
    743                                 tl.x *= sc/TILE_WIDTH; 
    744                                 tl.y *= sc/TILE_HEIGHT; 
     758                                tl.x *= _scale/TILE_WIDTH; 
     759                                tl.y *= _scale/TILE_HEIGHT; 
    745760                                _topLeftCoordinate = new Coordinate(tl.y, tl.x, zoomLevel);                      
    746761                        } 
     
    756771                        if (!_bottomRightCoordinate) { 
    757772                                var br:Point = invertedMatrix.transformPoint(new Point(mapWidth, mapHeight)); 
    758                                 br.x *= sc/TILE_WIDTH; 
    759                                 br.y *= sc/TILE_HEIGHT; 
     773                                br.x *= _scale/TILE_WIDTH; 
     774                                br.y *= _scale/TILE_HEIGHT; 
    760775                                _bottomRightCoordinate = new Coordinate(br.y, br.x, zoomLevel);                  
    761776                        } 
     
    772787                { 
    773788                        var c:Point = invertedMatrix.transformPoint(new Point(mapWidth/2, mapHeight/2)); 
    774                         c.x *= sc/TILE_WIDTH; 
    775                         c.y *= sc/TILE_HEIGHT; 
     789                        c.x *= _scale/TILE_WIDTH; 
     790                        c.y *= _scale/TILE_HEIGHT; 
    776791                        return new Coordinate(c.y, c.x, zoomLevel);                      
    777792                } 
     
    805820                         
    806821                        var p:Point = invertedMatrix.transformPoint(point); 
    807                         return new Coordinate(sc*p.y/TILE_HEIGHT, sc*p.x/TILE_WIDTH, zoomLevel); 
     822                        return new Coordinate(_scale*p.y/TILE_HEIGHT, _scale*p.x/TILE_WIDTH, zoomLevel); 
    808823                } 
    809824                 
     
    818833                                } 
    819834                        } 
    820                         startPan = new Point(tx,ty); 
     835                        startPan = new Point(_panX,_panY); 
    821836                        panning = true; 
    822837                        dispatchEvent(new MapEvent(MapEvent.START_PANNING)); 
     
    859874                { 
    860875                        // set new scale according to zoom 
    861                         sc = Math.pow(2, coord.zoom); 
     876                        _scale = Math.pow(2, coord.zoom); 
    862877 
    863878                        // figure out where in the world we are                  
    864                         tx = -TILE_WIDTH*coord.column/sc
    865                         ty = -TILE_HEIGHT*coord.row/sc
     879                        _panX = -TILE_WIDTH*coord.column/_scale
     880                        _panY = -TILE_HEIGHT*coord.row/_scale
    866881 
    867882                        // plus the offset                       
    868                         tx += point.x/sc
    869                         ty += point.y/sc
     883                        _panX += point.x/_scale
     884                        _panY += point.y/_scale
    870885 
    871886                        // reset the worldMatrix 
     
    875890                public function get panX():Number 
    876891                { 
    877                         return tx
     892                        return _panX
    878893                } 
    879894                public function set panX(n:Number):void 
    880895                { 
    881                         tx = n; 
     896                        var origin:Point = startPan || new Point(_panX,_panY); 
     897                        _panX = n; 
    882898                        calculateMatrix(); 
    883                         var origin:Point = startPan || new Point(tx,ty); 
    884                         dispatchEvent(new MapEvent(MapEvent.PANNED, new Point(-(origin.x-tx)*sc, -(origin.y-ty)*sc))); 
     899                        dispatchEvent(new MapEvent(MapEvent.PANNED, new Point(-(origin.x-_panX)*_scale, -(origin.y-_panY)*_scale))); 
    885900                } 
    886901                public function get panY():Number 
    887902                { 
    888                         return ty
     903                        return _panY
    889904                } 
    890905                public function set panY(n:Number):void 
    891906                { 
    892                         ty = n; 
     907                        var origin:Point = startPan || new Point(_panX,_panY); 
     908                        _panY = n; 
    893909                        calculateMatrix(); 
    894                         var origin:Point = startPan || new Point(tx,ty); 
    895                         dispatchEvent(new MapEvent(MapEvent.PANNED, new Point(-(origin.x-tx)*sc, -(origin.y-ty)*sc))); 
     910                        dispatchEvent(new MapEvent(MapEvent.PANNED, new Point(-(origin.x-_panX)*_scale, -(origin.y-_panY)*_scale))); 
    896911                } 
    897912 
    898913                public function get zoomLevel():Number 
    899914                { 
    900                         return Math.log(sc) / Math.log(2); 
     915                        return Math.log(_scale) / Math.log(2); 
    901916                } 
    902917 
    903918                public function set zoomLevel(n:Number):void 
    904919                { 
    905                         sc = Math.pow(2, n);                   
     920                        _scale = Math.pow(2, n);                                               
    906921                        calculateMatrix(); 
    907922                        var zoomEvent:MapEvent = new MapEvent(MapEvent.ZOOMED_BY); 
     
    913928                public function get scale():Number 
    914929                { 
    915                         return sc
     930                        return _scale
    916931                } 
    917932                public function set scale(n:Number):void 
    918933                { 
    919934                        var old:Number = zoomLevel; 
    920                         sc = n; 
     935                        _scale = n; 
    921936                        calculateMatrix();       
    922937                        prepareForZooming(); 
     
    944959                        calculateBounds(); 
    945960                         
    946                         // TODO: enforce bounds here 
     961                        // TODO: enforce bounds here? 
    947962 
    948963                        while (well.numChildren > 0) {                   
     
    9981013                        maxTy = br.row * TILE_HEIGHT; 
    9991014                         
    1000                         minTx -= TILE_WIDTH; 
    1001                         maxTx -= TILE_WIDTH; 
    1002                         minTy -= TILE_HEIGHT; 
    1003                         maxTy -= TILE_HEIGHT; 
    1004                          
    1005                         //trace("bounds: ", minTx, maxTx, minTy, maxTy);                         
     1015                        trace("bounds of useful map area: ", minTx, maxTx, minTy, maxTy);                        
     1016                } 
     1017                 
     1018                /** called inside of calculateMatrix before events are fired 
     1019                 *  don't use setters inside of here to correct values otherwise we'll get stuck in a loop! */ 
     1020                protected function enforceBounds():Boolean 
     1021                { 
     1022                        if (!ENFORCE_BOUNDS) { 
     1023                                return false; 
     1024                        } 
     1025                         
     1026                        var touched:Boolean = false; 
     1027                         
     1028                        var tl:Coordinate = topLeftCoordinate.zoomTo(0); 
     1029                        var br:Coordinate = bottomRightCoordinate.zoomTo(0); 
     1030                         
     1031                        var leftX:Number = tl.column * TILE_WIDTH; 
     1032                        var rightX:Number = br.column * TILE_WIDTH; 
     1033                         
     1034                        if (rightX-leftX > maxTx-minTx) { 
     1035                                trace("CENTERING X"); 
     1036                                _panX = -(minTx+maxTx)/2; 
     1037                                touched = true; 
     1038                        } 
     1039                        else if (leftX < minTx) { 
     1040                                trace("TOO LEFT"); 
     1041                                _panX += leftX-minTx; 
     1042                                touched = true; 
     1043                        } 
     1044                        else if (rightX > maxTx) { 
     1045                                trace("TOO RIGHT"); 
     1046                                _panX += rightX-maxTx; 
     1047                                touched = true; 
     1048                        }  
     1049 
     1050                        var upY:Number = tl.row * TILE_HEIGHT; 
     1051                        var downY:Number = br.row * TILE_HEIGHT; 
     1052 
     1053                        if (downY-upY > maxTy-minTy) { 
     1054                                trace("CENTERING Y"); 
     1055                                _panY = -(minTy+maxTy)/2; 
     1056                                touched = true; 
     1057                        } 
     1058                        else if (upY < minTy) { 
     1059                                trace("TOO HIGH"); 
     1060                                _panY += upY-minTy; 
     1061                                touched = true; 
     1062                        } 
     1063                        else if (downY > maxTy) { 
     1064                                trace("TOO LOW"); 
     1065                                _panY += downY-maxTy; 
     1066                                touched = true; 
     1067                        } 
     1068 
     1069                        return touched;                  
    10061070                } 
    10071071 
  • branches/tom-tweenlite/lib/com/modestmaps/Map.as

    r577 r580  
    139139                } 
    140140                else { 
    141                         // set a default extent 
    142                         // whole world at 512 x 512px 
    143                         // (don't use setExtent here because the whole world 
    144                         // won't fit in the default size, and not all  
    145                         // providers have a zoom level 0) 
    146                                 setCenterZoom(new Location(0,0), 1);                             
     141                        var extent:MapExtent = new MapExtent(85, -85, 180, -180); 
     142                        setExtent(extent); 
     143                         
     144                        var l1:Location = mapProvider.coordinateLocation(mapProvider.outerLimits()[0]); 
     145                        var l2:Location = mapProvider.coordinateLocation(mapProvider.outerLimits()[1]); 
     146 
     147                                if (!isNaN(l1.lat) && Math.abs(l1.lat) != Infinity) { 
     148                                        extent.north = l1.lat; 
     149                                }                        
     150                                if (!isNaN(l2.lat) && Math.abs(l2.lat) != Infinity) { 
     151                                        extent.south = l2.lat; 
     152                                }                        
     153                                if (!isNaN(l1.lon) && Math.abs(l1.lon) != Infinity) { 
     154                                        extent.west = l1.lon; 
     155                                }                        
     156                                if (!isNaN(l2.lon) && Math.abs(l2.lon) != Infinity) { 
     157                                        extent.east = l2.lon; 
     158                                } 
     159                                 
     160                                trace(extent);                   
     161 
     162                        setExtent(extent);                       
    147163                } 
    148164