From dfcf68007ee39abb1988c7de8f23451a5a7e300e Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sun, 12 Apr 2009 15:36:15 +0000 Subject: [PATCH] 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 --- examples/modify-feature.html | 4 +++- lib/OpenLayers/Control/DrawFeature.js | 5 +++++ lib/OpenLayers/Handler/Path.js | 10 ++++++--- lib/OpenLayers/Handler/Point.js | 26 ++++++++++++++++-------- lib/OpenLayers/Handler/Polygon.js | 10 ++++++--- lib/OpenLayers/Handler/RegularPolygon.js | 18 +++++++++------- lib/OpenLayers/Layer/Vector.js | 11 +++++++--- tests/Control/DrawFeature.html | 7 ++++++- tests/Handler/Path.html | 9 +++++++- tests/Handler/Point.html | 6 +++++- tests/Handler/Polygon.html | 9 +++++++- 11 files changed, 85 insertions(+), 30 deletions(-) diff --git a/examples/modify-feature.html b/examples/modify-feature.html index 09cfe66746..29bf490dd6 100644 --- a/examples/modify-feature.html +++ b/examples/modify-feature.html @@ -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, diff --git a/lib/OpenLayers/Control/DrawFeature.js b/lib/OpenLayers/Control/DrawFeature.js index 2dc16dd526..6056fda869 100644 --- a/lib/OpenLayers/Control/DrawFeature.js +++ b/lib/OpenLayers/Control/DrawFeature.js @@ -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 diff --git a/lib/OpenLayers/Handler/Path.js b/lib/OpenLayers/Handler/Path.js index e610b752f9..999470b5d5 100644 --- a/lib/OpenLayers/Handler/Path.js +++ b/lib/OpenLayers/Handler/Path.js @@ -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}); }, diff --git a/lib/OpenLayers/Handler/Point.js b/lib/OpenLayers/Handler/Point.js index bcd656123e..3ad06faaa2 100644 --- a/lib/OpenLayers/Handler/Point.js +++ b/lib/OpenLayers/Handler/Point.js @@ -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 - {} 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; }, diff --git a/lib/OpenLayers/Handler/Polygon.js b/lib/OpenLayers/Handler/Polygon.js index 5e4db7746c..f40f8473a5 100644 --- a/lib/OpenLayers/Handler/Polygon.js +++ b/lib/OpenLayers/Handler/Polygon.js @@ -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}); }, diff --git a/lib/OpenLayers/Handler/RegularPolygon.js b/lib/OpenLayers/Handler/RegularPolygon.js index a84b1b5216..88ae5f8dcb 100644 --- a/lib/OpenLayers/Handler/RegularPolygon.js +++ b/lib/OpenLayers/Handler/RegularPolygon.js @@ -115,16 +115,19 @@ OpenLayers.Handler.RegularPolygon = OpenLayers.Class(OpenLayers.Handler.Drag, { * * Parameters: * 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); }, diff --git a/lib/OpenLayers/Layer/Vector.js b/lib/OpenLayers/Layer/Vector.js index 3ec749c33c..9db527b700 100644 --- a/lib/OpenLayers/Layer/Vector.js +++ b/lib/OpenLayers/Layer/Vector.js @@ -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 diff --git a/tests/Control/DrawFeature.html b/tests/Control/DrawFeature.html index 57417aba2f..eefdea5ce3 100644 --- a/tests/Control/DrawFeature.html +++ b/tests/Control/DrawFeature.html @@ -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"); diff --git a/tests/Handler/Path.html b/tests/Handler/Path.html index b81b7634ce..a46a055633 100644 --- a/tests/Handler/Path.html +++ b/tests/Handler/Path.html @@ -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"); diff --git a/tests/Handler/Point.html b/tests/Handler/Point.html index da72b916c9..fb10edf8aa 100644 --- a/tests/Handler/Point.html +++ b/tests/Handler/Point.html @@ -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)}); diff --git a/tests/Handler/Polygon.html b/tests/Handler/Polygon.html index a78b7406f4..dba05cbb6f 100644 --- a/tests/Handler/Polygon.html +++ b/tests/Handler/Polygon.html @@ -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");