Make DragZoom interaction inherit from DragBox

This commit is contained in:
Éric Lemoine
2013-12-05 11:12:52 +01:00
committed by Antoine Abt
parent 6b305cd146
commit 0960584cfc

View File

@@ -1,30 +1,10 @@
// FIXME draw drag box
// FIXME works for View2D only
goog.provide('ol.interaction.DragZoom');
goog.require('goog.asserts');
goog.require('ol.Size');
goog.require('ol.View2D');
goog.require('ol.events.ConditionType');
goog.require('ol.events.condition');
goog.require('ol.extent');
goog.require('ol.interaction.Drag');
goog.require('ol.render.DragBox');
/**
* @define {number} Hysterisis pixels.
*/
ol.SHIFT_DRAG_ZOOM_HYSTERESIS_PIXELS = 8;
/**
* @const {number}
*/
ol.SHIFT_DRAG_ZOOM_HYSTERESIS_PIXELS_SQUARED =
ol.SHIFT_DRAG_ZOOM_HYSTERESIS_PIXELS *
ol.SHIFT_DRAG_ZOOM_HYSTERESIS_PIXELS;
goog.require('ol.interaction.DragBox');
@@ -33,77 +13,49 @@ ol.SHIFT_DRAG_ZOOM_HYSTERESIS_PIXELS_SQUARED =
* normally combined with an {@link ol.events.condition} that limits
* it to when the shift key is held down.
* @constructor
* @extends {ol.interaction.Drag}
* @extends {ol.interaction.DragBox}
* @param {olx.interaction.DragZoomOptions=} opt_options Options.
* @todo stability experimental
*/
ol.interaction.DragZoom = function(opt_options) {
goog.base(this);
var options = goog.isDef(opt_options) ? opt_options : {};
/**
* @private
* @type {ol.events.ConditionType}
*/
this.condition_ = goog.isDef(options.condition) ?
var condition = goog.isDef(options.condition) ?
options.condition : ol.events.condition.shiftKeyOnly;
/**
* @type {ol.render.DragBox}
* @private
* @type {function(ol.Map, ol.geom.Polygon)}
*/
this.dragBox_ = new ol.render.DragBox();
var behavior = (
/**
* @param {ol.Map} map Map.
* @param {ol.geom.Polygon} polygon Polugon.
*/
function(map, polygon) {
map.withFrozenRendering(function() {
// FIXME works for View2D only
var view = map.getView();
goog.asserts.assertInstanceof(view, ol.View2D);
var linearRings = polygon.getLinearRings();
goog.asserts.assert(linearRings.length == 2);
var innerLinearRing = linearRings[1];
var innerLinearRingExtent = innerLinearRing.getExtent();
var mapSize = /** @type {ol.Size} */ (map.getSize());
map.withFrozenRendering(function() {
view.fitExtent(innerLinearRingExtent, mapSize);
// FIXME we should preserve rotation
view.setRotation(0);
});
});
});
goog.base(this, {
behavior: behavior,
condition: condition
});
};
goog.inherits(ol.interaction.DragZoom, ol.interaction.Drag);
/**
* @inheritDoc
*/
ol.interaction.DragZoom.prototype.handleDrag = function(mapBrowserEvent) {
this.dragBox_.setCoordinates(
this.startCoordinate, mapBrowserEvent.getCoordinate());
};
/**
* @inheritDoc
*/
ol.interaction.DragZoom.prototype.handleDragEnd =
function(mapBrowserEvent) {
this.dragBox_.setMap(null);
if (this.deltaX * this.deltaX + this.deltaY * this.deltaY >=
ol.SHIFT_DRAG_ZOOM_HYSTERESIS_PIXELS_SQUARED) {
var map = mapBrowserEvent.map;
var extent = ol.extent.boundingExtent(
[this.startCoordinate, mapBrowserEvent.getCoordinate()]);
map.withFrozenRendering(function() {
// FIXME works for View2D only
var view = map.getView();
goog.asserts.assertInstanceof(view, ol.View2D);
var mapSize = /** @type {ol.Size} */ (map.getSize());
view.fitExtent(extent, mapSize);
// FIXME we should preserve rotation
view.setRotation(0);
});
}
};
/**
* @inheritDoc
*/
ol.interaction.DragZoom.prototype.handleDragStart =
function(mapBrowserEvent) {
var browserEvent = mapBrowserEvent.browserEvent;
if (browserEvent.isMouseActionButton() && this.condition_(mapBrowserEvent)) {
this.dragBox_.setCoordinates(this.startCoordinate, this.startCoordinate);
this.dragBox_.setMap(mapBrowserEvent.map);
return true;
} else {
return false;
}
};
goog.inherits(ol.interaction.DragZoom, ol.interaction.DragBox);