Add rotation constraints

This commit is contained in:
Tom Payne
2012-08-04 15:50:06 +02:00
parent 0fd122a1ed
commit 7ab3b3dd82
6 changed files with 63 additions and 14 deletions

View File

@@ -14,6 +14,7 @@ goog.require('ol.control.KeyboardPan');
goog.require('ol.control.KeyboardZoom');
goog.require('ol.control.MouseWheelZoom');
goog.require('ol.control.ResolutionConstraint');
goog.require('ol.control.RotationConstraint');
goog.require('ol.control.ShiftDragRotateAndZoom');
goog.require('ol.control.ShiftDragZoom');
goog.require('ol.dom');
@@ -82,8 +83,9 @@ ol.createMap = function(target, opt_values, opt_rendererHints) {
var centerConstraint = ol.control.CenterConstraint.snapToPixel;
var resolutionConstraint = ol.control.ResolutionConstraint.createSnapToPower(
2, ol.Projection.EPSG_3857_HALF_SIZE / 128);
var rotationConstraint = ol.control.RotationConstraint.none;
var constraints = new ol.control.Constraints(
centerConstraint, resolutionConstraint);
centerConstraint, resolutionConstraint, rotationConstraint);
if (!goog.object.containsKey(values, ol.MapProperty.CONTROLS)) {
var controls = new ol.Collection();

View File

@@ -1,9 +1,8 @@
// FIXME add rotation constraint
goog.provide('ol.control.Constraints');
goog.require('ol.control.CenterConstraintType');
goog.require('ol.control.ResolutionConstraintType');
goog.require('ol.control.RotationConstraintType');
@@ -12,8 +11,11 @@ goog.require('ol.control.ResolutionConstraintType');
* @param {ol.control.CenterConstraintType} centerConstraint Center constraint.
* @param {ol.control.ResolutionConstraintType} resolutionConstraint
* Resolution constraint.
* @param {ol.control.RotationConstraintType} rotationConstraint
* Rotation constraint.
*/
ol.control.Constraints = function(centerConstraint, resolutionConstraint) {
ol.control.Constraints =
function(centerConstraint, resolutionConstraint, rotationConstraint) {
/**
* @type {ol.control.CenterConstraintType}
@@ -25,4 +27,9 @@ ol.control.Constraints = function(centerConstraint, resolutionConstraint) {
*/
this.resolution = resolutionConstraint;
/**
* @type {ol.control.RotationConstraintType}
*/
this.rotation = rotationConstraint;
};

View File

@@ -59,21 +59,22 @@ ol.Control.prototype.pan = function(map, delta, opt_anchor) {
/**
* @param {ol.Map} map Map.
* @param {number|undefined} resolution Resolution.
* @param {number|undefined} rotation Rotation.
* @param {number} delta Delta.
*/
ol.Control.prototype.setResolution = function(map, resolution) {
resolution = this.constraints.resolution(resolution, 0);
map.setResolution(resolution);
ol.Control.prototype.rotate = function(map, rotation, delta) {
rotation = this.constraints.rotation(rotation, delta);
map.setRotation(rotation);
};
/**
* @param {ol.Map} map Map.
* @param {number} rotation Rotation.
* @param {number|undefined} resolution Resolution.
*/
ol.Control.prototype.setRotation = function(map, rotation) {
// FIXME use a constraint
map.setRotation(rotation);
ol.Control.prototype.setResolution = function(map, resolution) {
resolution = this.constraints.resolution(resolution, 0);
map.setResolution(resolution);
};

View File

@@ -0,0 +1,39 @@
goog.provide('ol.control.RotationConstraint');
goog.provide('ol.control.RotationConstraintType');
/**
* @typedef {function((number|undefined), number): (number|undefined)}
*/
ol.control.RotationConstraintType;
/**
* @param {number|undefined} rotation Rotation.
* @param {number} delta Delta.
* @return {number|undefined} Rotation.
*/
ol.control.RotationConstraint.none = function(rotation, delta) {
if (goog.isDef(rotation)) {
return rotation + delta;
} else {
return undefined;
}
};
/**
* @param {number} n N.
* @return {ol.control.RotationConstraintType} Rotation constraint.
*/
ol.control.RotationConstraint.createSnapToN = function(n) {
var theta = 2 * Math.PI / n;
return function(rotation, delta) {
if (goog.isDef(rotation)) {
rotation = Math.floor((rotation + delta) / theta + 0.5) * theta;
return rotation;
} else {
return undefined;
}
};
};

View File

@@ -33,7 +33,7 @@ ol.control.ShiftDragRotate.prototype.handleDrag = function(mapBrowserEvent) {
var theta = Math.atan2(
size.height / 2 - browserEvent.offsetY,
browserEvent.offsetX - size.width / 2);
map.setRotation(this.startRotation_ - theta);
this.rotate(map, this.startRotation_, -theta);
};

View File

@@ -45,7 +45,7 @@ ol.control.ShiftDragRotateAndZoom.prototype.handleDrag =
size.height / 2 - browserEvent.offsetY);
var theta = Math.atan2(delta.y, delta.x);
// FIXME this should use map.withFrozenRendering but an assertion fails :-(
this.setRotation(map, this.startRotation_ - theta);
this.rotate(map, this.startRotation_, -theta);
var resolution = this.startRatio_ * delta.magnitude();
this.setResolution(map, resolution);
};