Move zoom from View2D to Interaction

This commit is contained in:
Éric Lemoine
2013-04-10 08:32:13 +02:00
parent dbca68650c
commit 2d5381ae41
8 changed files with 118 additions and 102 deletions
+24 -5
View File
@@ -6,8 +6,10 @@ goog.require('goog.dom');
goog.require('goog.dom.TagName'); goog.require('goog.dom.TagName');
goog.require('goog.events'); goog.require('goog.events');
goog.require('goog.events.EventType'); goog.require('goog.events.EventType');
goog.require('ol.animation');
goog.require('ol.control.Control'); goog.require('ol.control.Control');
goog.require('ol.css'); goog.require('ol.css');
goog.require('ol.easing');
/** /**
@@ -74,8 +76,17 @@ ol.control.Zoom.prototype.handleIn_ = function(browserEvent) {
var map = this.getMap(); var map = this.getMap();
map.requestRenderFrame(); map.requestRenderFrame();
// FIXME works for View2D only // FIXME works for View2D only
map.getView().zoomByDelta(map, this.delta_, undefined, var view = map.getView().getView2D();
ol.control.ZOOM_DURATION); var currentResolution = view.getResolution();
if (goog.isDef(currentResolution)) {
map.addPreRenderFunction(ol.animation.zoom({
resolution: currentResolution,
duration: ol.control.ZOOM_DURATION,
easing: ol.easing.easeOut
}));
}
var resolution = view.constrainResolution(currentResolution, this.delta_);
view.setResolution(resolution);
}; };
@@ -87,8 +98,16 @@ ol.control.Zoom.prototype.handleOut_ = function(browserEvent) {
// prevent #zoomOut anchor from getting appended to the url // prevent #zoomOut anchor from getting appended to the url
browserEvent.preventDefault(); browserEvent.preventDefault();
var map = this.getMap(); var map = this.getMap();
map.requestRenderFrame();
// FIXME works for View2D only // FIXME works for View2D only
map.getView().zoomByDelta(map, -this.delta_, undefined, var view = map.getView().getView2D();
ol.control.ZOOM_DURATION); var currentResolution = view.getResolution();
if (goog.isDef(currentResolution)) {
map.addPreRenderFunction(ol.animation.zoom({
resolution: currentResolution,
duration: ol.control.ZOOM_DURATION,
easing: ol.easing.easeOut
}));
}
var resolution = view.constrainResolution(currentResolution, -this.delta_);
view.setResolution(resolution);
}; };
@@ -5,7 +5,6 @@ goog.provide('ol.interaction.DblClickZoom');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('ol.MapBrowserEvent'); goog.require('ol.MapBrowserEvent');
goog.require('ol.MapBrowserEvent.EventType'); goog.require('ol.MapBrowserEvent.EventType');
goog.require('ol.View2D');
goog.require('ol.interaction.Interaction'); goog.require('ol.interaction.Interaction');
@@ -46,9 +45,8 @@ ol.interaction.DblClickZoom.prototype.handleMapBrowserEvent =
var delta = mapBrowserEvent.browserEvent.shiftKey ? var delta = mapBrowserEvent.browserEvent.shiftKey ?
-this.delta_ : this.delta_; -this.delta_ : this.delta_;
// FIXME works for View2D only // FIXME works for View2D only
var view = map.getView(); var view = map.getView().getView2D();
goog.asserts.assertInstanceof(view, ol.View2D); ol.interaction.Interaction.zoomByDelta(map, view, delta, anchor,
view.zoomByDelta(map, delta, anchor,
ol.interaction.DBLCLICKZOOM_ANIMATION_DURATION); ol.interaction.DBLCLICKZOOM_ANIMATION_DURATION);
mapBrowserEvent.preventDefault(); mapBrowserEvent.preventDefault();
browserEvent.preventDefault(); browserEvent.preventDefault();
@@ -4,7 +4,6 @@ goog.provide('ol.interaction.DragRotateAndZoom');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.math.Vec2'); goog.require('goog.math.Vec2');
goog.require('ol.View2D');
goog.require('ol.interaction.ConditionType'); goog.require('ol.interaction.ConditionType');
goog.require('ol.interaction.Drag'); goog.require('ol.interaction.Drag');
goog.require('ol.interaction.Interaction'); goog.require('ol.interaction.Interaction');
@@ -56,8 +55,7 @@ ol.interaction.DragRotateAndZoom.prototype.handleDrag =
var theta = Math.atan2(delta.y, delta.x); var theta = Math.atan2(delta.y, delta.x);
var magnitude = delta.magnitude(); var magnitude = delta.magnitude();
// FIXME works for View2D only // FIXME works for View2D only
var view = map.getView(); var view = map.getView().getView2D();
goog.asserts.assertInstanceof(view, ol.View2D);
map.requestRenderFrame(); map.requestRenderFrame();
// FIXME the calls to map.rotate and map.zoomToResolution should use // FIXME the calls to map.rotate and map.zoomToResolution should use
// map.withFrozenRendering but an assertion fails :-( // map.withFrozenRendering but an assertion fails :-(
@@ -69,7 +67,7 @@ ol.interaction.DragRotateAndZoom.prototype.handleDrag =
this.lastAngle_ = theta; this.lastAngle_ = theta;
if (goog.isDef(this.lastMagnitude_)) { if (goog.isDef(this.lastMagnitude_)) {
var resolution = this.lastMagnitude_ * (view.getResolution() / magnitude); var resolution = this.lastMagnitude_ * (view.getResolution() / magnitude);
view.zoom(map, resolution); ol.interaction.Interaction.zoom(map, view, resolution);
} }
this.lastMagnitude_ = magnitude; this.lastMagnitude_ = magnitude;
}; };
+80
View File
@@ -99,3 +99,83 @@ ol.interaction.Interaction.rotateWithoutConstraints =
} }
} }
}; };
/**
* @param {ol.Map} map Map.
* @param {ol.View2D} view View.
* @param {number|undefined} resolution Resolution to go to.
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
* @param {number=} opt_duration Duration.
* @param {number=} opt_direction Zooming direction; > 0 indicates
* zooming out, in which case the constraints system will select
* the largest nearest resolution; < 0 indicates zooming in, in
* which case the constraints system will select the smallest
* nearest resolution; == 0 indicates that the zooming direction
* is unknown/not relevant, in which case the constraints system
* will select the nearest resolution. If not defined 0 is
* assumed.
*/
ol.interaction.Interaction.zoom =
function(map, view, resolution, opt_anchor, opt_duration, opt_direction) {
resolution = view.constrainResolution(resolution, 0, opt_direction);
ol.interaction.Interaction.zoomWithoutConstraints(
map, view, resolution, opt_anchor, opt_duration);
};
/**
* @param {ol.Map} map Map.
* @param {ol.View2D} view View.
* @param {number} delta Delta from previous zoom level.
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
* @param {number=} opt_duration Duration.
*/
ol.interaction.Interaction.zoomByDelta =
function(map, view, delta, opt_anchor, opt_duration) {
var currentResolution = view.getResolution();
var resolution = view.constrainResolution(currentResolution, delta, 0);
ol.interaction.Interaction.zoomWithoutConstraints(
map, view, resolution, opt_anchor, opt_duration);
};
/**
* @param {ol.Map} map Map.
* @param {ol.View2D} view View.
* @param {number|undefined} resolution Resolution to go to.
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
* @param {number=} opt_duration Duration.
*/
ol.interaction.Interaction.zoomWithoutConstraints =
function(map, view, resolution, opt_anchor, opt_duration) {
if (goog.isDefAndNotNull(resolution)) {
var currentResolution = view.getResolution();
var currentCenter = view.getCenter();
if (goog.isDef(currentResolution) && goog.isDef(currentCenter) &&
goog.isDef(opt_duration)) {
map.requestRenderFrame();
map.addPreRenderFunction(ol.animation.zoom({
resolution: currentResolution,
duration: opt_duration,
easing: ol.easing.easeOut
}));
if (goog.isDef(opt_anchor)) {
map.addPreRenderFunction(ol.animation.pan({
source: currentCenter,
duration: opt_duration,
easing: ol.easing.easeOut
}));
}
}
if (goog.isDefAndNotNull(opt_anchor)) {
var center = view.calculateCenterZoom(resolution, opt_anchor);
map.withFrozenRendering(function() {
view.setCenter(center);
view.setResolution(resolution);
});
} else {
view.setResolution(resolution);
}
}
};
@@ -4,7 +4,6 @@ goog.provide('ol.interaction.KeyboardZoom');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('goog.events.KeyHandler.EventType'); goog.require('goog.events.KeyHandler.EventType');
goog.require('ol.View2D');
goog.require('ol.interaction.Interaction'); goog.require('ol.interaction.Interaction');
@@ -50,9 +49,8 @@ ol.interaction.KeyboardZoom.prototype.handleMapBrowserEvent =
var delta = (charCode == '+'.charCodeAt(0)) ? this.delta_ : -this.delta_; var delta = (charCode == '+'.charCodeAt(0)) ? this.delta_ : -this.delta_;
map.requestRenderFrame(); map.requestRenderFrame();
// FIXME works for View2D only // FIXME works for View2D only
var view = map.getView(); var view = map.getView().getView2D();
goog.asserts.assertInstanceof(view, ol.View2D); ol.interaction.Interaction.zoomByDelta(map, view, delta, undefined,
view.zoomByDelta(map, delta, undefined,
ol.interaction.KEYBOARD_ZOOM_DURATION); ol.interaction.KEYBOARD_ZOOM_DURATION);
keyEvent.preventDefault(); keyEvent.preventDefault();
mapBrowserEvent.preventDefault(); mapBrowserEvent.preventDefault();
@@ -7,7 +7,6 @@ goog.require('goog.events.MouseWheelEvent');
goog.require('goog.events.MouseWheelHandler.EventType'); goog.require('goog.events.MouseWheelHandler.EventType');
goog.require('goog.math'); goog.require('goog.math');
goog.require('ol.Coordinate'); goog.require('ol.Coordinate');
goog.require('ol.View2D');
goog.require('ol.interaction.Interaction'); goog.require('ol.interaction.Interaction');
@@ -108,11 +107,10 @@ ol.interaction.MouseWheelZoom.prototype.doZoom_ = function(map) {
var delta = goog.math.clamp(this.delta_, -maxDelta, maxDelta); var delta = goog.math.clamp(this.delta_, -maxDelta, maxDelta);
// FIXME works for View2D only // FIXME works for View2D only
var view = map.getView(); var view = map.getView().getView2D();
goog.asserts.assertInstanceof(view, ol.View2D);
map.requestRenderFrame(); map.requestRenderFrame();
view.zoomByDelta(map, -delta, this.lastAnchor_, ol.interaction.Interaction.zoomByDelta(map, view, -delta, this.lastAnchor_,
ol.interaction.MOUSEWHEELZOOM_ANIMATION_DURATION); ol.interaction.MOUSEWHEELZOOM_ANIMATION_DURATION);
this.delta_ = 0; this.delta_ = 0;
+6 -4
View File
@@ -6,6 +6,7 @@ goog.require('goog.asserts');
goog.require('goog.style'); goog.require('goog.style');
goog.require('ol.View'); goog.require('ol.View');
goog.require('ol.ViewHint'); goog.require('ol.ViewHint');
goog.require('ol.interaction.Interaction');
goog.require('ol.interaction.Touch'); goog.require('ol.interaction.Touch');
@@ -65,7 +66,7 @@ ol.interaction.TouchZoom.prototype.handleTouchMove =
} }
var map = mapBrowserEvent.map; var map = mapBrowserEvent.map;
var view = map.getView(); var view = map.getView().getView2D();
// scale anchor point. // scale anchor point.
var viewportPosition = goog.style.getClientPosition(map.getViewport()); var viewportPosition = goog.style.getClientPosition(map.getViewport());
@@ -75,7 +76,8 @@ ol.interaction.TouchZoom.prototype.handleTouchMove =
var anchor = map.getCoordinateFromPixel(centroid); var anchor = map.getCoordinateFromPixel(centroid);
// scale, bypass the resolution constraint // scale, bypass the resolution constraint
view.zoomWithoutConstraints(map, view.getResolution() * scaleDelta, anchor); ol.interaction.Interaction.zoomWithoutConstraints(
map, view, view.getResolution() * scaleDelta, anchor);
}; };
@@ -87,12 +89,12 @@ ol.interaction.TouchZoom.prototype.handleTouchEnd =
function(mapBrowserEvent) { function(mapBrowserEvent) {
if (this.targetTouches.length < 2) { if (this.targetTouches.length < 2) {
var map = mapBrowserEvent.map; var map = mapBrowserEvent.map;
var view = map.getView(); var view = map.getView().getView2D();
// Zoom to final resolution, with an animation, and provide a // Zoom to final resolution, with an animation, and provide a
// direction not to zoom out/in if user was pinching in/out. // direction not to zoom out/in if user was pinching in/out.
// Direction is > 0 if pinching out, and < 0 if pinching in. // Direction is > 0 if pinching out, and < 0 if pinching in.
var direction = this.lastScaleDelta_ - 1; var direction = this.lastScaleDelta_ - 1;
view.zoom(map, view.getResolution(), undefined, ol.interaction.Interaction.zoom(map, view, view.getResolution(), undefined,
ol.interaction.TOUCHZOOM_ANIMATION_DURATION, direction); ol.interaction.TOUCHZOOM_ANIMATION_DURATION, direction);
view.setHint(ol.ViewHint.INTERACTING, -1); view.setHint(ol.ViewHint.INTERACTING, -1);
return false; return false;
-77
View File
@@ -15,9 +15,7 @@ goog.require('ol.RotationConstraint');
goog.require('ol.RotationConstraintType'); goog.require('ol.RotationConstraintType');
goog.require('ol.Size'); goog.require('ol.Size');
goog.require('ol.View'); goog.require('ol.View');
goog.require('ol.animation');
goog.require('ol.coordinate'); goog.require('ol.coordinate');
goog.require('ol.easing');
goog.require('ol.projection'); goog.require('ol.projection');
@@ -376,81 +374,6 @@ goog.exportProperty(
ol.View2D.prototype.setRotation); ol.View2D.prototype.setRotation);
/**
* @param {ol.Map} map Map.
* @param {number|undefined} resolution Resolution to go to.
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
* @param {number=} opt_duration Duration.
* @param {number=} opt_direction Zooming direction; > 0 indicates
* zooming out, in which case the constraints system will select
* the largest nearest resolution; < 0 indicates zooming in, in
* which case the constraints system will select the smallest
* nearest resolution; == 0 indicates that the zooming direction
* is unknown/not relevant, in which case the constraints system
* will select the nearest resolution. If not defined 0 is
* assumed.
*/
ol.View2D.prototype.zoom =
function(map, resolution, opt_anchor, opt_duration, opt_direction) {
resolution = this.constrainResolution(resolution, 0, opt_direction);
this.zoomWithoutConstraints(map, resolution, opt_anchor, opt_duration);
};
/**
* @param {ol.Map} map Map.
* @param {number} delta Delta from previous zoom level.
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
* @param {number=} opt_duration Duration.
*/
ol.View2D.prototype.zoomByDelta =
function(map, delta, opt_anchor, opt_duration) {
var currentResolution = this.getResolution();
var resolution = this.constrainResolution(currentResolution, delta, 0);
this.zoomWithoutConstraints(map, resolution, opt_anchor, opt_duration);
};
/**
* @param {ol.Map} map Map.
* @param {number|undefined} resolution Resolution to go to.
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
* @param {number=} opt_duration Duration.
*/
ol.View2D.prototype.zoomWithoutConstraints =
function(map, resolution, opt_anchor, opt_duration) {
if (goog.isDefAndNotNull(resolution)) {
var currentResolution = this.getResolution();
var currentCenter = this.getCenter();
if (goog.isDef(currentResolution) && goog.isDef(currentCenter) &&
goog.isDef(opt_duration)) {
map.requestRenderFrame();
map.addPreRenderFunction(ol.animation.zoom({
resolution: currentResolution,
duration: opt_duration,
easing: ol.easing.easeOut
}));
if (goog.isDef(opt_anchor)) {
map.addPreRenderFunction(ol.animation.pan({
source: currentCenter,
duration: opt_duration,
easing: ol.easing.easeOut
}));
}
}
if (goog.isDefAndNotNull(opt_anchor)) {
var center = this.calculateCenterZoom(resolution, opt_anchor);
map.withFrozenRendering(function() {
this.setCenter(center);
this.setResolution(resolution);
}, this);
} else {
this.setResolution(resolution);
}
}
};
/** /**
* @private * @private
* @param {ol.View2DOptions} options View2D options. * @param {ol.View2DOptions} options View2D options.