diff options
Diffstat (limited to 'public/assets/javascripts/rectangles/models')
5 files changed, 459 insertions, 185 deletions
diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 3f94740..a08176a 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -42,6 +42,7 @@ Rect.prototype.assign = function(r) { this.x.assign(r.x) this.y.assign(r.y) + return this } Rect.prototype.center = function(){ return new vec2(this.x.midpoint(), this.y.midpoint()) @@ -95,6 +96,9 @@ Rect.prototype.contains = function(x,y){ return this.x.contains(x) && this.y.contains(y) } + Rect.prototype.contains_point = function(p){ + return this.x.contains(p.x) && this.y.contains(p.y) + } Rect.prototype.containsDisc = function(x,y,r){ return this.x.containsDisc(x,r) && this.y.containsDisc(y,r) } @@ -111,6 +115,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() @@ -133,6 +140,7 @@ } Rect.prototype.width = function(){ return this.x.length() } Rect.prototype.height = function(){ return this.y.length() } + Rect.prototype.delta = function(){ return new vec2( this.x.magnitude(), this.y.magnitude() ) } Rect.prototype.toString = function(){ var sides = sidesToString(this.sides) var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides 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..53977c8 --- /dev/null +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -0,0 +1,281 @@ +(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.bounds = new Rect (new vec2(0, 0), new vec2(0, 0)) + this.faces = [] + if (face) { + this.add(face) + } + } + Surface.prototype.add = function(rect){ + if (this.faces.length == 0) { + this.bounds.x.a = rect.x.a + this.bounds.x.b = rect.x.a + this.bounds.y.a = rect.y.a + this.bounds.y.b = rect.y.a + } + this.bounds.x.b += rect.width() + this.bounds.y.a = Math.min(this.bounds.y.a, rect.y.a) + this.bounds.y.b = Math.max(this.bounds.y.b, rect.y.b) + this.faces.push(rect) + } + 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.bounds.x.b < v.a || this.bounds.y.b < 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 + } + + function center_for (dimension, position, delta) { + var center = new vec2( position.a + dimension.a/2, position.b + dimension.b/2 ) + if (delta) { + center.a += delta.a + center.b += delta.b + } + return center + } + + Surface.prototype.clamp_delta = function (bounds, dimension, position, delta) { + var left_edge = position.a + delta.a - bounds.x.a + if (left_edge < 0) { + delta.a -= left_edge + } + + var right_edge = position.a + dimension.a + delta.a - bounds.x.b + if (right_edge > 0) { + delta.a -= right_edge + } + var bottom_edge = position.b + delta.b - bounds.y.a + if (bottom_edge < 0) { + delta.b -= bottom_edge + } + + var top_edge = position.b + dimension.b + delta.b - bounds.y.b + if (top_edge > 0) { + delta.b -= top_edge + } + + return delta + } + + Surface.prototype.translate = function (old_bounds, dimension, position, delta) { + this.clamp_delta( this.bounds, dimension, position, delta ) + + var new_delta = delta.clone() + if (this.clamp_delta(old_bounds, dimension, position, new_delta).eq(delta)) { + return old_bounds + } + +// var left_index = this.index_for_x( position.a + delta.a, 0 ) +// var center_index = this.index_for_x( position.a + dimension.a/2 + delta.a, left_index ) +// var right_index = this.index_for_x( position.a + dimension.a + delta.a, center_index ) +// var new_bounds = this.bounds_at_index_with_dimensions(center_index, dimension) +// +// this.clamp_delta(new_bounds, dimension, position, delta) +// +// return new_bounds + + var left_side = this.index_for_x( position.a + delta.a, 0 ) + var right_side = this.index_for_x( position.a + dimension.a + delta.a, left_side ) + + var bounds = this.faces[left_side].clone() + var intersection + + for (var i = Math.min(left_side+1, right_side); i <= right_side; i++) { + intersection = bounds.y.intersection(this.faces[i].y) + if (intersection.length() < dimension.b) { + bounds = null + break + } + bounds.y.assign(intersection) + bounds.x.b = this.faces[i].x.b + } + if (! bounds || bounds.width() < dimension.a || bounds.height() < dimension.b || + bounds.y.a > position.b + delta.b || bounds.y.b < position.b + dimension.b + delta.b) { + bounds = old_bounds + } + + this.clamp_delta(bounds, dimension, position, delta) + return bounds + } + + Surface.prototype.index_for_x = function(x, min_i){ + min_i = min_i || 0 + for (var i = min_i; i < this.faces.length; i++) { + if (this.faces[i].x.contains(x)) { + return i + } + } + return -1 + } + + Surface.prototype.bounds_at_index_with_dimensions = function(index, dimensions){ + var faces = this.faces + if (index == -1) index = this.faces.length-1 + + var bounds, intersection + var width = dimensions.a + var height = dimensions.b + + for (var i = index; i >= 0; i--) { + var face = faces[i] + if (face.y.length() < height) { + if (bounds) break + else continue + } + if (! bounds) { + bounds = face.clone() + bounds.last = i + } + else if (bounds.width() < width) { + intersection = bounds.y.intersection(face.y) + if (intersection.length() < height) { + // not totally sure if we can clobber the bounds here since this would prevent + // us from looking rightwise later + break + } + else { + bounds.y.a = intersection.a + bounds.y.b = intersection.b + bounds.x.a = face.x.a + bounds.first = i + continue + } + } + else { + if (face.y.a > bounds.y.a) break + if (face.y.b < bounds.y.b) break + } + bounds.x.a = face.x.a + bounds.first = i + } + + for (var i = bounds ? bounds.last+1 : index+1; i < faces.length; i++) { + var face = faces[i] + if (face.y.length() < height) { + if (bounds) break + else continue + } + if (! bounds) { + bounds = face.clone() + bounds.first = i + } + else if (bounds.width() < width) { + intersection = bounds.y.intersection(face.y) + if (intersection.length() < height) { + // here, since the element is tall enough, we can clobber the bounds completely + bounds.y.a = face.y.a + bounds.y.b = face.y.b + bounds.x.a = face.x.a + bounds.x.b = face.x.b + bounds.first = bounds.last = i + continue + } + else { + bounds.y.a = intersection.a + bounds.y.b = intersection.b + bounds.x.b = face.x.b + bounds.last = i + continue + } + } + if (face.y.a > bounds.y.a) break + if (face.y.b < bounds.y.b) break + bounds.x.b = face.x.b + bounds.last = i + } + + if (! bounds) { + // console.log('no bounds') + return false + } + else if (bounds.width() < width) { + // console.log('too narrow') + return false + } + return bounds + } + + Surface.prototype.bounds_at_index = function(index){ + var faces = this.faces + if (index == -1) index = this.faces.length-1 + + 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) break + if (face.y.a > bounds.y.a) break + if (face.y.b < bounds.y.b) break + + bounds.x.a = face.x.a + bounds.first = i + } + + for (var i = index+1; i < faces.length; i++) { + var face = faces[i] + if (face.y.length() < height) break + if (face.y.a > bounds.y.a) break + if (face.y.b < bounds.y.b) break + + bounds.x.b = face.x.b + bounds.last = i + } + return bounds + } + + if ('window' in this) { + window.Surface = Surface + } + else { + module.exports = Surface + } +})() diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index 214feb9..0040435 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -20,6 +20,7 @@ vec2.prototype.assign = function(v){ this.a = v.a this.b = v.b + return this } vec2.prototype.abs = function(){ if (this.b < this.a) { @@ -123,12 +124,12 @@ } vec2.prototype.union = function(v){ if (this.intersects(v)) { - return new vec2( min(this.a,v.a), max(this.b, v.b) ) + return new vec2( Math.min(this.a,v.a), Math.max(this.b, v.b) ) } } vec2.prototype.intersection = function(v){ if (this.intersects(v)) { - return new vec2( max(this.a,v.a), min(this.b, v.b) ) + return new vec2( Math.max(this.a,v.a), Math.min(this.b, v.b) ) } } diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index 6e2c728..8723c3c 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,42 +32,118 @@ 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) { - var scenery = Scenery.addNextToWall(base) - - UndoStack.push({ - type: 'create-scenery', - undo: { id: scenery.id }, - redo: scenery.serialize(), + + 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){ + }, + mousedown: function(e){ + if (Scenery.nextMedia) { + var scenery = Scenery.addNextToWall({ + wall: base, + index: index + }) + + // scenery was not placed + if (! scenery) { + e.stopPropagation() + return + } + + 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) { + base.wallpaper() + } + else { + app.controller.hideExtras() + } } - else if (Scenery.nextWallpaper) { - base.wallpaper() - } - else { - app.controller.hideExtras() - } - } + }) }) this.outline() } + + + // 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 + } +// if (mx.width) { position.a -= mx.width / 2 } +// if (mx.height) { position.b -= mx.height / 2 } + return position + } Wall.prototype.bounds_for = function(img, scale) { scale = scale || 1 @@ -84,37 +167,25 @@ window.Wall = (function(){ Wall.prototype.center = function(offset){ - offset = offset || 0 - - var major_axis, minor_axis - if (this.side & FRONT_BACK) { - major_axis = this.rect.x - minor_axis = this.rect.y - } - else { - major_axis = this.rect.y - minor_axis = this.rect.x - } - switch (this.side) { case FRONT: - x = major_axis.midpoint() - z = minor_axis.a + painting_distance_from_wall + offset + x = this.vec.midpoint() + z = this.edge + painting_distance_from_wall break case BACK: - x = major_axis.midpoint() - z = minor_axis.b - painting_distance_from_wall + offset + x = this.vec.midpoint() + z = this.edge - painting_distance_from_wall break case LEFT: - x = minor_axis.a + painting_distance_from_wall + offset - z = major_axis.midpoint() + x = this.edge + painting_distance_from_wall + z = this.vec.midpoint() break case RIGHT: - x = minor_axis.b - painting_distance_from_wall + offset - z = major_axis.midpoint() + x = this.edge - painting_distance_from_wall + z = this.vec.midpoint() break } - + return new vec2 (x, z) } @@ -125,88 +196,71 @@ window.Wall = (function(){ Wall.prototype.wallpaper = function(){ var useX = this.side & FRONT_BACK var shouldFlip = this.side & (LEFT | BACK) - this.siblings().forEach(function(w){ - w.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 + 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 = Scenery.nextWallpaper - mx.el.style.backgroundPosition = (~~partitionOffset) + "px " + (~~floorOffset) + "px" - }) - }) + mx.el.style.backgroundImage = Scenery.nextWallpaper + mx.el.style.backgroundPosition = (~~partitionOffset) + "px " + (~~floorOffset) + "px" + }) } Wall.prototype.outline = function(){ - var canvas = document.createElement("canvas") - 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() + if (! shouldFlip) { + mx = mx.reverse() } - var len = sortedWalls.length + var len = this.mx.length + var backgroundColor = "rgba(255,255,255,0.95)" + var borderColor = "rgba(0,0,0,1.0)" + 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.fillRect(0, 0, canvas.width, canvas.height) - ctx.fillStyle = "rgba(0,0,0,1.0)" + mx.el.style.backgroundColor = backgroundColor - // all walls except top-half walls get bottom lines - ctx.fillRect(0, 0, canvas.width, 1) + // all walls get bottom lines + mx.el.style.borderBottom = "1px solid " + borderColor - // all walls except bottom-half walls get top lines - ctx.fillRect(0, canvas.height-1, canvas.width, 1) + // all walls get top lines + mx.el.style.borderTop = "1px solid " + borderColor // walls on initial sides get left lines // if their left edge lines up with the rect edge if (i == 0) { - ctx.fillRect(0, 0, 1, canvas.height) + mx.el.style.borderLeft = "1px solid " + borderColor } // 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) + mx.el.style.borderRight = "1px solid " + borderColor } - 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 +280,12 @@ window.Wall = (function(){ }) } - return Wall + if ('window' in this) { + window.Wall = Wall + } + else { + module.exports = Wall + } + })() |
