summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/map/tools/polyline.js
blob: 6716180a41ce88169ecbc0dd19b342319c31cb8e (plain)
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
var PolylineTool = MapTool.extend(function (base) {
  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 (shapes.workline.points.length > 2) {
          shapes.workline.build()
          shapes.add(shapes.workline)
        }
        else {
          shapes.workline.reset()
        }
        return
      }
      map.ui.tools.position.rightclick(e, cursor)
      return
    }

    // compare to initial point
    var p = last_point.clone()
    if (map.ui.placing) {
      if (shapes.workline.canCloseWith(p)) {
        shapes.workline.close()
        shapes.workline.build()
        shapes.add(shapes.workline)
        map.ui.placing = false
      }
      else {
        shapes.workline.add(p)
      }
    }
    else {
      map.ui.placing = true
      shapes.workline = new Polyline ()
      shapes.workline.add(p)
    }
  }
  exports.move = function(e, cursor){
    last_point.a = cursor.x.a
    last_point.b = cursor.y.a
    if (map.ui.placing && shapes.workline.canCloseWith(last_point)) {
      document.body.style.cursor = "pointer"
      last_point.assign(shapes.workline.points[0])
      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) { shapes.workline.reset() }
    map.ui.placing = false
  }
  return exports
})