summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/ui/blueprint/BlueprintScaler.js
blob: addf9a90304f25e5687bedaff00ab8232b3be609 (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
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
var BlueprintScaler = ModalFormView.extend({
	el: ".blueprintScaler",
	
	method: "/api/media/scale",
	
	events: {
    "change [name=blueprint-dimensions]": "changeDimensions",
    "change [name=blueprint-units]": "changeUnits",
    "click .uploadNewBlueprint": "showBlueprintUpload",
    "click #saveBlueprint": "save",
	},
	
	initialize: function(){
    this.$blueprintMap = this.$("#blueprintMap")
    this.$blueprintDimensionsRapper = this.$("#blueprintDimensions")
    this.$dimensions = this.$("[name=blueprint-dimensions]")
    this.$pixels = this.$("[name=blueprint-pixels]")
    this.$units = this.$("[name=blueprint-units]")
    this.$save = this.$("#saveBlueprint")

    this.map = map = new Map ({
      type: "ortho",
      el: this.$blueprintMap.get(0),
      width: window.innerWidth,
      height: window.innerHeight,
      zoom: -2,
      zoom_min: -6.2,
      zoom_max: 1,
    })
    this.lineTool = new LineTool
    map.ui.add_tool("line", this.lineTool)
    map.ui.set_tool("line")
    
    scene = scene || { camera: { x: 0, y: 0, z: 0 } }
    
    this.floorplan = new MX.Image ()
    
    this.animating = false
	},
	
	showBlueprintUpload: function(){
	  this.parent.blueprintUpload.show()
	},
	
	pick: function(media){
	  this.media = media
    
    if (!! media.scale) {
      this.parent.useFloorplan(media)
    }

    this.floorplan.load({ media: media, keepImage: true })

    if (! this.animating) {
      this.animating = true
      this.animate()
    }
	},
	
	animate: function(t){
    requestAnimationFrame(this.animate.bind(this))
    
    if (! this.animating) return

    var dt = t - this.last_t
    this.last_t = t

    if (! t) return

    this.map.update(t)

    this.map.draw.ctx.save()
    this.map.draw.translate()

    this.floorplan.draw(this.map.draw.ctx, true)
    
    this.map.draw.ctx.strokeStyle = "#f00"
    this.map.draw.ctx.lineWidth = 2/map.zoom
    switch (this.lineTool.line.length) {
      case 1:
        this.map.draw.line(
          this.lineTool.line[0].a,
          this.lineTool.line[0].b,
          this.lineTool.cursor.x.a,
          this.lineTool.cursor.y.a
        )
        break
      case 2:
        this.map.draw.line(
          this.lineTool.line[0].a,
          this.lineTool.line[0].b,
          this.lineTool.line[1].a,
          this.lineTool.line[1].b
        )
        break
    }

    this.map.draw.coords()

    this.map.draw.mouse(this.map.ui.mouse.cursor)

    this.map.draw.ctx.restore()
	},

	changeDimensions: function(){
	  app.units = this.$units.val()
	  this.$dimensions.unitVal()
	},
	changeUnits: function(){
	  app.units = this.$units.val()
	  this.$dimensions.resetUnitVal()
	},
	lineLength: function(){
	  if (this.lineTool.line.length !== 2) return 0
	  var line = this.lineTool.line
	  return dist( line[0].a, line[0].b, line[1].a, line[1].b )
	}

	validate: function(){
	  var val = this.$dimensions.unitVal()
	  var errors = []
	  if (! this.lineLength()) {
	    errors.push("no line")
	    alert("Please click two corners of a wall and then specify how long it is in feet or meters.")
	  }
	  else if (val == 0) {
	    errors.push("no measurement")
	    alert("Please tell us how long the wall is in feet or meters.")
	  }
    return errors
	},
	
	showErrors: function(){},
	
	serialize: function(){
    var fd = new FormData()
    fd.append( "_id", this.media._id)
    fd.append( "units", this.$units.val() )
    fd.append( "scale", this.$dimensions.unitVal() / this.lineLength() )
    fd.append( "_csrf", $("[name=_csrf]").val())
    return fd
	},
	
	success: function(){
	  this.animating = false
    this.parent.useFloorplan(media)
	},

})