summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models/floor.js
blob: 3f452e15b045fd7414969ff79c719e3a25e0b05d (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
(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
				  }

          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 )
          }
          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
	  this.background.y = background.y || this.background.y
	  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
	}
})()