summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models/wall.js
blob: fdf54f09541b09320c465b5bc25af6a8b78ef3f6 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
(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 Wall = function(opt){
		this.id = [ opt.side, opt.edge, opt.vec.a ].join("_")
		this.vec = opt.vec
		this.edge = opt.edge
		this.side = opt.side
		this.surface = opt.surface
		this.mx = opt.mx
		this.background = ""
	}
	
	Wall.prototype.toString = function(){
		return this.vec.toString()
	}

	Wall.prototype.reset = function(){
	}
	
	Wall.prototype.destroy = function(){
		this.mx.forEach(function(mx){
			mx.destroy && mx.destroy()
		})
		this.room = this.vec = this.mx = null
	}
	
	Wall.prototype.bind = function(){
		var base = this
		base.$walls = $( this.mx.map(function(mx){ return mx.el }) )
		
		this.mx.forEach(function(mx, index){
			$(mx.el).bind({
				mouseover: function(){
				},
				mouseenter: function(e){
					Scenery.mouse.mouseenter(e, {
						wall: base,
						index: index,
					})
				},
/*
				mousemove: function(e){
          var offset = offsetFromPoint(e, mx.el)
          var shouldFlip = base.side & (RIGHT | FRONT)
          if (offset) {
            var pos = base.mxOffsetToPosition( offset, index )
            
            var mx_pos = base.positionToMx(pos, new vec2(5,5))
            var mx_dot = new MX.Object3D
            mx_dot.move(mx_pos)
            mx_dot.width = 5
            mx_dot.height = 5
            mx_dot.rotationY = wall_rotation[base.side]
            mx_dot.el.style.backgroundColor = "red"
            scene.add(mx_dot)
          }
				},
*/
				mousedown: function(e){
					if (Scenery.nextMedia) {
            var scenery = Scenery.addNextToWall({
            	wall: base,
            	index: index
            })
            
            // scenery was not placed
            if (! scenery) {	
            	e.stopPropagation()
            	return
            }

            app.controller.toolbar.resetPermissions()
            Scenery.resize.show(scenery)
            Scenery.hovering = true 

  					UndoStack.push({
  					  type: 'create-scenery',
  					  undo: { id: scenery.id },
              redo: scenery.serialize(),
  					})

            // TODO: watch individual scenery 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 )
          }
					else {
						app.controller.hideExtras()
					}
				}
			})
		})

    // flip the mx order
		var shouldFlip = this.side & (LEFT | BACK)
		if (! shouldFlip) {
			this.mx.reverse()
		}

		// this.outline(wallColor, outlineColor)
	}
	
	Wall.prototype.serialize = function(){
	  return {
	    id: this.id,
	    background: this.background,
	  }
	}
	
	Wall.prototype.deserialize = function(data){
	  this.wallpaper( data.background )
	}


  // wall
  // side: corresponds to the orientation of this wall
  // vec: equivalent to the bounds of the Surface
  // edge: the coordinate of the normal of this surface
  // 			 var useX = side & LEFT_RIGHT
  //			 var useA = side & (FRONT | LEFT)
  //       edge = mx.rect[useX ? 'x': 'y'][ useA ? 'a': 'b']
  // surface: an ordered set of contiguous wall regions, corresponding to mx objects

	Wall.prototype.positionToMx = function(position, dimension) {
		var x, z
		switch (this.side) {
			case FRONT:
				x = position.a + dimension.a / 2
				z = this.edge + painting_distance_from_wall
				break
			case BACK:
				x = position.a + dimension.a / 2
				z = this.edge - painting_distance_from_wall
				break
			case LEFT:
				x = this.edge + painting_distance_from_wall
				z = position.a + dimension.a / 2
				break
			case RIGHT:
				x = this.edge - painting_distance_from_wall
 				z = position.a + dimension.a / 2
				break
		}
		return {
		  x: x,
		  y: position.b + dimension.b / 2,
		  z: z,
		  rotationY: wall_rotation[ this.side ],
		}
	}
	Wall.prototype.mxToPosition = function(mx, dimension) {
	  var position = new vec2(0,0)
	  switch (this.side) {
	    case FRONT:
	    case BACK:
	      position.a = mx.x
	      position.b = mx.y
	      break
	    case LEFT:
	    case RIGHT:
	      position.a = mx.z
	      position.b = mx.y
	      break
	  }
	  if (dimension) {
	  	position.a -= dimension.a / 2
	  	position.b -= dimension.b / 2
	  }
	  return position
	}
  Wall.prototype.mxOffsetToPosition = function( offset, index ) {
    var face = this.surface.faces[index]
    var shouldFlip = this.side & (RIGHT | FRONT)
    var position = new vec2(0,0)
    position.a = face.x.lerp(shouldFlip ? 1-offset.left : offset.left)
    position.b = face.y.lerp(1-offset.top)
    position.round()
    return position
  }

	Wall.prototype.color = function(color){
		this.$walls.css("background-color", color)
	}
	
	Wall.prototype.wallpaper = function(background){
		var useX = this.side & FRONT_BACK
		var shouldFlip = this.side & (LEFT | BACK)
		
		this.background = background || "none"
		
		this.mx.forEach(function(mx){
      var partitionOffset = useX ? mx.x : mx.z
      if (shouldFlip) partitionOffset *= -1
      partitionOffset += mx.width/2
      var floorOffset = mx.y + mx.height/2

      mx.el.style.backgroundImage = background
      mx.el.style.backgroundPosition = (~~partitionOffset) + "px " + (~~floorOffset) + "px"
    })
	}
	
	Wall.prototype.outline = function(wallColor, outlineColor){
		var useX = this.side & FRONT_BACK
		var mx = this.mx

		var len = this.mx.length
    var outlineString = app.defaults.outlineWidth + "px solid " + outlineColor
		zz = 0

		mx.forEach(function(mx, i){
			// if (mx.outlined) return
			// mx.outlined = true
      if (wallColor) {
        mx.el.style.backgroundColor = wallColor
      }
      if (outlineColor) {
        // all walls get bottom lines
        mx.el.style.borderBottom = outlineString
        
        // all walls get top lines
        mx.el.style.borderTop = outlineString
        
        // walls on initial sides get left lines
        // if their left edge lines up with the rect edge
        if (i == 0) {
          mx.el.style.borderLeft = outlineString
        }
        
        // walls on terminal sides get right lines....
        // if their right edge lines up with the rect edge
        if (i == len-1) {
          mx.el.style.borderRight = outlineString
        }
      }
		})
	}
	
	if ('window' in this) {
		window.Wall = Wall
	}
	else {
		module.exports = Wall
	}


})()