Adding a 'sketchstarted' event on the vector layer. This event is triggered at the start of each new sketch. r=ahocevar (closes #1945)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9269 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-04-12 15:36:15 +00:00
parent 70c3fd077d
commit dfcf68007e
11 changed files with 85 additions and 30 deletions

View File

@@ -37,7 +37,9 @@
"featuremodified": report,
"afterfeaturemodified": report,
"vertexmodified": report,
"sketchmodified": report
"sketchmodified": report,
"sketchstarted": report,
"sketchcomplete": report
});
controls = {
point: new OpenLayers.Control.DrawFeature(vectors,

View File

@@ -74,6 +74,11 @@ OpenLayers.Control.DrawFeature = OpenLayers.Class(OpenLayers.Control, {
this.layer.events.triggerEvent(
"sketchmodified", {vertex: vertex, feature: feature}
);
},
create: function(vertex, feature) {
this.layer.events.triggerEvent(
"sketchstarted", {vertex: vertex, feature: feature}
);
}
},
this.callbacks

View File

@@ -55,11 +55,13 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
* 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.
* create - Called when a sketch is first created. Callback called with
* the creation point geometry and sketch feature.
* modify - Called with each move of a vertex with the vertex (point)
* geometry and the sketch feature.
* point - Called as each point is added. Receives the new point geometry.
* done - Called when the point drawing is finished. The callback will
* recieve a single argument, the linestring geometry.
* cancel - Called when the handler is deactivated while drawing. The
* cancel callback will receive a geometry.
*/
@@ -83,6 +85,8 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
this.line = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString([this.point.geometry])
);
this.callback("create", [this.point.geometry, this.getSketch()]);
this.point.geometry.clearBounds();
this.layer.addFeatures([this.line, this.point], {silent: true});
},

View File

@@ -92,10 +92,12 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* handler
*
* Named callbacks:
* done - Called when the point drawing is finished. The callback will
* recieve a single argument, the point geometry.
* create - Called when a sketch is first created. Callback called with
* the creation point geometry and sketch feature.
* modify - Called with each move of a vertex with the vertex (point)
* geometry and the sketch feature.
* done - Called when the point drawing is finished. The callback will
* recieve a single argument, the point geometry.
* cancel - Called when the handler is deactivated while drawing. The
* cancel callback will receive a geometry.
*/
@@ -133,11 +135,17 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
/**
* Method: createFeature
* Add temporary features
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} A pixel location on the map.
*/
createFeature: function() {
createFeature: function(pixel) {
var lonlat = this.map.getLonLatFromPixel(pixel);
this.point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point()
new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
);
this.callback("create", [this.point.geometry, this.point]);
this.point.geometry.clearBounds();
this.layer.addFeatures([this.point], {silent: true});
},
@@ -250,6 +258,7 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
this.point.geometry.y = lonlat.lat;
this.callback("modify", [this.point.geometry, this.point]);
this.point.geometry.clearBounds();
this.drawFeature();
},
/**
@@ -308,16 +317,16 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
if(this.lastDown && this.lastDown.equals(evt.xy)) {
return true;
}
this.drawing = true;
if(this.lastDown == null) {
if(this.persist) {
this.destroyFeature();
}
this.createFeature();
this.createFeature(evt.xy);
} else {
this.modifyFeature(evt.xy);
}
this.lastDown = evt.xy;
this.drawing = true;
this.modifyFeature(evt.xy);
this.drawFeature();
return false;
},
@@ -335,7 +344,6 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
mousemove: function (evt) {
if(this.drawing) {
this.modifyFeature(evt.xy);
this.drawFeature();
}
return true;
},

View File

@@ -37,11 +37,13 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
* 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.
* create - Called when a sketch is first created. Callback called with
* the creation point geometry and sketch feature.
* modify - Called with each move of a vertex with the vertex (point)
* geometry and the sketch feature.
* point - Called as each point is added. Receives the new point geometry.
* done - Called when the point drawing is finished. The callback will
* recieve a single argument, the polygon geometry.
* cancel - Called when the handler is deactivated while drawing. The
* cancel callback will receive a geometry.
*/
@@ -68,6 +70,8 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
this.polygon = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon([this.line.geometry])
);
this.callback("create", [this.point.geometry, this.getSketch()]);
this.point.geometry.clearBounds();
this.layer.addFeatures([this.polygon, this.point], {silent: true});
},

View File

