Methods for programmatically manipulating sketches while digitizing features. r=bartvde (closes #3343)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@12103 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2011-06-17 18:59:16 +00:00
parent abdb336354
commit 2cf3f62d1b
12 changed files with 842 additions and 5 deletions

View File

@@ -67,6 +67,12 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
*/
timerId: null,
/**
* Property: redoStack
* {Array} Stack containing points removed with <undo>.
*/
redoStack: null,
/**
* Constructor: OpenLayers.Handler.Path
* Create a new path hander
@@ -170,6 +176,129 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
this.callback("point", [this.point.geometry, this.getGeometry()]);
this.callback("modify", [this.point.geometry, this.getSketch()]);
this.drawFeature();
delete this.redoStack;
},
/**
* Method: insertXY
* Insert a point in the current sketch given x & y coordinates. The new
* point is inserted immediately before the most recently drawn point.
*
* Parameters:
* x - {Number} The x-coordinate of the point.
* y - {Number} The y-coordinate of the point.
*/
insertXY: function(x, y) {
this.line.geometry.addComponent(
new OpenLayers.Geometry.Point(x, y),
this.getCurrentPointIndex()
);
this.drawFeature();
delete this.redoStack;
},
/**
* Method: insertDeltaXY
* Insert a point given offsets from the previously inserted point.
*
* Parameters:
* dx - {Number} The x-coordinate offset of the point.
* dy - {Number} The y-coordinate offset of the point.
*/
insertDeltaXY: function(dx, dy) {
var previousIndex = this.getCurrentPointIndex() - 1;
var p0 = this.line.geometry.components[previousIndex];
if (p0 && !isNaN(p0.x) && !isNaN(p0.y)) {
this.insertXY(p0.x + dx, p0.y + dy);
}
},
/**
* Method: insertDirectionLength
* Insert a point in the current sketch given a direction and a length.
*
* Parameters:
* direction - {Number} Degrees clockwise from the positive x-axis.
* length - {Number} Distance from the previously drawn point.
*/
insertDirectionLength: function(direction, length) {
direction *= Math.PI / 180;
var dx = length * Math.cos(direction);
var dy = length * Math.sin(direction);
this.insertDeltaXY(dx, dy);
},
/**
* Method: insertDeflectionLength
* Insert a point in the current sketch given a deflection and a length.
* The deflection should be degrees clockwise from the previously
* digitized segment.
*
* Parameters:
* deflection - {Number} Degrees clockwise from the previous segment.
* length - {Number} Distance from the previously drawn point.
*/
insertDeflectionLength: function(deflection, length) {
var previousIndex = this.getCurrentPointIndex() - 1;
if (previousIndex > 0) {
var p1 = this.line.geometry.components[previousIndex];
var p0 = this.line.geometry.components[previousIndex-1];
var theta = Math.atan2(p1.y - p0.y, p1.x - p0.x);
this.insertDirectionLength(
(theta * 180 / Math.PI) + deflection, length
);
}
},
/**
* Method: getCurrentPointIndex
*
* Returns:
* {Number} The index of the most recently drawn point.
*/
getCurrentPointIndex: function() {
return this.line.geometry.components.length - 1;
},
/**
* Method: undo
* Remove the most recently added point in the sketch geometry.
*
* Returns:
* {Boolean} A point was removed.
*/
undo: function() {
var geometry = this.line.geometry;
var components = geometry.components;
var index = this.getCurrentPointIndex() - 1;
var target = components[index];
var undone = geometry.removeComponent(target);
if (undone) {
if (!this.redoStack) {
this.redoStack = [];
}
this.redoStack.push(target);
this.drawFeature();
}
return undone;
},
/**
* Method: redo
* Reinsert the most recently removed point resulting from an <undo> call.
* The undo stack is deleted whenever a point is added by other means.
*
* Returns:
* {Boolean} A point was added.
*/
redo: function() {
var target = this.redoStack && this.redoStack.pop();
if (target) {
this.line.geometry.addComponent(target, this.getCurrentPointIndex());
this.drawFeature();
}
return !!target;
},
/**

View File

@@ -127,7 +127,17 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
}
OpenLayers.Handler.Path.prototype.addPoint.apply(this, arguments);
},
/**
* Method: getCurrentPointIndex
*
* Returns:
* {Number} The index of the most recently drawn point.
*/
getCurrentPointIndex: function() {
return this.line.geometry.components.length - 2;
},
/**
* Method: enforceTopology
* Simple topology enforcement for drawing interior rings. Ensures vertices