ArrowPathHandle.js 12.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for 
* full list of contributors). Published under the Clear BSD license.  
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */


/**
* @requires OpenLayers/Handler/Point.js
* @requires OpenLayers/Geometry/Point.js
* @requires OpenLayers/Geometry/LineString.js
*/

/**
* Class: OpenLayers.Handler.Path
* Handler to draw a path on the map.  Path is displayed on mouse down,
* moves on mouse move, and is finished on mouse up.
*
* Inherits from:
*  - <OpenLayers.Handler.Point>
*/
MilStdPathHandle = OpenLayers.Class(OpenLayers.Handler.Point, {

    /**
    * Property: Orgline 表示原始的路径
    * {<OpenLayers.Feature.Vector>}
    */
    Orgline: null,

    /**
    * Property: line
    * {<OpenLayers.Feature.Vector>}
    */
    line: null,

    /**
    * 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.
    */
    maxVertices: null,

    /**
    * Property: doubleTouchTolerance
    * {Number} Maximum number of pixels between two touches for
    *     the gesture to be considered a "finalize feature" action.
    *     Default is 20.
    */
    doubleTouchTolerance: 20,

    /**
    * Property: freehand
    * {Boolean} In freehand mode, the handler starts the path on mouse down,
    * adds a point for every mouse move, and finishes the path on mouse up.
    * Outside of freehand mode, a point is added to the path on every mouse
    * click and double-click finishes the path.
    */
    freehand: false,

    /**
    * Property: freehandToggle
    * {String} If set, freehandToggle is checked on mouse events and will set
    * the freehand mode to the opposite of this.freehand.  To disallow
    * toggling between freehand and non-freehand mode, set freehandToggle to
    * null.  Acceptable toggle values are 'shiftKey', 'ctrlKey', and 'altKey'.
    */
    freehandToggle: 'shiftKey',

    /**
    * Property: timerId
    * {Integer} The timer used to test the double touch.
    */
    timerId: null,

    /**
    * Constructor: OpenLayers.Handler.Path
    * Create a new path hander
    *
    * Parameters:
    * control - {<OpenLayers.Control>} The control that owns this handler
    * callbacks - {Object} An object with a properties whose values are
    *     functions.  Various callbacks described below.
    * options - {Object} An optional object with properties to be set on the
    *           handler
    *
    * Named callbacks:
    * 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.
    */
    initialize: function (control, callbacks, options) {
        OpenLayers.Handler.Point.prototype.initialize.apply(this, arguments);
    },

    /**
    * Method: createFeature
    * Add temporary geometries
    *
    * Parameters:
    * pixel - {<OpenLayers.Pixel>} The initial pixel location for the new
    *     feature.
    */
    createFeature: function (pixel) {
        var geometry;
        if (pixel) {
            var lonlat = this.map.getLonLatFromPixel(pixel);
            geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
        } else {
            geometry = new OpenLayers.Geometry.Point();
        }
        this.point = new OpenLayers.Feature.Vector(geometry);

        this.Orgline = new OpenLayers.Feature.Vector(
            new OpenLayers.Geometry.LineString([this.point.geometry])
        );

        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 });
    },

    /**
    * Method: destroyFeature
    * Destroy temporary geometries
    */
    destroyFeature: function () {
        OpenLayers.Handler.Point.prototype.destroyFeature.apply(this);
        this.line = null;
    },

    /**
    * Method: destroyPersistedFeature
    * Destroy the persisted feature.
    */
    destroyPersistedFeature: function () {
        var layer = this.layer;
        if (layer && layer.features.length > 2) {
            this.layer.features[0].destroy();
        }
    },

    /**
    * Method: removePoint
    * Destroy the temporary point.
    */
    removePoint: function () {
        if (this.point) {
            this.layer.removeFeatures([this.point]);
        }
    },

    /**
    * Method: addPoint
    * Add point to geometry.  Send the point index to override
    * the behavior of LinearRing that disregards adding duplicate points.
    *
    * Parameters:
    * pixel - {<OpenLayers.Pixel>} The pixel location for the new point.
    */
    addPoint: function (pixel) {
        this.layer.removeFeatures([this.point]);
        var lonlat = this.control.map.getLonLatFromPixel(pixel);
        this.point = new OpenLayers.Feature.Vector(
            new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
        );
        this.line.geometry.addComponent(
            this.point.geometry, this.line.geometry.components.length
        );
        this.layer.addFeatures([this.point]);
        this.callback("point", [this.point.geometry, this.getGeometry()]);
        this.callback("modify", [this.point.geometry, this.getSketch()]);
        this.drawFeature();
    },

    /**
    * Method: freehandMode
    * Determine whether to behave in freehand mode or not.
    *
    * Returns:
    * {Boolean}
    */
    freehandMode: function (evt) {
        return (this.freehandToggle && evt[this.freehandToggle]) ?
                    !this.freehand : this.freehand;
    },

    /**
    * Method: modifyFeature
    * Modify the existing geometry given the new point
    *
    * Parameters:
    * pixel - {<OpenLayers.Pixel>} The updated pixel location for the latest
    *     point.
    * drawing - {Boolean} Indicate if we're currently drawing.
    */
    modifyFeature: function (pixel, drawing) {
        var lonlat = this.control.map.getLonLatFromPixel(pixel);
        this.point.geometry.x = lonlat.lon;
        this.point.geometry.y = lonlat.lat;
        this.callback("modify", [this.point.geometry, this.getSketch(), drawing]);
        this.point.geometry.clearBounds();
        this.drawFeature();
    },

    /**
    * Method: drawFeature
    * Render geometries on the temporary layer.
    */
    drawFeature: function () {
        this.layer.drawFeature(this.line, this.style);
        this.layer.drawFeature(this.point, this.style);
    },

    /**
    * Method: getSketch
    * Return the sketch feature.
    *
    * Returns:
    * {<OpenLayers.Feature.Vector>}
    */
    getSketch: function () {
        return this.line;
    },

    /**
    * Method: getGeometry
    * Return the sketch geometry.  If <multi> is true, this will return
    *     a multi-part geometry.
    *
    * Returns:
    * {<OpenLayers.Geometry.LineString>}
    */
    getGeometry: function () {
        var geometry = this.line && this.line.geometry;
        if (geometry && this.multi) {
            geometry = new OpenLayers.Geometry.MultiLineString([geometry]);
        }
        return geometry;
    },

    /**
    * method: touchstart
    * handle touchstart.
    *
    * parameters:
    * evt - {event} the browser event
    *
    * returns:
    * {boolean} allow event propagation
    */
    touchstart: function (evt) {
        if (this.timerId &&
            this.passesTolerance(this.lastTouchPx, evt.xy,
                                 this.doubleTouchTolerance)) {
            // double-tap, finalize the geometry
            this.finishGeometry();
            window.clearTimeout(this.timerId);
            this.timerId = null;
            return false;
        } else {
            if (this.timerId) {
                window.clearTimeout(this.timerId);
                this.timerId = null;
            }
            this.timerId = window.setTimeout(
                OpenLayers.Function.bind(function () {
                    this.timerId = null;
                }, this), 300);
            return OpenLayers.Handler.Point.prototype.touchstart.call(this, evt);
        }
    },

    /**
    * Method: down
    * Handle mousedown and touchstart.  Add a new point to the geometry and
    * render it. Return determines whether to propagate the event on the map.
    * 
    * Parameters:
    * evt - {Event} The browser event
    *
    * Returns: 
    * {Boolean} Allow event propagation
    */
    down: function (evt) {
        var stopDown = this.stopDown;
        if (this.freehandMode(evt)) {
            stopDown = true;
        }
        if (!this.touch && (!this.lastDown ||
                            !this.passesTolerance(this.lastDown, evt.xy,
                                                  this.pixelTolerance))) {
            this.modifyFeature(evt.xy, !!this.lastUp);
        }
        this.mouseDown = true;
        this.lastDown = evt.xy;
        this.stoppedDown = stopDown;
        return !stopDown;
    },

    /**
    * Method: move
    * Handle mousemove and touchmove.  Adjust the geometry and redraw.
    * Return determines whether to propagate the event on the map.
    * 
    * Parameters:
    * evt - {Event} The browser event
    *
    * Returns: 
    * {Boolean} Allow event propagation
    */
    move: function (evt) {
        if (this.stoppedDown && this.freehandMode(evt)) {
            if (this.persist) {
                this.destroyPersistedFeature();
            }
            this.addPoint(evt.xy);
            return false;
        }
        if (!this.touch && (!this.mouseDown || this.stoppedDown)) {
            this.modifyFeature(evt.xy, !!this.lastUp);
        }
        return true;
    },

    /**
    * Method: up
    * Handle mouseup and touchend.  Send the latest point in the geometry to
    * the control. Return determines whether to propagate the event on the map.
    * 
    * Parameters:
    * evt - {Event} The browser event
    *
    * Returns: 
    * {Boolean} Allow event propagation
    */
    up: function (evt) {
        if (this.mouseDown && (!this.lastUp || !this.lastUp.equals(evt.xy))) {
            if (this.stoppedDown && this.freehandMode(evt)) {
                this.removePoint();
                this.finalize();
            } else {
                if (this.passesTolerance(this.lastDown, evt.xy,
                                         this.pixelTolerance)) {
                    if (this.touch) {
                        this.modifyFeature(evt.xy);
                    }
                    if (this.lastUp == null && this.persist) {
                        this.destroyPersistedFeature();
                    }
                    this.addPoint(evt.xy);
                    this.lastUp = evt.xy;
                    if (this.line.geometry.components.length === this.maxVertices + 1) {
                        this.finishGeometry();
                    }
                }
            }
        }
        this.stoppedDown = this.stopDown;
        this.mouseDown = false;
        return !this.stopUp;
    },

    /**
    * APIMethod: finishGeometry
    * Finish the geometry and send it back to the control.
    */
    finishGeometry: function () {
        var index = this.line.geometry.components.length - 1;
        this.line.geometry.removeComponent(this.line.geometry.components[index]);
        this.removePoint();
        this.finalize();
    },

    /**
    * Method: dblclick 
    * Handle double-clicks.
    * 
    * Parameters:
    * evt - {Event} The browser event
    *
    * Returns: 
    * {Boolean} Allow event propagation
    */
    dblclick: function (evt) {
        if (!this.freehandMode(evt)) {
            this.finishGeometry();
        }
        return false;
    },

    CLASS_NAME: "MilStdPathHandle"
});