Add ol.interaction.DragBox

This commit is contained in:
Éric Lemoine
2013-12-05 11:11:23 +01:00
committed by Antoine Abt
parent 91a61f3152
commit 6b305cd146
3 changed files with 111 additions and 0 deletions

View File

@@ -312,6 +312,15 @@
* @todo stability experimental
*/
/**
* @typedef {Object} olx.interaction.DragBoxOptions
* @property {function(ol.Map, ol.geom.Polygon)} behavior Behavior function.
* @property {ol.events.ConditionType|undefined} condition A conditional
* modifier (i.e. Shift key) that determines if the interaction is active
* or not, default is always.
* @todo stability experimental
*/
/**
* @typedef {Object} olx.interaction.DragZoomOptions
* @property {ol.events.ConditionType|undefined} condition A conditional

View File

@@ -0,0 +1 @@
@exportSymbol ol.interaction.DragBox

View File

@@ -0,0 +1,101 @@
// FIXME draw drag box
// FIXME works for View2D only
goog.provide('ol.interaction.DragBox');
goog.require('goog.asserts');
goog.require('ol.events.ConditionType');
goog.require('ol.events.condition');
goog.require('ol.interaction.Drag');
goog.require('ol.render.Box');
/**
* @define {number} Hysterisis pixels.
*/
ol.DRAG_BOX_HYSTERESIS_PIXELS = 8;
/**
* @const {number}
*/
ol.DRAG_BOX_HYSTERESIS_PIXELS_SQUARED =
ol.DRAG_BOX_HYSTERESIS_PIXELS *
ol.DRAG_BOX_HYSTERESIS_PIXELS;
/**
* @constructor
* @extends {ol.interaction.Drag}
* @param {olx.interaction.DragBoxOptions=} opt_options Options.
* @todo stability experimental
*/
ol.interaction.DragBox = function(opt_options) {
goog.base(this);
var options = goog.isDef(opt_options) ? opt_options : {};
/**
* @private
* @type {function(ol.Map, ol.geom.Polygon)}
*/
this.behavior_ = goog.isDef(options.behavior) ?
options.behavior : goog.nullFunction;
/**
* @type {ol.render.Box}
* @private
*/
this.box_ = new ol.render.Box();
/**
* @private
* @type {ol.events.ConditionType}
*/
this.condition_ = goog.isDef(options.condition) ?
options.condition : ol.events.condition.always;
};
goog.inherits(ol.interaction.DragBox, ol.interaction.Drag);
/**
* @inheritDoc
*/
ol.interaction.DragBox.prototype.handleDrag = function(mapBrowserEvent) {
this.box_.setCoordinates(
this.startCoordinate, mapBrowserEvent.getCoordinate());
};
/**
* @inheritDoc
*/
ol.interaction.DragBox.prototype.handleDragEnd =
function(mapBrowserEvent) {
this.box_.setMap(null);
if (this.deltaX * this.deltaX + this.deltaY * this.deltaY >=
ol.DRAG_BOX_HYSTERESIS_PIXELS_SQUARED) {
var map = mapBrowserEvent.map;
var geometry = this.box_.getGeometry();
this.behavior_(map, geometry);
}
};
/**
* @inheritDoc
*/
ol.interaction.DragBox.prototype.handleDragStart =
function(mapBrowserEvent) {
var browserEvent = mapBrowserEvent.browserEvent;
if (browserEvent.isMouseActionButton() && this.condition_(mapBrowserEvent)) {
this.box_.setCoordinates(this.startCoordinate, this.startCoordinate);
this.box_.setMap(mapBrowserEvent.map);
return true;
} else {
return false;
}
};