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.
This commit is contained in:
Antoine Abt
2013-12-19 15:48:41 +01:00
parent 43b8a72e62
commit adfe20dd12
5 changed files with 43 additions and 65 deletions

View File

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

View File

@@ -1,4 +1,5 @@
@exportSymbol ol.interaction.DragBox @exportSymbol ol.interaction.DragBox
@exportProperty ol.interaction.DragBox.prototype.getGeometry
@exportSymbol ol.DragBoxEvent @exportSymbol ol.DragBoxEvent
@exportProperty ol.DragBoxEvent.prototype.getCoordinate @exportProperty ol.DragBoxEvent.prototype.getCoordinate

View File

@@ -80,14 +80,6 @@ ol.interaction.DragBox = function(opt_options) {
var options = goog.isDef(opt_options) ? 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 * @private
* @type {ol.style.Style} * @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 * @inheritDoc
*/ */
@@ -128,9 +136,9 @@ ol.interaction.DragBox.prototype.handleDragEnd =
this.box_.setMap(null); this.box_.setMap(null);
if (this.deltaX * this.deltaX + this.deltaY * this.deltaY >= if (this.deltaX * this.deltaX + this.deltaY * this.deltaY >=
ol.DRAG_BOX_HYSTERESIS_PIXELS_SQUARED) { ol.DRAG_BOX_HYSTERESIS_PIXELS_SQUARED) {
var map = mapBrowserEvent.map; this.onBoxEnd(mapBrowserEvent);
var geometry = this.box_.getGeometry(); this.dispatchEvent(new ol.DragBoxEvent(ol.DragBoxEventType.BOXEND,
this.behavior_(map, geometry); mapBrowserEvent.getCoordinate()));
} }
}; };
@@ -144,6 +152,8 @@ ol.interaction.DragBox.prototype.handleDragStart =
if (browserEvent.isMouseActionButton() && this.condition_(mapBrowserEvent)) { if (browserEvent.isMouseActionButton() && this.condition_(mapBrowserEvent)) {
this.box_.setCoordinates(this.startCoordinate, this.startCoordinate); this.box_.setCoordinates(this.startCoordinate, this.startCoordinate);
this.box_.setMap(mapBrowserEvent.map); this.box_.setMap(mapBrowserEvent.map);
this.dispatchEvent(new ol.DragBoxEvent(ol.DragBoxEventType.BOXSTART,
mapBrowserEvent.getCoordinate()));
return true; return true;
} else { } else {
return false; return false;

View File

@@ -1,11 +1,9 @@
goog.provide('ol.interaction.DragZoom'); goog.provide('ol.interaction.DragZoom');
goog.require('goog.asserts'); goog.require('goog.asserts');
goog.require('ol.Size');
goog.require('ol.View2D');
goog.require('ol.events.condition'); goog.require('ol.events.condition');
goog.require('ol.interaction.DragBox'); goog.require('ol.interaction.DragBox');
goog.require('ol.style.Fill'); goog.require('ol.style.Stroke');
goog.require('ol.style.Style'); goog.require('ol.style.Style');
@@ -25,56 +23,36 @@ ol.interaction.DragZoom = function(opt_options) {
var condition = goog.isDef(options.condition) ? var condition = goog.isDef(options.condition) ?
options.condition : ol.events.condition.shiftKeyOnly; 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 * @private
* @type {ol.style.Style} * @type {ol.style.Style}
*/ */
var style = goog.isDef(options.style) ? var style = goog.isDef(options.style) ?
options.style : new ol.style.Style({ options.style : new ol.style.Style({
fill: new ol.style.Fill({ stroke: new ol.style.Stroke({
color: 'rgba(0,0,0,0.5)' color: 'blue'
}), })
image: null,
stroke: null,
text: null,
zIndex: 0
}); });
goog.base(this, { goog.base(this, {
behavior: behavior,
condition: condition, condition: condition,
style: style style: style
}); });
}; };
goog.inherits(ol.interaction.DragZoom, ol.interaction.DragBox); 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));
};

View File

@@ -59,23 +59,14 @@ goog.inherits(ol.render.Box, goog.Disposable);
/** /**
* @private * @private
* @param {ol.Extent} extent Extent.
* @return {ol.geom.Polygon} Geometry. * @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.startCoordinate_));
goog.asserts.assert(!goog.isNull(this.endCoordinate_)); goog.asserts.assert(!goog.isNull(this.endCoordinate_));
var startCoordinate = this.startCoordinate_; var startCoordinate = this.startCoordinate_;
var endCoordinate = this.endCoordinate_; var endCoordinate = this.endCoordinate_;
var coordinates = [ var coordinates = [
// outer ring
[
[extent[0], extent[1]],
[extent[0], extent[3]],
[extent[2], extent[3]],
[extent[2], extent[1]]
],
// inner ring
[ [
startCoordinate, startCoordinate,
[startCoordinate[0], endCoordinate[1]], [startCoordinate[0], endCoordinate[1]],
@@ -100,8 +91,7 @@ ol.render.Box.prototype.disposeInternal = function() {
* @private * @private
*/ */
ol.render.Box.prototype.handleMapPostCompose_ = function(event) { ol.render.Box.prototype.handleMapPostCompose_ = function(event) {
var extent = event.getFrameState().extent; this.geometry_ = this.createGeometry_();
this.geometry_ = this.createGeometry_(extent);
var style = this.style_; var style = this.style_;
goog.asserts.assert(!goog.isNull(style)); goog.asserts.assert(!goog.isNull(style));
var render = event.getRender(); var render = event.getRender();