[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * @requires OpenLayers/Handler/Point.js |
---|
| 9 | * @requires OpenLayers/Geometry/Point.js |
---|
| 10 | * @requires OpenLayers/Geometry/LineString.js |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | * Class: OpenLayers.Handler.Path |
---|
| 15 | * Handler to draw a path on the map. Path is displayed on mouse down, |
---|
| 16 | * moves on mouse move, and is finished on mouse up. |
---|
| 17 | * |
---|
| 18 | * Inherits from: |
---|
| 19 | * - <OpenLayers.Handler.Point> |
---|
| 20 | */ |
---|
| 21 | OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, { |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * Property: line |
---|
| 25 | * {<OpenLayers.Feature.Vector>} |
---|
| 26 | */ |
---|
| 27 | line: null, |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * Property: freehand |
---|
| 31 | * {Boolean} In freehand mode, the handler starts the path on mouse down, |
---|
| 32 | * adds a point for every mouse move, and finishes the path on mouse up. |
---|
| 33 | * Outside of freehand mode, a point is added to the path on every mouse |
---|
| 34 | * click and double-click finishes the path. |
---|
| 35 | */ |
---|
| 36 | freehand: false, |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * Property: freehandToggle |
---|
| 40 | * {String} If set, freehandToggle is checked on mouse events and will set |
---|
| 41 | * the freehand mode to the opposite of this.freehand. To disallow |
---|
| 42 | * toggling between freehand and non-freehand mode, set freehandToggle to |
---|
| 43 | * null. Acceptable toggle values are 'shiftKey', 'ctrlKey', and 'altKey'. |
---|
| 44 | */ |
---|
| 45 | freehandToggle: 'shiftKey', |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * Constructor: OpenLayers.Handler.Path |
---|
| 49 | * Create a new path hander |
---|
| 50 | * |
---|
| 51 | * Parameters: |
---|
| 52 | * control - {<OpenLayers.Control>} The control that owns this handler |
---|
| 53 | * callbacks - {Object} An object with a properties whose values are |
---|
| 54 | * functions. Various callbacks described below. |
---|
| 55 | * options - {Object} An optional object with properties to be set on the |
---|
| 56 | * handler |
---|
| 57 | * |
---|
| 58 | * Named callbacks: |
---|
| 59 | * create - Called when a sketch is first created. Callback called with |
---|
| 60 | * the creation point geometry and sketch feature. |
---|
| 61 | * modify - Called with each move of a vertex with the vertex (point) |
---|
| 62 | * geometry and the sketch feature. |
---|
| 63 | * point - Called as each point is added. Receives the new point geometry. |
---|
| 64 | * done - Called when the point drawing is finished. The callback will |
---|
| 65 | * recieve a single argument, the linestring geometry. |
---|
| 66 | * cancel - Called when the handler is deactivated while drawing. The |
---|
| 67 | * cancel callback will receive a geometry. |
---|
| 68 | */ |
---|
| 69 | initialize: function(control, callbacks, options) { |
---|
| 70 | OpenLayers.Handler.Point.prototype.initialize.apply(this, arguments); |
---|
| 71 | }, |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * Method: createFeature |
---|
| 75 | * Add temporary geometries |
---|
| 76 | * |
---|
| 77 | * Parameters: |
---|
| 78 | * pixel - {<OpenLayers.Pixel>} The initial pixel location for the new |
---|
| 79 | * feature. |
---|
| 80 | */ |
---|
| 81 | createFeature: function(pixel) { |
---|
| 82 | var lonlat = this.control.map.getLonLatFromPixel(pixel); |
---|
| 83 | this.point = new OpenLayers.Feature.Vector( |
---|
| 84 | new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat) |
---|
| 85 | ); |
---|
| 86 | this.line = new OpenLayers.Feature.Vector( |
---|
| 87 | new OpenLayers.Geometry.LineString([this.point.geometry]) |
---|
| 88 | ); |
---|
| 89 | this.callback("create", [this.point.geometry, this.getSketch()]); |
---|
| 90 | this.point.geometry.clearBounds(); |
---|
| 91 | this.layer.addFeatures([this.line, this.point], {silent: true}); |
---|
| 92 | }, |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | * Method: destroyFeature |
---|
| 96 | * Destroy temporary geometries |
---|
| 97 | */ |
---|
| 98 | destroyFeature: function() { |
---|
| 99 | OpenLayers.Handler.Point.prototype.destroyFeature.apply(this); |
---|
| 100 | this.line = null; |
---|
| 101 | }, |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Method: removePoint |
---|
| 105 | * Destroy the temporary point. |
---|
| 106 | */ |
---|
| 107 | removePoint: function() { |
---|
| 108 | if(this.point) { |
---|
| 109 | this.layer.removeFeatures([this.point]); |
---|
| 110 | } |
---|
| 111 | }, |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * Method: addPoint |
---|
| 115 | * Add point to geometry. Send the point index to override |
---|
| 116 | * the behavior of LinearRing that disregards adding duplicate points. |
---|
| 117 | * |
---|
| 118 | * Parameters: |
---|
| 119 | * pixel - {<OpenLayers.Pixel>} The pixel location for the new point. |
---|
| 120 | */ |
---|
| 121 | addPoint: function(pixel) { |
---|
| 122 | this.layer.removeFeatures([this.point]); |
---|
| 123 | var lonlat = this.control.map.getLonLatFromPixel(pixel); |
---|
| 124 | this.point = new OpenLayers.Feature.Vector( |
---|
| 125 | new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat) |
---|
| 126 | ); |
---|
| 127 | this.line.geometry.addComponent( |
---|
| 128 | this.point.geometry, this.line.geometry.components.length |
---|
| 129 | ); |
---|
| 130 | this.callback("point", [this.point.geometry, this.getGeometry()]); |
---|
| 131 | this.callback("modify", [this.point.geometry, this.getSketch()]); |
---|
| 132 | this.drawFeature(); |
---|
| 133 | }, |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | * Method: freehandMode |
---|
| 137 | * Determine whether to behave in freehand mode or not. |
---|
| 138 | * |
---|
| 139 | * Returns: |
---|
| 140 | * {Boolean} |
---|
| 141 | */ |
---|
| 142 | freehandMode: function(evt) { |
---|
| 143 | return (this.freehandToggle && evt[this.freehandToggle]) ? |
---|
| 144 | !this.freehand : this.freehand; |
---|
| 145 | }, |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | * Method: modifyFeature |
---|
| 149 | * Modify the existing geometry given the new point |
---|
| 150 | * |
---|
| 151 | * Parameters: |
---|
| 152 | * pixel - {<OpenLayers.Pixel>} The updated pixel location for the latest |
---|
| 153 | * point. |
---|
| 154 | */ |
---|
| 155 | modifyFeature: function(pixel) { |
---|
| 156 | var lonlat = this.control.map.getLonLatFromPixel(pixel); |
---|
| 157 | this.point.geometry.x = lonlat.lon; |
---|
| 158 | this.point.geometry.y = lonlat.lat; |
---|
| 159 | this.callback("modify", [this.point.geometry, this.getSketch()]); |
---|
| 160 | this.point.geometry.clearBounds(); |
---|
| 161 | this.drawFeature(); |
---|
| 162 | }, |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | * Method: drawFeature |
---|
| 166 | * Render geometries on the temporary layer. |
---|
| 167 | */ |
---|
| 168 | drawFeature: function() { |
---|
| 169 | this.layer.drawFeature(this.line, this.style); |
---|
| 170 | this.layer.drawFeature(this.point, this.style); |
---|
| 171 | }, |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * Method: getSketch |
---|
| 175 | * Return the sketch feature. |
---|
| 176 | * |
---|
| 177 | * Returns: |
---|
| 178 | * {<OpenLayers.Feature.Vector>} |
---|
| 179 | */ |
---|
| 180 | getSketch: function() { |
---|
| 181 | return this.line; |
---|
| 182 | }, |
---|
| 183 | |
---|
| 184 | /** |
---|
| 185 | * Method: getGeometry |
---|
| 186 | * Return the sketch geometry. If <multi> is true, this will return |
---|
| 187 | * a multi-part geometry. |
---|
| 188 | * |
---|
| 189 | * Returns: |
---|
| 190 | * {<OpenLayers.Geometry.LineString>} |
---|
| 191 | */ |
---|
| 192 | getGeometry: function() { |
---|
| 193 | var geometry = this.line && this.line.geometry; |
---|
| 194 | if(geometry && this.multi) { |
---|
| 195 | geometry = new OpenLayers.Geometry.MultiLineString([geometry]); |
---|
| 196 | } |
---|
| 197 | return geometry; |
---|
| 198 | }, |
---|
| 199 | |
---|
| 200 | /** |
---|
| 201 | * Method: mousedown |
---|
| 202 | * Handle mouse down. Add a new point to the geometry and |
---|
| 203 | * render it. Return determines whether to propagate the event on the map. |
---|
| 204 | * |
---|
| 205 | * Parameters: |
---|
| 206 | * evt - {Event} The browser event |
---|
| 207 | * |
---|
| 208 | * Returns: |
---|
| 209 | * {Boolean} Allow event propagation |
---|
| 210 | */ |
---|
| 211 | mousedown: function(evt) { |
---|
| 212 | // ignore double-clicks |
---|
| 213 | if (this.lastDown && this.lastDown.equals(evt.xy)) { |
---|
| 214 | return false; |
---|
| 215 | } |
---|
| 216 | if(this.lastDown == null) { |
---|
| 217 | if(this.persist) { |
---|
| 218 | this.destroyFeature(); |
---|
| 219 | } |
---|
| 220 | this.createFeature(evt.xy); |
---|
| 221 | } else if((this.lastUp == null) || !this.lastUp.equals(evt.xy)) { |
---|
| 222 | this.addPoint(evt.xy); |
---|
| 223 | } |
---|
| 224 | this.mouseDown = true; |
---|
| 225 | this.lastDown = evt.xy; |
---|
| 226 | this.drawing = true; |
---|
| 227 | return false; |
---|
| 228 | }, |
---|
| 229 | |
---|
| 230 | /** |
---|
| 231 | * Method: mousemove |
---|
| 232 | * Handle mouse move. Adjust the geometry and redraw. |
---|
| 233 | * Return determines whether to propagate the event on the map. |
---|
| 234 | * |
---|
| 235 | * Parameters: |
---|
| 236 | * evt - {Event} The browser event |
---|
| 237 | * |
---|
| 238 | * Returns: |
---|
| 239 | * {Boolean} Allow event propagation |
---|
| 240 | */ |
---|
| 241 | mousemove: function (evt) { |
---|
| 242 | if(this.drawing) { |
---|
| 243 | if(this.mouseDown && this.freehandMode(evt)) { |
---|
| 244 | this.addPoint(evt.xy); |
---|
| 245 | } else { |
---|
| 246 | this.modifyFeature(evt.xy); |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | return true; |
---|
| 250 | }, |
---|
| 251 | |
---|
| 252 | /** |
---|
| 253 | * Method: mouseup |
---|
| 254 | * Handle mouse up. Send the latest point in the geometry to |
---|
| 255 | * the control. Return determines whether to propagate the event on the map. |
---|
| 256 | * |
---|
| 257 | * Parameters: |
---|
| 258 | * evt - {Event} The browser event |
---|
| 259 | * |
---|
| 260 | * Returns: |
---|
| 261 | * {Boolean} Allow event propagation |
---|
| 262 | */ |
---|
| 263 | mouseup: function (evt) { |
---|
| 264 | this.mouseDown = false; |
---|
| 265 | if(this.drawing) { |
---|
| 266 | if(this.freehandMode(evt)) { |
---|
| 267 | this.removePoint(); |
---|
| 268 | this.finalize(); |
---|
| 269 | } else { |
---|
| 270 | if(this.lastUp == null) { |
---|
| 271 | this.addPoint(evt.xy); |
---|
| 272 | } |
---|
| 273 | this.lastUp = evt.xy; |
---|
| 274 | } |
---|
| 275 | return false; |
---|
| 276 | } |
---|
| 277 | return true; |
---|
| 278 | }, |
---|
| 279 | |
---|
| 280 | /** |
---|
| 281 | * Method: dblclick |
---|
| 282 | * Handle double-clicks. Finish the geometry and send it back |
---|
| 283 | * to the control. |
---|
| 284 | * |
---|
| 285 | * Parameters: |
---|
| 286 | * evt - {Event} The browser event |
---|
| 287 | * |
---|
| 288 | * Returns: |
---|
| 289 | * {Boolean} Allow event propagation |
---|
| 290 | */ |
---|
| 291 | dblclick: function(evt) { |
---|
| 292 | if(!this.freehandMode(evt)) { |
---|
| 293 | var index = this.line.geometry.components.length - 1; |
---|
| 294 | this.line.geometry.removeComponent(this.line.geometry.components[index]); |
---|
| 295 | this.removePoint(); |
---|
| 296 | this.finalize(); |
---|
| 297 | } |
---|
| 298 | return false; |
---|
| 299 | }, |
---|
| 300 | |
---|
| 301 | CLASS_NAME: "OpenLayers.Handler.Path" |
---|
| 302 | }); |
---|