Sketch handler updates. Patch by tschaub, review elemoine (Closes #1698)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@7964 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2008-09-05 15:06:04 +00:00
parent 629f370b75
commit c7b8a5bce6
3 changed files with 109 additions and 35 deletions
+31 -8
View File
@@ -73,6 +73,7 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
new OpenLayers.Geometry.LineString()); new OpenLayers.Geometry.LineString());
this.point = new OpenLayers.Feature.Vector( this.point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point()); new OpenLayers.Geometry.Point());
this.layer.addFeatures([this.line, this.point], {silent: true});
}, },
/** /**
@@ -81,12 +82,19 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
*/ */
destroyFeature: function() { destroyFeature: function() {
OpenLayers.Handler.Point.prototype.destroyFeature.apply(this); OpenLayers.Handler.Point.prototype.destroyFeature.apply(this);
if(this.line) {
this.line.destroy();
}
this.line = null; this.line = null;
}, },
/**
* Method: destroyPoint
* Destroy the temporary point.
*/
destroyPoint: function() {
if(this.point) {
this.layer.destroyFeatures([this.point]);
}
},
/** /**
* Method: addPoint * Method: addPoint
* Add point to geometry. Send the point index to override * Add point to geometry. Send the point index to override
@@ -95,7 +103,7 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
addPoint: function() { addPoint: function() {
this.line.geometry.addComponent(this.point.geometry.clone(), this.line.geometry.addComponent(this.point.geometry.clone(),
this.line.geometry.components.length); this.line.geometry.components.length);
this.callback("point", [this.point.geometry]); this.callback("point", [this.point.geometry, this.getGeometry()]);
}, },
/** /**
@@ -131,14 +139,19 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
}, },
/** /**
* Method: geometryClone * Method: getGeometry
* Return a clone of the relevant geometry. * Return the sketch geometry. If <multi> is true, this will return
* a multi-part geometry.
* *
* Returns: * Returns:
* {<OpenLayers.Geometry.LineString>} * {<OpenLayers.Geometry.LineString>}
*/ */
geometryClone: function() { getGeometry: function() {
return this.line.geometry.clone(); var geometry = this.line.geometry;
if(this.multi) {
geometry = new OpenLayers.Geometry.MultiLineString([geometry]);
}
return geometry;
}, },
/** /**
@@ -158,6 +171,9 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
return false; return false;
} }
if(this.lastDown == null) { if(this.lastDown == null) {
if(this.persist) {
this.destroyFeature();
}
this.createFeature(); this.createFeature();
} }
this.mouseDown = true; this.mouseDown = true;
@@ -165,6 +181,7 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
var lonlat = this.control.map.getLonLatFromPixel(evt.xy); var lonlat = this.control.map.getLonLatFromPixel(evt.xy);
this.point.geometry.x = lonlat.lon; this.point.geometry.x = lonlat.lon;
this.point.geometry.y = lonlat.lat; this.point.geometry.y = lonlat.lat;
this.point.geometry.clearBounds();
if((this.lastUp == null) || !this.lastUp.equals(evt.xy)) { if((this.lastUp == null) || !this.lastUp.equals(evt.xy)) {
this.addPoint(); this.addPoint();
} }
@@ -215,6 +232,9 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
this.mouseDown = false; this.mouseDown = false;
if(this.drawing) { if(this.drawing) {
if(this.freehandMode(evt)) { if(this.freehandMode(evt)) {
if(this.persist) {
this.destroyPoint();
}
this.finalize(); this.finalize();
} else { } else {
if(this.lastUp == null) { if(this.lastUp == null) {
@@ -242,6 +262,9 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
if(!this.freehandMode(evt)) { if(!this.freehandMode(evt)) {
var index = this.line.geometry.components.length - 1; var index = this.line.geometry.components.length - 1;
this.line.geometry.removeComponent(this.line.geometry.components[index]); this.line.geometry.removeComponent(this.line.geometry.components[index]);
if(this.persist) {
this.destroyPoint();
}
this.finalize(); this.finalize();
} }
return false; return false;
+64 -19
View File
@@ -32,6 +32,13 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
*/ */
layer: null, layer: null,
/**
* Property: multi
* {Boolean} Cast features to multi-part geometries before passing to the
* layer. Default is false.
*/
multi: false,
/** /**
* Property: drawing * Property: drawing
* {Boolean} A point is being drawn * {Boolean} A point is being drawn
@@ -56,6 +63,21 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
*/ */
lastUp: null, lastUp: null,
/**
* APIProperty: persist
* {Boolean} Leave the feature rendered until destroyFeature is called.
* Default is false. If set to true, the feature remains rendered until
* destroyFeature is called, typically by deactivating the handler or
* starting another drawing.
*/
persist: false,
/**
* Property: layerOptions
* {Object} Any optional properties to be set on the sketch layer.
*/
layerOptions: null,
/** /**
* Constructor: OpenLayers.Handler.Point * Constructor: OpenLayers.Handler.Point
* Create a new point handler. * Create a new point handler.
@@ -89,14 +111,14 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
} }
// create temporary vector layer for rendering geometry sketch // create temporary vector layer for rendering geometry sketch
// TBD: this could be moved to initialize/destroy - setting visibility here // TBD: this could be moved to initialize/destroy - setting visibility here
var options = { var options = OpenLayers.Util.extend({
displayInLayerSwitcher: false, displayInLayerSwitcher: false,
// indicate that the temp vector layer will never be out of range // indicate that the temp vector layer will never be out of range
// without this, resolution properties must be specified at the // without this, resolution properties must be specified at the
// map-level for this temporary layer to init its resolutions // map-level for this temporary layer to init its resolutions
// correctly // correctly
calculateInRange: function() { return true; } calculateInRange: function() { return true; }
}; }, this.layerOptions);
this.layer = new OpenLayers.Layer.Vector(this.CLASS_NAME, options); this.layer = new OpenLayers.Layer.Vector(this.CLASS_NAME, options);
this.map.addLayer(this.layer); this.map.addLayer(this.layer);
return true; return true;
@@ -108,7 +130,9 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
*/ */
createFeature: function() { createFeature: function() {
this.point = new OpenLayers.Feature.Vector( this.point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point()); new OpenLayers.Geometry.Point()
);
this.layer.addFeatures([this.point], {silent: true});
}, },
/** /**
@@ -123,6 +147,7 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
if(this.drawing) { if(this.drawing) {
this.cancel(); this.cancel();
} }
this.destroyFeature();
// If a layer's map property is set to null, it means that that layer // 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 // 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 // in activate(), we can assume that if this.layer.map is null it means
@@ -140,8 +165,8 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* Destroy the temporary geometries * Destroy the temporary geometries
*/ */
destroyFeature: function() { destroyFeature: function() {
if(this.point) { if(this.layer) {
this.point.destroy(); this.layer.destroyFeatures();
} }
this.point = null; this.point = null;
}, },
@@ -149,15 +174,21 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
/** /**
* Method: finalize * Method: finalize
* Finish the geometry and call the "done" callback. * Finish the geometry and call the "done" callback.
*
* Parameters:
* cancel - {Boolean} Call cancel instead of done callback. Default is
* false.
*/ */
finalize: function() { finalize: function(cancel) {
this.layer.renderer.clear(); var key = cancel ? "cancel" : "done";
this.drawing = false; this.drawing = false;
this.mouseDown = false; this.mouseDown = false;
this.lastDown = null; this.lastDown = null;
this.lastUp = null; this.lastUp = null;
this.callback("done", [this.geometryClone()]); this.callback(key, [this.geometryClone()]);
this.destroyFeature(); if(cancel || !this.persist) {
this.destroyFeature();
}
}, },
/** /**
@@ -165,13 +196,7 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* Finish the geometry and call the "cancel" callback. * Finish the geometry and call the "cancel" callback.
*/ */
cancel: function() { cancel: function() {
this.layer.renderer.clear(); this.finalize(true);
this.drawing = false;
this.mouseDown = false;
this.lastDown = null;
this.lastUp = null;
this.callback("cancel", [this.geometryClone()]);
this.destroyFeature();
}, },
/** /**
@@ -215,14 +240,30 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
}, },
/** /**
* Method: geometryClone * Method: getGeometry
* Return a clone of the relevant geometry. * Return the sketch geometry. If <multi> is true, this will return
* a multi-part geometry.
* *
* Returns: * Returns:
* {<OpenLayers.Geometry.Point>} * {<OpenLayers.Geometry.Point>}
*/ */
getGeometry: function() {
var geometry = this.point.geometry;
if(this.multi) {
geometry = new OpenLayers.Geometry.MultiPoint([geometry]);
}
return geometry;
},
/**
* Method: geometryClone
* Return a clone of the relevant geometry.
*
* Returns:
* {<OpenLayers.Geometry>}
*/
geometryClone: function() { geometryClone: function() {
return this.point.geometry.clone(); return this.getGeometry().clone();
}, },
/** /**
@@ -246,6 +287,9 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
return true; return true;
} }
if(this.lastDown == null) { if(this.lastDown == null) {
if(this.persist) {
this.destroyFeature();
}
this.createFeature(); this.createFeature();
} }
this.lastDown = evt.xy; this.lastDown = evt.xy;
@@ -253,6 +297,7 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
var lonlat = this.map.getLonLatFromPixel(evt.xy); var lonlat = this.map.getLonLatFromPixel(evt.xy);
this.point.geometry.x = lonlat.lon; this.point.geometry.x = lonlat.lon;
this.point.geometry.y = lonlat.lat; this.point.geometry.y = lonlat.lat;
this.point.geometry.clearBounds();
this.drawFeature(); this.drawFeature();
return false; return false;
}, },
+13 -7
View File
@@ -59,6 +59,7 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
this.polygon.geometry.addComponent(this.line.geometry); this.polygon.geometry.addComponent(this.line.geometry);
this.point = new OpenLayers.Feature.Vector( this.point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point()); new OpenLayers.Geometry.Point());
this.layer.addFeatures([this.polygon, this.point], {silent: true});
}, },
/** /**
@@ -67,9 +68,6 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
*/ */
destroyFeature: function() { destroyFeature: function() {
OpenLayers.Handler.Path.prototype.destroyFeature.apply(this); OpenLayers.Handler.Path.prototype.destroyFeature.apply(this);
if(this.polygon) {
this.polygon.destroy();
}
this.polygon = null; this.polygon = null;
}, },
@@ -95,14 +93,19 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
}, },
/** /**
* Method: geometryClone * Method: getGeometry
* Return a clone of the relevant geometry. * Return the sketch geometry. If <multi> is true, this will return
* a multi-part geometry.
* *
* Returns: * Returns:
* {<OpenLayers.Geometry.Polygon>} * {<OpenLayers.Geometry.Polygon>}
*/ */
geometryClone: function() { getGeometry: function() {
return this.polygon.geometry.clone(); var geometry = this.polygon.geometry;
if(this.multi) {
geometry = new OpenLayers.Geometry.MultiPolygon([geometry]);
}
return geometry;
}, },
/** /**
@@ -118,6 +121,9 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
// remove the penultimate point // remove the penultimate point
var index = this.line.geometry.components.length - 2; var index = this.line.geometry.components.length - 2;
this.line.geometry.removeComponent(this.line.geometry.components[index]); this.line.geometry.removeComponent(this.line.geometry.components[index]);
if(this.persist) {
this.destroyPoint();
}
this.finalize(); this.finalize();
} }
return false; return false;