summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/engine/shapes/shapelist.js
blob: 932ce36eeabc5f589718c33ddeef584d4d9864bd (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
var ShapeList = Fiber.extend(function(base){
  var exports = {}
  exports.init = function(){
    this.shapes = []
    this.workline = null
  }
  exports.add = function(shape){
    this.shapes.push(shape)
  }
  exports.remove = function(shape){
    var index = this.shapes.indexOf(shape)
    if (index !== -1) {
      this.shapes.splice(index, 1)
    }
  }
  exports.removeSegment = function (segment){
    var shape = segment.shape
    var head = shape.getHeadAtIndex(segment.head)
    var tail = shape.getTailAtIndex(segment.tail)
    this.remove(shape)
    shape.destroy()
    if (head) {
      this.add(head)
      head.build()
    }
    if (tail) {
      this.add(tail)
      tail.build()
    }
  }
  exports.findClosestPoint = function (p){
    var point
    for (var i = 0; i < this.shapes.length; i++) {
      point = this.shapes[i].hasPointNear(p)
      if (point) return { point: point, shape: this.shapes[i] }
    }
    return null
  }
  exports.findClosestEndPoint = function (p){
    var point
    for (var i = 0; i < this.shapes.length; i++) {
      point = this.shapes[i].hasEndPointNear(p)
      if (point) return { point: point, shape: this.shapes[i] }
    }
    return null
  }
  exports.findClosestSegment = function (p){
    var segment = null, closest_segment = null
    for (var i = 0; i < this.shapes.length; i++) {
      segment = this.shapes[i].hasSegmentNear(p, 10)
      if (segment && (! closest_segment || segment.distance < closest_segment.distance)) {
        closest_segment = segment
        closest_segment.shape = this.shapes[i]
      }
    }
    return closest_segment
  }
  exports.forEach = function(fn){
    this.shapes.forEach(fn)
  }
  return exports
})