Merge pull request #27 from elemoine/constraints
Refactoring to make constraints work at the map level
This commit is contained in:
27
src/ol/constraints.js
Normal file
27
src/ol/constraints.js
Normal file
@@ -0,0 +1,27 @@
|
||||
goog.provide('ol.Constraints');
|
||||
|
||||
goog.require('ol.ResolutionConstraintType');
|
||||
goog.require('ol.RotationConstraintType');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param {ol.ResolutionConstraintType} resolutionConstraint
|
||||
* Resolution constraint.
|
||||
* @param {ol.RotationConstraintType} rotationConstraint
|
||||
* Rotation constraint.
|
||||
*/
|
||||
ol.Constraints = function(resolutionConstraint, rotationConstraint) {
|
||||
|
||||
/**
|
||||
* @type {ol.ResolutionConstraintType}
|
||||
*/
|
||||
this.resolution = resolutionConstraint;
|
||||
|
||||
/**
|
||||
* @type {ol.RotationConstraintType}
|
||||
*/
|
||||
this.rotation = rotationConstraint;
|
||||
|
||||
};
|
||||
@@ -1,3 +1,5 @@
|
||||
// FIXME: remove constraints from here
|
||||
|
||||
goog.provide('ol.control.Zoom');
|
||||
|
||||
goog.require('goog.dom');
|
||||
@@ -5,6 +7,7 @@ goog.require('goog.dom.TagName');
|
||||
goog.require('goog.events');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.Projection');
|
||||
goog.require('ol.ResolutionConstraint');
|
||||
goog.require('ol.control.Control');
|
||||
|
||||
|
||||
@@ -30,9 +33,8 @@ ol.control.Zoom = function(map, opt_resolutions) {
|
||||
* @type {Function}
|
||||
* @private
|
||||
*/
|
||||
this.constraint_ =
|
||||
ol.interaction.ResolutionConstraint.createSnapToResolutions(
|
||||
opt_resolutions);
|
||||
this.constraint_ = ol.ResolutionConstraint.createSnapToResolutions(
|
||||
opt_resolutions);
|
||||
|
||||
/**
|
||||
* @type {Element}
|
||||
|
||||
@@ -8,11 +8,10 @@ goog.require('ol.interaction.Drag');
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.interaction.Drag}
|
||||
* @param {ol.interaction.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.interaction.AltDragRotate = function(constraints) {
|
||||
ol.interaction.AltDragRotate = function() {
|
||||
|
||||
goog.base(this, constraints);
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -34,7 +33,7 @@ ol.interaction.AltDragRotate.prototype.handleDrag = function(mapBrowserEvent) {
|
||||
var theta = Math.atan2(
|
||||
size.height / 2 - browserEvent.offsetY,
|
||||
browserEvent.offsetX - size.width / 2);
|
||||
this.rotate(map, this.startRotation_, -theta);
|
||||
map.rotate(this.startRotation_, -theta);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
goog.provide('ol.interaction.Constraints');
|
||||
|
||||
goog.require('ol.interaction.CenterConstraintType');
|
||||
goog.require('ol.interaction.ResolutionConstraintType');
|
||||
goog.require('ol.interaction.RotationConstraintType');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param {ol.interaction.CenterConstraintType} centerConstraint
|
||||
* Center constraint.
|
||||
* @param {ol.interaction.ResolutionConstraintType} resolutionConstraint
|
||||
* Resolution constraint.
|
||||
* @param {ol.interaction.RotationConstraintType} rotationConstraint
|
||||
* Rotation constraint.
|
||||
*/
|
||||
ol.interaction.Constraints =
|
||||
function(centerConstraint, resolutionConstraint, rotationConstraint) {
|
||||
|
||||
/**
|
||||
* @type {ol.interaction.CenterConstraintType}
|
||||
*/
|
||||
this.center = centerConstraint;
|
||||
|
||||
/**
|
||||
* @type {ol.interaction.ResolutionConstraintType}
|
||||
*/
|
||||
this.resolution = resolutionConstraint;
|
||||
|
||||
/**
|
||||
* @type {ol.interaction.RotationConstraintType}
|
||||
*/
|
||||
this.rotation = rotationConstraint;
|
||||
|
||||
};
|
||||
@@ -2,7 +2,6 @@ goog.provide('ol.interaction.DblClickZoom');
|
||||
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.MapBrowserEvent.EventType');
|
||||
goog.require('ol.interaction.Constraints');
|
||||
goog.require('ol.interaction.Interaction');
|
||||
|
||||
|
||||
@@ -10,10 +9,9 @@ goog.require('ol.interaction.Interaction');
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.interaction.Interaction}
|
||||
* @param {ol.interaction.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.interaction.DblClickZoom = function(constraints) {
|
||||
goog.base(this, constraints);
|
||||
ol.interaction.DblClickZoom = function() {
|
||||
goog.base(this);
|
||||
};
|
||||
goog.inherits(ol.interaction.DblClickZoom, ol.interaction.Interaction);
|
||||
|
||||
@@ -27,10 +25,9 @@ ol.interaction.DblClickZoom.prototype.handleMapBrowserEvent =
|
||||
if (mapBrowserEvent.type == ol.MapBrowserEvent.EventType.DBLCLICK &&
|
||||
mapBrowserEvent.isMouseActionButton()) {
|
||||
var map = mapBrowserEvent.map;
|
||||
var resolution = map.getResolution();
|
||||
var delta = mapBrowserEvent.browserEvent.shiftKey ? -1 : 1;
|
||||
var anchor = mapBrowserEvent.getCoordinate();
|
||||
this.zoom(map, resolution, delta, anchor);
|
||||
var delta = mapBrowserEvent.browserEvent.shiftKey ? -4 : 4;
|
||||
map.zoom(delta, anchor);
|
||||
mapBrowserEvent.preventDefault();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -6,7 +6,6 @@ goog.require('goog.events.EventType');
|
||||
goog.require('goog.functions');
|
||||
goog.require('ol.Coordinate');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.interaction.Constraints');
|
||||
goog.require('ol.interaction.Interaction');
|
||||
|
||||
|
||||
@@ -14,11 +13,10 @@ goog.require('ol.interaction.Interaction');
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.interaction.Interaction}
|
||||
* @param {ol.interaction.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.interaction.Drag = function(constraints) {
|
||||
ol.interaction.Drag = function() {
|
||||
|
||||
goog.base(this, constraints);
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
|
||||
@@ -2,7 +2,6 @@ goog.provide('ol.interaction.DragPan');
|
||||
|
||||
goog.require('ol.Coordinate');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.interaction.Constraints');
|
||||
goog.require('ol.interaction.Drag');
|
||||
|
||||
|
||||
@@ -10,10 +9,9 @@ goog.require('ol.interaction.Drag');
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.interaction.Drag}
|
||||
* @param {ol.interaction.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.interaction.DragPan = function(constraints) {
|
||||
goog.base(this, constraints);
|
||||
ol.interaction.DragPan = function() {
|
||||
goog.base(this);
|
||||
};
|
||||
goog.inherits(ol.interaction.DragPan, ol.interaction.Drag);
|
||||
|
||||
@@ -30,7 +28,9 @@ ol.interaction.DragPan.prototype.handleDrag = function(mapBrowserEvent) {
|
||||
if (map.canRotate() && goog.isDef(rotation)) {
|
||||
delta.rotate(rotation);
|
||||
}
|
||||
this.pan(map, delta, this.startCenter);
|
||||
var newCenter = new ol.Coordinate(
|
||||
this.startCenter.x + delta.x, this.startCenter.y + delta.y);
|
||||
map.setCenter(newCenter);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -3,38 +3,13 @@
|
||||
goog.provide('ol.interaction.Interaction');
|
||||
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.interaction.Constraints');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param {ol.interaction.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.interaction.Interaction = function(constraints) {
|
||||
|
||||
/**
|
||||
* @protected
|
||||
* @type {ol.interaction.Constraints}
|
||||
*/
|
||||
this.constraints = constraints;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Map} map Map.
|
||||
* @param {ol.Extent} extent Extent.
|
||||
*/
|
||||
ol.interaction.Interaction.prototype.fitExtent = function(map, extent) {
|
||||
var resolution = map.getResolutionForExtent(extent);
|
||||
resolution = this.constraints.resolution(resolution, 0);
|
||||
var center = extent.getCenter();
|
||||
center = this.constraints.center(center, resolution, ol.Coordinate.ZERO);
|
||||
map.withFrozenRendering(function() {
|
||||
map.setCenter(center);
|
||||
map.setResolution(resolution);
|
||||
});
|
||||
ol.interaction.Interaction = function() {
|
||||
};
|
||||
|
||||
|
||||
@@ -43,69 +18,3 @@ ol.interaction.Interaction.prototype.fitExtent = function(map, extent) {
|
||||
*/
|
||||
ol.interaction.Interaction.prototype.handleMapBrowserEvent =
|
||||
goog.abstractMethod;
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Map} map Map.
|
||||
* @param {ol.Coordinate} delta Delta.
|
||||
* @param {ol.Coordinate=} opt_anchor Anchor.
|
||||
*/
|
||||
ol.interaction.Interaction.prototype.pan = function(map, delta, opt_anchor) {
|
||||
var center = opt_anchor ? opt_anchor : map.getCenter();
|
||||
var resolution = map.getResolution();
|
||||
center = this.constraints.center(center, resolution, delta);
|
||||
map.setCenter(center);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Map} map Map.
|
||||
* @param {number|undefined} rotation Rotation.
|
||||
* @param {number} delta Delta.
|
||||
* @param {ol.Coordinate=} opt_anchor Anchor.
|
||||
*/
|
||||
ol.interaction.Interaction.prototype.rotate = function(
|
||||
map, rotation, delta, opt_anchor) {
|
||||
// FIXME handle rotation about anchor
|
||||
rotation = this.constraints.rotation(rotation, delta);
|
||||
map.setRotation(rotation);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Map} map Map.
|
||||
* @param {number|undefined} resolution Resolution.
|
||||
*/
|
||||
ol.interaction.Interaction.prototype.setResolution = function(map, resolution) {
|
||||
resolution = this.constraints.resolution(resolution, 0);
|
||||
map.setResolution(resolution);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Map} map Map.
|
||||
* @param {number|undefined} resolution Resolution.
|
||||
* @param {number} delta Delta.
|
||||
* @param {ol.Coordinate=} opt_anchor Anchor.
|
||||
*/
|
||||
ol.interaction.Interaction.prototype.zoom = function(
|
||||
map, resolution, delta, opt_anchor) {
|
||||
if (goog.isDefAndNotNull(opt_anchor)) {
|
||||
var anchor = opt_anchor;
|
||||
var mapCenter = /** @type {!ol.Coordinate} */ map.getCenter();
|
||||
var mapResolution = map.getResolution();
|
||||
resolution = this.constraints.resolution(resolution, delta);
|
||||
var x = anchor.x - resolution * (anchor.x - mapCenter.x) / mapResolution;
|
||||
var y = anchor.y - resolution * (anchor.y - mapCenter.y) / mapResolution;
|
||||
var center = new ol.Coordinate(x, y);
|
||||
center = this.constraints.center(center, resolution, ol.Coordinate.ZERO);
|
||||
map.withFrozenRendering(function() {
|
||||
map.setCenter(center);
|
||||
map.setResolution(resolution);
|
||||
});
|
||||
} else {
|
||||
resolution = this.constraints.resolution(resolution, delta);
|
||||
map.setResolution(resolution);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ goog.require('ol.interaction.Interaction');
|
||||
*/
|
||||
ol.interaction.Keyboard = function() {
|
||||
|
||||
goog.base(this, null);
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
|
||||
@@ -2,7 +2,6 @@ goog.provide('ol.interaction.KeyboardPan');
|
||||
|
||||
goog.require('goog.events.KeyCodes');
|
||||
goog.require('goog.events.KeyHandler.EventType');
|
||||
goog.require('ol.interaction.Constraints');
|
||||
goog.require('ol.interaction.Interaction');
|
||||
|
||||
|
||||
@@ -10,12 +9,11 @@ goog.require('ol.interaction.Interaction');
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.interaction.Interaction}
|
||||
* @param {ol.interaction.Constraints} constraints Constraints.
|
||||
* @param {number} pixelDelta Pixel delta.
|
||||
*/
|
||||
ol.interaction.KeyboardPan = function(constraints, pixelDelta) {
|
||||
ol.interaction.KeyboardPan = function(pixelDelta) {
|
||||
|
||||
goog.base(this, constraints);
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -54,7 +52,10 @@ ol.interaction.KeyboardPan.prototype.handleMapBrowserEvent =
|
||||
goog.asserts.assert(keyCode == goog.events.KeyCodes.UP);
|
||||
delta = new ol.Coordinate(0, mapUnitsDelta);
|
||||
}
|
||||
this.pan(map, delta);
|
||||
var oldCenter = map.getCenter();
|
||||
var newCenter = new ol.Coordinate(
|
||||
oldCenter.x + delta.x, oldCenter.y + delta.y);
|
||||
map.setCenter(newCenter);
|
||||
keyEvent.preventDefault();
|
||||
mapBrowserEvent.preventDefault();
|
||||
}
|
||||
|
||||
@@ -3,17 +3,15 @@ goog.provide('ol.interaction.KeyboardZoom');
|
||||
goog.require('goog.events.KeyCodes');
|
||||
goog.require('goog.events.KeyHandler.EventType');
|
||||
goog.require('ol.interaction.Interaction');
|
||||
goog.require('ol.interaction.ResolutionConstraintType');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.interaction.Interaction}
|
||||
* @param {ol.interaction.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.interaction.KeyboardZoom = function(constraints) {
|
||||
goog.base(this, constraints);
|
||||
ol.interaction.KeyboardZoom = function() {
|
||||
goog.base(this);
|
||||
};
|
||||
goog.inherits(ol.interaction.KeyboardZoom, ol.interaction.Interaction);
|
||||
|
||||
@@ -29,9 +27,8 @@ ol.interaction.KeyboardZoom.prototype.handleMapBrowserEvent =
|
||||
var charCode = keyEvent.charCode;
|
||||
if (charCode == '+'.charCodeAt(0) || charCode == '-'.charCodeAt(0)) {
|
||||
var map = mapBrowserEvent.map;
|
||||
var resolution = map.getResolution();
|
||||
var delta = charCode == '+'.charCodeAt(0) ? 1 : -1;
|
||||
this.zoom(map, resolution, delta);
|
||||
var delta = (charCode == '+'.charCodeAt(0)) ? 4 : -4;
|
||||
map.zoom(delta);
|
||||
keyEvent.preventDefault();
|
||||
mapBrowserEvent.preventDefault();
|
||||
}
|
||||
|
||||
@@ -3,17 +3,15 @@ goog.provide('ol.interaction.MouseWheelZoom');
|
||||
goog.require('goog.events.MouseWheelEvent');
|
||||
goog.require('goog.events.MouseWheelHandler.EventType');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.interaction.Constraints');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.interaction.Interaction}
|
||||
* @param {ol.interaction.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.interaction.MouseWheelZoom = function(constraints) {
|
||||
goog.base(this, constraints);
|
||||
ol.interaction.MouseWheelZoom = function() {
|
||||
goog.base(this);
|
||||
};
|
||||
goog.inherits(ol.interaction.MouseWheelZoom, ol.interaction.Interaction);
|
||||
|
||||
@@ -29,13 +27,14 @@ ol.interaction.MouseWheelZoom.prototype.handleMapBrowserEvent =
|
||||
var mouseWheelEvent = /** @type {goog.events.MouseWheelEvent} */
|
||||
mapBrowserEvent.browserEvent;
|
||||
goog.asserts.assert(mouseWheelEvent instanceof goog.events.MouseWheelEvent);
|
||||
if (mouseWheelEvent.deltaY !== 0) {
|
||||
var delta = mouseWheelEvent.deltaY < 0 ? 1 : -1;
|
||||
var resolution = map.getResolution();
|
||||
var anchor = mapBrowserEvent.getCoordinate();
|
||||
this.zoom(map, resolution, delta, anchor);
|
||||
mapBrowserEvent.preventDefault();
|
||||
mouseWheelEvent.preventDefault();
|
||||
var anchor = mapBrowserEvent.getCoordinate();
|
||||
var oldResolution = map.getResolution();
|
||||
var factor = Math.exp(Math.log(2) / 4);
|
||||
if (mouseWheelEvent.deltaY < 0) {
|
||||
factor = 1 / factor;
|
||||
}
|
||||
map.zoomToResolution(oldResolution * factor, anchor);
|
||||
mapBrowserEvent.preventDefault();
|
||||
mouseWheelEvent.preventDefault();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -2,7 +2,6 @@ goog.provide('ol.interaction.ShiftDragRotateAndZoom');
|
||||
|
||||
goog.require('goog.math.Vec2');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.interaction.Constraints');
|
||||
goog.require('ol.interaction.Drag');
|
||||
|
||||
|
||||
@@ -10,11 +9,10 @@ goog.require('ol.interaction.Drag');
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.interaction.Drag}
|
||||
* @param {ol.interaction.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.interaction.ShiftDragRotateAndZoom = function(constraints) {
|
||||
ol.interaction.ShiftDragRotateAndZoom = function() {
|
||||
|
||||
goog.base(this, constraints);
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -45,9 +43,9 @@ ol.interaction.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.rotate(map, this.startRotation_, -theta);
|
||||
map.rotate(this.startRotation_, -theta);
|
||||
var resolution = this.startRatio_ * delta.magnitude();
|
||||
this.setResolution(map, resolution);
|
||||
map.zoomToResolution(resolution);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ goog.provide('ol.interaction.ShiftDragZoom');
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
goog.require('ol.control.DragBox');
|
||||
goog.require('ol.interaction.Constraints');
|
||||
goog.require('ol.interaction.Drag');
|
||||
|
||||
|
||||
@@ -27,10 +26,9 @@ ol.SHIFT_DRAG_ZOOM_HYSTERESIS_PIXELS_SQUARED =
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.interaction.Drag}
|
||||
* @param {ol.interaction.Constraints} constraints Constraints.
|
||||
*/
|
||||
ol.interaction.ShiftDragZoom = function(constraints) {
|
||||
goog.base(this, constraints);
|
||||
ol.interaction.ShiftDragZoom = function() {
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @type {ol.control.DragBox}
|
||||
@@ -54,7 +52,7 @@ ol.interaction.ShiftDragZoom.prototype.handleDragEnd =
|
||||
var extent = ol.Extent.boundingExtent(
|
||||
this.startCoordinate,
|
||||
mapBrowserEvent.getCoordinate());
|
||||
this.fitExtent(map, extent);
|
||||
map.fitExtent(extent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ goog.require('goog.fx.anim.Animated');
|
||||
goog.require('goog.object');
|
||||
goog.require('ol.Collection');
|
||||
goog.require('ol.Color');
|
||||
goog.require('ol.Constraints');
|
||||
goog.require('ol.Coordinate');
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.MapBrowserEvent');
|
||||
@@ -134,6 +135,12 @@ ol.Map = function(container, mapOptionsLiteral) {
|
||||
*/
|
||||
this.container_ = container;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.Constraints}
|
||||
*/
|
||||
this.constraints_ = mapOptions.constraints;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Element}
|
||||
@@ -214,7 +221,9 @@ ol.Map.prototype.canRotate = function() {
|
||||
ol.Map.prototype.fitExtent = function(extent) {
|
||||
this.withFrozenRendering(function() {
|
||||
this.setCenter(extent.getCenter());
|
||||
this.setResolution(this.getResolutionForExtent(extent));
|
||||
var resolution = this.getResolutionForExtent(extent);
|
||||
resolution = this.constraints_.resolution(resolution, 0);
|
||||
this.setResolution(resolution);
|
||||
if (this.canRotate()) {
|
||||
this.setRotation(0);
|
||||
}
|
||||
@@ -611,6 +620,16 @@ ol.Map.prototype.renderFrame_ = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number|undefined} rotation Rotation.
|
||||
* @param {number} delta Delta.
|
||||
*/
|
||||
ol.Map.prototype.rotate = function(rotation, delta) {
|
||||
rotation = this.constraints_.rotation(rotation, delta);
|
||||
this.setRotation(rotation);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.Color} backgroundColor Background color.
|
||||
*/
|
||||
@@ -787,3 +806,46 @@ ol.Map.prototype.withFrozenRendering = function(f, opt_obj) {
|
||||
this.unfreezeRendering();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {number|undefined} resolution Resolution to go to.
|
||||
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
|
||||
*/
|
||||
ol.Map.prototype.zoom_ = function(resolution, opt_anchor) {
|
||||
if (goog.isDefAndNotNull(resolution) && goog.isDefAndNotNull(opt_anchor)) {
|
||||
var anchor = opt_anchor;
|
||||
var oldCenter = /** @type {!ol.Coordinate} */ this.getCenter();
|
||||
var oldResolution = this.getResolution();
|
||||
var x = anchor.x - resolution * (anchor.x - oldCenter.x) / oldResolution;
|
||||
var y = anchor.y - resolution * (anchor.y - oldCenter.y) / oldResolution;
|
||||
var center = new ol.Coordinate(x, y);
|
||||
this.withFrozenRendering(function() {
|
||||
this.setCenter(center);
|
||||
this.setResolution(resolution);
|
||||
}, this);
|
||||
} else {
|
||||
this.setResolution(resolution);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} delta Delta from previous zoom level.
|
||||
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
|
||||
*/
|
||||
ol.Map.prototype.zoom = function(delta, opt_anchor) {
|
||||
var resolution = this.constraints_.resolution(this.getResolution(), delta);
|
||||
this.zoom_(resolution, opt_anchor);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number|undefined} resolution Resolution to go to.
|
||||
* @param {ol.Coordinate=} opt_anchor Anchor coordinate.
|
||||
*/
|
||||
ol.Map.prototype.zoomToResolution = function(resolution, opt_anchor) {
|
||||
resolution = this.constraints_.resolution(resolution, 0);
|
||||
this.zoom_(resolution, opt_anchor);
|
||||
};
|
||||
|
||||
@@ -4,17 +4,17 @@ goog.provide('ol.MapOptionsType');
|
||||
goog.provide('ol.RendererHint');
|
||||
|
||||
goog.require('ol.Collection');
|
||||
goog.require('ol.Constraints');
|
||||
goog.require('ol.Projection');
|
||||
goog.require('ol.ResolutionConstraint');
|
||||
goog.require('ol.RotationConstraint');
|
||||
goog.require('ol.interaction.AltDragRotate');
|
||||
goog.require('ol.interaction.CenterConstraint');
|
||||
goog.require('ol.interaction.Constraints');
|
||||
goog.require('ol.interaction.DblClickZoom');
|
||||
goog.require('ol.interaction.DragPan');
|
||||
goog.require('ol.interaction.KeyboardPan');
|
||||
goog.require('ol.interaction.KeyboardZoom');
|
||||
goog.require('ol.interaction.MouseWheelZoom');
|
||||
goog.require('ol.interaction.ResolutionConstraint');
|
||||
goog.require('ol.interaction.RotationConstraint');
|
||||
goog.require('ol.interaction.ShiftDragZoom');
|
||||
goog.require('ol.renderer.Map');
|
||||
goog.require('ol.renderer.dom');
|
||||
@@ -151,14 +151,34 @@ ol.MapOptions.create = function(mapOptionsLiteral) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {ol.Constraints}
|
||||
*/
|
||||
var constraints = ol.MapOptions.createConstraints_(mapOptionsLiteral);
|
||||
|
||||
return {
|
||||
rendererConstructor: rendererConstructor,
|
||||
constraints: constraints,
|
||||
values: values
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {ol.MapOptionsLiteral} mapOptionsLiteral Map options literal.
|
||||
* @return {ol.Constraints} Map constraints.
|
||||
*/
|
||||
ol.MapOptions.createConstraints_ = function(mapOptionsLiteral) {
|
||||
// FIXME this should be configurable
|
||||
var resolutionConstraint = ol.ResolutionConstraint.createSnapToPower(
|
||||
Math.exp(Math.log(2) / 4), ol.Projection.EPSG_3857_HALF_SIZE / 128);
|
||||
var rotationConstraint = ol.RotationConstraint.none;
|
||||
return new ol.Constraints(resolutionConstraint, rotationConstraint);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {ol.MapOptionsLiteral} mapOptionsLiteral Map options literal.
|
||||
@@ -166,33 +186,24 @@ ol.MapOptions.create = function(mapOptionsLiteral) {
|
||||
*/
|
||||
ol.MapOptions.createInteractions_ = function(mapOptionsLiteral) {
|
||||
|
||||
// FIXME this should be a configuration option
|
||||
var centerConstraint = ol.interaction.CenterConstraint.snapToPixel;
|
||||
var resolutionConstraint =
|
||||
ol.interaction.ResolutionConstraint.createSnapToPower(
|
||||
Math.exp(Math.log(2) / 8), ol.Projection.EPSG_3857_HALF_SIZE / 128);
|
||||
var rotationConstraint = ol.interaction.RotationConstraint.none;
|
||||
var constraints = new ol.interaction.Constraints(
|
||||
centerConstraint, resolutionConstraint, rotationConstraint);
|
||||
|
||||
var interactions = new ol.Collection();
|
||||
|
||||
var rotate = goog.isDef(mapOptionsLiteral.rotate) ?
|
||||
mapOptionsLiteral.rotate : true;
|
||||
if (rotate) {
|
||||
interactions.push(new ol.interaction.AltDragRotate(constraints));
|
||||
interactions.push(new ol.interaction.AltDragRotate());
|
||||
}
|
||||
|
||||
var doubleClickZoom = goog.isDef(mapOptionsLiteral.doubleClickZoom) ?
|
||||
mapOptionsLiteral.doubleClickZoom : true;
|
||||
if (doubleClickZoom) {
|
||||
interactions.push(new ol.interaction.DblClickZoom(constraints));
|
||||
interactions.push(new ol.interaction.DblClickZoom());
|
||||
}
|
||||
|
||||
var dragPan = goog.isDef(mapOptionsLiteral.dragPan) ?
|
||||
mapOptionsLiteral.dragPan : true;
|
||||
if (dragPan) {
|
||||
interactions.push(new ol.interaction.DragPan(constraints));
|
||||
interactions.push(new ol.interaction.DragPan());
|
||||
}
|
||||
|
||||
var keyboard = goog.isDef(mapOptionsLiteral.keyboard) ?
|
||||
@@ -200,21 +211,20 @@ ol.MapOptions.createInteractions_ = function(mapOptionsLiteral) {
|
||||
var keyboardPanOffset = goog.isDef(mapOptionsLiteral.keyboardPanOffset) ?
|
||||
mapOptionsLiteral.keyboardPanOffset : 80;
|
||||
if (keyboard) {
|
||||
interactions.push(
|
||||
new ol.interaction.KeyboardPan(constraints, keyboardPanOffset));
|
||||
interactions.push(new ol.interaction.KeyboardZoom(constraints));
|
||||
interactions.push(new ol.interaction.KeyboardPan(keyboardPanOffset));
|
||||
interactions.push(new ol.interaction.KeyboardZoom());
|
||||
}
|
||||
|
||||
var mouseWheelZoom = goog.isDef(mapOptionsLiteral.mouseWheelZoom) ?
|
||||
mapOptionsLiteral.mouseWheelZoom : true;
|
||||
if (mouseWheelZoom) {
|
||||
interactions.push(new ol.interaction.MouseWheelZoom(constraints));
|
||||
interactions.push(new ol.interaction.MouseWheelZoom());
|
||||
}
|
||||
|
||||
var shiftDragZoom = goog.isDef(mapOptionsLiteral.shiftDragZoom) ?
|
||||
mapOptionsLiteral.shiftDragZoom : true;
|
||||
if (shiftDragZoom) {
|
||||
interactions.push(new ol.interaction.ShiftDragZoom(constraints));
|
||||
interactions.push(new ol.interaction.ShiftDragZoom());
|
||||
}
|
||||
|
||||
return interactions;
|
||||
|
||||
@@ -155,12 +155,12 @@ ol.renderer.dom.Map.prototype.resetLayersPane_ = function() {
|
||||
ol.renderer.dom.Map.prototype.setOrigin_ = function() {
|
||||
var center = this.map.getCenter();
|
||||
var resolution = this.map.getResolution();
|
||||
var targetSize = this.map.getSize();
|
||||
var targetWidth = targetSize.width;
|
||||
var targetHeight = targetSize.height;
|
||||
var mapSize = this.map.getSize();
|
||||
var mapWidth = mapSize.width;
|
||||
var mapHeight = mapSize.height;
|
||||
var origin = new ol.Coordinate(
|
||||
center.x - resolution * targetWidth / 2,
|
||||
center.y + resolution * targetHeight / 2);
|
||||
center.x - resolution * mapWidth / 2,
|
||||
center.y + resolution * mapHeight / 2);
|
||||
goog.object.forEach(this.layerRenderers, function(layerRenderer) {
|
||||
layerRenderer.setOrigin(origin);
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
goog.provide('ol.interaction.ResolutionConstraint');
|
||||
goog.provide('ol.interaction.ResolutionConstraintType');
|
||||
goog.provide('ol.ResolutionConstraint');
|
||||
goog.provide('ol.ResolutionConstraintType');
|
||||
|
||||
goog.require('goog.math');
|
||||
goog.require('ol.array');
|
||||
@@ -8,16 +8,16 @@ goog.require('ol.array');
|
||||
/**
|
||||
* @typedef {function((number|undefined), number): (number|undefined)}
|
||||
*/
|
||||
ol.interaction.ResolutionConstraintType;
|
||||
ol.ResolutionConstraintType;
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} power Power.
|
||||
* @param {number} maxResolution Maximum resolution.
|
||||
* @param {number=} opt_minResolution Minimum resolution.
|
||||
* @return {ol.interaction.ResolutionConstraintType} Zoom function.
|
||||
* @return {ol.ResolutionConstraintType} Zoom function.
|
||||
*/
|
||||
ol.interaction.ResolutionConstraint.createContinuous =
|
||||
ol.ResolutionConstraint.createContinuous =
|
||||
function(power, maxResolution, opt_minResolution) {
|
||||
var minResolution = opt_minResolution || 0;
|
||||
return function(resolution, delta) {
|
||||
@@ -33,9 +33,9 @@ ol.interaction.ResolutionConstraint.createContinuous =
|
||||
|
||||
/**
|
||||
* @param {Array.<number>} resolutions Resolutions.
|
||||
* @return {ol.interaction.ResolutionConstraintType} Zoom function.
|
||||
* @return {ol.ResolutionConstraintType} Zoom function.
|
||||
*/
|
||||
ol.interaction.ResolutionConstraint.createSnapToResolutions =
|
||||
ol.ResolutionConstraint.createSnapToResolutions =
|
||||
function(resolutions) {
|
||||
return function(resolution, delta) {
|
||||
if (goog.isDef(resolution)) {
|
||||
@@ -53,9 +53,9 @@ ol.interaction.ResolutionConstraint.createSnapToResolutions =
|
||||
* @param {number} power Power.
|
||||
* @param {number} maxResolution Maximum resolution.
|
||||
* @param {number=} opt_maxLevel Maximum level.
|
||||
* @return {ol.interaction.ResolutionConstraintType} Zoom function.
|
||||
* @return {ol.ResolutionConstraintType} Zoom function.
|
||||
*/
|
||||
ol.interaction.ResolutionConstraint.createSnapToPower =
|
||||
ol.ResolutionConstraint.createSnapToPower =
|
||||
function(power, maxResolution, opt_maxLevel) {
|
||||
return function(resolution, delta) {
|
||||
if (goog.isDef(resolution)) {
|
||||
@@ -1,11 +1,11 @@
|
||||
goog.provide('ol.interaction.RotationConstraint');
|
||||
goog.provide('ol.interaction.RotationConstraintType');
|
||||
goog.provide('ol.RotationConstraint');
|
||||
goog.provide('ol.RotationConstraintType');
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {function((number|undefined), number): (number|undefined)}
|
||||
*/
|
||||
ol.interaction.RotationConstraintType;
|
||||
ol.RotationConstraintType;
|
||||
|
||||
|
||||
/**
|
||||
@@ -13,7 +13,7 @@ ol.interaction.RotationConstraintType;
|
||||
* @param {number} delta Delta.
|
||||
* @return {number|undefined} Rotation.
|
||||
*/
|
||||
ol.interaction.RotationConstraint.none = function(rotation, delta) {
|
||||
ol.RotationConstraint.none = function(rotation, delta) {
|
||||
if (goog.isDef(rotation)) {
|
||||
return rotation + delta;
|
||||
} else {
|
||||
@@ -24,9 +24,9 @@ ol.interaction.RotationConstraint.none = function(rotation, delta) {
|
||||
|
||||
/**
|
||||
* @param {number} n N.
|
||||
* @return {ol.interaction.RotationConstraintType} Rotation constraint.
|
||||
* @return {ol.RotationConstraintType} Rotation constraint.
|
||||
*/
|
||||
ol.interaction.RotationConstraint.createSnapToN = function(n) {
|
||||
ol.RotationConstraint.createSnapToN = function(n) {
|
||||
var theta = 2 * Math.PI / n;
|
||||
return function(rotation, delta) {
|
||||
if (goog.isDef(rotation)) {
|
||||
@@ -80,7 +80,7 @@
|
||||
<script type="text/javascript" src="spec/ol/rectangle.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/tilerange.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/tileurlfunction.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/interaction/resolutionconstraint.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/resolutionconstraint.test.js"></script>
|
||||
<script type="text/javascript" src="spec/ol/layer/xyz.test.js"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
describe('ol.interaction.ResolutionConstraint', function() {
|
||||
describe('ol.ResolutionConstraint', function() {
|
||||
|
||||
describe('SnapToResolution', function() {
|
||||
|
||||
@@ -6,7 +6,7 @@ describe('ol.interaction.ResolutionConstraint', function() {
|
||||
|
||||
beforeEach(function() {
|
||||
resolutionConstraint =
|
||||
ol.interaction.ResolutionConstraint.createSnapToResolutions(
|
||||
ol.ResolutionConstraint.createSnapToResolutions(
|
||||
[1000, 500, 250, 100]);
|
||||
});
|
||||
|
||||
@@ -44,7 +44,7 @@ describe('ol.interaction.ResolutionConstraint', function() {
|
||||
|
||||
beforeEach(function() {
|
||||
resolutionConstraint =
|
||||
ol.interaction.ResolutionConstraint.createSnapToResolutions(
|
||||
ol.ResolutionConstraint.createSnapToResolutions(
|
||||
[1000, 500, 250, 100]);
|
||||
});
|
||||
|
||||
@@ -94,7 +94,7 @@ describe('ol.interaction.ResolutionConstraint', function() {
|
||||
|
||||
beforeEach(function() {
|
||||
resolutionConstraint =
|
||||
ol.interaction.ResolutionConstraint.createSnapToPower(2, 1024, 10);
|
||||
ol.ResolutionConstraint.createSnapToPower(2, 1024, 10);
|
||||
});
|
||||
|
||||
describe('delta 0', function() {
|
||||
@@ -152,7 +152,7 @@ describe('ol.interaction.ResolutionConstraint', function() {
|
||||
|
||||
beforeEach(function() {
|
||||
resolutionConstraint =
|
||||
ol.interaction.ResolutionConstraint.createSnapToPower(2, 1024, 10);
|
||||
ol.ResolutionConstraint.createSnapToPower(2, 1024, 10);
|
||||
});
|
||||
|
||||
describe('delta 0', function() {
|
||||
@@ -184,4 +184,4 @@ describe('ol.interaction.ResolutionConstraint', function() {
|
||||
});
|
||||
});
|
||||
|
||||
goog.require('ol.interaction.ResolutionConstraint');
|
||||
goog.require('ol.ResolutionConstraint');
|
||||
Reference in New Issue
Block a user