Changeset 454

Show
Ignore:
Timestamp:
01/24/08 18:33:18 (10 months ago)
Author:
tom
Message:

added panTo and zoomTo in as3 for animating, removed animation by default in setCenter and setZoom

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/as3/lib/com/modestmaps/Map.as

    r453 r454  
    3232        import com.stamen.twisted.Reactor; 
    3333         
     34        import flash.display.DisplayObject; 
    3435        import flash.display.Sprite; 
    35         import flash.geom.Point; 
     36        import flash.events.Event; 
    3637        import flash.events.MouseEvent; 
    3738        import flash.external.ExternalInterface; 
    38         import flash.events.Event; 
    39         import flash.display.DisplayObject; 
     39        import flash.geom.Point; 
    4040 
    4141        [Event(name="startZooming",      type="com.modestmaps.events.MapEvent")] 
     
    185185                } 
    186186            
    187                         /* 
    188                          * Based on a zoom level, determine appropriate initial 
    189                          * tile coordinate and point using calculateMapCenter(), and inform 
    190                          * the grid of tile coordinate and point by calling grid.resetTiles(). 
    191                          * 
    192                          * @param       Desired zoom level. 
    193                          * 
    194                          * @see com.modestmaps.Map#calculateMapExtent 
    195                          * @see com.modestmaps.core.TileGrid#resetTiles 
    196                          */ 
    197                         public function setZoom(zoom:Number):void 
    198                         { 
    199                            if (zoom == grid.zoomLevel) { // do nothing! 
    200                                   return; 
    201                            } 
    202                            else if (zoom - grid.zoomLevel == 1) { // if 1 step in, delegate to zoomIn animation 
    203                                   zoomIn(); 
    204                            } 
    205                            else if (zoom - grid.zoomLevel == -1) {  // if 1 step out, delegate to zoomOut animation 
    206                                   zoomOut(); 
    207                            } 
    208                            else { // else hard reset 
    209                                   var center:MapPosition = coordinatePosition(grid.centerCoordinate().zoomTo(zoom)); 
    210                                   // tell grid what the rock is cooking 
    211                                   grid.resetTiles(center.coord, center.point); 
    212                                   onExtentChanged(this.getExtent()); 
    213                                   Reactor.callNextFrame(callCopyright); 
    214                            } 
    215                         } 
     187                /* 
     188                 * Based on a zoom level, determine appropriate initial 
     189                 * tile coordinate and point using calculateMapCenter(), and inform 
     190                 * the grid of tile coordinate and point by calling grid.resetTiles(). 
     191                 * 
     192                 * @param       Desired zoom level. 
     193                 * 
     194                 * @see com.modestmaps.Map#calculateMapExtent 
     195                 * @see com.modestmaps.core.TileGrid#resetTiles 
     196                 */ 
     197                public function setZoom(zoom:Number):void 
     198                { 
     199                        if (zoom == grid.zoomLevel) return; 
     200                        // else hard reset 
     201                        var center:MapPosition = coordinatePosition(grid.centerCoordinate().zoomTo(zoom)); 
     202                        // tell grid what the rock is cooking 
     203                        grid.resetTiles(center.coord, center.point); 
     204                        onExtentChanged(this.getExtent()); 
     205                        Reactor.callNextFrame(callCopyright); 
     206                } 
    216207                                 
    217208           /* 
     
    534525           /** 
    535526                * put the given location in the middle of the map 
    536                 * uses setCenterZoom if location is offscreen 
    537                 * or animated in panFrames if the location is visible 
     527                * (use panBy to animate if that's what you want) 
    538528                * @see com.modestmaps.Map#panFrames 
    539529                */ 
    540530                public function setCenter(location:Location):void 
    541531                { 
     532                        var center:MapPosition = coordinatePosition(__mapProvider.locationCoordinate(location).zoomTo(grid.zoomLevel)); 
     533                        // tell grid what the rock is cooking 
     534                        grid.resetTiles(center.coord, center.point); 
     535                        onExtentChanged(this.getExtent()); 
     536                        Reactor.callNextFrame(callCopyright); 
     537                } 
     538 
     539           /** 
     540                 * Put the given location in the middle of the map, animated in panFrames. 
     541                 * Use setCenter or setCenterZoom for big jumps, set forceAnimate to true 
     542                 * if you really want to animate to a location that's currently off screen. 
     543                 * But no promises!  
     544                 *  
     545                 * @see com.modestmaps.Map#panFrames 
     546                 */ 
     547                public function panTo(location:Location, forceAnimate:Boolean=false):void 
     548                { 
    542549                        var p:Point = locationPoint(location,this); 
    543                         if (p.x >= 0 && p.x <= getWidth() && p.y >= 0 && p.y <= getHeight()) { 
     550                        if (forceAnimate || (p.x >= 0 && p.x <= __width && p.y >= 0 && p.y <= __height)) { 
    544551                                var centerPoint:Point = new Point(getWidth()/2, getHeight()/2); 
    545552                                var perFrame:Point = p.subtract(centerPoint); 
     
    549556                        } 
    550557                        else { 
    551                                 var center:MapPosition = coordinatePosition(__mapProvider.locationCoordinate(location).zoomTo(grid.zoomLevel)); 
    552                                 // tell grid what the rock is cooking 
    553                                 grid.resetTiles(center.coord, center.point); 
    554                                 onExtentChanged(this.getExtent()); 
    555                                 Reactor.callNextFrame(callCopyright); 
     558                                setCenter(location); 
    556559                        } 
    557560                } 
     
    563566                public function zoomIn(event:Event=null):void 
    564567                { 
    565                         zoom(1); 
     568                        zoomBy(1); 
    566569                } 
    567570 
     
    572575                public function zoomOut(event:Event=null):void 
    573576                { 
    574                         zoom(-1); 
    575                 } 
    576                          
    577                 // keeping it DRY, as they say   
    578                 // dir should be 1, for in, or -1, for out 
    579                 private function zoom(dir:int):void 
    580                 { 
     577                        zoomBy(-1); 
     578                } 
     579 
     580           /** 
     581                * Zoom to the given level over the course of several frames. 
     582                * @see com.modestmaps.Map#zoomFrames 
     583                */               
     584                public function zoomTo(level:Number):void 
     585                { 
     586                        zoomBy(level-grid.zoomLevel); 
     587                } 
     588                 
     589           /** 
     590                * Zoom in or out over the course of several frames. 
     591                * @see com.modestmaps.Map#zoomFrames 
     592                */               
     593                private function zoomBy(amount:int):void 
     594                { 
     595                        if (amount == 0) return; 
     596                         
    581597                        for(var i:uint = 1; i <= zoomFrames; i += 1) 
    582598                        { 
    583                                 __animSteps.push(new ZoomAnimationStep(ZOOM, dir/zoomFrames, i == zoomFrames)); 
     599                                __animSteps.push(new ZoomAnimationStep(ZOOM, amount/zoomFrames, i == zoomFrames)); 
    584600                        } 
    585601