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:
@@ -79,6 +79,9 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
|
||||
* feature.
|
||||
*/
|
||||
createFeature: function(pixel) {
|
||||
if(!pixel) {
|
||||
pixel = new OpenLayers.Pixel(-50, -50);
|
||||
}
|
||||
var lonlat = this.control.map.getLonLatFromPixel(pixel);
|
||||
this.point = new OpenLayers.Feature.Vector(
|
||||
new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
|
||||
@@ -100,6 +103,17 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
|
||||
this.line = null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: destroyPersistedFeature
|
||||
* Destroy the persisted feature.
|
||||
*/
|
||||
destroyPersistedFeature: function() {
|
||||
var layer = this.layer;
|
||||
if(layer && layer.features.length > 2) {
|
||||
this.layer.features[0].destroy();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: removePoint
|
||||
* Destroy the temporary point.
|
||||
@@ -151,12 +165,13 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
|
||||
* Parameters:
|
||||
* pixel - {<OpenLayers.Pixel>} The updated pixel location for the latest
|
||||
* point.
|
||||
* drawing - {Boolean} Indicate if we're currently drawing.
|
||||
*/
|
||||
modifyFeature: function(pixel) {
|
||||
modifyFeature: function(pixel, drawing) {
|
||||
var lonlat = this.control.map.getLonLatFromPixel(pixel);
|
||||
this.point.geometry.x = lonlat.lon;
|
||||
this.point.geometry.y = lonlat.lat;
|
||||
this.callback("modify", [this.point.geometry, this.getSketch()]);
|
||||
this.callback("modify", [this.point.geometry, this.getSketch(), drawing]);
|
||||
this.point.geometry.clearBounds();
|
||||
this.drawFeature();
|
||||
},
|
||||
@@ -209,22 +224,17 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
|
||||
* {Boolean} Allow event propagation
|
||||
*/
|
||||
mousedown: function(evt) {
|
||||
// ignore double-clicks
|
||||
if (this.lastDown && this.lastDown.equals(evt.xy)) {
|
||||
return false;
|
||||
var stopDown = this.stopDown;
|
||||
if(this.freehandMode(evt)) {
|
||||
stopDown = true;
|
||||
}
|
||||
if(this.lastDown == null) {
|
||||
if(this.persist) {
|
||||
this.destroyFeature();
|
||||
}
|
||||
this.createFeature(evt.xy);
|
||||
} else if((this.lastUp == null) || !this.lastUp.equals(evt.xy)) {
|
||||
this.addPoint(evt.xy);
|
||||
if(!this.lastDown || !this.lastDown.equals(evt.xy)) {
|
||||
this.modifyFeature(evt.xy, !!this.lastUp);
|
||||
}
|
||||
this.mouseDown = true;
|
||||
this.lastDown = evt.xy;
|
||||
this.drawing = true;
|
||||
return false;
|
||||
this.stoppedDown = stopDown;
|
||||
return !stopDown;
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -239,12 +249,15 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
|
||||
* {Boolean} Allow event propagation
|
||||
*/
|
||||
mousemove: function (evt) {
|
||||
if(this.drawing) {
|
||||
if(this.mouseDown && this.freehandMode(evt)) {
|
||||
this.addPoint(evt.xy);
|
||||
} else {
|
||||
this.modifyFeature(evt.xy);
|
||||
if(this.stoppedDown && this.freehandMode(evt)) {
|
||||
if(this.persist) {
|
||||
this.destroyPersistedFeature();
|
||||
}
|
||||
this.addPoint(evt.xy);
|
||||
return false;
|
||||
}
|
||||
if(!this.mouseDown || this.stoppedDown) {
|
||||
this.modifyFeature(evt.xy, !!this.lastUp);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
@@ -261,20 +274,23 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
|
||||
* {Boolean} Allow event propagation
|
||||
*/
|
||||
mouseup: function (evt) {
|
||||
this.mouseDown = false;
|
||||
if(this.drawing) {
|
||||
if(this.freehandMode(evt)) {
|
||||
if(this.mouseDown && (!this.lastUp || !this.lastUp.equals(evt.xy))) {
|
||||
if(this.stoppedDown && this.freehandMode(evt)) {
|
||||
this.removePoint();
|
||||
this.finalize();
|
||||
} else {
|
||||
if(this.lastUp == null) {
|
||||
this.addPoint(evt.xy);
|
||||
if(this.lastDown.equals(evt.xy)) {
|
||||
if(this.lastUp == null && this.persist) {
|
||||
this.destroyPersistedFeature();
|
||||
}
|
||||
this.addPoint(evt.xy);
|
||||
this.lastUp = evt.xy;
|
||||
}
|
||||
this.lastUp = evt.xy;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
this.stoppedDown = this.stopDown;
|
||||
this.mouseDown = false;
|
||||
return !this.stopUp;
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -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"
|
||||
});
|
||||
|
||||
@@ -75,6 +75,9 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
|
||||
* feature.
|
||||
*/
|
||||
createFeature: function(pixel) {
|
||||
if(!pixel) {
|
||||
pixel = new OpenLayers.Pixel(-50, -50);
|
||||
}
|
||||
var lonlat = this.control.map.getLonLatFromPixel(pixel);
|
||||
this.point = new OpenLayers.Feature.Vector(
|
||||
new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
|
||||
@@ -82,13 +85,27 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
|
||||
this.line = new OpenLayers.Feature.Vector(
|
||||
new OpenLayers.Geometry.LinearRing([this.point.geometry])
|
||||
);
|
||||
|
||||
// check for hole digitizing
|
||||
var polygon;
|
||||
if (this.holeModifier && (this.evt[this.holeModifier])) {
|
||||
this.polygon = new OpenLayers.Feature.Vector(
|
||||
new OpenLayers.Geometry.Polygon([this.line.geometry])
|
||||
);
|
||||
this.callback("create", [this.point.geometry, this.getSketch()]);
|
||||
this.point.geometry.clearBounds();
|
||||
this.layer.addFeatures([this.polygon, this.point], {silent: true});
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: addPoint
|
||||
* Add point to geometry.
|
||||
*
|
||||
* Parameters:
|
||||
* pixel - {<OpenLayers.Pixel>} The pixel location for the new point.
|
||||
*/
|
||||
addPoint: function(pixel) {
|
||||
if(!this.drawingHole && this.holeModifier &&
|
||||
this.evt && this.evt[this.holeModifier]) {
|
||||
var geometry = this.point.geometry;
|
||||
var features = this.control.layer.features;
|
||||
var candidate;
|
||||
var candidate, polygon;
|
||||
// look for intersections, last drawn gets priority
|
||||
for (var i=features.length-1; i>=0; --i) {
|
||||
candidate = features[i].geometry;
|
||||
@@ -110,15 +127,7 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!polygon) {
|
||||
this.polygon = new OpenLayers.Feature.Vector(
|
||||
new OpenLayers.Geometry.Polygon([this.line.geometry])
|
||||
);
|
||||
}
|
||||
|
||||
this.callback("create", [this.point.geometry, this.getSketch()]);
|
||||
this.point.geometry.clearBounds();
|
||||
this.layer.addFeatures([this.polygon, this.point], {silent: true});
|
||||
OpenLayers.Handler.Path.prototype.addPoint.apply(this, arguments);
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user