Merge pull request #820 from twpayne/use-view2dstate-in-interactions

Use view2DState in interactions
This commit is contained in:
Tom Payne
2013-06-27 04:06:05 -07:00
8 changed files with 45 additions and 30 deletions

View File

@@ -119,11 +119,12 @@ ol.interaction.Drag.prototype.handleMapBrowserEvent =
}
} else if (mapBrowserEvent.type == ol.MapBrowserEvent.EventType.DRAGSTART) {
goog.asserts.assertInstanceof(browserEvent, goog.events.BrowserEvent);
var view2DState = view.getView2D().getView2DState();
this.startX = browserEvent.clientX;
this.startY = browserEvent.clientY;
this.deltaX = 0;
this.deltaY = 0;
this.startCenter = /** @type {!ol.Coordinate} */ (view.getCenter());
this.startCenter = view2DState.center;
this.startCoordinate = /** @type {ol.Coordinate} */
(mapBrowserEvent.getCoordinate());
var handled = this.handleDragStart(mapBrowserEvent);

View File

@@ -80,16 +80,16 @@ ol.interaction.DragPan.prototype.handleDragEnd = function(mapBrowserEvent) {
// FIXME works for View2D only
var map = mapBrowserEvent.map;
var view = map.getView();
var view = map.getView().getView2D();
if (this.kinetic_ && this.kinetic_.end()) {
var view2DState = view.getView2DState();
var distance = this.kinetic_.getDistance();
var angle = this.kinetic_.getAngle();
var center = view.getCenter();
this.kineticPreRenderFn_ = this.kinetic_.pan(center);
this.kineticPreRenderFn_ = this.kinetic_.pan(view2DState.center);
map.addPreRenderFunction(this.kineticPreRenderFn_);
var centerpx = map.getPixelFromCoordinate(center);
var centerpx = map.getPixelFromCoordinate(view2DState.center);
var dest = map.getCoordinateFromPixel([
centerpx[0] - distance * Math.cos(angle),
centerpx[1] - distance * Math.sin(angle)

View File

@@ -72,15 +72,16 @@ ol.interaction.DragRotateAndZoom.prototype.handleDrag =
var magnitude = delta.magnitude();
// FIXME works for View2D only
var view = map.getView().getView2D();
var view2DState = view.getView2DState();
map.requestRenderFrame();
if (goog.isDef(this.lastAngle_)) {
var angleDelta = theta - this.lastAngle_;
ol.interaction.Interaction.rotateWithoutConstraints(
map, view, view.getRotation() - angleDelta);
map, view, view2DState.rotation - angleDelta);
}
this.lastAngle_ = theta;
if (goog.isDef(this.lastMagnitude_)) {
var resolution = this.lastMagnitude_ * (view.getResolution() / magnitude);
var resolution = this.lastMagnitude_ * (view2DState.resolution / magnitude);
ol.interaction.Interaction.zoomWithoutConstraints(map, view, resolution);
}
if (goog.isDef(this.lastMagnitude_)) {
@@ -96,12 +97,15 @@ ol.interaction.DragRotateAndZoom.prototype.handleDrag =
ol.interaction.DragRotateAndZoom.prototype.handleDragEnd =
function(mapBrowserEvent) {
var map = mapBrowserEvent.map;
// FIXME works for View2D only
var view = map.getView().getView2D();
var view2DState = view.getView2DState();
var direction = this.lastScaleDelta_ - 1;
map.withFrozenRendering(function() {
ol.interaction.Interaction.rotate(map, view, view.getRotation());
ol.interaction.Interaction.zoom(map, view, view.getResolution(), undefined,
ol.interaction.DRAGROTATEANDZOOM_ANIMATION_DURATION, direction);
ol.interaction.Interaction.rotate(map, view, view2DState.rotation);
ol.interaction.Interaction.zoom(map, view, view2DState.resolution,
undefined, ol.interaction.DRAGROTATEANDZOOM_ANIMATION_DURATION,
direction);
});
this.lastScaleDelta_ = 0;
return true;

View File

@@ -1,7 +1,6 @@
goog.provide('ol.interaction.DragRotate');
goog.require('goog.asserts');
goog.require('ol.View2D');
goog.require('ol.interaction.ConditionType');
goog.require('ol.interaction.Drag');
goog.require('ol.interaction.Interaction');
@@ -54,12 +53,12 @@ ol.interaction.DragRotate.prototype.handleDrag = function(mapBrowserEvent) {
Math.atan2(size[1] / 2 - offset[1], offset[0] - size[0] / 2);
if (goog.isDef(this.lastAngle_)) {
var delta = theta - this.lastAngle_;
var view = map.getView();
// FIXME supports View2D only
goog.asserts.assertInstanceof(view, ol.View2D);
// FIXME works for View2D only
var view = map.getView().getView2D();
var view2DState = view.getView2DState();
map.requestRenderFrame();
ol.interaction.Interaction.rotateWithoutConstraints(
map, view, view.getRotation() - delta);
map, view, view2DState.rotation - delta);
}
this.lastAngle_ = theta;
};
@@ -70,10 +69,10 @@ ol.interaction.DragRotate.prototype.handleDrag = function(mapBrowserEvent) {
*/
ol.interaction.DragRotate.prototype.handleDragEnd = function(mapBrowserEvent) {
var map = mapBrowserEvent.map;
// FIXME supports View2D only
var view = map.getView();
goog.asserts.assertInstanceof(view, ol.View2D);
ol.interaction.Interaction.rotate(map, view, view.getRotation(), undefined,
// FIXME works for View2D only
var view = map.getView().getView2D();
var view2DState = view.getView2DState();
ol.interaction.Interaction.rotate(map, view, view2DState.rotation, undefined,
ol.interaction.DRAGROTATE_ANIMATION_DURATION);
};
@@ -86,9 +85,6 @@ ol.interaction.DragRotate.prototype.handleDragStart =
var browserEvent = mapBrowserEvent.browserEvent;
if (browserEvent.isMouseActionButton() && this.condition_(browserEvent)) {
var map = mapBrowserEvent.map;
// FIXME supports View2D only
var view = map.getView();
goog.asserts.assertInstanceof(view, ol.View2D);
map.requestRenderFrame();
this.lastAngle_ = undefined;
return true;

View File

@@ -61,11 +61,12 @@ ol.interaction.TouchPan.prototype.handleTouchMove = function(mapBrowserEvent) {
var deltaX = this.lastCentroid[0] - centroid[0];
var deltaY = centroid[1] - this.lastCentroid[1];
var map = mapBrowserEvent.map;
var view = map.getView();
var view = map.getView().getView2D();
var view2DState = view.getView2DState();
var center = [deltaX, deltaY];
ol.coordinate.scale(center, view.getResolution());
ol.coordinate.rotate(center, view.getRotation());
ol.coordinate.add(center, view.getCenter());
ol.coordinate.scale(center, view2DState.resolution);
ol.coordinate.rotate(center, view2DState.rotation);
ol.coordinate.add(center, view2DState.center);
map.requestRenderFrame();
view.setCenter(center);
}

View File

@@ -101,10 +101,12 @@ ol.interaction.TouchRotate.prototype.handleTouchMove =
// rotate
if (this.rotating_) {
// FIXME works for View2D only
var view = map.getView().getView2D();
var view2DState = view.getView2DState();
map.requestRenderFrame();
ol.interaction.Interaction.rotateWithoutConstraints(map, view,
view.getRotation() + rotationDelta, this.anchor_);
view2DState.rotation + rotationDelta, this.anchor_);
}
};
@@ -116,10 +118,12 @@ ol.interaction.TouchRotate.prototype.handleTouchEnd =
function(mapBrowserEvent) {
if (this.targetTouches.length < 2) {
var map = mapBrowserEvent.map;
// FIXME works for View2D only
var view = map.getView().getView2D();
var view2DState = view.getView2DState();
if (this.rotating_) {
ol.interaction.Interaction.rotate(
map, view, view.getRotation(), this.anchor_,
map, view, view2DState.rotation, this.anchor_,
ol.interaction.TOUCHROTATE_ANIMATION_DURATION);
}
return false;

View File

@@ -71,7 +71,9 @@ ol.interaction.TouchZoom.prototype.handleTouchMove =
}
var map = mapBrowserEvent.map;
// FIXME works for View2D only
var view = map.getView().getView2D();
var view2DState = view.getView2DState();
// scale anchor point.
var viewportPosition = goog.style.getClientPosition(map.getViewport());
@@ -83,7 +85,7 @@ ol.interaction.TouchZoom.prototype.handleTouchMove =
// scale, bypass the resolution constraint
map.requestRenderFrame();
ol.interaction.Interaction.zoomWithoutConstraints(
map, view, view.getResolution() * scaleDelta, this.anchor_);
map, view, view2DState.resolution * scaleDelta, this.anchor_);
};
@@ -95,12 +97,14 @@ ol.interaction.TouchZoom.prototype.handleTouchEnd =
function(mapBrowserEvent) {
if (this.targetTouches.length < 2) {
var map = mapBrowserEvent.map;
// FIXME works for View2D only
var view = map.getView().getView2D();
var view2DState = view.getView2DState();
// Zoom to final resolution, with an animation, and provide a
// direction not to zoom out/in if user was pinching in/out.
// Direction is > 0 if pinching out, and < 0 if pinching in.
var direction = this.lastScaleDelta_ - 1;
ol.interaction.Interaction.zoom(map, view, view.getResolution(),
ol.interaction.Interaction.zoom(map, view, view2DState.resolution,
this.anchor_, ol.interaction.TOUCHZOOM_ANIMATION_DURATION, direction);
return false;
} else {

View File

@@ -659,6 +659,11 @@ ol.Map.prototype.handleLayersRemove_ = function(collectionEvent) {
* @param {ol.MapBrowserEvent} mapBrowserEvent The event to handle.
*/
ol.Map.prototype.handleMapBrowserEvent = function(mapBrowserEvent) {
if (goog.isNull(this.frameState_)) {
// With no view defined, we cannot translate pixels into geographical
// coordinates so interactions cannot be used.
return;
}
mapBrowserEvent.frameState = this.frameState_;
var interactions = this.getInteractions();
var interactionsArray = /** @type {Array.<ol.interaction.Interaction>} */