summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models/floor.js
blob: 63eebcc5033e378810acf28759f662b063003073 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
(function(){

	var vec2, Rect, sort
	if ('window' in this) {
		vec2 = window.vec2
		Rect = window.Rect
		sort = window.sort
	}
	else {
		vec2 = require('./vec2')
		Rect = require('./rect')
		UidGenerator = require('../util/uid')
	}
	
	var Floor = function(opt){
		this.id = opt.id
		this.side = opt.side
		this.ceiling = opt.side & CEILING
		this.mx = opt.mx
	}
	
	Floor.prototype.serialize = function(){
	  return {
	    id: this.id,
	    background: this.background,
	  }
	}
	
	Floor.prototype.deserialize = function(data){
	  this.wallpaper( data.background )
	}

	Floor.prototype.bind = function(){
		var base = this
		base.$els = $( this.mx.map(function(mx){ return mx.el }) )

		this.mx.forEach(function(mx, index){
			$(mx.el).bind({
        contextmenu: function(e){
          if (! (e.ctrlKey || e.metaKey || e.shiftKey) ) {
            e.preventDefault()
          }
          if (Scenery.nextMedia) {
            e.preventDefault()
            Scenery.nextMedia = null
            app.tube('cancel-scenery')
          }
          else if (Scenery.nextWallpaper) {
            e.preventDefault()
            Scenery.nextWallpaper = null
            app.tube('cancel-wallpaper')
          }
        },

				mousedown: function(e){
				  
				  // right-click
				  if (e.which == 3) {
				    if (Scenery.nextMedia) {
				      e.preventDefault()
              Scenery.nextMedia = null
              app.tube('cancel-scenery')
				    }
				    else if (Scenery.nextWallpaper) {
				      e.preventDefault()
              Scenery.nextWallpaper = null
              app.tube('cancel-wallpaper')
				    }
				    return
				  }
          

          var offset = offsetFromPoint(e, mx.el)
          if (! offset) { return }

          var x = mx.x + mx.width * (offset.left-0.5)
          var z = mx.z + mx.height * (0.5-offset.top)
  
          if (Scenery.nextMedia) {
            e.preventDefault()
          
            var sculpture = Sculpture.addNext({
              position: { x: x, y: 0, z: z },
            })
          
            // scenery was not placed
            if (! sculpture) {	
              e.stopPropagation()
              return
            }

            app.controller.toolbar.resetPermissions()
            Sculpture.resize.show(sculpture)
            Sculpture.hovering = true 
          
            // app.controller.pick(sculpture)

            UndoStack.push({
              type: 'create-sculpture',
              undo: { id: sculpture.id },
              redo: sculpture.serialize(),
            })
          
            // TODO: watch individual sculpture object here
            Minotaur.watch( app.router.editorView.settings )
          }
          else if (Scenery.nextWallpaper) {
					  var oldState = base.serialize()
						base.wallpaper(Scenery.nextWallpaper)
						// Scenery.nextWallpaper = null

  					UndoStack.push({
  					  type: 'update-wallpaper',
  					  undo: oldState,
              redo: base.serialize(),
  					})

            // TODO: watch individual scenery object here
            Minotaur.watch( app.router.editorView.settings )

						app.controller.pickWall(base, null)
          }
          else {
            app.controller.pickWall(base, null)
          }
				}
			})
		})

    // flip the mx order
		var shouldFlip = this.side & (CEILING)
		if (! shouldFlip) {
			this.mx.reverse()
		}
	}
	
	Floor.prototype.color = function(color){
		this.$els.css("background-color", color)
	}
	
	Floor.prototype.wallpaper = function(background){
	  if (! background) {
	    background = { src: "none" }
	  }
	  else if (typeof background == "string") {
	    background = { src: background }
	  }
	  else if (! background.src) {
	    background = { src: "none" }
	  }
    background.x = background.x || 0
    background.y = background.y || 0
    background.scale = background.scale || 1

		this.background = background
    this.background.src = this.background.src.replace(/url\(\"?\'?/,"").replace(/\"?\'?\)/,"")

		if (this.background.src == "none") {
		  this.wallpaperLoad(this.background.src)
		  return
		}
		
		var img = new Image ()
		img.onload = function(){
		  this.backgroundImage = img
		  this.wallpaperLoad(this.background.src)
		  this.wallpaperPosition(background)
		}.bind(this)
		img.src = this.background.src
		img.complete && img.onload()
	}
	
	Floor.prototype.wallpaperLoad = function(url){
	  if (url !== "none") {
	    url = "url(" + url + ")"
	  }
		this.mx.forEach(function(mx){
      mx.el.style.backgroundImage = url
    })
	}

	Floor.prototype.wallpaperPosition = function(background){
	  if (this.background.src == "none") { return }

	  this.background.x = background.x || this.background.x || 0
	  this.background.y = background.y || this.background.y || 0
	  this.background.scale = background.scale || this.background.scale || 1

		var mx, dx, dy
		var w = Math.round( this.backgroundImage.naturalWidth * this.background.scale )
		var h = Math.round( this.backgroundImage.naturalHeight * this.background.scale )

		this.mx.forEach(function(mx, i){
      
      var region = mx.rect
      
      if (this.ceiling) {
        dx = Math.round( this.background.x - region.x.a )
        dy = Math.round( this.background.y - region.y.a )
      }
      else {
        dx = Math.round( this.background.x - region.x.a )
        dy = Math.round( this.background.y + region.y.b )
      }
      
      mx.el.style.backgroundPosition = dx + 'px ' + dy + 'px'
      mx.el.style.backgroundSize = w + 'px ' + h + 'px'
    }.bind(this))
    bbb = this
	}

	if ('window' in this) {
		window.Floor = Floor
	}
	else {
		module.exports = Floor
	}
})()