From b6e783fb660ee49820361c2a81cfe05baa70ca0d Mon Sep 17 00:00:00 2001 From: crschmidt Date: Sat, 24 Mar 2007 23:11:18 +0000 Subject: [PATCH] With Erik's approval on #341, I'm happy with this. This restores the mousewheel functionality we had in earlier releases in a similar way, rewritten for handlers. (The benefit of handlers in this case is that handlers are write-once, use many, so the next mousewheel thing we get will *also* be able to use this code.) git-svn-id: http://svn.openlayers.org/trunk/openlayers@2877 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Control/Navigation.js | 21 +++++++++++++++++---- lib/OpenLayers/Handler/MouseWheel.js | 15 +++++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/OpenLayers/Control/Navigation.js b/lib/OpenLayers/Control/Navigation.js index 4d449f234f..ea927f3ecd 100644 --- a/lib/OpenLayers/Control/Navigation.js +++ b/lib/OpenLayers/Control/Navigation.js @@ -59,20 +59,33 @@ OpenLayers.Control.Navigation.prototype = return false; }, + wheelChange: function(evt, deltaZ) { + var newZoom = this.map.getZoom() + deltaZ; + if (!this.map.isValidZoomLevel(newZoom)) return; + + var size = this.map.getSize(); + var deltaX = size.w/2 - evt.xy.x; + var deltaY = evt.xy.y - size.h/2; + var newRes = this.map.baseLayer.resolutions[newZoom]; + var zoomPoint = this.map.getLonLatFromPixel(evt.xy); + var newCenter = new OpenLayers.LonLat( + zoomPoint.lon + deltaX * newRes, + zoomPoint.lat + deltaY * newRes ); + this.map.setCenter( newCenter, newZoom ); + }, + /** User spun scroll wheel up * */ wheelUp: function(evt) { - this.map.setCenter(this.map.getLonLatFromPixel(evt.xy), - this.map.getZoom() + 1); + this.wheelChange(evt, 1); }, /** User spun scroll wheel down * */ wheelDown: function(evt) { - this.map.setCenter(this.map.getLonLatFromPixel(evt.xy), - this.map.getZoom() - 1); + this.wheelChange(evt, -1); }, /** @final @type String */ diff --git a/lib/OpenLayers/Handler/MouseWheel.js b/lib/OpenLayers/Handler/MouseWheel.js index 76116bfd6d..4d370384c4 100644 --- a/lib/OpenLayers/Handler/MouseWheel.js +++ b/lib/OpenLayers/Handler/MouseWheel.js @@ -13,6 +13,14 @@ OpenLayers.Handler.MouseWheel.prototype = OpenLayers.Class.inherit( OpenLayers.H /** @type function **/ wheelListener: null, + /** @type OpenLayers.Pixel + * @private + * + * mousePosition is necessary because evt.clientX/Y is buggy in Moz on + * wheel events, so we cache and use the value from the last mousemove. + **/ + mousePosition: null, + /** * @constructor * @@ -68,8 +76,7 @@ OpenLayers.Handler.MouseWheel.prototype = OpenLayers.Class.inherit( OpenLayers.H // add the mouse position to the event because mozilla has a bug // with clientX and clientY (see https://bugzilla.mozilla.org/show_bug.cgi?id=352179) // getLonLatFromViewPortPx(e) returns wrong values - // TODO FIXME FIXME this might not be the right way to port the 2.3 behavior - e.xy = this.map.events.getMousePosition(e); + e.xy = this.mousePosition; if (delta < 0) { this.callback("down", [e, delta]); } else { @@ -82,6 +89,10 @@ OpenLayers.Handler.MouseWheel.prototype = OpenLayers.Class.inherit( OpenLayers.H } }, + mousemove: function (evt) { + this.mousePosition = evt.xy; + }, + activate: function (evt) { OpenLayers.Handler.prototype.activate.apply(this, arguments); //register mousewheel events specifically on the window and document