@@ -115,16 +115,19 @@ OpenLayers.Handler.RegularPolygon = OpenLayers.Class(OpenLayers.Handler.Drag, {
*
* Parameters:
* control - {<OpenLayers.Control>} The control that owns this handler
* callbacks - {Array} An object with a 'done' property whos value is a
* function to be called when the polygon drawing is finished.
* The callback should expect to recieve a single argument,
* the polygon 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 object with properties to be set on the handler.
* If the options.sides property is not specified, the number of sides
* will default to 4.
*
* Named callbacks:
* create - Called when a sketch is first created. Callback called with
* the creation point geometry and sketch feature.
* done - Called when the sketch drawing is finished. The callback will
* recieve a single argument, the sketch geometry.
* cancel - Called when the handler is deactivated while drawing. The
* cancel callback will receive a geometry.
*/
initialize: function(control, callbacks, options) {
this.style = OpenLayers.Util.extend(OpenLayers.Feature.Vector.style['default'], {});
@@ -225,6 +228,7 @@ OpenLayers.Handler.RegularPolygon = OpenLayers.Class(OpenLayers.Handler.Drag, {
}
this.feature = new OpenLayers.Feature.Vector();
this.createGeometry();
this.callback("create", [this.origin, this.feature]);
this.layer.addFeatures([this.feature], {silent: true});
this.layer.drawFeature(this.feature, this.style);
},

View File

@@ -81,9 +81,14 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
* property referencing the vertex modified (always a point geometry),
* and a *pixel* property referencing the pixel location of the
* modification.
* sketchstarted - Triggered when a feature sketch bound for this layer
* is started. Listeners will receive an object with a *feature*
* property referencing the new sketch feature and a *vertex* property
* referencing the creation point.
* sketchmodified - Triggered when a feature sketch bound for this layer
* is modified. Listeners will receive an object with a *vertex*
* property referencing the modified vertex.
* property referencing the modified vertex and a *feature* property
* referencing the sketch feature.
* sketchcomplete - Triggered when a feature sketch bound for this layer
* is complete. Listeners will receive an object with a *feature*
* property referencing the sketch feature. By returning false, a
@@ -96,8 +101,8 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
"beforefeatureremoved", "featureremoved", "featuresremoved",
"beforefeatureselected", "featureselected", "featureunselected",
"beforefeaturemodified", "featuremodified", "afterfeaturemodified",
"vertexmodified", "sketchmodified", "sketchcomplete",
"refresh"],
"vertexmodified", "sketchstarted", "sketchmodified",
"sketchcomplete", "refresh"],
/**
* APIProperty: isBaseLayer

View File

@@ -32,7 +32,7 @@
}
function test_sketch_events(t) {
t.plan(4);
t.plan(6);
var map = new OpenLayers.Map("map", {
resolutions: [1]
});
@@ -50,6 +50,9 @@
var log = {};
layer.events.on({
sketchstarted: function(event) {
log.event = event;
},
sketchmodified: function(event) {
log.event = event;
},
@@ -60,6 +63,8 @@
// mock up draw/modify of a point
map.events.triggerEvent("mousedown", {xy: new OpenLayers.Pixel(0, 0)});
t.eq(log.event.type, "sketchstarted", "[mousedown] sketchstarted triggered");
t.geom_eq(log.event.vertex, new OpenLayers.Geometry.Point(-200, 125), "[mousedown] correct vertex");
map.events.triggerEvent("mousemove", {xy: new OpenLayers.Pixel(10, 10)});
t.eq(log.event.type, "sketchmodified", "[mousemove] sketchmodified triggered");
t.geom_eq(log.event.vertex, new OpenLayers.Geometry.Point(-190, 115), "[mousemove] correct vertex");

View File

@@ -70,7 +70,7 @@
}
function test_callbacks(t) {
t.plan(12);
t.plan(15);
var map = new OpenLayers.Map("map", {
resolutions: [1]
});
@@ -83,6 +83,10 @@
});
var log = {};
var handler = new OpenLayers.Handler.Path(control, {
create: function() {
log.type = "create",
log.args = arguments
},
modify: function() {
log.type = "modify",
log.args = arguments
@@ -103,6 +107,9 @@
// mock up feature drawing
handler.activate();
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
t.eq(log.type, "create", "[mousedown] create called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mousedown] correct vertex");
t.ok(log.args[1] === handler.line, "[mousedown] correct sketch feature");
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 0)});
t.eq(log.type, "modify", "[mouseup] modify called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mouseup] correct vertex");

View File

@@ -112,6 +112,10 @@
});
var log = {};
var handler = new OpenLayers.Handler.Point(control, {
create: function() {
log.type = "create",
log.args = arguments
},
modify: function() {
log.type = "modify",
log.args = arguments
@@ -132,7 +136,7 @@
// mock up feature drawing
handler.activate();
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
t.eq(log.type, "modify", "[mousedown] modify called");
t.eq(log.type, "create", "[mousedown] create called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mousedown] correct point");
t.geom_eq(log.args[1].geometry, new OpenLayers.Geometry.Point(-150, 75), "[mousedown] correct sketch feature");
handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(1, 0)});

View File

@@ -72,7 +72,7 @@
}
function test_callbacks(t) {
t.plan(12);
t.plan(15);
var map = new OpenLayers.Map("map", {
resolutions: [1]
});
@@ -85,6 +85,10 @@
});
var log = {};
var handler = new OpenLayers.Handler.Polygon(control, {
create: function() {
log.type = "create",
log.args = arguments
},
modify: function() {
log.type = "modify",
log.args = arguments
@@ -106,6 +110,9 @@
handler.activate();
// click at 0, 0
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
t.eq(log.type, "create", "[mousedown] create called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mousedown] correct vertex");
t.ok(log.args[1] === handler.polygon, "[mousedown] correct sketch feature");
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 0)});
t.eq(log.type, "modify", "[mouseup] modify called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mouseup] correct vertex");