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;

View File

@@ -11,9 +11,11 @@
/**
* 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' and 'cancel'. Create a new instance with the
* <OpenLayers.Handler.Point> constructor.
* 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 sketc and will receive the latest point
* drawn. Create a new instance with the <OpenLayers.Handler.Point>
* constructor.
*
* Inherits from:
* - <OpenLayers.Handler>
@@ -84,19 +86,23 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
*
* Parameters:
* control - {<OpenLayers.Control>} The control that owns this handler
* callbacks - {Object} An object with a 'done' property whose value is a
* function to be called when the point drawing is finished.
* The callback should expect to recieve a single argument,
* the point geometry. 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.
* 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 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) {
// TBD: deal with style
this.style = OpenLayers.Util.extend(OpenLayers.Feature.Vector.style['default'], {});
if(!(options && options.layerOptions && options.layerOptions.styleMap)) {
this.style = OpenLayers.Util.extend(OpenLayers.Feature.Vector.style['default'], {});
}
OpenLayers.Handler.prototype.initialize.apply(this, arguments);
},
@@ -231,6 +237,21 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
return false;
},
/**
* Method: modifyFeature
* Modify the existing geometry given a pixel location.
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} A pixel location on the map.
*/
modifyFeature: function(pixel) {
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.point.geometry.clearBounds();
},
/**
* Method: drawFeature
* Render features on the temporary layer.
@@ -294,10 +315,7 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
}
this.lastDown = evt.xy;
this.drawing = true;
var lonlat = this.map.getLonLatFromPixel(evt.xy);
this.point.geometry.x = lonlat.lon;
this.point.geometry.y = lonlat.lat;
this.point.geometry.clearBounds();
this.modifyFeature(evt.xy);
this.drawFeature();
return false;
},
@@ -315,10 +333,7 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
*/
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();
this.modifyFeature(evt.xy);
this.drawFeature();
}
return true;

View File

@@ -30,18 +30,20 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
* Create a Polygon Handler.
*
* 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 polygon 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.
* options - {Object}
* 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 polygon 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.Path.prototype.initialize.apply(this, arguments);
@@ -50,15 +52,22 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
/**
* Method: createFeature
* Add temporary geometries
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} The initial pixel location for the new
* feature.
*/
createFeature: function() {
this.polygon = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon());
this.line = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LinearRing());
this.polygon.geometry.addComponent(this.line.geometry);
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.LinearRing([this.point.geometry])
);
this.polygon = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon([this.line.geometry])
);
this.layer.addFeatures([this.polygon, this.point], {silent: true});
},
@@ -71,18 +80,6 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
this.polygon = null;
},
/**
* Method: modifyFeature
* Modify the existing geometry given the new point
*
*/
modifyFeature: function() {
var index = this.line.geometry.components.length - 2;
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();
},
/**
* Method: drawFeature
* Render geometries on the temporary layer.
@@ -92,6 +89,17 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
this.layer.drawFeature(this.point, this.style);
},
/**
* Method: getSketch
* Return the sketch feature.
*
* Returns:
* {<OpenLayers.Feature.Vector>}
*/
getSketch: function() {
return this.polygon;
},
/**
* Method: getGeometry
* Return the sketch geometry. If <multi> is true, this will return
@@ -121,9 +129,7 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
// remove the penultimate point
var index = this.line.geometry.components.length - 2;
this.line.geometry.removeComponent(this.line.geometry.components[index]);
if(this.persist) {
this.destroyPoint();
}
this.removePoint();
this.finalize();
}
return false;