From 70f42b062934784c1ef8e593cbc7f8b05049f946 Mon Sep 17 00:00:00 2001 From: Xavier Mamano Date: Thu, 12 Jan 2012 08:46:15 +0100 Subject: [PATCH 1/2] Stop freehand using "maxVertices" --- lib/OpenLayers/Handler/Path.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/OpenLayers/Handler/Path.js b/lib/OpenLayers/Handler/Path.js index 9557a7f4c7..57d112b2a0 100644 --- a/lib/OpenLayers/Handler/Path.js +++ b/lib/OpenLayers/Handler/Path.js @@ -30,8 +30,7 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, { * APIProperty: maxVertices * {Number} The maximum number of vertices which can be drawn by this * handler. When the number of vertices reaches maxVertices, the - * geometry is automatically finalized. This property doesn't - * apply if freehand is set. Default is null. + * geometry is automatically finalized. Default is null. */ maxVertices: null, @@ -446,7 +445,13 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, { if(this.persist) { this.destroyPersistedFeature(); } - this.addPoint(evt.xy); + if(this.maxVertices && this.line && + this.line.geometry.components.length === this.maxVertices) { + this.removePoint() + this.finalize(); + } else { + this.addPoint(evt.xy); + } return false; } if (!this.touch && (!this.mouseDown || this.stoppedDown)) { From 59dfb7ec110fc041cc2c9f75a9384b8299fa99a8 Mon Sep 17 00:00:00 2001 From: Xavier Mamano Date: Thu, 12 Jan 2012 20:23:01 +0100 Subject: [PATCH 2/2] Add test for stop freehand using "maxVertices" --- tests/Handler/Path.html | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/Handler/Path.html b/tests/Handler/Path.html index 24ab4c38fb..66548d624a 100644 --- a/tests/Handler/Path.html +++ b/tests/Handler/Path.html @@ -534,6 +534,43 @@ map.destroy(); } + function test_freehand_maxVertices(t) { + t.plan(1); + var map = new OpenLayers.Map("map", { + resolutions: [1] + }); + var layer = new OpenLayers.Layer.Vector("foo", { + maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10), + isBaseLayer: true + }); + map.addLayer(layer); + var control = new OpenLayers.Control({}); + var log = {}; + var MAX_VERTICES = 2; + var doneCallback = function(geo) { + t.eq(geo.components.length, MAX_VERTICES, + 'When maxVertices is reached, the geometry is finalized automatically'); + }; + var handler = new OpenLayers.Handler.Path(control, + {'done': doneCallback}, + {freehand: true, + maxVertices: MAX_VERTICES}); + control.handler = handler; + map.addControl(control); + map.setCenter(new OpenLayers.LonLat(0, 0), 0); + + // mock up feature freehand drawing + handler.activate(); + var evt = {xy: new OpenLayers.Pixel(0, 0)}; + handler.mousemove(evt); + handler.mousedown(evt); + evt = {xy: new OpenLayers.Pixel(20, 20)}; + handler.mousemove(evt); + evt = {xy: new OpenLayers.Pixel(40, 40)}; + handler.mousemove(evt); + map.destroy(); + } + /** * Helper functions for editing method tests */