diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index 8c6ea96f23..d6f41a4461 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -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 diff --git a/src/ol/interaction/dragboxinteraction.exports b/src/ol/interaction/dragboxinteraction.exports new file mode 100644 index 0000000000..13c1329169 --- /dev/null +++ b/src/ol/interaction/dragboxinteraction.exports @@ -0,0 +1 @@ +@exportSymbol ol.interaction.DragBox diff --git a/src/ol/interaction/dragboxinteraction.js b/src/ol/interaction/dragboxinteraction.js new file mode 100644 index 0000000000..933f07d303 --- /dev/null +++ b/src/ol/interaction/dragboxinteraction.js @@ -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; + } +};