OpenLayers OpenLayers

Ticket #110: animatedPanning#2.patch

File animatedPanning#2.patch, 13.1 kB (added by emanuel, 2 years ago)

fix bug in layers.js; add unit tests

  • tests/test_Util.html

    old new  
    563563         
    564564  
    565565    } 
    566     function test_Util_createUniqueIDSeq(t) { 
     566    function test_16_Util_createUniqueIDSeq(t) { 
    567567        t.plan(1); 
    568568        OpenLayers.Util.lastSeqID = 0; 
    569569        OpenLayers.Util.createDiv(); 
    570570        OpenLayers.Util.createDiv(); 
    571571        t.eq(OpenLayers.Util.createDiv().id, "OpenLayersDiv3", "Div created is sequential, starting at lastSeqID in Util."); 
    572572    } 
     573 
     574    function test_17_Util_easeInOutPan(t) { 
     575        t.plan(1); 
     576        var delta = 5;  
     577        var totalSteps = 4; 
     578        var step = 1; 
     579        var power = 1; 
     580        var value = OpenLayers.Util.easeInOutPan(delta,totalSteps,step,power); 
     581        t.eq( value, 2, "calculation correct"); 
     582    } 
     583 
     584 
     585 
     586 
     587 
     588 
     589 
     590 
     591 
     592 
     593 
     594 
     595 
     596 
    573597   // --> 
    574598  </script> 
    575599</head> 
  • tests/test_Map.html

    old new  
    316316        t.ok(map.getCenter().equals(new OpenLayers.LonLat(0,0)), "safely sets out-of-bounds lonlat"); 
    317317    } 
    318318 
    319      
     319    /** animated panning **/ 
     320    function test_08_Map_pan(t) { 
     321        t.plan(2); 
     322 
     323        map = new OpenLayers.Map('map'); 
     324        var baseLayer = new OpenLayers.Layer.WMS("Test Layer",  
     325            "http://octo.metacarta.com/cgi-bin/mapserv?", 
     326            {map: "/mapdata/vmap_wms.map", layers: "basic"}, 
     327            {maxResolution: 'auto', maxExtent: new OpenLayers.Bounds(-10,-10,10,10)}); 
     328        map.addLayer(baseLayer); 
     329        var ll = new OpenLayers.LonLat(0,0); 
     330        map.setCenter(ll, 0); 
     331 
     332        map.pan(0,3); 
     333        t.delay_call( 
     334            1, function() {     
     335                var newCent = new OpenLayers.LonLat(0,-0.1); 
     336                t.ok( map.getCenter().equals(newCent),  
     337                        "map center is correct after panning with animation"); 
     338 
     339                map.pan(0,-3, false); 
     340                newCent = new OpenLayers.LonLat(0,0); 
     341                t.ok( map.getCenter().equals(newCent),  
     342                        "map center is correct after panning without animation"); 
     343            } 
     344        ); 
     345    } 
     346 
     347 
    320348    function test_01_Map_defaultTheme(t) { 
    321349        t.plan(5); 
    322350         
  • tests/Control/test_Permalink.html

    old new  
    2020        if (!map.getCenter())  map.zoomToMaxExtent(); 
    2121        map.addControl(control); 
    2222        map.pan(5, 0); 
    23         if (/MSIE/.test(navigator.userAgent)) { 
    24             t.eq(OpenLayers.Util.getElement('permalink').href, "?lat=0&lon=1.75781&zoom=2&layers=B", "Panning sets permalink"); 
    25         } else { 
    26             t.eq(OpenLayers.Util.getElement('permalink').href, location+"?lat=0&lon=1.75781&zoom=2&layers=B", "Panning sets permalink"); 
    27         } 
     23        t.delay_call( 1, function() { 
     24            if (/MSIE/.test(navigator.userAgent)) { 
     25                t.eq(OpenLayers.Util.getElement('permalink').href, "?lat=0&lon=7.03125&zoom=2&layers=B", "Panning sets permalink"); 
     26            } else { 
     27                t.eq(OpenLayers.Util.getElement('permalink').href, location+"?lat=0&lon=7.03125&zoom=2&layers=B", "Panning sets permalink"); 
     28            } 
     29        }); 
    2830    } 
    2931    function test_03_Control_Permalink_updateLinksBase (t) { 
    3032        t.plan( 2 ); 
     
    3739        if (!map.getCenter())  map.zoomToMaxExtent(); 
    3840        map.addControl(control); 
    3941        map.pan(5, 0); 
    40         OpenLayers.Util.getElement('edit_permalink').href = './edit.html?lat=0&lon=1.75781&zoom=2&layers=B'; 
    41         t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base"); 
     42        t.delay_call( 1, function() { 
     43            OpenLayers.Util.getElement('edit_permalink').href = './edit.html?lat=0&lon=7.03125&zoom=2&layers=B'; 
     44            t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with base"); 
     45        }); 
    4246  } 
    4347  function test_04_Control_Permalink_noElement (t) { 
    4448        t.plan( 2 ); 
  • tests/Control/test_PanZoom.html

    old new  
    4242        map.setCenter(centerLL, 5); 
    4343 
    4444        control.buttons[0].onmousedown(evt); 
    45         t.ok( map.getCenter().lat > centerLL.lat, "Pan up works correctly" ); 
     45        t.delay_call(  
     46            1, function() { 
     47                t.ok( map.getCenter().lat > centerLL.lat, "Pan up works correctly" ); 
     48                control.buttons[1].onmousedown(evt); 
     49            }, 
     50             
     51            1, function() { 
     52                t.ok( map.getCenter().lon < centerLL.lon, "Pan left works correctly" ); 
     53                control.buttons[2].onmousedown(evt); 
     54            }, 
     55             
     56            1, function() { 
     57                t.ok( map.getCenter().lon == centerLL.lon, "Pan right works correctly" ); 
     58                control.buttons[3].onmousedown(evt); 
     59            }, 
     60             
     61            1, function() { 
     62                t.ok( map.getCenter().lat == centerLL.lat, "Pan down works correctly" ); 
     63                control.buttons[4].onmousedown(evt); 
     64                 
     65                t.eq( map.getZoom(), 6, "zoomin works correctly" ); 
    4666 
    47         control.buttons[1].onmousedown(evt); 
    48         t.ok( map.getCenter().lon < centerLL.lon, "Pan left works correctly" ); 
     67                control.buttons[6].onmousedown(evt); 
     68                t.eq( map.getZoom(), 5, "zoomout works correctly" ); 
    4969 
    50         control.buttons[2].onmousedown(evt); 
    51         t.ok( map.getCenter().lon == centerLL.lon, "Pan right works correctly" ); 
     70                control.buttons[5].onmousedown(evt); 
     71                t.eq( map.getZoom(), 2, "zoomworld works correctly" ); 
     72            } 
     73        ); 
    5274 
    53         control.buttons[3].onmousedown(evt); 
    54         t.ok( map.getCenter().lat == centerLL.lat, "Pan down works correctly" ); 
    55  
    56         control.buttons[4].onmousedown(evt); 
    57         t.eq( map.getZoom(), 6, "zoomin works correctly" ); 
    58  
    59         control.buttons[6].onmousedown(evt); 
    60         t.eq( map.getZoom(), 5, "zoomout works correctly" ); 
    61  
    62         control.buttons[5].onmousedown(evt); 
    63         t.eq( map.getZoom(), 2, "zoomworld works correctly" ); 
    64  
    6575    } 
    6676  // --> 
    6777  </script> 
  • lib/OpenLayers/Control/PanZoom.js

    old new  
    1515  OpenLayers.Class.inherit( OpenLayers.Control, { 
    1616 
    1717    /** @type int */ 
    18     slideFactor: 50
     18    slideFactor: 75
    1919 
    2020    /** @type Array of Button Divs */ 
    2121    buttons: null, 
     
    114114 
    115115        switch (this.action) { 
    116116            case "panup":  
    117                 this.map.pan(0, -50); 
     117                this.map.pan(0, -this.slideFactor); 
    118118                break; 
    119119            case "pandown":  
    120                 this.map.pan(0, 50); 
     120                this.map.pan(0, this.slideFactor); 
    121121                break; 
    122122            case "panleft":  
    123                 this.map.pan(-50, 0); 
     123                this.map.pan(-this.slideFactor, 0); 
    124124                break; 
    125125            case "panright":  
    126                 this.map.pan(50, 0); 
     126                this.map.pan(this.slideFactor, 0); 
    127127                break; 
    128128            case "zoomin":  
    129129                this.map.zoomIn();  
  • lib/OpenLayers/Layer.js

    old new  
    529529            var size = this.map.getSize(); 
    530530            var center = this.map.getCenter(); 
    531531            var res  = this.map.getResolution(); 
    532          
    533             var delta_x = viewPortPx.x - (size.w / 2); 
    534             var delta_y = viewPortPx.y - (size.h / 2); 
     532 
     533            var delta_x = viewPortPx.x - Math.ceil(size.w / 2); 
     534            var delta_y = viewPortPx.y - Math.ceil(size.h / 2); 
    535535             
    536536            lonlat = new OpenLayers.LonLat(center.lon + delta_x * res , 
    537537                                         center.lat - delta_y * res);  
  • lib/OpenLayers/Map.js

    old new  
    136136    /** @type string */ 
    137137    theme: null, 
    138138 
     139 
     140    //ANIMATION 
     141     
     142    /** @type Boolean */ 
     143    animated: true, 
     144 
     145    /** steps in the slide 
     146     *  
     147     * @type int */ 
     148    slideSteps: 4,   
     149     
     150    /** millisecondss between each step 
     151     *  
     152     * @type int */ 
     153    slideWait: 1, 
     154 
     155    /** power used to calculate width of slide steps 
     156     *  
     157     * @type float*/ 
     158    slidePower: 0.7, 
     159 
     160    /** @type int */ 
     161    animatedPanningIntervalID: null, 
     162 
    139163    /** Should OpenLayers allow events on the map to fall through to other 
    140164     *   elements on the page, or should it swallow them? (#457) 
    141165     *  
     
    749773     * @param {int} dx 
    750774     * @param {int} dy 
    751775     */ 
    752     pan: function(dx, dy) { 
     776    pan: function(dx, dy, animated) { 
     777        if (animated == null) {  
     778            animated = this.animated;  
     779        }  
     780         
     781        if (animated) { 
     782            this.panSlide(dx, dy,  
     783                          this.slideSteps,  
     784                          this.slideWait,  
     785                          this.slidePower); 
     786        } else { 
    753787 
    754         // getCenter 
    755         var centerPx = this.getViewPortPxFromLonLat(this.getCenter()); 
     788            // getCenter 
     789            var centerPx = this.getViewPortPxFromLonLat(this.getCenter()); 
    756790 
    757         // adjust 
    758         var newCenterPx = centerPx.add(dx, dy); 
    759          
    760         // only call setCenter if there has been a change 
    761         if (!newCenterPx.equals(centerPx)) { 
    762             var newCenterLonLat = this.getLonLatFromViewPortPx(newCenterPx); 
    763             this.setCenter(newCenterLonLat); 
     791            // adjust 
     792            var newCenterPx = centerPx.add(dx, dy); 
     793             
     794            // only call setCenter if there has been a change 
     795            if (!newCenterPx.equals(centerPx)) { 
     796                var newCenterLonLat = this.getLonLatFromViewPortPx(newCenterPx); 
     797                this.setCenter(newCenterLonLat); 
     798            } 
    764799        } 
    765  
    766800   }, 
    767801 
    768802    /** 
     
    871905            this.layerContainerDiv.style.top  = (originPx.y - newPx.y) + "px"; 
    872906        } 
    873907    }, 
     908    /** Position changer with Memory by www.hesido.com 
     909     *  modified by dncpax for OpenLayers 
     910     *  
     911     * @param {int} slideX 
     912     * @param {int} slideY 
     913     * @param {int} totalSteps 
     914     * @param {int} intervals 
     915     * @param {float} power  
     916     */ 
     917    panSlide: function( slideX, slideY, totalSteps, interval, power) { 
     918        if (this.animatedPanningIntervalID) { 
     919            window.clearInterval(this.animatedPanningIntervalID); 
     920            this.animatedPanningIntervalID = null; 
     921        } 
     922        var context = { 
     923            'map': this, 
     924            'slideX': slideX, 
     925            'slideY': slideY, 
     926            'totalSteps': totalSteps, 
     927            'step': 0, 
     928            'power': power 
     929        }; 
     930        var move = function() { 
     931            var dx = OpenLayers.Util.easeInOutPan(this.slideX,  
     932                                                this.totalSteps,  
     933                                                this.step,  
     934                                                this.power); 
     935            var dy = OpenLayers.Util.easeInOutPan(this.slideY, 
     936                                                this.totalSteps,  
     937                                                this.step,  
     938                                                this.power); 
     939            this.map.pan(dx, dy, false); 
     940            this.step++; 
     941            if (this.step > this.totalSteps) { 
     942                window.clearInterval(this.map.animatedPanningIntervalID); 
     943                this.map.animatedPanningIntervalID = null; 
     944            } 
     945        }; 
    874946 
     947        this.animatedPanningIntervalID =  
     948            window.setInterval(move.bindAsEventListener(context),  
     949                               interval); 
     950    }, 
     951 
    875952    /** 
    876953     * @private  
    877954     *  
  • lib/OpenLayers/Util.js

    old new  
    965965    } 
    966966    return head; 
    967967}; 
     968 
     969 
     970/** Generic Animation Step Value Generator By www.hesido.com, slightly modified 
     971 *  
     972 *  
     973 * @param {int} delta 
     974 * @param {int} totalSteps 
     975 * @param {int} step 
     976 * @param {float} power 
     977 *  
     978 * @returns The delta to the next position in the pan animation movement 
     979 * @type int 
     980 */ 
     981OpenLayers.Util.easeInOutPan = function(delta, totalSteps, step, power) { 
     982    var prevStepVal = Math.pow(((1/totalSteps)*(step-1)),power) * delta; 
     983    var stepVal = Math.pow(((1/totalSteps)*step),power) * delta; 
     984    return Math.ceil(stepVal) - Math.ceil(prevStepVal); 
     985}; 
     986