summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--public/assets/test/ortho3.html117
1 files changed, 116 insertions, 1 deletions
diff --git a/public/assets/test/ortho3.html b/public/assets/test/ortho3.html
index d6241ab..319f788 100644
--- a/public/assets/test/ortho3.html
+++ b/public/assets/test/ortho3.html
@@ -211,6 +211,108 @@ var PolylineTool = MapTool.extend(function (base) {
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 (placing) {
+ // close polyline or cancel
+ placing = false
+ if (line.points.length > 2) {
+ line.build()
+ }
+ else {
+ line.reset()
+ }
+ return
+ }
+ map.ui.tools.position.down(e, cursor)
+ return
+ }
+
+ // compare to initial point
+ var p = last_point.clone()
+ if (placing) {
+ if (line.lastPoint().eq(p)) {
+ return
+ }
+ else if (line.canCloseWith(p)) {
+ line.close()
+ line.build()
+ placing = false
+ }
+ else {
+ line.add(p)
+ prev_point = p
+ horizontal = ! horizontal
+ }
+ }
+ else {
+ 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 (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
+ }
+ }
+ else {
+ document.body.style.cursor = "crosshair"
+ }
+ }
+ exports.cancel = function(){
+ if (placing) { line.reset() }
+ first_point = null
+ placing = false
+ }
+ return exports
})
@@ -239,6 +341,13 @@ var Polyline = Fiber.extend(function(base){
this.mx_points.push( new MX.Point(p) )
}
+ exports.firstPoint = function(){
+ return this.points[0]
+ }
+ exports.lastPoint = function(){
+ return this.points[this.points.length-1]
+ }
+
exports.canCloseWith = function(p){
return (this.points.length > 2 && this.points[0].distanceTo( p ) < 10/map.zoom)
}
@@ -437,16 +546,21 @@ var Toolbar = Fiber.extend(function(base){
exports.init = function(rapper){
this.rapper = (typeof rapper == "string") ? $(rapper)[0] : rapper
this.tools = {}
+ this.els = {}
}
exports.add = function(role, fn){
var self = this
this.tools[role] = fn
- $("[data-role=" + role + "]", self.rapper).click(function(){
+ this.els[role] = $("[data-role=" + role + "]", self.rapper)
+ this.els[role].click(function(){
$(".active", self.rapper).removeClass('active')
$(this).addClass('active')
fn()
})
}
+ exports.pick = function(role){
+ this.els[role].trigger("click")
+ }
return exports
})
@@ -476,6 +590,7 @@ OrthographicToolbar.add("polyline-mode", function(){
OrthographicToolbar.add("ortho-polyline-mode", function(){
map.ui.set_tool("ortho-polyline")
})
+OrthographicToolbar.pick("ortho-polyline-mode")
document.addEventListener('DOMContentLoaded', build)
function build () {