make it possible to pan the map while drawing geometries, r=tschaub (closes #3052)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@11381 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Éric Lemoine
2011-02-24 09:00:49 +00:00
parent f8db509725
commit 30aeab365a
11 changed files with 1665 additions and 309 deletions

View File

@@ -11,9 +11,9 @@
/**
* Class: OpenLayers.Handler.Point
* Handler to draw a point on the map. Point is displayed on mouse down,
* moves on mouse move, and is finished on mouse up. The handler triggers
* callbacks for 'done', 'cancel', and 'modify'. The modify callback is
* Handler to draw a point on the map. Point is displayed on activation,
* moves on mouse move, and is finished on mouse up. The handler triggers
* callbacks for 'done', 'cancel', and 'modify'. The modify callback is
* called with each change in the sketch and will receive the latest point
* drawn. Create a new instance with the <OpenLayers.Handler.Point>
* constructor.
@@ -42,18 +42,19 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
*/
multi: false,
/**
* Property: drawing
* {Boolean} A point is being drawn
*/
drawing: false,
/**
* Property: mouseDown
* {Boolean} The mouse is down
*/
mouseDown: false,
/**
* Property: stoppedDown
* {Boolean} Indicate whether the last mousedown stopped the event
* propagation.
*/
stoppedDown: null,
/**
* Property: lastDown
* {<OpenLayers.Pixel>} Location of the last mouse down
@@ -75,6 +76,20 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
*/
persist: false,
/**
* APIProperty: stopDown
* {Boolean} Stop event propagation on mousedown. Must be false to
* allow "pan while drawing". Defaults to false.
*/
stopDown: false,
/**
* APIPropery: stopUp
* {Boolean} Stop event propagation on mouse. Must be false to
* allow "pan while dragging". Defaults to fase.
*/
stopUp: false,
/**
* Property: layerOptions
* {Object} Any optional properties to be set on the sketch layer.
@@ -130,6 +145,7 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
}, this.layerOptions);
this.layer = new OpenLayers.Layer.Vector(this.CLASS_NAME, options);
this.map.addLayer(this.layer);
this.createFeature();
return true;
},
@@ -141,6 +157,9 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* pixel - {<OpenLayers.Pixel>} A pixel location on the map.
*/
createFeature: function(pixel) {
if(!pixel) {
pixel = new OpenLayers.Pixel(-50, -50);
}
var lonlat = this.map.getLonLatFromPixel(pixel);
this.point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
@@ -158,17 +177,14 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
if(!OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
return false;
}
// call the cancel callback if mid-drawing
if(this.drawing) {
this.cancel();
}
this.destroyFeature();
this.cancel(true);
// If a layer's map property is set to null, it means that that layer
// isn't added to the map. Since we ourself added the layer to the map
// in activate(), we can assume that if this.layer.map is null it means
// that the layer has been destroyed (as a result of map.destroy() for
// example.
if (this.layer.map != null) {
this.destroyFeature();
this.layer.destroy(false);
}
this.layer = null;
@@ -186,6 +202,17 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
this.point = null;
},
/**
* Method: destroyPersistedFeature
* Destroy the persisted feature.
*/
destroyPersistedFeature: function() {
var layer = this.layer;
if(layer && layer.features.length > 1) {
this.layer.features[0].destroy();
}
},
/**
* Method: finalize
* Finish the geometry and call the "done" callback.
@@ -193,8 +220,10 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* Parameters:
* cancel - {Boolean} Call cancel instead of done callback. Default is
* false.
* noNew - {Boolean} Do not create a new feature after
* finalization. Default is false.
*/
finalize: function(cancel) {
finalize: function(cancel, noNew) {
var key = cancel ? "cancel" : "done";
this.drawing = false;
this.mouseDown = false;
@@ -204,14 +233,21 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
if(cancel || !this.persist) {
this.destroyFeature();
}
if(!noNew) {
this.createFeature();
}
},
/**
* APIMethod: cancel
* Finish the geometry and call the "cancel" callback.
*
* Parameters:
* noNew - {Boolean} Do not create a new feature after
* cancelation. Default is false.
*/
cancel: function() {
this.finalize(true);
cancel: function(noNew) {
this.finalize(true, noNew);
},
/**
@@ -257,7 +293,7 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
var lonlat = this.map.getLonLatFromPixel(pixel);
this.point.geometry.x = lonlat.lon;
this.point.geometry.y = lonlat.lat;
this.callback("modify", [this.point.geometry, this.point]);
this.callback("modify", [this.point.geometry, this.point, false]);
this.point.geometry.clearBounds();
this.drawFeature();
},
@@ -310,25 +346,11 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* {Boolean} Allow event propagation
*/
mousedown: function(evt) {
// check keyboard modifiers
if(!this.checkModifiers(evt)) {
return true;
}
// ignore double-clicks
if(this.lastDown && this.lastDown.equals(evt.xy)) {
return true;
}
this.drawing = true;
if(this.lastDown == null) {
if(this.persist) {
this.destroyFeature();
}
this.createFeature(evt.xy);
} else {
this.modifyFeature(evt.xy);
}
this.mouseDown = true;
this.lastDown = evt.xy;
return false;
this.modifyFeature(evt.xy);
this.stoppedDown = this.stopDown;
return !this.stopDown;
},
/**
@@ -343,7 +365,7 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* {Boolean} Allow event propagation
*/
mousemove: function (evt) {
if(this.drawing) {
if(!this.mouseDown || this.stoppedDown) {
this.modifyFeature(evt.xy);
}
return true;
@@ -361,13 +383,42 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* {Boolean} Allow event propagation
*/
mouseup: function (evt) {
if(this.drawing) {
this.mouseDown = false;
this.stoppedDown = this.stopDown;
// check keyboard modifiers
if(!this.checkModifiers(evt)) {
return true;
}
// ignore double-clicks
if(this.lastUp && this.lastUp.equals(evt.xy)) {
return true;
}
if(this.lastDown && this.lastDown.equals(evt.xy)) {
if(this.persist) {
this.destroyPersistedFeature();
}
this.lastUp = evt.xy;
this.finalize();
return false;
return !this.stopUp;
} else {
return true;
}
},
/**
* Method: mouseout
* Handle mouse out. For better user experience reset mouseDown
* and stoppedDown when the mouse leaves the map viewport.
*
* Parameters:
* evt - {Event} The browser event
*/
mouseout: function(evt) {
if(OpenLayers.Util.mouseLeft(evt, this.map.viewPortDiv)) {
this.stoppedDown = this.stopDown;
this.mouseDown = false;
}
},
CLASS_NAME: "OpenLayers.Handler.Point"
});