adding irregular option to regular polygon control - now, go draw rectangles, and more - thanks for the review elem (closes #1098).
git-svn-id: http://svn.openlayers.org/trunk/openlayers@5200 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -269,10 +269,11 @@ OpenLayers.Geometry.Collection = OpenLayers.Class(OpenLayers.Geometry, {
|
||||
* (lines, for example, will be twice as long, and polygons
|
||||
* will have four times the area).
|
||||
* origin - {<OpenLayers.Geometry.Point>} Point of origin for resizing
|
||||
* ratio - {Float} Optional x:y ratio for resizing. Default ratio is 1.
|
||||
*/
|
||||
resize: function(scale, origin) {
|
||||
resize: function(scale, origin, ratio) {
|
||||
for(var i=0; i<this.components.length; ++i) {
|
||||
this.components[i].resize(scale, origin);
|
||||
this.components[i].resize(scale, origin, ratio);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -145,10 +145,11 @@ OpenLayers.Geometry.LinearRing = OpenLayers.Class(
|
||||
* (lines, for example, will be twice as long, and polygons
|
||||
* will have four times the area).
|
||||
* origin - {<OpenLayers.Geometry.Point>} Point of origin for resizing
|
||||
* ratio - {Float} Optional x:y ratio for resizing. Default ratio is 1.
|
||||
*/
|
||||
resize: function(scale, origin) {
|
||||
resize: function(scale, origin, ratio) {
|
||||
for(var i=0; i<this.components.length - 1; ++i) {
|
||||
this.components[i].resize(scale, origin);
|
||||
this.components[i].resize(scale, origin, ratio);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -160,10 +160,11 @@ OpenLayers.Geometry.Point = OpenLayers.Class(OpenLayers.Geometry, {
|
||||
* distance from the origin. A scale of 2 doubles the
|
||||
* distance between the point and origin.
|
||||
* origin - {<OpenLayers.Geometry.Point>} Point of origin for resizing
|
||||
* ratio - {Float} Optional x:y ratio for resizing. Default ratio is 1.
|
||||
*/
|
||||
resize: function(scale, origin) {
|
||||
this.x = origin.x + (scale * (this.x - origin.x));
|
||||
|
||||
resize: function(scale, origin, ratio) {
|
||||
ratio = (ratio == undefined) ? 1 : ratio;
|
||||
this.x = origin.x + (scale * ratio * (this.x - origin.x));
|
||||
this.y = origin.y + (scale * (this.y - origin.y));
|
||||
this.clearBounds();
|
||||
},
|
||||
|
||||
@@ -62,6 +62,18 @@ OpenLayers.Handler.RegularPolygon = OpenLayers.Class(OpenLayers.Handler.Drag, {
|
||||
*/
|
||||
persist: false,
|
||||
|
||||
/**
|
||||
* APIProperty: irregular
|
||||
* {Boolean} Draw an irregular polygon instead of a regular polygon.
|
||||
* Default is false. If true, the initial mouse down will represent
|
||||
* one corner of the polygon bounds and with each mouse movement, the
|
||||
* polygon will be stretched so the opposite corner of its bounds
|
||||
* follows the mouse position. This property takes precedence over
|
||||
* the radius property. If set to true, the radius property will
|
||||
* be ignored.
|
||||
*/
|
||||
irregular: false,
|
||||
|
||||
/**
|
||||
* Property: angle
|
||||
* {Float} The angle from the origin (mouse down) to the current mouse
|
||||
@@ -194,7 +206,7 @@ OpenLayers.Handler.RegularPolygon = OpenLayers.Class(OpenLayers.Handler.Drag, {
|
||||
var maploc = this.map.getLonLatFromPixel(evt.xy);
|
||||
this.origin = new OpenLayers.Geometry.Point(maploc.lon, maploc.lat);
|
||||
// create the new polygon
|
||||
if(!this.fixedRadius) {
|
||||
if(!this.fixedRadius || this.irregular) {
|
||||
// smallest radius should not be less one pixel in map units
|
||||
// VML doesn't behave well with smaller
|
||||
this.radius = this.map.getResolution();
|
||||
@@ -218,7 +230,10 @@ OpenLayers.Handler.RegularPolygon = OpenLayers.Class(OpenLayers.Handler.Drag, {
|
||||
move: function(evt) {
|
||||
var maploc = this.map.getLonLatFromPixel(evt.xy);
|
||||
var point = new OpenLayers.Geometry.Point(maploc.lon, maploc.lat);
|
||||
if(this.fixedRadius) {
|
||||
if(this.irregular) {
|
||||
var ry = Math.sqrt(2) * Math.abs(point.y - this.origin.y) / 2;
|
||||
this.radius = Math.max(this.map.getResolution() / 2, ry);
|
||||
} else if(this.fixedRadius) {
|
||||
this.origin = point;
|
||||
} else {
|
||||
this.calculateAngle(point, evt);
|
||||
@@ -226,6 +241,18 @@ OpenLayers.Handler.RegularPolygon = OpenLayers.Class(OpenLayers.Handler.Drag, {
|
||||
point.distanceTo(this.origin));
|
||||
}
|
||||
this.modifyGeometry();
|
||||
if(this.irregular) {
|
||||
var dx = point.x - this.origin.x;
|
||||
var dy = point.y - this.origin.y;
|
||||
var ratio;
|
||||
if(dy == 0) {
|
||||
ratio = dx / (this.radius * Math.sqrt(2));
|
||||
} else {
|
||||
ratio = dx / dy;
|
||||
}
|
||||
this.feature.geometry.resize(1, this.origin, ratio);
|
||||
this.feature.geometry.move(dx / 2, dy / 2);
|
||||
}
|
||||
this.layer.drawFeature(this.feature, this.style);
|
||||
},
|
||||
|
||||
@@ -277,6 +304,7 @@ OpenLayers.Handler.RegularPolygon = OpenLayers.Class(OpenLayers.Handler.Drag, {
|
||||
// if the number of sides ever changes, create a new geometry
|
||||
if(ring.components.length != (this.sides + 1)) {
|
||||
this.createGeometry();
|
||||
ring = this.feature.geometry.components[0];
|
||||
}
|
||||
for(var i=0; i<this.sides; ++i) {
|
||||
point = ring.components[i];
|
||||
|
||||
Reference in New Issue
Block a user