var OrthoPolylineTool = MapTool.extend(function (base) { // this will work like normal polyline except all walls will be orthogonal var prev_point, horizontal = false, first_edge_is_horizontal = false var exports = {} exports.down = function(e, cursor){ // rightclick? if (e.ctrlKey || e.which === 3) { e.preventDefault() e.stopPropagation() if (map.ui.placing) { // close polyline or cancel map.ui.placing = false if (line.points.length > 2) { line.build() } else { line.reset() } return } else { map.ui.tools.position.rightclick(e, cursor) } return } // compare to initial point var p = last_point.clone() if (map.ui.placing) { if (line.lastPoint().eq(p)) { return } else if (line.canCloseWith(p)) { line.close() line.build() map.ui.placing = false } else { line.add(p) prev_point = p horizontal = ! horizontal } } else { map.ui.placing = true line = new Polyline () line.add(p) first_point = prev_point = p horizontal = false } } exports.move = function(e, cursor){ last_point.a = cursor.x.a last_point.b = cursor.y.a if (map.ui.placing) { if (line.points.length == 1) { var x = abs(prev_point.a - last_point.a) var y = abs(prev_point.b - last_point.b) if (x > y) { last_point.b = prev_point.b first_edge_is_horizontal = horizontal = true } else { last_point.a = prev_point.a first_edge_is_horizontal = horizontal = false } } else { if (horizontal) { last_point.b = prev_point.b } else { last_point.a = prev_point.a } if (horizontal == first_edge_is_horizontal) { // check if this point is within N pixels of the normal // and lock it into place if so if (horizontal && abs( first_point.a - last_point.a ) < 10/map.zoom) { last_point.a = first_point.a } else if (! horizontal && abs( first_point.b - last_point.b ) < 10/map.zoom) { last_point.b = first_point.b } } } if (line.canCloseWith(last_point)) { document.body.style.cursor = "pointer" last_point.assign(first_point) cursor.x.a = cursor.x.b = last_point.a cursor.y.a = cursor.y.b = last_point.b } return } var end_point = shapes.findClosestEndPoint(last_point) if (end_point) { document.body.style.cursor = "pointer" last_point.assign(end_point.point) cursor.x.a = cursor.x.b = last_point.a cursor.y.a = cursor.y.b = last_point.b return } else { document.body.style.cursor = "crosshair" } } exports.cancel = function(){ if (map.ui.placing) { line.reset() } first_point = null map.ui.placing = false } return exports })