Adding support for a modify callback in the sketch handlers. This lets controls know about each sketch modification as a sketch is being drawn. Also a bit of a refactor of the sketch handlers to share sketch geometry parts. r=ahocevar (closes #1903)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@8831 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-02-04 22:45:16 +00:00
parent 112debe5f5
commit f4bae0a011
6 changed files with 352 additions and 103 deletions

View File

@@ -48,17 +48,20 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
* Create a new path hander
*
* Parameters:
* control - {<OpenLayers.Control>}
* callbacks - {Object} An object with a 'done' property whos value is a
* function to be called when the path drawing is finished. The
* callback should expect to recieve a single argument, the line
* string geometry. If the callbacks object contains a 'point'
* property, this function will be sent each point as they are added.
* If the callbacks object contains a 'cancel' property, this function
* will be called when the handler is deactivated while drawing. The
* cancel should expect to receive a geometry.
* control - {<OpenLayers.Control>} The control that owns this handler
* callbacks - {Object} An object with a properties whose values are
* functions. Various callbacks described below.
* options - {Object} An optional object with properties to be set on the
* handler
*
* Named callbacks:
* done - Called when the point drawing is finished. The callback will
* recieve a single argument, the linestring geometry.
* point - Called as each point is added. Receives the new point geometry.
* modify - Called with each move of a vertex with the vertex (point)
* geometry and the sketch feature.
* cancel - Called when the handler is deactivated while drawing. The
* cancel callback will receive a geometry.
*/
initialize: function(control, callbacks, options) {
OpenLayers.Handler.Point.prototype.initialize.apply(this, arguments);
@@ -67,12 +70,19 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
/**
* Method: createFeature
* Add temporary geometries
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} The initial pixel location for the new
* feature.
*/
createFeature: function() {
this.line = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString());
createFeature: function(pixel) {
var lonlat = this.control.map.getLonLatFromPixel(pixel);
this.point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point());
new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
);
this.line = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString([this.point.geometry])
);
this.layer.addFeatures([this.line, this.point], {silent: true});
},
@@ -86,12 +96,12 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
},
/**
* Method: destroyPoint
* Method: removePoint
* Destroy the temporary point.
*/
destroyPoint: function() {
removePoint: function() {
if(this.point) {
this.layer.destroyFeatures([this.point]);
this.layer.removeFeatures([this.point]);
}
},
@@ -99,11 +109,22 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
* Method: addPoint
* Add point to geometry. Send the point index to override
* the behavior of LinearRing that disregards adding duplicate points.
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} The pixel location for the new point.
*/
addPoint: function() {
this.line.geometry.addComponent(this.point.geometry.clone(),
this.line.geometry.components.length);
addPoint: function(pixel) {
this.layer.removeFeatures([this.point]);
var lonlat = this.control.map.getLonLatFromPixel(pixel);
this.point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
);
this.line.geometry.addComponent(
this.point.geometry, this.line.geometry.components.length
);
this.callback("point", [this.point.geometry, this.getGeometry()]);
this.callback("modify", [this.point.geometry, this.getSketch()]);
this.drawFeature();
},
/**
@@ -121,14 +142,20 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
/**
* Method: modifyFeature
* Modify the existing geometry given the new point
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} The updated pixel location for the latest
* point.
*/
modifyFeature: function() {
var index = this.line.geometry.components.length - 1;
this.line.geometry.components[index].x = this.point.geometry.x;
this.line.geometry.components[index].y = this.point.geometry.y;
this.line.geometry.components[index].clearBounds();
modifyFeature: function(pixel) {
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.point.geometry.clearBounds();
this.drawFeature();
},
/**
* Method: drawFeature
* Render geometries on the temporary layer.
@@ -138,6 +165,17 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
this.layer.drawFeature(this.point, this.style);
},
/**
* Method: getSketch
* Return the sketch feature.
*
* Returns:
* {<OpenLayers.Feature.Vector>}
*/
getSketch: function() {
return this.line;
},
/**
* Method: getGeometry
* Return the sketch geometry. If <multi> is true, this will return
@@ -174,18 +212,12 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
if(this.persist) {
this.destroyFeature();
}
this.createFeature();
this.createFeature(evt.xy);
} else if((this.lastUp == null) || !this.lastUp.equals(evt.xy)) {
this.addPoint(evt.xy);
}
this.mouseDown = true;
this.lastDown = evt.xy;
var lonlat = this.control.map.getLonLatFromPixel(evt.xy);
this.point.geometry.x = lonlat.lon;
this.point.geometry.y = lonlat.lat;
this.point.geometry.clearBounds();
if((this.lastUp == null) || !this.lastUp.equals(evt.xy)) {
this.addPoint();
}
this.drawFeature();
this.drawing = true;
return false;
},
@@ -203,16 +235,11 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
*/
mousemove: function (evt) {
if(this.drawing) {
var lonlat = this.map.getLonLatFromPixel(evt.xy);
this.point.geometry.x = lonlat.lon;
this.point.geometry.y = lonlat.lat;
this.point.geometry.clearBounds();
if(this.mouseDown && this.freehandMode(evt)) {
this.addPoint();
this.addPoint(evt.xy);
} else {
this.modifyFeature();
this.modifyFeature(evt.xy);
}
this.drawFeature();
}
return true;
},
@@ -232,13 +259,11 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
this.mouseDown = false;
if(this.drawing) {
if(this.freehandMode(evt)) {
if(this.persist) {
this.destroyPoint();
}
this.removePoint();
this.finalize();
} else {
if(this.lastUp == null) {
this.addPoint();
this.addPoint(evt.xy);
}
this.lastUp = evt.xy;
}
@@ -262,9 +287,7 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
if(!this.freehandMode(evt)) {
var index = this.line.geometry.components.length - 1;
this.line.geometry.removeComponent(this.line.geometry.components[index]);
if(this.persist) {
this.destroyPoint();
}
this.removePoint();
this.finalize();
}
return false;