summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/javascripts/rectangles/models')
-rw-r--r--public/assets/javascripts/rectangles/models/rect.js3
-rw-r--r--public/assets/javascripts/rectangles/models/room.js76
-rw-r--r--public/assets/javascripts/rectangles/models/surface.js179
-rw-r--r--public/assets/javascripts/rectangles/models/wall.js137
4 files changed, 258 insertions, 137 deletions
diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js
index 3f94740..5952f6a 100644
--- a/public/assets/javascripts/rectangles/models/rect.js
+++ b/public/assets/javascripts/rectangles/models/rect.js
@@ -111,6 +111,9 @@
Rect.prototype.eq = function(r){
return this.x.eq(r.x) && this.y.eq(r.y)
}
+ Rect.prototype.fits = function(v){
+ return this.x.length() >= v.a && this.y.length() >= v.b
+ }
Rect.prototype.zero = function(){
this.a.zero()
this.b.zero()
diff --git a/public/assets/javascripts/rectangles/models/room.js b/public/assets/javascripts/rectangles/models/room.js
index aa18f6d..33a94d0 100644
--- a/public/assets/javascripts/rectangles/models/room.js
+++ b/public/assets/javascripts/rectangles/models/room.js
@@ -93,83 +93,7 @@
})
})
}
-
- Room.prototype.group_mx_walls = function(){
- var base = this
- var side_groups = {}, walls = []
-
- // group the walls by side
- base.mx_walls.forEach(function(wall){
-
- // ignore half-walls for now
- var side = wall.side || wall.half_side
-
- if (side_groups[side]) {
- side_groups[side].push(wall)
- }
- else {
- side_groups[side] = [wall]
- }
- })
-
- // sort the subgroups, and then combine mx objects under wall objects
- pairs(side_groups).forEach(function(pair){
- var side = pair[0], els = pair[1]
-
- if (side & LEFT_RIGHT) {
- els.sort(sort.compare_rect_x)
- }
- else if (side & FRONT_BACK) {
- els.sort(sort.compare_rect_y)
- }
-
- // wall holds state for the last wall we created/saw..
- // right now we keep walls subdivided where there are half-walls to simplify
- // calculations involving placing media on walls, calculating wall boundaries, etc..
- // one consequence of this is we can't place things on the walls above "doorways".
- // in the future, we should have more robust placement algorithm, which takes
- // this into account, and only use one wall "object" per plane
- var wall
- els.forEach(function(el){
- if (el.half_side) {
- wall = new_wall(el)
- walls.push(wall)
- wall = null
- }
- else if (! wall) {
- wall = new_wall(el)
- walls.push(wall)
- }
- else if (side & FRONT_BACK && wall.rect.x.b == el.rect.x.a) {
- wall.rect.x.b = el.rect.x.b
- wall.mx.push(el)
- }
- else if (side & LEFT_RIGHT && wall.rect.y.b == el.rect.y.a) {
- wall.rect.y.b = el.rect.y.b
- wall.mx.push(el)
- }
- else {
- wall = new_wall(el)
- walls.push(wall)
- }
- })
- })
-
- function new_wall (el) {
- return new Wall ({
- id: base.id + "_" + (el.side | el.half_side) + "_" + walls.length,
- room: base.id,
- side: el.side | el.half_side,
- half_side: el.half_side,
- rect: el.rect.clone(),
- el: el,
- })
- }
-
- return walls
- }
-
Room.prototype.clipTo = function(r){
// for each of this rect's regions split the region if necessary
var regions = this.regions
diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js
new file mode 100644
index 0000000..171376f
--- /dev/null
+++ b/public/assets/javascripts/rectangles/models/surface.js
@@ -0,0 +1,179 @@
+(function(){
+
+ var vec2, Rect
+ if ('window' in this) {
+ vec2 = window.vec2
+ Rect = window.Rect
+ }
+ else {
+ vec2 = require('./vec2')
+ Rect = require('./rect')
+ }
+
+ var Surface = function (face){
+ this.width = 0
+ this.height = 0
+ this.faces = []
+ if (face) {
+ this.add(face)
+ }
+ }
+ Surface.prototype.add = function(rect){
+ this.faces.push(rect)
+ this.width += rect.width()
+ this.height = Math.max(this.height, rect.height())
+ }
+ Surface.prototype.fits_scale = function(v, scale){
+ v = v.clone().mul(scale)
+ return this.fits(v)
+ }
+ Surface.prototype.fits = function(v){
+ var faces = this.faces
+ var scratch
+ if (this.width < v.a || this.height < v.b) {
+ return null
+ }
+ for (var i = 0; i < faces.length; i++) {
+ if (faces[i].fits(v)) {
+ return faces[i]
+ }
+ }
+ scratch = new Rect (0,0,0,0)
+ for (var i = 0; i < faces.length; i++) {
+ if (faces[i].y.length() < v.b) {
+ continue
+ }
+ scratch.x.a = faces[i].x.a
+ scratch.x.b = faces[i].x.b
+ scratch.y.a = faces[i].y.a
+ scratch.y.b = faces[i].y.b
+ SEARCH: for (var j = i+1; j < faces.length; j++) {
+ if (faces[j].y.a > scratch.y.a) {
+ scratch.y.a = faces[j].y.a
+ }
+ if (faces[j].y.b < scratch.y.b) {
+ scratch.y.b = faces[j].y.b
+ }
+ if (scratch.y.b <= scratch.y.a || scratch.y.length() < v.b) {
+ break SEARCH
+ }
+ scratch.x.b = faces[j].x.b
+ if (scratch.fits(v)) {
+ return scratch
+ }
+ }
+ }
+ return null
+ }
+ Surface.prototype.place = function(v, index){
+ var face, faces = this.faces
+ }
+ Surface.prototype.bounds = function(index){
+ var bounds = faces[index].clone()
+ var height = faces[index].height()
+ bounds.first = bounds.last = index
+
+ for (var i = index-1; i >= 0; i--) {
+ var face = faces[i]
+ if (face.y.length() < height) {
+ continue
+ }
+ if (face.y.a > bounds.y.a) {
+ continue
+ }
+ if (face.y.b < bounds.y.b) {
+ continue
+ }
+ bounds.x.a = bounds.x.a
+ bounds.first = i
+ }
+
+ for (var i = index+1; i < faces.length; i++) {
+ var face = faces[i]
+ if (face.y.length() < height) {
+ continue
+ }
+ if (face.y.a > bounds.y.a) {
+ continue
+ }
+ if (face.y.b < bounds.y.b) {
+ continue
+ }
+ bounds.x.b = bounds.x.b
+ bounds.last = i
+ }
+ return bounds
+ }
+ Surface.prototype.clamp = function (bounds, dimension, position, dx, dy) {
+ // we start out with a set of boundaries where we know the object should fall
+ // we then check if we've moved the box off any edge of the bounds
+ // if so, check if the new position is valid with respect to the surface.
+ // for horizontal movement, this means checking the height and boundaries of
+ // faces with adjacent indexes
+ var p = position.clone(), nextIndex, nextFace, newBounds
+ p.a += dx
+ p.b += dy
+
+ // left edge
+ if (p.a < bounds.x.a) {
+ if (bounds.first == 0) {
+ p.a = bounds.x.a
+ }
+ else {
+ nextIndex = bounds.first - 1
+ nextFace = this.faces[nextIndex]
+ if (nextFace.y.a <= p.b && p.b + dimension.b <= nextFace.y.b) {
+ // it appears the div to the left will accomodate this element
+ }
+ else {
+ p.a = bounds.x.a
+ }
+ }
+ }
+ // right edge
+ else if (p.a + dimension.a > bounds.x.b) {
+ if (bounds.last == this.faces.length-1) {
+ p.a = bounds.x.b - dimension.a
+ }
+ else {
+ nextIndex = bounds.last + 1
+ nextFace = this.faces[nextIndex]
+ if (nextFace.y.a <= p.b && p.b + dimension.b <= nextFace.y.b) {
+ // it appears the div to the right will accomodate this element
+ }
+ else {
+ p.a = bounds.x.b - dimension.a
+ }
+ }
+ }
+
+ // for vertical movement, this means checking the height and boundaries of
+ // elements in the set of bounds' indexes
+ if (p.b < bounds.y.a) {
+ for (var i = Math.min(nextIndex, bounds.first), last = Math.max(nextIndex, bounds.last); i < last; i++) {
+ face = this.faces[i]
+ // loop over each of the faces in the list
+ // given a face, figure out if the new top-left is in its bounds
+ // if so, find which one contains its right edge
+ // get the lowest height of this set
+ // then clamp to that lowest height
+ // additionally, this is the new center-index
+ // recalculate the bounds
+// if (face.x.b < p.
+// for (var j = bounds.first+1; j < bounds.last
+ }
+ }
+ // bottom edge, so we can clamp here trivially
+ else if (p.b + dimension.b > bounds.y.b) {
+ p.b = bounds.y.b - dimension.b
+ }
+ // if we're able to move out of bounds in that direction, recalculate the bounds
+ }
+
+ if ('window' in this) {
+ window.Surface = Surface
+ }
+ else {
+ module.exports = Surface
+ }
+})()
diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js
index 6e2c728..f015a44 100644
--- a/public/assets/javascripts/rectangles/models/wall.js
+++ b/public/assets/javascripts/rectangles/models/wall.js
@@ -1,21 +1,28 @@
-window.Wall = (function(){
+(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.id
- this.uid = Uid()
- this.room = opt.room
- this.rect = opt.rect || new Rect (0,0,0,0)
- this.rect.sides = opt.side
+ this.vec = opt.vec
+ this.edge = opt.edge
this.side = opt.side
- this.mx = []
- this.els = []
- if (opt.el) {
- this.mx.push(opt.el)
- }
+ this.surface = opt.surface
+ this.mx = opt.mx
}
Wall.prototype.toString = function(){
- return this.rect.toString()
+ return this.vec.toString()
}
Wall.prototype.reset = function(){
@@ -25,39 +32,42 @@ window.Wall = (function(){
this.mx.forEach(function(mx){
mx.destroy && mx.destroy()
})
- this.room = this.rect = this.mx = this.els = null
+ this.room = this.vec = this.mx = null
}
Wall.prototype.bind = function(){
var base = this
base.$walls = $( this.mx.map(function(mx){ return mx.el }) )
- base.$walls.bind({
- mouseover: function(){
- },
- mouseenter: function(e){
- Scenery.mouse.mouseenter(e, base)
- },
- mousemove: function(e){
- },
- mousedown: function(){
- // base.randomize_colors()
- // console.log(sidesToString(base.side))
- if (Scenery.nextMedia) {
+
+ this.mx.forEach(function(mx){
+ $(mx.el).bind({
+ mouseover: function(){
+ },
+ mouseenter: function(e){
+ Scenery.mouse.mouseenter(e, base, mx)
+ },
+ mousemove: function(e){
+ },
+ mousedown: function(){
+ // base.randomize_colors()
+ // console.log(sidesToString(base.side))
+ if (Scenery.nextMedia) {
var scenery = Scenery.addNextToWall(base)
- UndoStack.push({
- type: 'create-scenery',
- undo: { id: scenery.id },
- redo: scenery.serialize(),
- })
- }
- else if (Scenery.nextWallpaper) {
- base.wallpaper()
+ UndoStack.push({
+ type: 'create-scenery',
+ undo: { id: scenery.id },
+ redo: scenery.serialize(),
+ })
+ }
+ else if (Scenery.nextWallpaper) {
+ base.wallpaper()
+ }
+ else {
+ app.controller.hideExtras()
+ }
}
- else {
- app.controller.hideExtras()
- }
- }
+ })
})
this.outline()
}
@@ -72,6 +82,12 @@ window.Wall = (function(){
new vec2( halfHeight, Rooms.list[this.room].height - halfHeight ) )
}
+ Wall.prototype.bounds_for_mx = function(img, scale, mx) {
+ //
+ }
+ Wall.prototype.fits_mx = function(img, scale, mx) {
+ }
+
Wall.prototype.fits = function(img, scale){
if (this.side & FRONT_BACK && this.rect.x.length() < img.width * scale) {
return false
@@ -144,32 +160,30 @@ window.Wall = (function(){
var ctx = canvas.getContext('2d')
var useX = this.side & FRONT_BACK
var shouldFlip = this.side & (LEFT | BACK)
+ var mx = this.mx
- var sortedWalls = this.siblings().reduce(function(a,w){
- return a.concat(w.mx)
- }, []).sort( useX ? sort.compare_x : sort.compare_z )
-
- if (shouldFlip) {
- sortedWalls = sortedWalls.reverse()
+ // console.log( sidesToString(this.side), mx.length )
+ if (! shouldFlip) {
+ mx = mx.reverse()
}
- var len = sortedWalls.length
+ var len = this.mx.length
zz = window.zz || 0
- sortedWalls.forEach(function(mx, i){
+ mx.forEach(function(mx, i){
if (mx.outlined) return
mx.outlined = true
canvas.width = mx.width
canvas.height = mx.height
- ctx.fillStyle = "rgba(255,255,255,0.9)"
+ ctx.fillStyle = "rgba(255,255,255,0.95)"
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = "rgba(0,0,0,1.0)"
- // all walls except top-half walls get bottom lines
+ // all walls get bottom lines
ctx.fillRect(0, 0, canvas.width, 1)
- // all walls except bottom-half walls get top lines
+ // all walls get top lines
ctx.fillRect(0, canvas.height-1, canvas.width, 1)
// walls on initial sides get left lines
@@ -181,32 +195,27 @@ window.Wall = (function(){
// walls on terminal sides get right lines....
// if their right edge lines up with the rect edge
if (i == len-1) {
- // ctx.fillStyle = "rgba(255,0,0,1.0)"
ctx.fillRect(canvas.width-1, 0, 1, canvas.height)
}
var dataUrl = canvas.toDataURL()
mx.el.style.backgroundImage = "url(" + dataUrl + ")"
})
- window.zz += 1
}
Wall.prototype.siblings = function(){
- var base = this
- var match = base.side | base.half_side
- var walls = Rooms.list[this.room].walls.filter(function(w){
- return (w.side | w.half_side) & match && w.$walls
- })
- return walls
+ return this
+// var base = this
+// var match = base.side | base.half_side
+// var walls = Rooms.list[this.room].walls.filter(function(w){
+// return (w.side | w.half_side) & match && w.$walls
+// })
+// return walls
}
Wall.prototype.randomize_colors = function(){
var color = window.grayColors[ this.side | this.half_side ]
- var siblings = this.siblings()
- siblings.forEach(function(w, i){
- if (! w.$walls) return
- w.color(color)
- })
+ this.color(color)
}
Wall.prototype.stroke_colors = function(){
@@ -226,6 +235,12 @@ window.Wall = (function(){
})
}
- return Wall
+ if ('window' in this) {
+ window.Wall = Wall
+ }
+ else {
+ module.exports = Wall
+ }
+
})()