From 0b6e1e91c0c6a7f0e84080c5bbcddc9d4ecf6b63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 5 Dec 2013 09:40:50 +0100 Subject: [PATCH 01/15] ol.render.DragBox renamed to ol.render.Box --- src/ol/render/{dragbox.js => box.js} | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) rename src/ol/render/{dragbox.js => box.js} (86%) diff --git a/src/ol/render/dragbox.js b/src/ol/render/box.js similarity index 86% rename from src/ol/render/dragbox.js rename to src/ol/render/box.js index 80e938a0c8..1dc131c591 100644 --- a/src/ol/render/dragbox.js +++ b/src/ol/render/box.js @@ -1,6 +1,6 @@ // FIXME add rotation -goog.provide('ol.render.DragBox'); +goog.provide('ol.render.Box'); goog.require('goog.Disposable'); goog.require('goog.events'); @@ -16,7 +16,7 @@ goog.require('ol.style.Style'); * @extends {goog.Disposable} * @param {ol.style.Style=} opt_style Style. */ -ol.render.DragBox = function(opt_style) { +ol.render.Box = function(opt_style) { /** * @private @@ -57,13 +57,13 @@ ol.render.DragBox = function(opt_style) { }); }; -goog.inherits(ol.render.DragBox, goog.Disposable); +goog.inherits(ol.render.Box, goog.Disposable); /** * @inheritDoc */ -ol.render.DragBox.prototype.disposeInternal = function() { +ol.render.Box.prototype.disposeInternal = function() { this.setMap(null); }; @@ -72,7 +72,7 @@ ol.render.DragBox.prototype.disposeInternal = function() { * @param {ol.render.Event} event Event. * @private */ -ol.render.DragBox.prototype.handleMapPostCompose_ = function(event) { +ol.render.Box.prototype.handleMapPostCompose_ = function(event) { var render = event.getRender(); var startCoordinate = this.startCoordinate_; var endCoordinate = this.endCoordinate_; @@ -104,7 +104,7 @@ ol.render.DragBox.prototype.handleMapPostCompose_ = function(event) { /** * @private */ -ol.render.DragBox.prototype.requestMapRenderFrame_ = function() { +ol.render.Box.prototype.requestMapRenderFrame_ = function() { if (!goog.isNull(this.map_) && !goog.isNull(this.startCoordinate_) && !goog.isNull(this.endCoordinate_)) { @@ -116,7 +116,7 @@ ol.render.DragBox.prototype.requestMapRenderFrame_ = function() { /** * @param {ol.Map} map Map. */ -ol.render.DragBox.prototype.setMap = function(map) { +ol.render.Box.prototype.setMap = function(map) { if (goog.isDef(this.postComposeListenKey_)) { goog.events.unlistenByKey(this.postComposeListenKey_); this.postComposeListenKey_ = undefined; @@ -137,7 +137,7 @@ ol.render.DragBox.prototype.setMap = function(map) { * @param {ol.Coordinate} startCoordinate Start coordinate. * @param {ol.Coordinate} endCoordinate End coordinate. */ -ol.render.DragBox.prototype.setCoordinates = +ol.render.Box.prototype.setCoordinates = function(startCoordinate, endCoordinate) { this.startCoordinate_ = startCoordinate; this.endCoordinate_ = endCoordinate; From 712f451179ff5e3705bf249990a74ba4a3394c0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 5 Dec 2013 09:43:17 +0100 Subject: [PATCH 02/15] Use better type --- src/ol/render/box.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ol/render/box.js b/src/ol/render/box.js index 1dc131c591..14c80516d9 100644 --- a/src/ol/render/box.js +++ b/src/ol/render/box.js @@ -26,9 +26,9 @@ ol.render.Box = function(opt_style) { /** * @private - * @type {goog.events.Key|undefined} + * @type {goog.events.Key} */ - this.postComposeListenKey_ = undefined; + this.postComposeListenKey_ = null; /** * @private @@ -117,9 +117,9 @@ ol.render.Box.prototype.requestMapRenderFrame_ = function() { * @param {ol.Map} map Map. */ ol.render.Box.prototype.setMap = function(map) { - if (goog.isDef(this.postComposeListenKey_)) { + if (!goog.isNull(this.postComposeListenKey_)) { goog.events.unlistenByKey(this.postComposeListenKey_); - this.postComposeListenKey_ = undefined; + this.postComposeListenKey_ = null; this.map_.requestRenderFrame(); this.map_ = null; } From 07243006854bcd602b7bc663de37722d65c8ad29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 5 Dec 2013 09:44:18 +0100 Subject: [PATCH 03/15] Rename a property --- src/ol/render/box.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ol/render/box.js b/src/ol/render/box.js index 14c80516d9..820641ed8c 100644 --- a/src/ol/render/box.js +++ b/src/ol/render/box.js @@ -28,7 +28,7 @@ ol.render.Box = function(opt_style) { * @private * @type {goog.events.Key} */ - this.postComposeListenKey_ = null; + this.postComposeListenerKey_ = null; /** * @private @@ -117,15 +117,15 @@ ol.render.Box.prototype.requestMapRenderFrame_ = function() { * @param {ol.Map} map Map. */ ol.render.Box.prototype.setMap = function(map) { - if (!goog.isNull(this.postComposeListenKey_)) { - goog.events.unlistenByKey(this.postComposeListenKey_); - this.postComposeListenKey_ = null; + if (!goog.isNull(this.postComposeListenerKey_)) { + goog.events.unlistenByKey(this.postComposeListenerKey_); + this.postComposeListenerKey_ = null; this.map_.requestRenderFrame(); this.map_ = null; } this.map_ = map; if (!goog.isNull(this.map_)) { - this.postComposeListenKey_ = goog.events.listen( + this.postComposeListenerKey_ = goog.events.listen( map, ol.render.EventType.POSTCOMPOSE, this.handleMapPostCompose_, false, this); this.requestMapRenderFrame_(); From 91a61f3152872407957b65ca5c5eafe9a59f377d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 5 Dec 2013 11:10:25 +0100 Subject: [PATCH 04/15] ol.render.Box refactoring --- src/ol/render/box.js | 56 ++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/src/ol/render/box.js b/src/ol/render/box.js index 820641ed8c..d5c60c9b1f 100644 --- a/src/ol/render/box.js +++ b/src/ol/render/box.js @@ -3,6 +3,7 @@ goog.provide('ol.render.Box'); goog.require('goog.Disposable'); +goog.require('goog.asserts'); goog.require('goog.events'); goog.require('ol.geom.Polygon'); goog.require('ol.render.EventType'); @@ -42,6 +43,12 @@ ol.render.Box = function(opt_style) { */ this.endCoordinate_ = null; + /** + * @private + * @type {ol.geom.Polygon} + */ + this.geometry_ = null; + /** * @private * @type {ol.style.Style} @@ -61,23 +68,15 @@ goog.inherits(ol.render.Box, goog.Disposable); /** - * @inheritDoc - */ -ol.render.Box.prototype.disposeInternal = function() { - this.setMap(null); -}; - - -/** - * @param {ol.render.Event} event Event. * @private + * @param {ol.Extent} extent Extent. + * @return {ol.geom.Polygon} Geometry. */ -ol.render.Box.prototype.handleMapPostCompose_ = function(event) { - var render = event.getRender(); +ol.render.Box.prototype.createGeometry_ = function(extent) { + goog.asserts.assert(!goog.isNull(this.startCoordinate_)); + goog.asserts.assert(!goog.isNull(this.endCoordinate_)); var startCoordinate = this.startCoordinate_; var endCoordinate = this.endCoordinate_; - - var extent = event.getFrameState().extent; var coordinates = [ // outer ring [ @@ -94,10 +93,37 @@ ol.render.Box.prototype.handleMapPostCompose_ = function(event) { [endCoordinate[0], startCoordinate[1]] ] ]; - var geometry = new ol.geom.Polygon(coordinates); + return new ol.geom.Polygon(coordinates); +}; + + +/** + * @inheritDoc + */ +ol.render.Box.prototype.disposeInternal = function() { + this.setMap(null); +}; + + +/** + * @param {ol.render.Event} event Event. + * @private + */ +ol.render.Box.prototype.handleMapPostCompose_ = function(event) { + var extent = event.getFrameState().extent; + this.geometry_ = this.createGeometry_(extent); var style = this.style_; + var render = event.getRender(); render.setFillStrokeStyle(style.getFill(), style.getStroke()); - render.drawPolygonGeometry(geometry, null); + render.drawPolygonGeometry(this.geometry_, null); +}; + + +/** + * @return {ol.geom.Polygon} Geometry. + */ +ol.render.Box.prototype.getGeometry = function() { + return this.geometry_; }; From 6b305cd146cad63307b359df25bd79e357a2c1b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 5 Dec 2013 11:11:23 +0100 Subject: [PATCH 05/15] Add ol.interaction.DragBox --- src/objectliterals.jsdoc | 9 ++ src/ol/interaction/dragboxinteraction.exports | 1 + src/ol/interaction/dragboxinteraction.js | 101 ++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 src/ol/interaction/dragboxinteraction.exports create mode 100644 src/ol/interaction/dragboxinteraction.js 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; + } +}; From 0960584cfcdbbfd8dadc30f109df56dfd6a93021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 5 Dec 2013 11:12:52 +0100 Subject: [PATCH 06/15] Make DragZoom interaction inherit from DragBox --- src/ol/interaction/dragzoominteraction.js | 118 +++++++--------------- 1 file changed, 35 insertions(+), 83 deletions(-) diff --git a/src/ol/interaction/dragzoominteraction.js b/src/ol/interaction/dragzoominteraction.js index b7a6737613..084bcd1579 100644 --- a/src/ol/interaction/dragzoominteraction.js +++ b/src/ol/interaction/dragzoominteraction.js @@ -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); From de329b02d3a5c6373252b8099e2178db6f3a5bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 5 Dec 2013 11:13:30 +0100 Subject: [PATCH 07/15] Rename drag zoom interaction exports file --- .../interaction/{dragzoom.exports => dragzoominteraction.exports} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/ol/interaction/{dragzoom.exports => dragzoominteraction.exports} (100%) diff --git a/src/ol/interaction/dragzoom.exports b/src/ol/interaction/dragzoominteraction.exports similarity index 100% rename from src/ol/interaction/dragzoom.exports rename to src/ol/interaction/dragzoominteraction.exports From 18f9eeab12f7935063a7824d2a2bf40d20e68712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 5 Dec 2013 11:30:18 +0100 Subject: [PATCH 08/15] No default style in ol.render.Box --- src/ol/render/box.js | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/ol/render/box.js b/src/ol/render/box.js index d5c60c9b1f..4ea8e0cf6f 100644 --- a/src/ol/render/box.js +++ b/src/ol/render/box.js @@ -7,17 +7,15 @@ goog.require('goog.asserts'); goog.require('goog.events'); goog.require('ol.geom.Polygon'); goog.require('ol.render.EventType'); -goog.require('ol.style.Fill'); -goog.require('ol.style.Style'); /** * @constructor * @extends {goog.Disposable} - * @param {ol.style.Style=} opt_style Style. + * @param {ol.style.Style} style Style. */ -ol.render.Box = function(opt_style) { +ol.render.Box = function(style) { /** * @private @@ -53,15 +51,7 @@ ol.render.Box = function(opt_style) { * @private * @type {ol.style.Style} */ - this.style_ = goog.isDef(opt_style) ? opt_style : new ol.style.Style({ - fill: new ol.style.Fill({ - color: 'rgba(0,0,0,0.5)' - }), - image: null, - stroke: null, - text: null, - zIndex: 0 - }); + this.style_ = style; }; goog.inherits(ol.render.Box, goog.Disposable); @@ -113,6 +103,7 @@ ol.render.Box.prototype.handleMapPostCompose_ = function(event) { var extent = event.getFrameState().extent; this.geometry_ = this.createGeometry_(extent); var style = this.style_; + goog.asserts.assert(!goog.isNull(style)); var render = event.getRender(); render.setFillStrokeStyle(style.getFill(), style.getStroke()); render.drawPolygonGeometry(this.geometry_, null); From eb840777e898a4de1edada0a29fc633818297e2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 5 Dec 2013 11:30:46 +0100 Subject: [PATCH 09/15] Add style option to DragBox interaction --- src/objectliterals.jsdoc | 1 + src/ol/interaction/dragboxinteraction.js | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index d6f41a4461..2501eab27d 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -318,6 +318,7 @@ * @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. + * @property {ol.style.Style} style Style for the box. * @todo stability experimental */ diff --git a/src/ol/interaction/dragboxinteraction.js b/src/ol/interaction/dragboxinteraction.js index 933f07d303..81c03af21f 100644 --- a/src/ol/interaction/dragboxinteraction.js +++ b/src/ol/interaction/dragboxinteraction.js @@ -44,11 +44,18 @@ ol.interaction.DragBox = function(opt_options) { this.behavior_ = goog.isDef(options.behavior) ? options.behavior : goog.nullFunction; + + /** + * @private + * @type {ol.style.Style} + */ + var style = goog.isDef(options.style) ? options.style : null; + /** * @type {ol.render.Box} * @private */ - this.box_ = new ol.render.Box(); + this.box_ = new ol.render.Box(style); /** * @private From cf8adffdb272c28d99bb56c846fa4ce7e8d7f550 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 5 Dec 2013 11:31:07 +0100 Subject: [PATCH 10/15] Add style option to DragZoom interaction --- src/objectliterals.jsdoc | 1 + src/ol/interaction/dragzoominteraction.js | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index 2501eab27d..040dc6faba 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -327,6 +327,7 @@ * @property {ol.events.ConditionType|undefined} condition A conditional * modifier (i.e. Shift key) that determines if the interaction is active * or not, default is shift key. + * @property {ol.style.Style} style Style for the box. * @todo stability experimental */ diff --git a/src/ol/interaction/dragzoominteraction.js b/src/ol/interaction/dragzoominteraction.js index 084bcd1579..35c96be4b8 100644 --- a/src/ol/interaction/dragzoominteraction.js +++ b/src/ol/interaction/dragzoominteraction.js @@ -5,6 +5,8 @@ goog.require('ol.Size'); goog.require('ol.View2D'); goog.require('ol.events.condition'); goog.require('ol.interaction.DragBox'); +goog.require('ol.style.Fill'); +goog.require('ol.style.Style'); @@ -53,9 +55,26 @@ ol.interaction.DragZoom = function(opt_options) { }); }); + /** + * @private + * @type {ol.style.Style} + */ + var style = goog.isDef(options.style) ? + options.style : new ol.style.Style({ + fill: new ol.style.Fill({ + color: 'rgba(0,0,0,0.5)' + }), + image: null, + stroke: null, + text: null, + zIndex: 0 + }); + goog.base(this, { behavior: behavior, - condition: condition + condition: condition, + style: style }); + }; goog.inherits(ol.interaction.DragZoom, ol.interaction.DragBox); From 0eb00ba86b00d08253e89e20babfdd12f5a1d40e Mon Sep 17 00:00:00 2001 From: Antoine Abt Date: Thu, 19 Dec 2013 15:38:00 +0100 Subject: [PATCH 11/15] Remove dragbox.exports --- src/ol/render/dragbox.exports | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 src/ol/render/dragbox.exports diff --git a/src/ol/render/dragbox.exports b/src/ol/render/dragbox.exports deleted file mode 100644 index c98c668fb9..0000000000 --- a/src/ol/render/dragbox.exports +++ /dev/null @@ -1,3 +0,0 @@ -@exportSymbol ol.render.DragBox -@exportProperty ol.render.DragBox.prototype.setCoordinates -@exportProperty ol.render.DragBox.prototype.setMap From 43b8a72e62122117df731f24e721f0e6e008b9e9 Mon Sep 17 00:00:00 2001 From: Antoine Abt Date: Thu, 19 Dec 2013 15:41:52 +0100 Subject: [PATCH 12/15] Add ol.DragBoxEvent --- src/ol/interaction/dragboxinteraction.exports | 3 ++ src/ol/interaction/dragboxinteraction.js | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/ol/interaction/dragboxinteraction.exports b/src/ol/interaction/dragboxinteraction.exports index 13c1329169..8678205ef9 100644 --- a/src/ol/interaction/dragboxinteraction.exports +++ b/src/ol/interaction/dragboxinteraction.exports @@ -1 +1,4 @@ @exportSymbol ol.interaction.DragBox + +@exportSymbol ol.DragBoxEvent +@exportProperty ol.DragBoxEvent.prototype.getCoordinate diff --git a/src/ol/interaction/dragboxinteraction.js b/src/ol/interaction/dragboxinteraction.js index 81c03af21f..6eb31f6434 100644 --- a/src/ol/interaction/dragboxinteraction.js +++ b/src/ol/interaction/dragboxinteraction.js @@ -1,9 +1,11 @@ // FIXME draw drag box // FIXME works for View2D only +goog.provide('ol.DragBoxEvent'); goog.provide('ol.interaction.DragBox'); goog.require('goog.asserts'); +goog.require('goog.events.Event'); goog.require('ol.events.ConditionType'); goog.require('ol.events.condition'); goog.require('ol.interaction.Drag'); @@ -24,6 +26,47 @@ ol.DRAG_BOX_HYSTERESIS_PIXELS_SQUARED = ol.DRAG_BOX_HYSTERESIS_PIXELS; +/** + * @enum {string} + */ +ol.DragBoxEventType = { + BOXSTART: 'boxstart', + BOXEND: 'boxend' +}; + + + +/** + * Object representing a dragbox event. + * + * @param {string} type The event type. + * @param {ol.Coordinate} coordinate The event coordinate. + * @extends {goog.events.Event} + * @constructor + */ +ol.DragBoxEvent = function(type, coordinate) { + goog.base(this, type); + + /** + * The coordinate of the drag event. + * @type {ol.Coordinate} + * @private + */ + this.coordinate_ = coordinate; + +}; +goog.inherits(ol.DragBoxEvent, goog.events.Event); + + +/** + * Get the name of the property associated with this event. + * @return {ol.Coordinate} Event coordinate. + */ +ol.DragBoxEvent.prototype.getCoordinate = function() { + return this.coordinate_; +}; + + /** * @constructor From adfe20dd12fcfb50588517bb5bc49631af5b8e6e Mon Sep 17 00:00:00 2001 From: Antoine Abt Date: Thu, 19 Dec 2013 15:48:41 +0100 Subject: [PATCH 13/15] Make DragBox interaction dispatch events - boxstart/boxend events are dispatched, - behavior is removed, - geometry drawn by box render is a simple polygon instead of one with hole. --- src/objectliterals.jsdoc | 1 - src/ol/interaction/dragboxinteraction.exports | 1 + src/ol/interaction/dragboxinteraction.js | 32 ++++++---- src/ol/interaction/dragzoominteraction.js | 60 ++++++------------- src/ol/render/box.js | 14 +---- 5 files changed, 43 insertions(+), 65 deletions(-) diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index 040dc6faba..a463e2b778 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -314,7 +314,6 @@ /** * @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. diff --git a/src/ol/interaction/dragboxinteraction.exports b/src/ol/interaction/dragboxinteraction.exports index 8678205ef9..bb21d81015 100644 --- a/src/ol/interaction/dragboxinteraction.exports +++ b/src/ol/interaction/dragboxinteraction.exports @@ -1,4 +1,5 @@ @exportSymbol ol.interaction.DragBox +@exportProperty ol.interaction.DragBox.prototype.getGeometry @exportSymbol ol.DragBoxEvent @exportProperty ol.DragBoxEvent.prototype.getCoordinate diff --git a/src/ol/interaction/dragboxinteraction.js b/src/ol/interaction/dragboxinteraction.js index 6eb31f6434..d50d887838 100644 --- a/src/ol/interaction/dragboxinteraction.js +++ b/src/ol/interaction/dragboxinteraction.js @@ -80,14 +80,6 @@ ol.interaction.DragBox = function(opt_options) { 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; - - /** * @private * @type {ol.style.Style} @@ -120,6 +112,22 @@ ol.interaction.DragBox.prototype.handleDrag = function(mapBrowserEvent) { }; +/** + * Returns geometry of last drawn box. + * @return {ol.geom.Geometry} Geometry. + */ +ol.interaction.DragBox.prototype.getGeometry = function() { + return this.box_.getGeometry(); +}; + + +/** + * To be overriden by child classes. + * @protected + */ +ol.interaction.DragBox.prototype.onBoxEnd = goog.nullFunction; + + /** * @inheritDoc */ @@ -128,9 +136,9 @@ ol.interaction.DragBox.prototype.handleDragEnd = 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); + this.onBoxEnd(mapBrowserEvent); + this.dispatchEvent(new ol.DragBoxEvent(ol.DragBoxEventType.BOXEND, + mapBrowserEvent.getCoordinate())); } }; @@ -144,6 +152,8 @@ ol.interaction.DragBox.prototype.handleDragStart = if (browserEvent.isMouseActionButton() && this.condition_(mapBrowserEvent)) { this.box_.setCoordinates(this.startCoordinate, this.startCoordinate); this.box_.setMap(mapBrowserEvent.map); + this.dispatchEvent(new ol.DragBoxEvent(ol.DragBoxEventType.BOXSTART, + mapBrowserEvent.getCoordinate())); return true; } else { return false; diff --git a/src/ol/interaction/dragzoominteraction.js b/src/ol/interaction/dragzoominteraction.js index 35c96be4b8..64c7e959ef 100644 --- a/src/ol/interaction/dragzoominteraction.js +++ b/src/ol/interaction/dragzoominteraction.js @@ -1,11 +1,9 @@ goog.provide('ol.interaction.DragZoom'); goog.require('goog.asserts'); -goog.require('ol.Size'); -goog.require('ol.View2D'); goog.require('ol.events.condition'); goog.require('ol.interaction.DragBox'); -goog.require('ol.style.Fill'); +goog.require('ol.style.Stroke'); goog.require('ol.style.Style'); @@ -25,56 +23,36 @@ ol.interaction.DragZoom = function(opt_options) { var condition = goog.isDef(options.condition) ? options.condition : ol.events.condition.shiftKeyOnly; - /** - * @type {function(ol.Map, ol.geom.Polygon)} - */ - 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); - }); - }); - }); - /** * @private * @type {ol.style.Style} */ var style = goog.isDef(options.style) ? options.style : new ol.style.Style({ - fill: new ol.style.Fill({ - color: 'rgba(0,0,0,0.5)' - }), - image: null, - stroke: null, - text: null, - zIndex: 0 + stroke: new ol.style.Stroke({ + color: 'blue' + }) }); goog.base(this, { - behavior: behavior, condition: condition, style: style }); }; goog.inherits(ol.interaction.DragZoom, ol.interaction.DragBox); + + +/** + * @inheritDoc + */ +ol.interaction.DragZoom.prototype.onBoxEnd = function() { + this.getMap().withFrozenRendering(goog.bind(function() { + // FIXME works for View2D only + var view = this.getMap().getView().getView2D(); + + view.fitExtent(this.getGeometry().getExtent(), this.getMap().getSize()); + // FIXME we should preserve rotation + view.setRotation(0); + }, this)); +}; diff --git a/src/ol/render/box.js b/src/ol/render/box.js index 4ea8e0cf6f..5e728073e4 100644 --- a/src/ol/render/box.js +++ b/src/ol/render/box.js @@ -59,23 +59,14 @@ goog.inherits(ol.render.Box, goog.Disposable); /** * @private - * @param {ol.Extent} extent Extent. * @return {ol.geom.Polygon} Geometry. */ -ol.render.Box.prototype.createGeometry_ = function(extent) { +ol.render.Box.prototype.createGeometry_ = function() { goog.asserts.assert(!goog.isNull(this.startCoordinate_)); goog.asserts.assert(!goog.isNull(this.endCoordinate_)); var startCoordinate = this.startCoordinate_; var endCoordinate = this.endCoordinate_; var coordinates = [ - // outer ring - [ - [extent[0], extent[1]], - [extent[0], extent[3]], - [extent[2], extent[3]], - [extent[2], extent[1]] - ], - // inner ring [ startCoordinate, [startCoordinate[0], endCoordinate[1]], @@ -100,8 +91,7 @@ ol.render.Box.prototype.disposeInternal = function() { * @private */ ol.render.Box.prototype.handleMapPostCompose_ = function(event) { - var extent = event.getFrameState().extent; - this.geometry_ = this.createGeometry_(extent); + this.geometry_ = this.createGeometry_(); var style = this.style_; goog.asserts.assert(!goog.isNull(style)); var render = event.getRender(); From c2330b978644da6e5fb3d9447a5e5ce20e62ed93 Mon Sep 17 00:00:00 2001 From: Antoine Abt Date: Thu, 19 Dec 2013 16:02:30 +0100 Subject: [PATCH 14/15] Remove DragZoom from default interactions As it only works with canvas renderer. --- src/objectliterals.jsdoc | 2 -- src/ol/interaction/interactiondefaults.js | 7 ------- 2 files changed, 9 deletions(-) diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index a463e2b778..ea3509e0cb 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -342,8 +342,6 @@ * desired. Default is `true`. * @property {boolean|undefined} mouseWheelZoom Whether mousewheel zoom is * desired. Default is `true`. - * @property {boolean|undefined} shiftDragZoom Whether Shift-drag zoom is - * desired. Default is `true`. * @property {boolean|undefined} touchPan Whether touch pan is * desired. Default is `true`. * @property {boolean|undefined} touchRotate Whether touch rotate is desired. Default is `true`. diff --git a/src/ol/interaction/interactiondefaults.js b/src/ol/interaction/interactiondefaults.js index 32b4e5569c..b664e24edb 100644 --- a/src/ol/interaction/interactiondefaults.js +++ b/src/ol/interaction/interactiondefaults.js @@ -5,7 +5,6 @@ goog.require('ol.Kinetic'); goog.require('ol.interaction.DoubleClickZoom'); goog.require('ol.interaction.DragPan'); goog.require('ol.interaction.DragRotate'); -goog.require('ol.interaction.DragZoom'); goog.require('ol.interaction.KeyboardPan'); goog.require('ol.interaction.KeyboardZoom'); goog.require('ol.interaction.MouseWheelZoom'); @@ -99,12 +98,6 @@ ol.interaction.defaults = function(opt_options) { })); } - var shiftDragZoom = goog.isDef(options.shiftDragZoom) ? - options.shiftDragZoom : true; - if (shiftDragZoom) { - interactions.push(new ol.interaction.DragZoom()); - } - return interactions; }; From 8354400e52a0889c1d7ef8d840b74cfb4c936fa1 Mon Sep 17 00:00:00 2001 From: Antoine Abt Date: Thu, 19 Dec 2013 16:52:15 +0100 Subject: [PATCH 15/15] Add dragzoom example, configured with canvas renderer --- examples/dragzoom.html | 51 ++++++++++++++++++++++++++++++++++++++++++ examples/dragzoom.js | 24 ++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 examples/dragzoom.html create mode 100644 examples/dragzoom.js diff --git a/examples/dragzoom.html b/examples/dragzoom.html new file mode 100644 index 0000000000..0cd64bb6c1 --- /dev/null +++ b/examples/dragzoom.html @@ -0,0 +1,51 @@ + + + + + + + + + + + DragZoom example + + + + + +
+ +
+
+
+
+
+ +
+ +
+

DragZoom example

+

Example of dragzoom interaction

+
+

See the dragzoom.js source to see how this is done.

+

For the moment, only works with the canvas renderer.

+
+
dragzoom, openstreetmap
+
+ +
+ +
+ + + + + + diff --git a/examples/dragzoom.js b/examples/dragzoom.js new file mode 100644 index 0000000000..86d83114d4 --- /dev/null +++ b/examples/dragzoom.js @@ -0,0 +1,24 @@ +goog.require('ol.Map'); +goog.require('ol.RendererHint'); +goog.require('ol.View2D'); +goog.require('ol.interaction.DragZoom'); +goog.require('ol.layer.Tile'); +goog.require('ol.source.OSM'); + + +var map = new ol.Map({ + layers: [ + new ol.layer.Tile({ + source: new ol.source.OSM() + }) + ], + renderer: ol.RendererHint.CANVAS, + target: 'map', + view: new ol.View2D({ + center: [0, 0], + zoom: 2 + }) +}); + +var dragzoom = new ol.interaction.DragZoom(); +map.addInteraction(dragzoom);