From be7412073a5e0b6641f125f29d254315f50bc95c Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 1 Aug 2014 18:03:20 -0400 Subject: tests for grouper --- .../javascripts/rectangles/engine/rooms/builder.js | 18 +---- .../assets/javascripts/rectangles/models/room.js | 76 ---------------------- .../assets/javascripts/rectangles/models/wall.js | 37 +++++++---- 3 files changed, 28 insertions(+), 103 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/rooms/builder.js b/public/assets/javascripts/rectangles/engine/rooms/builder.js index dfabc86..1f21636 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/builder.js +++ b/public/assets/javascripts/rectangles/engine/rooms/builder.js @@ -7,7 +7,7 @@ } else { MX = require('../../../../../../test/mocks/mx.js') - scene = MX.scene + scene = MX.Scene Rooms = require('./_rooms') sort = require('../../util/sort') FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 @@ -43,7 +43,7 @@ if (window.scene) { base.clear() base.build() - base.bind_walls() + Rooms.grouper.group() } } @@ -62,18 +62,7 @@ }) }.bind(this)) } - - base.bind_walls = function (){ - Rooms.forEach(function(room){ - room.walls = room.group_mx_walls() - room.walls.forEach(function(wall){ - Rooms.walls[ wall.id ] = wall - wall.bind() - wall.randomize_colors() - }) - }) - } - + base.clear = function (){ els.forEach(function(el){ scene.remove(el) @@ -84,7 +73,6 @@ this.build_walls = function (region) { var room = Rooms.list[ region.id ] - var list = [], el = null var width = region.x.length() diff --git a/public/assets/javascripts/rectangles/models/room.js b/public/assets/javascripts/rectangles/models/room.js index c2850ba..937c928 100644 --- a/public/assets/javascripts/rectangles/models/room.js +++ b/public/assets/javascripts/rectangles/models/room.js @@ -85,83 +85,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/wall.js b/public/assets/javascripts/rectangles/models/wall.js index 027d5f5..f2b8bca 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -1,21 +1,31 @@ -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_uid = new UidGenerator({}) + 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.uid = wall_uid + this.vec = opt.vec this.side = opt.side this.mx = [] - this.els = [] if (opt.el) { this.mx.push(opt.el) } } Wall.prototype.toString = function(){ - return this.rect.toString() + return this.vec.toString() } Wall.prototype.reset = function(){ @@ -25,7 +35,7 @@ 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(){ @@ -146,8 +156,6 @@ window.Wall = (function(){ if (shouldFlip) { sortedWalls = sortedWalls.reverse() } - -console.log(sortedWalls.map(function(z){return useX ? z.x : z.z}).join(" ")) var len = sortedWalls.length @@ -184,7 +192,6 @@ console.log(sortedWalls.map(function(z){return useX ? z.x : z.z}).join(" ")) mx.el.style.backgroundImage = "url(" + dataUrl + ")" }) - window.zz += 1 } Wall.prototype.siblings = function(){ @@ -222,6 +229,12 @@ console.log(sortedWalls.map(function(z){return useX ? z.x : z.z}).join(" ")) }) } - return Wall + if ('window' in this) { + window.Wall = Wall + } + else { + module.exports = Wall + } + })() -- cgit v1.2.3-70-g09d2 From e2e1565f5cd3154c0f515dbe3d88c9943dd0580e Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 4 Aug 2014 17:41:04 -0400 Subject: group walls and render using 'wireframe' look --- .../javascripts/rectangles/engine/rooms/builder.js | 2 +- .../javascripts/rectangles/engine/rooms/grouper.js | 147 +++++++++++++++++++++ .../assets/javascripts/rectangles/models/wall.js | 39 +++--- .../assets/javascripts/rectangles/util/colors.js | 3 +- public/assets/javascripts/rectangles/util/sort.js | 33 ++++- server/index.js | 2 +- test/06-test-grouper.js | 129 +++++++++++++++++- views/partials/scripts.ejs | 1 + 8 files changed, 325 insertions(+), 31 deletions(-) create mode 100644 public/assets/javascripts/rectangles/engine/rooms/grouper.js (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/rooms/builder.js b/public/assets/javascripts/rectangles/engine/rooms/builder.js index 1f21636..6b2e65a 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/builder.js +++ b/public/assets/javascripts/rectangles/engine/rooms/builder.js @@ -43,7 +43,7 @@ if (window.scene) { base.clear() base.build() - Rooms.grouper.group() + Rooms.grouper.build() } } diff --git a/public/assets/javascripts/rectangles/engine/rooms/grouper.js b/public/assets/javascripts/rectangles/engine/rooms/grouper.js new file mode 100644 index 0000000..532146d --- /dev/null +++ b/public/assets/javascripts/rectangles/engine/rooms/grouper.js @@ -0,0 +1,147 @@ +(function(){ + + var Rooms, UidGenerator, Wall, sort + if ('window' in this) { + Rooms = window.Rooms + UidGenerator = window.UidGenerator + Wall = window.Wall + sort = window.sort + } + else { + Rooms = require('./_rooms') + UidGenerator = require('../../util/uid') + Wall = require('../../models/wall') + sort = require('../../util/sort') + FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 + PI = Math.PI + HALF_PI = PI/2 + TOP = CEILING, BOTTOM = FLOOR + function sidesToString(sides){ + var s = "" + if (sides & FRONT) s += "front " + if (sides & BACK) s += "back " + if (sides & LEFT) s += "left " + if (sides & RIGHT) s += "right " + if (sides & TOP) s += "top " + if (sides & BOTTOM) s += "bottom " + return s + } + } + + Rooms.grouper = new function(){ + + var base = this + + base.list = {} + + base.uid = new UidGenerator(base.list) + + base.build = function (){ + var walls = [] + var collections = base.collect() + base.cull(collections) + base.group(walls, collections, FRONT) + base.group(walls, collections, BACK) + base.group(walls, collections, LEFT) + base.group(walls, collections, RIGHT) + Rooms.walls = walls + base.bind() + } + base.collect = function(){ + var collections = {} + collections[FRONT] = [] + collections[BACK] = [] + collections[LEFT] = [] + collections[RIGHT] = [] + + Rooms.forEach(function(room){ + room.mx_walls.forEach(function(mx){ + var side = mx.side || mx.half_side + collections[side].push(mx) + }) + }) + + base.cull(collections) + + return collections + } + base.cull = function(collections){ + [FRONT, BACK, LEFT, RIGHT].forEach(function(side){ + var collection = collections[side] + var useX = side & FRONT_BACK + var useA = side & (FRONT | RIGHT) + var last_mx + + collection.sort( useX ? sort.compare_zx : sort.compare_xz ) + collection.forEach(function(mx){ + if (last_mx && last_mx.rect.eq(mx.rect)) { +// console.log( mx.y - mx.height/2 ) +// console.log( last_mx.y - last_mx.height/2 ) +// console.log(last_mx.y, mx.y) +// console.log(last_mx.height, mx.height) +// console.log(Rooms.list[ last_mx.rect.id ].height, Rooms.list[ mx.rect.id ].height) +// console.log("___") +// last_mx.height += mx.height/2 +// last_mx.y += mx.height/4 + if (last_mx.rect.id == mx.rect.id) { + last_mx.height += mx.height/2 + last_mx.y += mx.height/4 + } + last_mx.side = side + + mx.culled = true + scene.remove(mx) + return + } + last_mx = mx + }) + }) + return collections + } + base.group = function(walls, collections, side){ + var collection = collections[side] + var wall + var useX = side & FRONT_BACK + var useA = side & (FRONT | RIGHT) + + // collection.sort( useX ? sort.compare_zx : sort.compare_xz ) + + collection.forEach(function(mx){ + if (mx.culled) return + var coplanar = wall && wall.edge == mx.rect[useX ? 'y': 'x'][useA ? 'a': 'b'] + + if (wall && coplanar) { + if (useX && wall.vec.b == mx.rect.x.a) { + wall.vec.b = mx.rect.x.b + wall.mx.push(mx) + return + } + else if (! useX && wall.vec.b == mx.rect.y.a) { + wall.vec.b = mx.rect.y.b + wall.mx.push(mx) + return + } + } + wall = new Wall ({ + id: base.uid(), + side: side, + vec: mx.rect[ useX ? 'x' : 'y' ].clone(), + edge: mx.rect[useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], + el: mx, + }) + walls.push(wall) + }) + + return walls + } + + base.bind = function(){ + Rooms.walls.forEach(function(wall){ + wall.bind() + wall.randomize_colors() + }) + } + + } + +})() diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index f2b8bca..41d7235 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -17,6 +17,7 @@ this.id = opt.id this.uid = wall_uid this.vec = opt.vec + this.edge = opt.edge this.side = opt.side this.mx = [] if (opt.el) { @@ -148,20 +149,18 @@ 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 @@ -170,10 +169,10 @@ 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 @@ -185,7 +184,6 @@ // 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() @@ -195,21 +193,18 @@ } 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(){ diff --git a/public/assets/javascripts/rectangles/util/colors.js b/public/assets/javascripts/rectangles/util/colors.js index 95827cc..16d34dd 100644 --- a/public/assets/javascripts/rectangles/util/colors.js +++ b/public/assets/javascripts/rectangles/util/colors.js @@ -58,7 +58,8 @@ select.blur() }) - window.colors = color_palettes[select ? select.value : 'alphaQuad'] + window.colors = color_palettes[select ? select.value : 'colors'] +// window.colors = color_palettes[select ? select.value : 'alphaQuad'] window.grayColors = {} _.zip([FRONT, LEFT, BACK, RIGHT], color_palettes.alphaQuad).map(function(pair){ window.grayColors[pair[0]] = pair[1] diff --git a/public/assets/javascripts/rectangles/util/sort.js b/public/assets/javascripts/rectangles/util/sort.js index 7aa40a2..cf8d6b1 100644 --- a/public/assets/javascripts/rectangles/util/sort.js +++ b/public/assets/javascripts/rectangles/util/sort.js @@ -95,9 +95,38 @@ return a.x < b.x ? -1 : a.x == b.x ? 0 : 1 } sort.compare_z = function (a,b){ - return a.z > b.z ? -1 : a.z == b.z ? 0 : 1 + return a.z < b.z ? -1 : a.z == b.z ? 0 : 1 } - + sort.compare_xz = function(a,b){ + if (a.x < b.x) { + return -1 + } + if (a.x > b.x) { + return 1 + } + if (a.z < b.z) { + return -1 + } + if (a.z > b.z) { + return 1 + } + return 0 + } + sort.compare_zx = function(a,b){ + if (a.z < b.z) { + return -1 + } + if (a.z > b.z) { + return 1 + } + if (a.x < b.x) { + return -1 + } + if (a.x > b.x) { + return 1 + } + return 0 + } if ("window" in this) { window.sort = sort } diff --git a/server/index.js b/server/index.js index 71cd862..ad437ff 100644 --- a/server/index.js +++ b/server/index.js @@ -50,7 +50,7 @@ site.setup = function(){ app.use(express.session({ key: 'vvalls.sid', secret: 'flibbertigibbet', - cookie: { domain: '.' + config.hostName, maxAge: 432000000 }, + cookie: { domain: '.' + config.hostName, maxAge: 43200000000 }, store: SessionStore })); app.use(bodyParser()); diff --git a/test/06-test-grouper.js b/test/06-test-grouper.js index 1707e4c..41ed0b0 100644 --- a/test/06-test-grouper.js +++ b/test/06-test-grouper.js @@ -29,10 +29,12 @@ var east = new Rect( new vec(2,6), new vec(1,5) ) var corner = new Rect( new vec(3,7), new vec(3,7) ) var peninsula = new Rect( new vec(4,6), new vec(6,8) ) -var rect_room = new Room({ id: "rect", rect: rect, height: 2 }) -var east_room = new Room({ id: "east", rect: east, height: 2 }) -var corner_room = new Room({ id: "corner", rect: corner, height: 2 }) -var peninsula_room = new Room({ id: "peninsula", rect: peninsula, height: 2 }) +var rect_room = new Room({ id: "rect", rect: rect, height: 2 }) +var east_room = new Room({ id: "east", rect: east, height: 2 }) +var corner_room = new Room({ id: "corner", rect: corner, height: 2 }) +var peninsula_room = new Room({ id: "peninsula", rect: peninsula, height: 2 }) +var peninsula_taller = new Room({ id: "peninsula", rect: peninsula, height: 3 }) +var peninsula_shorter = new Room({ id: "peninsula", rect: peninsula, height: 1 }) var taller_room = new Room({ id: "taller", rect: rect, height: 3 }) @@ -118,4 +120,123 @@ describe('grouper(rect,east)', function(){ }) }) +describe('grouper(rect,corner)', function(){ + reset() + Rooms.add( rect_room ) + Rooms.add( corner_room ) + rebuild() + + var collections = Rooms.grouper.collect() + + describe('#group(rect,corner)', function(){ + var front_walls = Rooms.grouper.group([], collections, FRONT) + var back_walls = Rooms.grouper.group([], collections, BACK) + var left_walls = Rooms.grouper.group([], collections, LEFT) + var right_walls = Rooms.grouper.group([], collections, RIGHT) + + it("left has 2 walls", function(){ + assert.equal(2, left_walls.length) + }) + it("right has 2 walls", function(){ + assert.equal(2, right_walls.length) + }) + it("front has 2 walls", function(){ + assert.equal(2, front_walls.length) + }) + it("back has 2 walls", function(){ + assert.equal(2, back_walls.length) + }) + it("front/right walls are one narrow, one wide", function(){ + assert.equal(4, front_walls[0].vec.length()) + assert.equal(2, front_walls[1].vec.length()) + assert.equal(2, right_walls[0].vec.length()) + assert.equal(4, right_walls[1].vec.length()) + }) + it("back/left walls are one wide, one narrow", function(){ + assert.equal(2, back_walls[0].vec.length()) + assert.equal(4, back_walls[1].vec.length()) + assert.equal(4, left_walls[0].vec.length()) + assert.equal(2, left_walls[1].vec.length()) + }) + }) +}) + +describe('grouper(rect,corner,peninsula)', function(){ + reset() + Rooms.add( rect_room ) + Rooms.add( corner_room ) + Rooms.add( peninsula_room ) + rebuild() + + var collections = Rooms.grouper.collect() + + describe('#collect(rect,corner,peninsula)', function(){ + it("should find an appropriate number of wall segments", function(){ + assert.equal(3, collections[FRONT].length) + assert.equal(4, collections[BACK].length) + assert.equal(5, collections[LEFT].length) + assert.equal(5, collections[RIGHT].length) + }) + }) + + describe('#group(rect,corner,peninsula)', function(){ + var front_walls = Rooms.grouper.group([], collections, FRONT) + var back_walls = Rooms.grouper.group([], collections, BACK) + var left_walls = Rooms.grouper.group([], collections, LEFT) + var right_walls = Rooms.grouper.group([], collections, RIGHT) + + it("left has 3 walls", function(){ + assert.equal(3, left_walls.length) + }) + it("right has 4 walls", function(){ + assert.equal(4, right_walls.length) + }) + it("front has 2 walls", function(){ + assert.equal(2, front_walls.length) + }) + it("back has 4 walls", function(){ + assert.equal(4, back_walls.length) + }) + }) +}) + + +describe('grouper(rect,corner,peninsula_taller)', function(){ + reset() + Rooms.add( rect_room ) + Rooms.add( corner_room ) + Rooms.add( peninsula_taller ) + rebuild() + + var collections = Rooms.grouper.collect() + + describe('#collect(rect,corner,peninsula_taller)', function(){ + it("should find an appropriate number of wall segments", function(){ + assert.equal(5, collections[FRONT].length) + assert.equal(4, collections[BACK].length) + assert.equal(6, collections[LEFT].length) + assert.equal(6, collections[RIGHT].length) + }) + }) + + describe('#group(rect,corner,peninsula_taller)', function(){ + var front_walls = Rooms.grouper.group([], collections, FRONT) + var back_walls = Rooms.grouper.group([], collections, BACK) + var left_walls = Rooms.grouper.group([], collections, LEFT) + var right_walls = Rooms.grouper.group([], collections, RIGHT) + + it("left has 4 walls", function(){ + assert.equal(4, left_walls.length) + }) + it("right has 5 walls", function(){ + assert.equal(5, right_walls.length) + }) + it("front has 3 walls", function(){ + assert.equal(3, front_walls.length) + }) + it("back has 4 walls", function(){ + assert.equal(4, back_walls.length) + }) + }) +}) diff --git a/views/partials/scripts.ejs b/views/partials/scripts.ejs index 0133ad0..915027d 100644 --- a/views/partials/scripts.ejs +++ b/views/partials/scripts.ejs @@ -38,6 +38,7 @@ + -- cgit v1.2.3-70-g09d2 From a8307b74bd429227fe1f2a06d8fac5d61530608f Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 5 Aug 2014 14:31:56 -0400 Subject: test bounds --- .../javascripts/rectangles/engine/rooms/builder.js | 2 +- .../javascripts/rectangles/engine/rooms/grouper.js | 34 +++++++------ .../rectangles/engine/scenery/_scenery.js | 20 +++++--- .../javascripts/rectangles/engine/scenery/move.js | 2 +- .../rectangles/engine/scenery/types/_object.js | 4 +- .../assets/javascripts/rectangles/models/wall.js | 59 ++++++++++++---------- test/06-test-grouper.js | 46 +++++++++++++++++ 7 files changed, 115 insertions(+), 52 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/rooms/builder.js b/public/assets/javascripts/rectangles/engine/rooms/builder.js index 6b2e65a..f321f71 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/builder.js +++ b/public/assets/javascripts/rectangles/engine/rooms/builder.js @@ -293,7 +293,7 @@ el.side = 0 el.rect = null el.destroy = function(){ - this.el = this.rect = null + this.el = this.rect = this.face = null } // possible if walls are opaque diff --git a/public/assets/javascripts/rectangles/engine/rooms/grouper.js b/public/assets/javascripts/rectangles/engine/rooms/grouper.js index 532146d..ba081e3 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/grouper.js +++ b/public/assets/javascripts/rectangles/engine/rooms/grouper.js @@ -1,7 +1,9 @@ (function(){ - var Rooms, UidGenerator, Wall, sort + var vec2, Rect, Rooms, UidGenerator, Wall, sort if ('window' in this) { + vec2 = window.vec2 + Rect = window.Rect Rooms = window.Rooms UidGenerator = window.UidGenerator Wall = window.Wall @@ -10,6 +12,8 @@ else { Rooms = require('./_rooms') UidGenerator = require('../../util/uid') + vec2 = require('../../models/vec2') + Rect = require('../../models/rect') Wall = require('../../models/wall') sort = require('../../util/sort') FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 @@ -71,28 +75,25 @@ var useX = side & FRONT_BACK var useA = side & (FRONT | RIGHT) var last_mx + var widthVec, heightVec collection.sort( useX ? sort.compare_zx : sort.compare_xz ) collection.forEach(function(mx){ if (last_mx && last_mx.rect.eq(mx.rect)) { -// console.log( mx.y - mx.height/2 ) -// console.log( last_mx.y - last_mx.height/2 ) -// console.log(last_mx.y, mx.y) -// console.log(last_mx.height, mx.height) -// console.log(Rooms.list[ last_mx.rect.id ].height, Rooms.list[ mx.rect.id ].height) -// console.log("___") -// last_mx.height += mx.height/2 -// last_mx.y += mx.height/4 if (last_mx.rect.id == mx.rect.id) { last_mx.height += mx.height/2 last_mx.y += mx.height/4 + last_mx.face.y.b += mx.height/2 } last_mx.side = side - mx.culled = true + mx.destroy() scene.remove(mx) return } + widthVec = mx.rect[useX ? 'x' : 'y'].clone() + heightVec = new vec2( mx.y - mx.height/2, mx.y + mx.height/2 ) + mx.face = new Rect( widthVec, heightVec ) last_mx = mx }) }) @@ -114,20 +115,23 @@ if (useX && wall.vec.b == mx.rect.x.a) { wall.vec.b = mx.rect.x.b wall.mx.push(mx) + wall.faces.push(mx.face) return } else if (! useX && wall.vec.b == mx.rect.y.a) { wall.vec.b = mx.rect.y.b wall.mx.push(mx) + wall.faces.push(mx.face) return } } wall = new Wall ({ - id: base.uid(), - side: side, - vec: mx.rect[ useX ? 'x' : 'y' ].clone(), - edge: mx.rect[useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], - el: mx, + id: base.uid(), + side: side, + mx: [ mx ], + faces: [ mx.face ], + vec: mx.rect[ useX ? 'x' : 'y' ].clone(), + edge: mx.rect[ useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], }) walls.push(wall) }) diff --git a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js index fe5f037..137c74a 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js +++ b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js @@ -12,25 +12,29 @@ var Scenery = new function(){ base.resize.init() } - base.add = function(wall, media, id){ + base.add = function(opt){ var scene_media switch (media.type) { case 'image': - scene_media = new Scenery.types.image ({ media: media, wall: wall, id: id }) + scene_media = new Scenery.types.image (opt) break case 'video': case 'youtube': case 'vimeo': - scene_media = new Scenery.types.video ({ media: media, wall: wall, id: id }) + scene_media = new Scenery.types.video (opt) break } base.list[scene_media.id] = scene_media return scene_media } - base.addNextToWall = function(wall){ - base.add(wall, base.nextMedia) + base.addNextToWall = function(wall, mx){ + base.add({ + wall: wall, + media: base.nextMedia, + mx: mx + }) base.nextMedia = null } @@ -64,7 +68,11 @@ var Scenery = new function(){ base.deserialize = function(scenery_data){ scenery_data.forEach(function(data){ var wall = Rooms.walls[data.wall_id] - var scene_media = base.add(wall, data.media, data.id) + var scene_media = base.add({ + wall: wall, + media: data.media, + id: data.id + }) scene_media.deserialize(data) }) } diff --git a/public/assets/javascripts/rectangles/engine/scenery/move.js b/public/assets/javascripts/rectangles/engine/scenery/move.js index 94a4e52..cc5b014 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/move.js +++ b/public/assets/javascripts/rectangles/engine/scenery/move.js @@ -69,7 +69,7 @@ Scenery.move = function(base){ function switch_wall (e, new_wall, cursor){ if (! dragging) return - if (new_wall.uid == base.wall.uid) return + if (new_wall.id == base.wall.id) return if (! new_wall.fits(base.media, base.scale)) return var old_wall_side = base.wall.side diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js index 7202ce0..aa1fefb 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js @@ -12,7 +12,7 @@ Scenery.types.base = Fiber.extend(function(base){ this.scale = this.media.scale if (opt.wall) { - this.set_wall(opt.wall) + this.set_wall(opt.wall, opt.mx) } }, @@ -60,7 +60,7 @@ Scenery.types.base = Fiber.extend(function(base){ } }, - set_wall: function(wall){ + set_wall: function(wall, mx){ this.wall = wall || this.wall this.bounds = this.wall.bounds_for(this.media, this.scale) this.center = this.wall.center() diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index 41d7235..bbe6b97 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -11,18 +11,14 @@ Rect = require('./rect') UidGenerator = require('../util/uid') } - var wall_uid = new UidGenerator({}) var Wall = function(opt){ this.id = opt.id - this.uid = wall_uid this.vec = opt.vec this.edge = opt.edge this.side = opt.side - this.mx = [] - if (opt.el) { - this.mx.push(opt.el) - } + this.faces = opt.faces + this.mx = opt.mx } Wall.prototype.toString = function(){ @@ -42,27 +38,30 @@ 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) { - Scenery.addNextToWall(base) - } - else if (Scenery.nextWallpaper) { - base.wallpaper() - } - else { - app.controller.hideExtras() + + 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) { + Scenery.addNextToWall(base, mx) + } + else if (Scenery.nextWallpaper) { + base.wallpaper() + } + else { + app.controller.hideExtras() + } } - } + }) }) this.outline() } @@ -77,6 +76,12 @@ 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 @@ -165,7 +170,7 @@ 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)" diff --git a/test/06-test-grouper.js b/test/06-test-grouper.js index 41ed0b0..5388875 100644 --- a/test/06-test-grouper.js +++ b/test/06-test-grouper.js @@ -29,6 +29,9 @@ var east = new Rect( new vec(2,6), new vec(1,5) ) var corner = new Rect( new vec(3,7), new vec(3,7) ) var peninsula = new Rect( new vec(4,6), new vec(6,8) ) +var big_rect = new Rect( new vec(1,5), new vec(1,5) ) +var hall_rect = new Rect( new vec(3,4), new vec(4,8) ) + var rect_room = new Room({ id: "rect", rect: rect, height: 2 }) var east_room = new Room({ id: "east", rect: east, height: 2 }) var corner_room = new Room({ id: "corner", rect: corner, height: 2 }) @@ -38,6 +41,9 @@ var peninsula_shorter = new Room({ id: "peninsula", rect: peninsula, height: 1 } var taller_room = new Room({ id: "taller", rect: rect, height: 3 }) +var big_room = new Room({ id: "big_room", rect: big_rect, height: 4 }) +var hallway = new Room({ id: "hallway", rect: hall_rect, height: 2 }) + function report(a) { console.log( a.join("\n") ) } @@ -230,6 +236,7 @@ describe('grouper(rect,corner,peninsula_taller)', function(){ }) it("right has 5 walls", function(){ assert.equal(5, right_walls.length) + // console.log(right_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.faces+"" })) }) it("front has 3 walls", function(){ assert.equal(3, front_walls.length) @@ -240,3 +247,42 @@ describe('grouper(rect,corner,peninsula_taller)', function(){ }) }) +describe('grouper(room,hallway)', function(){ + reset() + Rooms.add( big_room ) + Rooms.add( hallway ) + rebuild() + + var collections = Rooms.grouper.collect() + + describe('#collect(room,hallway)', function(){ + it("should find an appropriate number of wall segments", function(){ + assert.equal(3, collections[FRONT].length) + assert.equal(4, collections[BACK].length) + assert.equal(3, collections[LEFT].length) + assert.equal(3, collections[RIGHT].length) + }) + }) + + describe('#group(rect,corner,peninsula_taller)', function(){ + var front_walls = Rooms.grouper.group([], collections, FRONT) + var back_walls = Rooms.grouper.group([], collections, BACK) + var left_walls = Rooms.grouper.group([], collections, LEFT) + var right_walls = Rooms.grouper.group([], collections, RIGHT) + + it("left has 2 walls", function(){ + assert.equal(2, left_walls.length) + }) + it("right has 2 walls", function(){ + assert.equal(2, right_walls.length) + }) + it("front has 1 wall", function(){ + assert.equal(1, front_walls.length) + }) + it("back has 2 walls", function(){ + assert.equal(2, back_walls.length) + // console.log(back_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.faces+"" })) + }) + }) +}) + -- cgit v1.2.3-70-g09d2 From 50ad04ca01da10f87bf907fdf3c185a1d68be506 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 6 Aug 2014 17:41:12 -0400 Subject: surface stuff --- .../javascripts/rectangles/engine/rooms/grouper.js | 20 ++-- .../assets/javascripts/rectangles/models/rect.js | 3 + .../javascripts/rectangles/models/surface.js | 111 +++++++++++++++++++++ .../assets/javascripts/rectangles/models/wall.js | 2 +- test/06-test-grouper.js | 4 +- test/07-test-bounds.js | 67 ------------- test/07-test-surface.js | 60 +++++++++++ views/partials/scripts.ejs | 3 +- 8 files changed, 190 insertions(+), 80 deletions(-) create mode 100644 public/assets/javascripts/rectangles/models/surface.js delete mode 100644 test/07-test-bounds.js create mode 100644 test/07-test-surface.js (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/rooms/grouper.js b/public/assets/javascripts/rectangles/engine/rooms/grouper.js index ba081e3..4ad3bd8 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/grouper.js +++ b/public/assets/javascripts/rectangles/engine/rooms/grouper.js @@ -1,9 +1,10 @@ (function(){ - var vec2, Rect, Rooms, UidGenerator, Wall, sort + var vec2, Rect, Rooms, UidGenerator, Wall, Surface, sort if ('window' in this) { vec2 = window.vec2 Rect = window.Rect + Surface = window.Surface Rooms = window.Rooms UidGenerator = window.UidGenerator Wall = window.Wall @@ -15,6 +16,7 @@ vec2 = require('../../models/vec2') Rect = require('../../models/rect') Wall = require('../../models/wall') + Surface = require('../../models/surface') sort = require('../../util/sort') FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 PI = Math.PI @@ -115,23 +117,23 @@ if (useX && wall.vec.b == mx.rect.x.a) { wall.vec.b = mx.rect.x.b wall.mx.push(mx) - wall.faces.push(mx.face) + wall.surface.add(mx.face) return } else if (! useX && wall.vec.b == mx.rect.y.a) { wall.vec.b = mx.rect.y.b wall.mx.push(mx) - wall.faces.push(mx.face) + wall.surface.add(mx.face) return } } wall = new Wall ({ - id: base.uid(), - side: side, - mx: [ mx ], - faces: [ mx.face ], - vec: mx.rect[ useX ? 'x' : 'y' ].clone(), - edge: mx.rect[ useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], + id: base.uid(), + side: side, + mx: [ mx ], + surface: new Surface( mx.face ), + vec: mx.rect[ useX ? 'x' : 'y' ].clone(), + edge: mx.rect[ useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], }) walls.push(wall) }) diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 590440a..500ee6d 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -107,6 +107,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.nearEdge = function (x, y, r) { var edges = 0 if (x < this.x.a+r) { diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js new file mode 100644 index 0000000..448722b --- /dev/null +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -0,0 +1,111 @@ +(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() + + 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 + } + + 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 + } + return 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 bbe6b97..fdc91fd 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -17,7 +17,7 @@ this.vec = opt.vec this.edge = opt.edge this.side = opt.side - this.faces = opt.faces + this.surface = opt.surface this.mx = opt.mx } diff --git a/test/06-test-grouper.js b/test/06-test-grouper.js index 5388875..0f14217 100644 --- a/test/06-test-grouper.js +++ b/test/06-test-grouper.js @@ -236,7 +236,7 @@ describe('grouper(rect,corner,peninsula_taller)', function(){ }) it("right has 5 walls", function(){ assert.equal(5, right_walls.length) - // console.log(right_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.faces+"" })) + // console.log(right_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.surface +"" })) }) it("front has 3 walls", function(){ assert.equal(3, front_walls.length) @@ -281,7 +281,7 @@ describe('grouper(room,hallway)', function(){ }) it("back has 2 walls", function(){ assert.equal(2, back_walls.length) - // console.log(back_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.faces+"" })) + // console.log(back_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.surface +"" })) }) }) }) diff --git a/test/07-test-bounds.js b/test/07-test-bounds.js deleted file mode 100644 index 20afbfa..0000000 --- a/test/07-test-bounds.js +++ /dev/null @@ -1,67 +0,0 @@ -var assert = require("assert") -var vec = require("../public/assets/javascripts/rectangles/models/vec2.js") -var Rect = require("../public/assets/javascripts/rectangles/models/rect.js") -var Room = require("../public/assets/javascripts/rectangles/models/room.js") -var Rooms = require("../public/assets/javascripts/rectangles/engine/rooms/_rooms.js") -var Clipper = require("../public/assets/javascripts/rectangles/engine/rooms/clipper.js") -var Builder = require("../public/assets/javascripts/rectangles/engine/rooms/builder.js") -var Grouper = require("../public/assets/javascripts/rectangles/engine/rooms/grouper.js") -var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 -var ALL = FRONT | BACK | LEFT | RIGHT - -var rect = new Rect( new vec(1,5), new vec(1,5) ) -var east = new Rect( new vec(2,6), new vec(1,5) ) -var corner = new Rect( new vec(3,7), new vec(3,7) ) -var peninsula = new Rect( new vec(4,6), new vec(6,8) ) -var big_rect = new Rect( new vec(1,5), new vec(1,5) ) -var hall_rect = new Rect( new vec(3,4), new vec(4,8) ) - -var rect_room = new Room({ id: "rect", rect: rect, height: 2 }) -var east_room = new Room({ id: "east", rect: east, height: 2 }) -var corner_room = new Room({ id: "corner", rect: corner, height: 2 }) -var peninsula_room = new Room({ id: "peninsula", rect: peninsula, height: 2 }) -var peninsula_taller = new Room({ id: "peninsula", rect: peninsula, height: 3 }) -var peninsula_shorter = new Room({ id: "peninsula", rect: peninsula, height: 1 }) - -var taller_room = new Room({ id: "taller", rect: rect, height: 3 }) - -var big_room = new Room({ id: "big_room", rect: big_rect, height: 4 }) -var hallway = new Room({ id: "hallway", rect: hall_rect, height: 2 }) - -function reset(){ - Rooms.forEach(function(room){ - room.reset() - }) - Rooms.list = {} - Rooms.regions = [] -} -function rebuild(){ - Rooms.clipper.solve_rects() - Rooms.builder.build() -} - -describe('wall', function(){ - reset() - Rooms.add( big_room ) - Rooms.add( hallway ) - rebuild() - - var collections = Rooms.grouper.collect() - var front_walls = Rooms.grouper.group([], collections, FRONT) - var back_walls = Rooms.grouper.group([], collections, BACK) - var left_walls = Rooms.grouper.group([], collections, LEFT) - var right_walls = Rooms.grouper.group([], collections, RIGHT) - - console.log(back_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.faces+"" })) - var u_wall = back_walls[0] - - describe('#fits()', function(){ - it("", function(){ - }) - }) - describe('#bounds_for()', function(){ - it("", function(){ - }) - }) -}) - diff --git a/test/07-test-surface.js b/test/07-test-surface.js new file mode 100644 index 0000000..8f69e77 --- /dev/null +++ b/test/07-test-surface.js @@ -0,0 +1,60 @@ +var assert = require("assert") +var vec2 = require("../public/assets/javascripts/rectangles/models/vec2.js") +var Rect = require("../public/assets/javascripts/rectangles/models/rect.js") +var Surface = require("../public/assets/javascripts/rectangles/models/surface.js") + +// [[1 3] [0 4]] front back left right +// [[3 4] [2 4]] front back left right +// [[4 5] [0 4]] front back left right + +describe('basic surface', function(){ + var surface = new Surface () + surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(3, 4), new vec2(2, 4) ) ) + surface.add( new Rect( new vec2(4, 6), new vec2(0, 4) ) ) + + var small = new vec2(2, 2) + var large = new vec2(10, 10) + var oblong = new vec2(4, 1) + + describe('#fits()', function(){ + it("fits something small", function(){ + assert.equal(true, !! surface.fits(small)) + }) + it("doesn't fit something large", function(){ + assert.equal(false, !! surface.fits(large)) + }) + it("fits something oblong", function(){ + assert.equal(true, !! surface.fits(oblong)) + }) + }) + describe('#fits_scale()', function(){ + it("fits something large, scaled down", function(){ + assert.equal(true, !! surface.fits_scale(large, 0.1)) + }) + it("doesn't fit something small, scaled up", function(){ + assert.equal(false, !! surface.fits_scale(small, 10)) + }) + it("doesn't fit something oblong, scaled up", function(){ + assert.equal(false, !! surface.fits_scale(oblong, 10)) + }) + }) + + describe('#place()', function(){ + it("fits a small element on the top left", function(){ + var bounds = surface.place(small, new vec2(1,3)) + console.log(bounds) + }) + it("places a small element on the right", function(){ + var bounds = surface.place(small, new vec2(4,6)) + console.log(bounds) + }) + }) + + describe('#clamp()', function(){ + it("", function(){ + }) + }) + +}) + diff --git a/views/partials/scripts.ejs b/views/partials/scripts.ejs index 915027d..acb3bff 100644 --- a/views/partials/scripts.ejs +++ b/views/partials/scripts.ejs @@ -31,6 +31,7 @@ + @@ -99,4 +100,4 @@ - \ No newline at end of file + -- cgit v1.2.3-70-g09d2 From 466ccfdccd2d761f31ba78a74a40544b77b358e5 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 6 Aug 2014 18:49:58 -0400 Subject: clamping --- .../javascripts/rectangles/models/surface.js | 70 +++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 448722b..171376f 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -71,8 +71,9 @@ 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--) { + for (var i = index-1; i >= 0; i--) { var face = faces[i] if (face.y.length() < height) { continue @@ -84,6 +85,7 @@ continue } bounds.x.a = bounds.x.a + bounds.first = i } for (var i = index+1; i < faces.length; i++) { @@ -98,9 +100,75 @@ 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 -- cgit v1.2.3-70-g09d2 From 04c2c21ad2ed872137b7a7e633d6bebcd7b04b61 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Mon, 11 Aug 2014 00:09:29 -0400 Subject: alt move function --- .../javascripts/rectangles/models/surface.js | 47 ++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 171376f..0fb90a8 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -65,6 +65,41 @@ } return null } + + Surface.prototype.move = function(dimensions, position, dx, dy) { + var face, faces = this.faces + var p = new Rect( new vec2( position.a, position.a + dimensions.a ), + new vec2( position.b, position.b + dimensions.b ) ) + dx = clamp(dx, -p.x.a, this.width - p.x.b) + dy = clamp(dy, -p.y.a, this.height - p.y.b) + + p.x.add(dx) + p.y.add(dy) + + var i = 0 + for (; i < faces.length; i++) { + face = faces[i] + // find the element that contains the top-left corner + if (face.x.a <= p.x.a && face.x.b <= p.x.a) { + break + } + } + if (! face) return null + + if (face.y.a <= p.x.a && p.y.b <= face.y.b) { + } + + if (face.x.b >= p.y.b) { + return p + } + else { + } + + + + } + + Surface.prototype.place = function(v, index){ var face, faces = this.faces } @@ -121,9 +156,15 @@ } 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 + for (var i = nextIndex; i >= 0; i--) { + nextFace = this.faces[i] + if (nextFace.y.a <= p.b && p.b + dimension.b <= nextFace.y.b) { + // it appears the div to the left will accomodate this element + nextIndex = i + } + else { + + } } else { p.a = bounds.x.a -- cgit v1.2.3-70-g09d2 From b7b881a00a9b73ba54cc3a62edc402a903ec9142 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 13 Aug 2014 17:54:15 -0400 Subject: comment --- public/assets/javascripts/rectangles/engine/scenery/move.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/scenery/move.js b/public/assets/javascripts/rectangles/engine/scenery/move.js index ef9bc32..c3f78d7 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/move.js +++ b/public/assets/javascripts/rectangles/engine/scenery/move.js @@ -76,8 +76,6 @@ Scenery.move = function(base){ dragging = false document.body.classList.remove("dragging") - console.log("pushing", oldState, base.serialize()) - UndoStack.push({ type: 'update-scenery', undo: oldState, -- cgit v1.2.3-70-g09d2 From b07ed6868895f883640fc003ba4f5cc108c27b3e Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 13 Aug 2014 18:30:28 -0400 Subject: pull in minotaur --- .../assets/javascripts/rectangles/util/minotaur.js | 58 ++++++++++++++++++++++ public/assets/javascripts/ui/_router.js | 1 + 2 files changed, 59 insertions(+) create mode 100644 public/assets/javascripts/rectangles/util/minotaur.js (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/util/minotaur.js b/public/assets/javascripts/rectangles/util/minotaur.js new file mode 100644 index 0000000..b4c81f0 --- /dev/null +++ b/public/assets/javascripts/rectangles/util/minotaur.js @@ -0,0 +1,58 @@ +$(function(){ + + var Monitor = function () { + var base = this; + base.$el = $("#save"); + base.timeout = null; + base.delay = 500; + base.objects = {}; + + base.init = function () { + base.$el.addClass('saved').html('Save'); + base.$el.click(base.save); + } + + base.watch = function (object) { + base.objects[object.type] = base.objects[object.type] || {}; + base.objects[object.type][object.id] = object; + base.clear(); + base.timeout = setTimeout(base.save, base.delay); + }; + + base.clear = function () { + if (base.timeout) clearTimeout(base.timeout); + base.timeout = false; + }; + + base.save = function () { + var saving = false; + base.clear(); + + for (var type in base.objects) { + for (var id in base.objects[type]) { + if (base.timeout) + return; + var obj = base.objects[type][id]; + if (obj) obj.save(function(){ + base.$el.removeClass('unsaved saving').addClass('saved').html('Saved'); + saving = true; + }); + base.objects[type][id] = false; + } + } + + if (saving) { + base.$el.removeClass('unsaved saved').addClass('saving').html('Saving'); + } + else { + base.$el.removeClass('unsaved saving').addClass('saved').html('Saved'); + } + + base.objects = {}; + }; + + base.init(); + }; + + window.Minotaur = new Monitor (); +}); diff --git a/public/assets/javascripts/ui/_router.js b/public/assets/javascripts/ui/_router.js index d07810e..794079e 100644 --- a/public/assets/javascripts/ui/_router.js +++ b/public/assets/javascripts/ui/_router.js @@ -27,6 +27,7 @@ var SiteRouter = Router.extend({ "/profile": 'profile', "/profile/edit": 'editProfile', + "/profile/:name": 'profile', "/about/:name/edit": 'editDocument', "/about/new": 'newDocument', -- cgit v1.2.3-70-g09d2 From 0f649f2cfb034ad8458f08c8f23a2251135af059 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 13 Aug 2014 19:18:44 -0400 Subject: css --- .../assets/javascripts/rectangles/util/minotaur.js | 23 +++++++++++----------- public/assets/stylesheets/app.css | 19 +++++++++++++++++- views/partials/header.ejs | 4 ++++ 3 files changed, 34 insertions(+), 12 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/util/minotaur.js b/public/assets/javascripts/rectangles/util/minotaur.js index b4c81f0..6eb36ec 100644 --- a/public/assets/javascripts/rectangles/util/minotaur.js +++ b/public/assets/javascripts/rectangles/util/minotaur.js @@ -1,14 +1,14 @@ -$(function(){ +(function(){ var Monitor = function () { var base = this; - base.$el = $("#save"); + base.$el = $("#minotaur"); base.timeout = null; base.delay = 500; base.objects = {}; base.init = function () { - base.$el.addClass('saved').html('Save'); + base.$el.addClass('saved'); base.$el.click(base.save); } @@ -17,12 +17,12 @@ $(function(){ base.objects[object.type][object.id] = object; base.clear(); base.timeout = setTimeout(base.save, base.delay); - }; + } base.clear = function () { if (base.timeout) clearTimeout(base.timeout); base.timeout = false; - }; + } base.save = function () { var saving = false; @@ -34,7 +34,7 @@ $(function(){ return; var obj = base.objects[type][id]; if (obj) obj.save(function(){ - base.$el.removeClass('unsaved saving').addClass('saved').html('Saved'); + base.$el.removeClass('unsaved saving').addClass('saved'); saving = true; }); base.objects[type][id] = false; @@ -42,17 +42,18 @@ $(function(){ } if (saving) { - base.$el.removeClass('unsaved saved').addClass('saving').html('Saving'); + base.$el.removeClass('unsaved saved').addClass('saving'); } else { - base.$el.removeClass('unsaved saving').addClass('saved').html('Saved'); + base.$el.removeClass('unsaved saving').addClass('saved'); } base.objects = {}; - }; + } base.init(); - }; + } window.Minotaur = new Monitor (); -}); + +})() diff --git a/public/assets/stylesheets/app.css b/public/assets/stylesheets/app.css index e15ba84..63a4496 100755 --- a/public/assets/stylesheets/app.css +++ b/public/assets/stylesheets/app.css @@ -629,7 +629,24 @@ iframe.embed { background-size: 100% 100%; } - +#minotaur { + position: absolute; + top: 26px; + right: 260px; + opacity: 0; +} +#minotaur .label:after { + padding: 6px; + font-size: 13px; + font-weight: 300; +} +#minotaur.saving { + background: #8fd; + opacity: 1; +} +#minotaur.saving .label:after { + content: 'SAVING'; +} .rapper { position:relative; diff --git a/views/partials/header.ejs b/views/partials/header.ejs index e83f42a..6697aff 100644 --- a/views/partials/header.ejs +++ b/views/partials/header.ejs @@ -1,5 +1,9 @@ +
+ +
+ [[ if (logged_in) { ]] -- cgit v1.2.3-70-g09d2 From 410607684c7273a61f937635b41397208e245473 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 14 Aug 2014 13:57:42 -0400 Subject: autosave --- .../javascripts/rectangles/engine/scenery/move.js | 8 ++- .../rectangles/engine/scenery/resize.js | 3 + .../javascripts/rectangles/engine/scenery/undo.js | 18 ++++++ .../assets/javascripts/rectangles/models/wall.js | 3 + .../assets/javascripts/rectangles/util/minotaur.js | 67 ++++++++++++---------- .../javascripts/ui/builder/BuilderSettings.js | 3 + .../assets/javascripts/ui/editor/EditorSettings.js | 10 +++- public/assets/javascripts/ui/editor/MediaEditor.js | 1 + public/assets/javascripts/ui/lib/FormView.js | 28 +++++++-- public/assets/javascripts/ui/lib/View.js | 17 +++--- views/partials/scripts.ejs | 1 + 11 files changed, 111 insertions(+), 48 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/scenery/move.js b/public/assets/javascripts/rectangles/engine/scenery/move.js index c3f78d7..edeb24b 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/move.js +++ b/public/assets/javascripts/rectangles/engine/scenery/move.js @@ -30,6 +30,9 @@ Scenery.move = function(base){ redo: { id: base.id }, }) + // TODO: watch individual scenery object here + Minotaur.watch( app.router.editorView.settings ) + Scenery.remove(base.id) return } @@ -81,7 +84,10 @@ Scenery.move = function(base){ undo: oldState, redo: base.serialize(), }) - + + // TODO: watch individual scenery object here + Minotaur.watch( app.router.editorView.settings ) + oldState = null } diff --git a/public/assets/javascripts/rectangles/engine/scenery/resize.js b/public/assets/javascripts/rectangles/engine/scenery/resize.js index c5c754a..6b2e52c 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/resize.js +++ b/public/assets/javascripts/rectangles/engine/scenery/resize.js @@ -200,6 +200,9 @@ Scenery.resize = new function(){ redo: obj.serialize(), }) + // TODO: watch individual scenery object here + Minotaur.watch( app.router.editorView.settings ) + document.body.classList.remove("dragging") } diff --git a/public/assets/javascripts/rectangles/engine/scenery/undo.js b/public/assets/javascripts/rectangles/engine/scenery/undo.js index 7798550..54ab755 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/undo.js +++ b/public/assets/javascripts/rectangles/engine/scenery/undo.js @@ -4,9 +4,15 @@ type: "create-scenery", undo: function(state){ Scenery.remove(state.id) + + // TODO: watch individual scenery object here + Minotaur.watch( app.router.editorView.settings ) }, redo: function(state){ Scenery.deserialize([ state ]) + + // TODO: watch individual scenery object here + Minotaur.watch( app.router.editorView.settings ) }, }, { @@ -19,6 +25,9 @@ if (editor.permissions.resize) { Scenery.resize.show(scenery) } + + // TODO: watch individual scenery object here + Minotaur.watch( app.router.editorView.settings ) }, redo: function(state){ var scenery = Scenery.find(state.id) @@ -30,15 +39,24 @@ Scenery.resize.rotate_dots() Scenery.resize.move_dots() } + + // TODO: watch individual scenery object here + Minotaur.watch( app.router.editorView.settings ) }, }, { type: "destroy-scenery", undo: function(state){ Scenery.deserialize([ state ]) + + // TODO: watch individual scenery object here + Minotaur.watch( app.router.editorView.settings ) }, redo: function(state){ Scenery.remove(state.id) + + // TODO: watch individual scenery object here + Minotaur.watch( app.router.editorView.settings ) }, }, diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index 6e2c728..fa09444 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -50,6 +50,9 @@ window.Wall = (function(){ 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() diff --git a/public/assets/javascripts/rectangles/util/minotaur.js b/public/assets/javascripts/rectangles/util/minotaur.js index 6eb36ec..039a053 100644 --- a/public/assets/javascripts/rectangles/util/minotaur.js +++ b/public/assets/javascripts/rectangles/util/minotaur.js @@ -1,54 +1,59 @@ (function(){ var Monitor = function () { - var base = this; - base.$el = $("#minotaur"); - base.timeout = null; - base.delay = 500; - base.objects = {}; + var base = this + base.$el = $("#minotaur") + base.timeout = null + base.delay = 500 + base.objects = {} base.init = function () { - base.$el.addClass('saved'); - base.$el.click(base.save); + base.$el.removeClass() + base.$el.click(base.save) } base.watch = function (object) { - base.objects[object.type] = base.objects[object.type] || {}; - base.objects[object.type][object.id] = object; - base.clear(); - base.timeout = setTimeout(base.save, base.delay); + base.objects[object.type] = base.objects[object.type] || {} + base.objects[object.type][object._id] = object + base.clear() + base.timeout = setTimeout(base.save, base.delay) + } + + base.unwatch = function (object) { + if (base.objects[object.type] && base.objects[object.type][object._id]) { + delete base.objects[object.type][object._id] + } } base.clear = function () { - if (base.timeout) clearTimeout(base.timeout); - base.timeout = false; + if (base.timeout) clearTimeout(base.timeout) + base.timeout = false } base.save = function () { - var saving = false; - base.clear(); + var saving = false + base.clear() for (var type in base.objects) { for (var id in base.objects[type]) { - if (base.timeout) - return; - var obj = base.objects[type][id]; - if (obj) obj.save(function(){ - base.$el.removeClass('unsaved saving').addClass('saved'); - saving = true; - }); - base.objects[type][id] = false; + var obj = base.objects[type][id] + if (obj) { + obj.save(null, function(){ base.hide() }, function(){}) + } + delete base.objects[type][id] + saving = true } } - if (saving) { - base.$el.removeClass('unsaved saved').addClass('saving'); - } - else { - base.$el.removeClass('unsaved saving').addClass('saved'); - } - - base.objects = {}; + saving ? base.show() : base.hide() + } + + base.show = function () { + base.$el.removeClass().addClass('saving') + } + + base.hide = function () { + base.$el.removeClass() } base.init(); diff --git a/public/assets/javascripts/ui/builder/BuilderSettings.js b/public/assets/javascripts/ui/builder/BuilderSettings.js index c551f95..0091454 100644 --- a/public/assets/javascripts/ui/builder/BuilderSettings.js +++ b/public/assets/javascripts/ui/builder/BuilderSettings.js @@ -122,6 +122,9 @@ var BuilderSettings = FormView.extend({ this.$name.val(data.name) this.action = this.updateAction + Minotaur.unwatch(this) + Minotaur.hide() + window.history.pushState(null, document.title, "/layout/" + data.slug) }, diff --git a/public/assets/javascripts/ui/editor/EditorSettings.js b/public/assets/javascripts/ui/editor/EditorSettings.js index d6a79fb..9d75f66 100644 --- a/public/assets/javascripts/ui/editor/EditorSettings.js +++ b/public/assets/javascripts/ui/editor/EditorSettings.js @@ -5,7 +5,7 @@ var EditorSettings = FormView.extend({ createAction: "/api/project/new", updateAction: "/api/project/edit", destroyAction: "/api/project/destroy", - + events: { "keydown": 'stopPropagation', "keydown [name=name]": 'enterSubmit', @@ -32,7 +32,10 @@ var EditorSettings = FormView.extend({ data.rooms && Rooms.deserialize(data.rooms) data.startPosition && scene.camera.move(data.startPosition) - if (! data.isNew) { + if (data.isNew) { + this.$name.val( "Room " + moment().format("DD/MM/YYYY ha") ) + } + else { // console.log(data) this.$id.val( data._id ) @@ -130,6 +133,9 @@ var EditorSettings = FormView.extend({ this.$name.val(data.name) this.action = this.updateAction + Minotaur.unwatch(this) + Minotaur.hide() + window.history.pushState(null, document.title, "/project/" + data.slug + "/edit") }, diff --git a/public/assets/javascripts/ui/editor/MediaEditor.js b/public/assets/javascripts/ui/editor/MediaEditor.js index e3a8f2e..cc924da 100644 --- a/public/assets/javascripts/ui/editor/MediaEditor.js +++ b/public/assets/javascripts/ui/editor/MediaEditor.js @@ -4,6 +4,7 @@ var MediaEditor = FormView.extend({ events: { "keydown": 'stopPropagation', + "focus [name]": "clearMinotaur", "click [data-role=play-media]": "togglePaused", "mousedown [name=keyframe]": "stopPropagation", "mousedown": "stopPropagation", diff --git a/public/assets/javascripts/ui/lib/FormView.js b/public/assets/javascripts/ui/lib/FormView.js index 219952d..ab33bc0 100644 --- a/public/assets/javascripts/ui/lib/FormView.js +++ b/public/assets/javascripts/ui/lib/FormView.js @@ -54,15 +54,20 @@ var FormView = View.extend({ return fd }, - save: function(e){ - e.preventDefault() + save: function(e, successCallback, errorCallback){ + e && e.preventDefault() this.$errors.hide().css("opacity", 0.0); if (this.validate) { var errors = this.validate() if (errors && errors.length) { - this.showErrors(errors) + if (errorCallback) { + errorCallback(errors) + } + else { + this.showErrors(errors) + } return } } @@ -74,18 +79,29 @@ var FormView = View.extend({ dataType: "json", processData: false, contentType: false, - }); + }) + request.done($.proxy(function (response) { if (response.error) { var errors = [] for (var key in response.error.errors) { errors.push(response.error.errors[key].message); } - this.showErrors(errors) + if (errorCallback) { + errorCallback(errors) + } + else { + this.showErrors(errors) + } return } else { - this.success && this.success(response) + if (successCallback) { + successCallback(response) + } + if (this.success) { + this.success(response) + } } }, this)); } diff --git a/public/assets/javascripts/ui/lib/View.js b/public/assets/javascripts/ui/lib/View.js index 999a0e5..d94e6db 100644 --- a/public/assets/javascripts/ui/lib/View.js +++ b/public/assets/javascripts/ui/lib/View.js @@ -1,13 +1,14 @@ var View = (function($, _){ var View = function(options) { - this.cid = _.uniqueId('view'); + this._id = _.uniqueId('view') + this.type = "view" options || (options = {}); - _.extend(this, _.pick(options, viewOptions)); - this._ensureElement(); - this.initialize.apply(this, arguments); - this.delegateEvents(); - }; + _.extend(this, _.pick(options, viewOptions)) + this._ensureElement() + this.initialize.apply(this, arguments) + this.delegateEvents() + } var delegateEventSplitter = /^(\S+)\s*(.*)$/; @@ -58,7 +59,7 @@ var View = (function($, _){ var match = key.match(delegateEventSplitter); var eventName = match[1], selector = match[2]; method = _.bind(method, this); - eventName += '.delegateEvents' + this.cid; + eventName += '.delegateEvents' + this._id; if (selector === '') { this.$el.on(eventName, method); } else { @@ -70,7 +71,7 @@ var View = (function($, _){ // Clears all callbacks previously bound to the view with `delegateEvents`. undelegateEvents: function() { - this.$el.off('.delegateEvents' + this.cid); + this.$el.off('.delegateEvents' + this._id); return this; }, diff --git a/views/partials/scripts.ejs b/views/partials/scripts.ejs index dfb3a83..4839de8 100644 --- a/views/partials/scripts.ejs +++ b/views/partials/scripts.ejs @@ -30,6 +30,7 @@ + -- cgit v1.2.3-70-g09d2 From 8ed5bf2ceba8e85be9c911f8b33483242e979ab7 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 14 Aug 2014 17:45:08 -0400 Subject: fix last wrinkle in grouper --- .../javascripts/rectangles/engine/rooms/grouper.js | 73 +++++++++++++--------- .../rectangles/engine/scenery/_scenery.js | 4 +- .../rectangles/engine/scenery/types/_object.js | 4 +- .../rectangles/engine/scenery/types/image.js | 8 ++- .../rectangles/engine/scenery/types/video.js | 8 ++- .../assets/javascripts/rectangles/models/wall.js | 19 +++--- test/06-test-grouper.js | 15 ++--- test/09-test-undo.js | 2 +- 8 files changed, 79 insertions(+), 54 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/rooms/grouper.js b/public/assets/javascripts/rectangles/engine/rooms/grouper.js index 4ad3bd8..cde9fbb 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/grouper.js +++ b/public/assets/javascripts/rectangles/engine/rooms/grouper.js @@ -1,6 +1,6 @@ (function(){ - var vec2, Rect, Rooms, UidGenerator, Wall, Surface, sort + var vec2, Rect, Rooms, UidGenerator, Wall, Surface, sort, _ if ('window' in this) { vec2 = window.vec2 Rect = window.Rect @@ -9,6 +9,7 @@ UidGenerator = window.UidGenerator Wall = window.Wall sort = window.sort + _ = window._ } else { Rooms = require('./_rooms') @@ -18,6 +19,7 @@ Wall = require('../../models/wall') Surface = require('../../models/surface') sort = require('../../util/sort') + _ = require('lodash') FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20 PI = Math.PI HALF_PI = PI/2 @@ -82,6 +84,7 @@ collection.sort( useX ? sort.compare_zx : sort.compare_xz ) collection.forEach(function(mx){ if (last_mx && last_mx.rect.eq(mx.rect)) { + // culls half-walls if (last_mx.rect.id == mx.rect.id) { last_mx.height += mx.height/2 last_mx.y += mx.height/4 @@ -103,41 +106,53 @@ } base.group = function(walls, collections, side){ var collection = collections[side] - var wall var useX = side & FRONT_BACK - var useA = side & (FRONT | RIGHT) + var useA = side & (FRONT | LEFT) // collection.sort( useX ? sort.compare_zx : sort.compare_xz ) + var planes = {} + collection.forEach(function(mx){ if (mx.culled) return - var coplanar = wall && wall.edge == mx.rect[useX ? 'y': 'x'][useA ? 'a': 'b'] - - if (wall && coplanar) { - if (useX && wall.vec.b == mx.rect.x.a) { - wall.vec.b = mx.rect.x.b - wall.mx.push(mx) - wall.surface.add(mx.face) - return - } - else if (! useX && wall.vec.b == mx.rect.y.a) { - wall.vec.b = mx.rect.y.b - wall.mx.push(mx) - wall.surface.add(mx.face) - return - } - } - wall = new Wall ({ - id: base.uid(), - side: side, - mx: [ mx ], - surface: new Surface( mx.face ), - vec: mx.rect[ useX ? 'x' : 'y' ].clone(), - edge: mx.rect[ useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], - }) - walls.push(wall) + var edge = mx.rect[useX ? 'y': 'x'][ useA ? 'a': 'b'] + planes[edge] = planes[edge] || [] + planes[edge].push(mx) }) - + + var edges = _.keys(planes) + edges.forEach(function(edge){ + + var wall + + planes[edge].forEach(function(mx){ + + if (wall) { + if (useX && wall.vec.b == mx.rect.x.a) { + wall.vec.b = mx.rect.x.b + wall.mx.push(mx) + wall.surface.add(mx.face) + return + } + else if (! useX && wall.vec.b == mx.rect.y.a) { + wall.vec.b = mx.rect.y.b + wall.mx.push(mx) + wall.surface.add(mx.face) + return + } + } + wall = new Wall ({ + id: base.uid(), + side: side, + mx: [ mx ], + surface: new Surface( mx.face ), + vec: mx.rect[ useX ? 'x' : 'y' ].clone(), + edge: mx.rect[ useX ? 'y' : 'x' ][ useA ? 'a' : 'b' ], + }) + walls.push(wall) + }) + }) + return walls } diff --git a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js index c43ef14..b4a38f8 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js +++ b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js @@ -14,7 +14,7 @@ var Scenery = new function(){ base.add = function(opt){ var scene_media - switch (media.type) { + switch (opt.media.type) { case 'image': scene_media = new Scenery.types.image (opt) break @@ -74,11 +74,11 @@ var Scenery = new function(){ scenery_data.forEach(function(data){ var wall = Rooms.walls[data.wall_id] var scene_media = base.add({ + data: data, wall: wall, media: data.media, id: data.id }) - scene_media.deserialize(data) }) } diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js index 46bc0e7..3a2dcc2 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js @@ -62,8 +62,8 @@ Scenery.types.base = Fiber.extend(function(base){ set_wall: function(wall, mx){ this.wall = wall || this.wall - this.bounds = this.wall.bounds_for(this.media, this.scale) - this.center = this.wall.center() + // this.bounds = this.wall.bounds_for(this.media, this.scale) + // this.center = this.wall.center() }, set_scale: function(scale){ diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/image.js b/public/assets/javascripts/rectangles/engine/scenery/types/image.js index 99c1810..576242e 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/image.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/image.js @@ -10,7 +10,13 @@ Scenery.types.image = Scenery.types.base.extend(function(base){ this.build() this.bind() this.set_wall() - this.recenter() + + if (opt.data) { + this.deserialize(opt.data) + } + else { + this.recenter() + } }, build: function(){ diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/video.js b/public/assets/javascripts/rectangles/engine/scenery/types/video.js index a8df875..0bd5c06 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/video.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/video.js @@ -10,7 +10,13 @@ Scenery.types.video = Scenery.types.base.extend(function(base){ this.build() this.bind() this.set_wall() - this.recenter() + + if (opt.data) { + this.deserialize(opt.data) + } + else { + this.recenter() + } }, build: function(){ diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index b66d5f5..460963b 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -144,18 +144,15 @@ 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(){ diff --git a/test/06-test-grouper.js b/test/06-test-grouper.js index 0f14217..184a3d7 100644 --- a/test/06-test-grouper.js +++ b/test/06-test-grouper.js @@ -194,8 +194,8 @@ describe('grouper(rect,corner,peninsula)', function(){ it("left has 3 walls", function(){ assert.equal(3, left_walls.length) }) - it("right has 4 walls", function(){ - assert.equal(4, right_walls.length) + it("right has 3 walls", function(){ + assert.equal(3, right_walls.length) }) it("front has 2 walls", function(){ assert.equal(2, front_walls.length) @@ -229,14 +229,15 @@ describe('grouper(rect,corner,peninsula_taller)', function(){ var front_walls = Rooms.grouper.group([], collections, FRONT) var back_walls = Rooms.grouper.group([], collections, BACK) var left_walls = Rooms.grouper.group([], collections, LEFT) + Rooms.grouper.debug = true var right_walls = Rooms.grouper.group([], collections, RIGHT) + Rooms.grouper.debug = false - it("left has 4 walls", function(){ - assert.equal(4, left_walls.length) + it("left has 3 walls", function(){ + assert.equal(3, left_walls.length) }) - it("right has 5 walls", function(){ - assert.equal(5, right_walls.length) - // console.log(right_walls.map(function(m){ return m.vec + " " + m.edge + " " + m.surface +"" })) + it("right has 3 walls", function(){ + assert.equal(3, right_walls.length) }) it("front has 3 walls", function(){ assert.equal(3, front_walls.length) diff --git a/test/09-test-undo.js b/test/09-test-undo.js index dbca90e..60fbbb6 100644 --- a/test/09-test-undo.js +++ b/test/09-test-undo.js @@ -1,5 +1,5 @@ var assert = require("assert") -var UndoStack = require("../public/assets/javascripts/rectangles/util/undo.js") +var UndoStack = require("../public/assets/javascripts/rectangles/util/undostack.js") UndoStack.debug = false describe('undo', function(){ -- cgit v1.2.3-70-g09d2 From d72d13d06abb3fbfcb9eef561f9fe391d433e7a5 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 14 Aug 2014 18:36:54 -0400 Subject: click fix --- public/assets/javascripts/rectangles/engine/scenery/types/_object.js | 1 - 1 file changed, 1 deletion(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js index 3a2dcc2..6bd5863 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js @@ -83,7 +83,6 @@ Scenery.types.base = Fiber.extend(function(base){ serialize: function(){ var data = { id: this.id, - room_id: this.wall.room_id, wall_id: this.wall.id, side: this.wall.side, dimensions: this.dimensions.serialize(), -- cgit v1.2.3-70-g09d2 From 174077e0a9779f73b1b87253c8c7bae8d03400fb Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 15 Aug 2014 17:04:44 -0400 Subject: clamp stuff --- .../javascripts/rectangles/models/surface.js | 76 ++++++++++++++++++++-- test/07-test-surface.js | 66 ++++++++++++++++++- 2 files changed, 133 insertions(+), 9 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 171376f..8330d41 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -11,17 +11,22 @@ } var Surface = function (face){ - this.width = 0 - this.height = 0 + 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.b = Math.max(this.bounds.y.b, rect.height()) 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) @@ -30,7 +35,7 @@ Surface.prototype.fits = function(v){ var faces = this.faces var scratch - if (this.width < v.a || this.height < v.b) { + if (this.bounds.x.b < v.a || this.bounds.y.b < v.b) { return null } for (var i = 0; i < faces.length; i++) { @@ -68,7 +73,65 @@ Surface.prototype.place = function(v, index){ var face, faces = this.faces } - Surface.prototype.bounds = function(index){ + + function center (dimension, position) { + return new vec2( position.a + dimension.a/2, position.b + dimension.b/2 ) + } + function edges (dimension, position) { + return new Rect( position.a, position.a + dimension.a, + position.b, position.b + dimension.b ) + } + + 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 + } + } + + Surface.prototype.fit = function (old_bounds, dimension, position, delta) { + // to reposition one of these fucking things + // assume it's already in a valid position + + this.clamp_delta( this.bounds, dimension, position, delta ) + + // if it's moved and it's within the current bounds, we're done + // it's outside the general bounds, do an initial clamp + // given the center, find the element it wants to lie on + // if it's too short for this element, look to the (left? right? compare dx) for the first element tall enough for it + // move the (right? left?) edge to whatever div that is and move the center + // find the elements containing the left and right edges, and any in between + // + } + + Surface.prototype.index_for_x = function(x){ + if (x < 0 || x > width) { + return -1 + } + for (var i = 0; i < this.faces.length; i++) { + if (this.faces[i].x.contains(x)) { + return i + } + } + return -1 + } + + Surface.prototype.bounds_at_index = function(index){ var bounds = faces[index].clone() var height = faces[index].height() bounds.first = bounds.last = index @@ -104,6 +167,7 @@ } 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 diff --git a/test/07-test-surface.js b/test/07-test-surface.js index b2e4769..2a51b7f 100644 --- a/test/07-test-surface.js +++ b/test/07-test-surface.js @@ -9,7 +9,67 @@ var Surface = require("../public/assets/javascripts/rectangles/models/surface.js describe('basic surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(1, 5), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(1, 6), new vec2(0, 4) ) ) + + var small = new vec2(2, 2) + var oblong = new vec2(4, 1) + + describe('#clamp_delta()', function(){ + var position = new vec2(2,1) + it("does not alter a zero delta", function(){ + var delta = new vec2(0,0) + surface.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, 0 ))) + }) + it("does not alter a minimal delta", function(){ + var delta = new vec2(1,1) + surface.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 1, 1 ))) + }) + it("clamps leftward delta", function(){ + var delta = new vec2(-10, 0) + surface.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( -1, 0 ))) + }) + it("clamps rightward delta", function(){ + var delta = new vec2(10, 0) + surface.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 2, 0 ))) + }) + it("clamps upward delta", function(){ + var delta = new vec2(0, 10) + surface.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, 1 ))) + }) + it("clamps downward delta", function(){ + var delta = new vec2(0, -10) + surface.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, -1 ))) + }) + }) + + describe('#place()', function(){ + it("fits a small element on the top left", function(){ + var bounds = surface.place(small, new vec2(1,3)) +// console.log(bounds) + }) + it("places a small element on the right", function(){ + var bounds = surface.place(small, new vec2(4,6)) +// console.log(bounds) + }) + }) + + // describe placement + // describe dragging up (clamp at top edge) + // describe dragging down (clamp at bottom edge) + // describe dragging left (clamp at left edge) + // describe dragging right (clamp at right edge) +}) + +describe('double surface', function(){ + var surface = new Surface () + surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(3, 5), new vec2(0, 4) ) ) var small = new vec2(2, 2) var oblong = new vec2(4, 1) @@ -23,7 +83,7 @@ describe('basic surface', function(){ describe('two-level surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) surface.add( new Rect( new vec2(3, 5), new vec2(0, 6) ) ) var small = new vec2(2, 2) @@ -39,7 +99,7 @@ describe('two-level surface', function(){ describe('door surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) surface.add( new Rect( new vec2(3, 4), new vec2(2, 4) ) ) surface.add( new Rect( new vec2(4, 6), new vec2(0, 4) ) ) -- cgit v1.2.3-70-g09d2 From a8ed33d8313b093f589a5483cb00c7163b7b8dc3 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 18 Aug 2014 15:01:56 -0400 Subject: surface tests --- .../javascripts/rectangles/models/surface.js | 226 +++++++++-------- .../assets/javascripts/rectangles/models/vec2.js | 4 +- test/01-test-vec2.js | 19 ++ test/07-test-surface.js | 271 ++++++++++++++++----- 4 files changed, 355 insertions(+), 165 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 8330d41..9b82e4e 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -70,16 +70,14 @@ } return null } - Surface.prototype.place = function(v, index){ - var face, faces = this.faces - } - function center (dimension, position) { - return new vec2( position.a + dimension.a/2, position.b + dimension.b/2 ) - } - function edges (dimension, position) { - return new Rect( position.a, position.a + dimension.a, - position.b, position.b + dimension.b ) + 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) { @@ -104,26 +102,38 @@ } } - Surface.prototype.fit = function (old_bounds, dimension, position, delta) { - // to reposition one of these fucking things - // assume it's already in a valid position - + Surface.prototype.translate = function (old_bounds, dimension, position, delta) { this.clamp_delta( this.bounds, dimension, position, delta ) - // if it's moved and it's within the current bounds, we're done - // it's outside the general bounds, do an initial clamp - // given the center, find the element it wants to lie on - // if it's too short for this element, look to the (left? right? compare dx) for the first element tall enough for it - // move the (right? left?) edge to whatever div that is and move the center - // find the elements containing the left and right edges, and any in between - // + 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.sides[left_side].clone() + + for (var i = left_side+1; i <= right_side; i++) { + if (this.faces[i].y.a > bounds.y.a) { + bounds.y.a = this.faces[i].y.a + } + if (this.faces[i].y.b < bounds.y.b) { + bounds.y.b = this.faces[i].y.b + } + bounds.x.b = this.faces[i].x.b + } + + if (bounds.width() > dimension.a || bounds.height() > dimension.b) { + bounds = old_bounds + } + + this.clamp_delta(bounds, dimension, position, delta) + return bounds } - Surface.prototype.index_for_x = function(x){ + Surface.prototype.index_for_x = function(x, min_i){ + min_i = min_i || 0 if (x < 0 || x > width) { return -1 } - for (var i = 0; i < this.faces.length; i++) { + for (var i = min_i; i < this.faces.length; i++) { if (this.faces[i].x.contains(x)) { return i } @@ -131,107 +141,113 @@ return -1 } - Surface.prototype.bounds_at_index = function(index){ - var bounds = faces[index].clone() - var height = faces[index].height() - bounds.first = bounds.last = index + Surface.prototype.bounds_at_index_with_dimensions = function(index, dimensions){ + var faces = this.faces + if (index == -1) index = this.faces.length-1 - for (var i = index-1; i >= 0; i--) { + 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) { - continue - } - if (face.y.a > bounds.y.a) { - continue + if (bounds) break + else continue } - if (face.y.b < bounds.y.b) { - continue + if (! bounds) { + bounds = face.clone() + bounds.last = i + } + else if (bounds.width() < width) { + intersection = bounds.y.intersection(face.y) + if (intersection.length() < height) { + 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 = bounds.x.a + bounds.x.a = face.x.a bounds.first = i } - - for (var i = index+1; i < faces.length; i++) { + + for (var i = bounds ? bounds.last+1 : index+1; i < faces.length; i++) { var face = faces[i] if (face.y.length() < height) { - continue + if (bounds) break + else continue } - if (face.y.a > bounds.y.a) { - continue - } - if (face.y.b < bounds.y.b) { - continue - } - bounds.x.b = bounds.x.b + if (! bounds) { + bounds = face.clone() + bounds.first = i + } + else if (bounds.width() < width) { + intersection = bounds.y.intersection(face.y) + if (intersection.length() < height) { + break + } + 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.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 - } - } + 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 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 + 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 } - // if we're able to move out of bounds in that direction, recalculate the bounds + return bounds } if ('window' in this) { diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index 214feb9..a36c614 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -123,12 +123,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/test/01-test-vec2.js b/test/01-test-vec2.js index 6104f92..429c537 100644 --- a/test/01-test-vec2.js +++ b/test/01-test-vec2.js @@ -37,6 +37,25 @@ describe('vec2', function(){ }) }) + describe('#intersection()', function(){ + it('intersects from left', function(){ + var inter = vec.intersection( new vec2( 5, 15 ) ) + assert.equal(true, inter.eq( new vec2(5, 10) )) + }) + it('intersects from right', function(){ + var inter = vec.intersection( new vec2( -5, 5 ) ) + assert.equal(true, inter.eq( new vec2(0, 5) )) + }) + it('intersects inner', function(){ + var inter = vec.intersection( new vec2( 2, 5 ) ) + assert.equal(true, inter.eq( new vec2(2, 5) )) + }) + it('intersects outer', function(){ + var inter = vec.intersection( new vec2( -5, 15 ) ) + assert.equal(true, inter.eq( new vec2(0, 10) )) + }) + }) + describe('#contains()', function(){ it('contains itself', function(){ assert.equal(true, vec.contains( 0 )); diff --git a/test/07-test-surface.js b/test/07-test-surface.js index 2a51b7f..fa05d43 100644 --- a/test/07-test-surface.js +++ b/test/07-test-surface.js @@ -7,15 +7,19 @@ var Surface = require("../public/assets/javascripts/rectangles/models/surface.js // [[3 4] [2 4]] front back left right // [[4 5] [0 4]] front back left right +var small = new vec2(2, 2) +var wide = new vec2(5, 1) +var tall = new vec2(1, 5) +var large = new vec2(7, 7) +var position = new vec2(2, 1) + describe('basic surface', function(){ var surface = new Surface () surface.add( new Rect( new vec2(1, 6), new vec2(0, 4) ) ) - var small = new vec2(2, 2) - var oblong = new vec2(4, 1) + var position = new vec2(2, 1) describe('#clamp_delta()', function(){ - var position = new vec2(2,1) it("does not alter a zero delta", function(){ var delta = new vec2(0,0) surface.clamp_delta(surface.bounds, small, position, delta) @@ -48,64 +52,152 @@ describe('basic surface', function(){ }) }) - describe('#place()', function(){ - it("fits a small element on the top left", function(){ - var bounds = surface.place(small, new vec2(1,3)) -// console.log(bounds) + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with wide", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) }) - it("places a small element on the right", function(){ - var bounds = surface.place(small, new vec2(4,6)) -// console.log(bounds) + it("returns false for large image", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, large) + assert.equal(false, bounds) }) }) - - // describe placement - // describe dragging up (clamp at top edge) - // describe dragging down (clamp at bottom edge) - // describe dragging left (clamp at left edge) - // describe dragging right (clamp at right edge) }) describe('double surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) - surface.add( new Rect( new vec2(3, 5), new vec2(0, 4) ) ) - - var small = new vec2(2, 2) - var oblong = new vec2(4, 1) + surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(3, 6), new vec2(0, 4) ) ) - // describe placement - // describe dragging up (clamp at top edge) - // describe dragging down (clamp at bottom edge) - // describe dragging left (clamp at left edge) - // describe dragging right (clamp at right edge) + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with wide", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with wide from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("returns false for large image", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, large) + assert.equal(false, bounds) + }) + }) }) describe('two-level surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) - surface.add( new Rect( new vec2(3, 5), new vec2(0, 6) ) ) - - var small = new vec2(2, 2) - var oblong = new vec2(4, 1) - - // describe placement/centering - // describe dragging up (clamp at top edge) - // describe dragging down (clamp at bottom edge) - // describe dragging left (clamp at left edge) - // describe dragging right (clamp at right edge) - // describe dragging up and right (clamp at top edge, then pop into peninsula space) + surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(3, 6), new vec2(0, 6) ) ) + + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(3,0, 6,6))) + }) + it("generates proper bounds with wide", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with wide from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 6,4))) + }) + it("generates proper bounds with tall", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, tall) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(3,0, 6,6))) + }) + it("generates the same bounds with tall from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, tall) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(3,0, 6,6))) + }) + it("returns false for large image", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, large) + assert.equal(false, bounds) + }) + + }) + }) describe('door surface', function(){ var surface = new Surface () - surface.add( new Rect( new vec2(0, 3), new vec2(0, 4) ) ) - surface.add( new Rect( new vec2(3, 4), new vec2(2, 4) ) ) - surface.add( new Rect( new vec2(4, 6), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(1, 4), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(4, 5), new vec2(2, 4) ) ) + surface.add( new Rect( new vec2(5, 8), new vec2(0, 4) ) ) - var small = new vec2(2, 2) - var large = new vec2(10, 10) - var oblong = new vec2(4, 1) + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 4,4))) + }) + it("generates proper bounds from middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 8,4))) + }) + it("generates proper bounds from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(2, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(5,0, 8,4))) + }) + + it("generates proper bounds for wide from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 8,4))) + }) + it("generates proper bounds for wide from middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 8,4))) + }) + it("generates proper bounds for wide from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(2, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 8,4))) + }) + + it("returns false for tall", function(){ + assert.equal(false, surface.bounds_at_index_with_dimensions(0, tall)) + assert.equal(false, surface.bounds_at_index_with_dimensions(1, tall)) + assert.equal(false, surface.bounds_at_index_with_dimensions(2, tall)) + }) + it("returns false for large image", function(){ + assert.equal(false, surface.bounds_at_index_with_dimensions(0, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(1, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(2, large)) + }) + }) describe('#fits()', function(){ it("fits something small", function(){ @@ -114,8 +206,8 @@ describe('door surface', function(){ it("doesn't fit something large", function(){ assert.equal(false, !! surface.fits(large)) }) - it("fits something oblong", function(){ - assert.equal(true, !! surface.fits(oblong)) + it("fits something wide", function(){ + assert.equal(true, !! surface.fits(wide)) }) }) describe('#fits_scale()', function(){ @@ -125,24 +217,87 @@ describe('door surface', function(){ it("doesn't fit something small, scaled up", function(){ assert.equal(false, !! surface.fits_scale(small, 10)) }) - it("doesn't fit something oblong, scaled up", function(){ - assert.equal(false, !! surface.fits_scale(oblong, 10)) + it("doesn't fit something wide, scaled up", function(){ + assert.equal(false, !! surface.fits_scale(wide, 10)) }) }) - describe('#place()', function(){ - it("fits a small element on the top left", function(){ - var bounds = surface.place(small, new vec2(1,3)) -// console.log(bounds) +}) + +describe('double door surface', function(){ + var surface = new Surface () + surface.add( new Rect( new vec2(1, 4), new vec2(0, 4) ) ) + surface.add( new Rect( new vec2(4, 5), new vec2(2, 4) ) ) + surface.add( new Rect( new vec2(5, 8), new vec2(0, 6) ) ) + surface.add( new Rect( new vec2(8, 10), new vec2(2, 4) ) ) + surface.add( new Rect( new vec2(10, 14), new vec2(0, 4) ) ) + + describe('#bounds_at_index_with_dimensions()', function(){ + it("generates proper bounds from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,0, 4,4))) }) - it("places a small element on the right", function(){ - var bounds = surface.place(small, new vec2(4,6)) -// console.log(bounds) + it("generates proper bounds from left door", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) }) - }) + it("generates proper bounds from middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(2, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + }) + it("generates proper bounds from right door", function(){ + var bounds = surface.bounds_at_index_with_dimensions(3, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) + }) + it("generates proper bounds from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(4, small) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(10,0, 14,4))) + }) + + it("generates proper bounds for wide from left", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) + }) + it("generates proper bounds for wide from middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(1, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) + }) + it("generates proper bounds for wide from right", function(){ + var bounds = surface.bounds_at_index_with_dimensions(4, wide) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(1,2, 14,4))) + }) + + it("places tall in middle", function(){ + var bounds = surface.bounds_at_index_with_dimensions(0, tall) + assert.notEqual(false, bounds) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + + var bounds = surface.bounds_at_index_with_dimensions(1, tall) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) - describe('#clamp()', function(){ - it("", function(){ + var bounds = surface.bounds_at_index_with_dimensions(2, tall) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + + var bounds = surface.bounds_at_index_with_dimensions(3, tall) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + + var bounds = surface.bounds_at_index_with_dimensions(4, tall) + assert.equal(true, bounds.eq(new Rect(5,0, 8,6))) + }) + it("returns false for large image", function(){ + assert.equal(false, surface.bounds_at_index_with_dimensions(0, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(1, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(2, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(3, large)) + assert.equal(false, surface.bounds_at_index_with_dimensions(4, large)) }) }) -- cgit v1.2.3-70-g09d2 From 0ce017601ea3393f6ea1d0cab105737a6e460138 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 18 Aug 2014 16:31:27 -0400 Subject: etc --- public/assets/javascripts/rectangles/models/surface.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 9b82e4e..0e0926f 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -193,7 +193,13 @@ else if (bounds.width() < width) { intersection = bounds.y.intersection(face.y) if (intersection.length() < height) { - break + // 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 -- cgit v1.2.3-70-g09d2 From 559799f707a8c7fbd0d6296dfbce81ac6f9b376a Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 18 Aug 2014 18:42:41 -0400 Subject: translation tests --- .../assets/javascripts/rectangles/models/rect.js | 3 + .../javascripts/rectangles/models/surface.js | 23 +++++++- test/07-test-surface.js | 68 ++++++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 5952f6a..f23ab9e 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -95,6 +95,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) } diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 0e0926f..3f43ec2 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -100,12 +100,28 @@ 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 center_index = this.index_for_x( position.a + dimension.a/2 + delta.a, 0 ) + var new_bounds = this.bounds_at_index_with_dimensions(center_index, dimension) + + this.clamp_delta(new_bounds, dimension, position, delta) - var left_side = this.index_for_x( position.a + delta.a, 0 ) + 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.sides[left_side].clone() @@ -126,11 +142,12 @@ this.clamp_delta(bounds, dimension, position, delta) return bounds +*/ } Surface.prototype.index_for_x = function(x, min_i){ min_i = min_i || 0 - if (x < 0 || x > width) { + if (x < 0 || x > this.width) { return -1 } for (var i = min_i; i < this.faces.length; i++) { @@ -162,6 +179,8 @@ 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 right later break } else { diff --git a/test/07-test-surface.js b/test/07-test-surface.js index fa05d43..8a6d3d0 100644 --- a/test/07-test-surface.js +++ b/test/07-test-surface.js @@ -210,6 +210,7 @@ describe('door surface', function(){ assert.equal(true, !! surface.fits(wide)) }) }) + describe('#fits_scale()', function(){ it("fits something large, scaled down", function(){ assert.equal(true, !! surface.fits_scale(large, 0.1)) @@ -222,6 +223,73 @@ describe('door surface', function(){ }) }) + var position = new vec2(1, 2) + var bounds = surface.bounds_at_index_with_dimensions(0, small) + + describe('#translate()', function(){ + it("does not alter a zero delta", function(){ + var delta = new vec2(0,0) + var new_bounds = surface.translate(bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, 0 ))) + }) + it("does not alter a minimal delta", function(){ + var delta = new vec2(1, 1) + surface.translate(bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 1, 1 ))) + }) + it("clamps leftward delta", function(){ + var delta = new vec2(-10, 0) + surface.translate(bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, 0 ))) + }) + it("clamps 2px rightward delta", function(){ + var delta = new vec2(2, 0) + surface.translate(bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 1, 0 ))) + }) + it("clamps 3px rightward delta", function(){ + var delta = new vec2(3, 0) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+"") + assert.equal(true, delta.eq(new vec2( 3, 0 ))) + }) + it("clamps 4px rightward delta", function(){ + var delta = new vec2(4, 0) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+"") + assert.equal(true, delta.eq(new vec2( 4, 0 ))) + }) + it("clamps 5px rightward delta to new bounds", function(){ + var delta = new vec2(5, 0) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+"") + assert.equal(true, delta.eq(new vec2( 5, 0 ))) + }) + it("clamps 6px rightward delta", function(){ + var delta = new vec2(6, 0) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+"") + assert.equal(true, delta.eq(new vec2( 5, 0 ))) + }) + it("clamps 20px rightward delta", function(){ + var delta = new vec2(7, 0) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+"") + assert.equal(true, delta.eq(new vec2( 5, 0 ))) + }) + it("clamps upward delta", function(){ + var delta = new vec2(0, 10) + surface.translate(bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, 1 ))) + }) + it("clamps downward delta", function(){ + var delta = new vec2(0, -10) + surface.translate(bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, -1 ))) + }) + + }) + }) describe('double door surface', function(){ -- cgit v1.2.3-70-g09d2 From 24870ab6ebd31c18b3533ec96ff04ad16153c844 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 19 Aug 2014 18:19:02 -0400 Subject: bounds tester --- .../assets/javascripts/rectangles/models/rect.js | 1 + .../javascripts/rectangles/models/surface.js | 4 +- public/assets/javascripts/rectangles/util/mouse.js | 2 +- public/assets/test/surface.html | 77 ++++++++++++++++++++++ test/07-test-surface.js | 58 ++++++++++++++-- 5 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 public/assets/test/surface.html (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index f23ab9e..67852b4 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -139,6 +139,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/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 3f43ec2..f7fe890 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -113,7 +113,9 @@ return old_bounds } - var center_index = this.index_for_x( position.a + dimension.a/2 + delta.a, 0 ) + var left_index = this.index_for_x( position.a + dimension.a/2 + 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/2 + delta.a, center_index ) var new_bounds = this.bounds_at_index_with_dimensions(center_index, dimension) this.clamp_delta(new_bounds, dimension, position, delta) diff --git a/public/assets/javascripts/rectangles/util/mouse.js b/public/assets/javascripts/rectangles/util/mouse.js index 06958af..fea3376 100644 --- a/public/assets/javascripts/rectangles/util/mouse.js +++ b/public/assets/javascripts/rectangles/util/mouse.js @@ -8,7 +8,7 @@ // cursor.x.a // cursor.y.a }, - move: function(e, cursor, delta){ + move: function(e, cursor){ // delta.a (x) // delta.b (y) }, diff --git a/public/assets/test/surface.html b/public/assets/test/surface.html new file mode 100644 index 0000000..85e3a7b --- /dev/null +++ b/public/assets/test/surface.html @@ -0,0 +1,77 @@ + + + + + + + + + + diff --git a/test/07-test-surface.js b/test/07-test-surface.js index 8a6d3d0..a9feffa 100644 --- a/test/07-test-surface.js +++ b/test/07-test-surface.js @@ -223,10 +223,11 @@ describe('door surface', function(){ }) }) - var position = new vec2(1, 2) + var position = new vec2(1, 1) var bounds = surface.bounds_at_index_with_dimensions(0, small) describe('#translate()', function(){ + /* it("does not alter a zero delta", function(){ var delta = new vec2(0,0) var new_bounds = surface.translate(bounds, small, position, delta) @@ -277,16 +278,65 @@ describe('door surface', function(){ console.log(new_bounds+"") assert.equal(true, delta.eq(new vec2( 5, 0 ))) }) - it("clamps upward delta", function(){ + */ + it("clamps upward delta 0,10", function(){ var delta = new vec2(0, 10) - surface.translate(bounds, small, position, delta) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+" <<<__c:w<") + console.log(delta+"") assert.equal(true, delta.eq(new vec2( 0, 1 ))) }) + it("clamps upward delta 1,10", function(){ + var delta = new vec2(1, 10) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+" <<<__c:w<") + console.log(delta+"") + assert.equal(true, delta.eq(new vec2( 1, 1 ))) + }) + it("clamps upward delta 2,10", function(){ + var delta = new vec2(2, 10) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+" <<<__c:w<") + console.log(delta+" <<<<<<<<<") + assert.equal(true, delta.eq(new vec2( 2, 1 ))) + }) + it("clamps upward delta 3,10", function(){ + var delta = new vec2(3, 10) + var bounds2 = surface.translate(bounds, small, position, delta) + console.log(bounds2+" <<<__c:w<") + console.log(delta+"") + assert.equal(true, delta.eq(new vec2( 3, 1 ))) + }) + it("clamps upward delta 4,10", function(){ + var delta = new vec2(4, 10) + var bounds2 = surface.translate(bounds, small, position, delta) + console.log(bounds2+" <<<__c:w<") + console.log(delta+"") + assert.equal(true, delta.eq(new vec2( 4, 1 ))) + }) + it("clamps upward delta 5,10", function(){ + var delta = new vec2(5, 10) + var bounds2 = surface.translate(bounds, small, position, delta) + console.log(bounds2+" <<<__c:w<") + console.log(delta+"") + assert.equal(true, delta.eq(new vec2( 5, 1 ))) + }) + it("clamps upward delta 6,10", function(){ + var delta = new vec2(6, 10) + var bounds2 = surface.translate(bounds, small, position, delta) + console.log(bounds2+" <<<__c:w<") + console.log(delta+"") + assert.equal(true, delta.eq(new vec2( 6, 1 ))) + }) +/* it("clamps downward delta", function(){ var delta = new vec2(0, -10) - surface.translate(bounds, small, position, delta) + var new_bounds = surface.translate(bounds, small, position, delta) + console.log(new_bounds+" <<<__c:w<") + console.log(delta+"") assert.equal(true, delta.eq(new vec2( 0, -1 ))) }) +*/ }) -- cgit v1.2.3-70-g09d2 From 4f1d9a04ee6e598e93de298bd9c5081c68b9db92 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Tue, 19 Aug 2014 23:31:58 -0400 Subject: translation tests passing!! --- .../javascripts/rectangles/models/surface.js | 43 ++++++------- test/00-setup.js | 2 +- test/07-test-surface.js | 73 +++++++++++++--------- 3 files changed, 67 insertions(+), 51 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index eebd566..a700fd4 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -25,7 +25,7 @@ this.bounds.y.b = rect.y.a } this.bounds.x.b += rect.width() - this.bounds.y.b = Math.max(this.bounds.y.b, rect.height()) + this.bounds.y.b = Math.max(this.bounds.y.b, rect.y.b) this.faces.push(rect) } Surface.prototype.fits_scale = function(v, scale){ @@ -91,12 +91,14 @@ delta.a -= right_edge } - var bottom_edge = position.b + delta.b + bounds.y.a + 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 +// console.log(position.b, 0, delta.b, bounds.y.a, bottom_edge) +// console.log(position.b, dimension.b, delta.b, bounds.y.b, top_edge) if (top_edge > 0) { delta.b -= top_edge } @@ -113,38 +115,37 @@ return old_bounds } - var left_index = this.index_for_x( position.a + dimension.a/2 + 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/2 + 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_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.sides[left_side].clone() + var bounds = this.faces[left_side].clone() + var intersection - for (var i = left_side+1; i <= right_side; i++) { - if (this.faces[i].y.a > bounds.y.a) { - bounds.y.a = this.faces[i].y.a - } - if (this.faces[i].y.b < bounds.y.b) { - bounds.y.b = this.faces[i].y.b + 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.width() > dimension.a || bounds.height() > dimension.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){ diff --git a/test/00-setup.js b/test/00-setup.js index 20f9d66..e95c4ea 100644 --- a/test/00-setup.js +++ b/test/00-setup.js @@ -1 +1 @@ -Error.stackTraceLimit = 5 +Error.stackTraceLimit = 1 diff --git a/test/07-test-surface.js b/test/07-test-surface.js index 53a4b73..d060943 100644 --- a/test/07-test-surface.js +++ b/test/07-test-surface.js @@ -70,6 +70,46 @@ describe('basic surface', function(){ }) }) +describe('half surface', function(){ + var surface = new Surface () + surface.add( new Rect( new vec2(1, 6), new vec2(2, 6) ) ) + + var position = new vec2(2, 3) + + describe('#clamp_delta()', function(){ + it("does not alter a zero delta", function(){ + var delta = new vec2(0,0) + surface.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, 0 ))) + }) + it("does not alter a minimal delta", function(){ + var delta = new vec2(1,1) + surface.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 1, 1 ))) + }) + it("clamps leftward delta", function(){ + var delta = new vec2(-10, 0) + surface.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( -1, 0 ))) + }) + it("clamps rightward delta", function(){ + var delta = new vec2(10, 0) + surface.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 2, 0 ))) + }) + it("clamps upward delta", function(){ + var delta = new vec2(0, 10) + surface.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, 1 ))) + }) + it("clamps downward delta", function(){ + var delta = new vec2(0, -10) + surface.clamp_delta(surface.bounds, small, position, delta) + assert.equal(true, delta.eq(new vec2( 0, -1 ))) + }) + }) +}) + describe('double surface', function(){ var surface = new Surface () surface.add( new Rect( new vec2(1, 3), new vec2(0, 4) ) ) @@ -242,7 +282,6 @@ describe('door surface', function(){ var bounds = surface.bounds_at_index_with_dimensions(0, small) describe('#translate()', function(){ - /* it("does not alter a zero delta", function(){ var delta = new vec2(0,0) var new_bounds = surface.translate(bounds, small, position, delta) @@ -266,93 +305,68 @@ describe('door surface', function(){ it("clamps 3px rightward delta", function(){ var delta = new vec2(3, 0) var new_bounds = surface.translate(bounds, small, position, delta) - console.log(new_bounds+"") - assert.equal(true, delta.eq(new vec2( 3, 0 ))) + assert.equal(true, delta.eq(new vec2( 1, 0 ))) }) it("clamps 4px rightward delta", function(){ var delta = new vec2(4, 0) var new_bounds = surface.translate(bounds, small, position, delta) - console.log(new_bounds+"") - assert.equal(true, delta.eq(new vec2( 4, 0 ))) + assert.equal(true, delta.eq(new vec2( 1, 0 ))) }) it("clamps 5px rightward delta to new bounds", function(){ var delta = new vec2(5, 0) var new_bounds = surface.translate(bounds, small, position, delta) - console.log(new_bounds+"") assert.equal(true, delta.eq(new vec2( 5, 0 ))) }) it("clamps 6px rightward delta", function(){ var delta = new vec2(6, 0) var new_bounds = surface.translate(bounds, small, position, delta) - console.log(new_bounds+"") assert.equal(true, delta.eq(new vec2( 5, 0 ))) }) it("clamps 20px rightward delta", function(){ var delta = new vec2(7, 0) var new_bounds = surface.translate(bounds, small, position, delta) - console.log(new_bounds+"") assert.equal(true, delta.eq(new vec2( 5, 0 ))) }) - */ it("clamps upward delta 0,10", function(){ var delta = new vec2(0, 10) var new_bounds = surface.translate(bounds, small, position, delta) - console.log(new_bounds+" <<<__c:w<") - console.log(delta+"") assert.equal(true, delta.eq(new vec2( 0, 1 ))) }) it("clamps upward delta 1,10", function(){ var delta = new vec2(1, 10) var new_bounds = surface.translate(bounds, small, position, delta) - console.log(new_bounds+" <<<__c:w<") - console.log(delta+"") assert.equal(true, delta.eq(new vec2( 1, 1 ))) }) it("clamps upward delta 2,10", function(){ var delta = new vec2(2, 10) var new_bounds = surface.translate(bounds, small, position, delta) - console.log(new_bounds+" <<<__c:w<") - console.log(delta+" <<<<<<<<<") assert.equal(true, delta.eq(new vec2( 2, 1 ))) }) it("clamps upward delta 3,10", function(){ var delta = new vec2(3, 10) var bounds2 = surface.translate(bounds, small, position, delta) - console.log(bounds2+" <<<__c:w<") - console.log(delta+"") assert.equal(true, delta.eq(new vec2( 3, 1 ))) }) it("clamps upward delta 4,10", function(){ var delta = new vec2(4, 10) var bounds2 = surface.translate(bounds, small, position, delta) - console.log(bounds2+" <<<__c:w<") - console.log(delta+"") assert.equal(true, delta.eq(new vec2( 4, 1 ))) }) it("clamps upward delta 5,10", function(){ var delta = new vec2(5, 10) var bounds2 = surface.translate(bounds, small, position, delta) - console.log(bounds2+" <<<__c:w<") - console.log(delta+"") assert.equal(true, delta.eq(new vec2( 5, 1 ))) }) it("clamps upward delta 6,10", function(){ var delta = new vec2(6, 10) var bounds2 = surface.translate(bounds, small, position, delta) - console.log(bounds2+" <<<__c:w<") - console.log(delta+"") - assert.equal(true, delta.eq(new vec2( 6, 1 ))) + assert.equal(true, delta.eq(new vec2( 5, 1 ))) }) -/* it("clamps downward delta", function(){ var delta = new vec2(0, -10) var new_bounds = surface.translate(bounds, small, position, delta) - console.log(new_bounds+" <<<__c:w<") - console.log(delta+"") assert.equal(true, delta.eq(new vec2( 0, -1 ))) }) -*/ - }) }) @@ -436,3 +450,4 @@ describe('double door surface', function(){ }) + -- cgit v1.2.3-70-g09d2 From a48beac4e7e93f78ef7229b0b1e4776dc04cac92 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 20 Aug 2014 18:55:11 -0400 Subject: converting between position and mx coordinates --- .../rectangles/engine/scenery/_scenery.js | 4 +- .../rectangles/engine/scenery/types/_object.js | 16 ++- .../assets/javascripts/rectangles/models/wall.js | 122 +++++++++++++-------- 3 files changed, 92 insertions(+), 50 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js index b4a38f8..5d36b4f 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js +++ b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js @@ -29,11 +29,11 @@ var Scenery = new function(){ return scene_media } - base.addNextToWall = function(wall, mx){ + base.addNextToWall = function(wall, index){ base.add({ wall: wall, media: base.nextMedia, - mx: mx + index: index || 0, }) base.nextMedia = null return media diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js index 6bd5863..70d1d26 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js @@ -10,9 +10,17 @@ Scenery.types.base = Fiber.extend(function(base){ this.media = opt.media this.dimensions = new vec2(this.media.width, this.media.height) this.scale = this.media.scale + + this.scaleDimensions = this.dimensions.clone().mul(this.media.scale) + this.position = new vec2(0,0) + if (opt.data && opt.data.position) { + // position is the coordinate of one of the corners with respect to a + // wall surface + // subtract x/z offset of wall + } if (opt.wall) { - this.set_wall(opt.wall, opt.mx) + this.set_wall(opt.wall, index) } }, @@ -60,7 +68,7 @@ Scenery.types.base = Fiber.extend(function(base){ } }, - set_wall: function(wall, mx){ + set_wall: function(wall, index){ this.wall = wall || this.wall // this.bounds = this.wall.bounds_for(this.media, this.scale) // this.center = this.wall.center() @@ -83,8 +91,8 @@ Scenery.types.base = Fiber.extend(function(base){ serialize: function(){ var data = { id: this.id, - wall_id: this.wall.id, - side: this.wall.side, + wall_id: this.wall && this.wall.id, + side: this.wall && this.wall.side, dimensions: this.dimensions.serialize(), position: app.position(this.mx), media: this.media, diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index 460963b..3afe49a 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -39,7 +39,7 @@ var base = this base.$walls = $( this.mx.map(function(mx){ return mx.el }) ) - this.mx.forEach(function(mx){ + this.mx.forEach(function(mx, index){ $(mx.el).bind({ mouseover: function(){ }, @@ -52,7 +52,7 @@ // base.randomize_colors() // console.log(sidesToString(base.side)) if (Scenery.nextMedia) { - var scenery = Scenery.addNextToWall(base) + var scenery = Scenery.addNextToWall(base, index) UndoStack.push({ type: 'create-scenery', @@ -74,6 +74,64 @@ }) 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 & FRONT_BACK + // var useA = side & (FRONT | LEFT) + // mx.rect[useX ? 'y': 'x'][ useA ? 'a': 'b'] + // surface: an ordered set of contiguous wall regions + + Wall.prototype.positionToMx = function(position, dimension) { + switch (this.side) { + case FRONT: + x = this.vec.a + position.a + dimension.a / 2 + z = this.edge + painting_distance_from_wall + break + case BACK: + x = this.vec.a + position.a + dimension.a / 2 + z = this.edge - painting_distance_from_wall + break + case LEFT: + x = this.edge + painting_distance_from_wall + z = this.vec.a + position.a + dimension.a / 2 + break + case RIGHT: + x = this.edge - painting_distance_from_wall + z = this.vec.a + position.a + dimension.a / 2 + break + } + return { + x: x, + y: position.b + dimension.b / 2 + z: z, + } + } + Wall.prototype.mxToPosition = function(mx) { + var position = new vec2(0,0) + switch (this.side) { + case FRONT: + position.a = mx.x - mx.width / 2 - this.vec.a + position.b = mx.y - mx.height / 2 + break + case BACK: + position.a = mx.x - mx.width / 2 - this.vec.a + position.b = mx.y - mx.height / 2 + break + case LEFT: + position.a = mx.z - mx.width / 2 - this.vec.a + position.b = mx.y - mx.height / 2 + break + case RIGHT: + position.a = mx.z - mx.width / 2 - this.vec.a + position.b = mx.y - mx.height / 2 + break + } + return position + } Wall.prototype.bounds_for = function(img, scale) { scale = scale || 1 @@ -85,12 +143,6 @@ 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 @@ -103,37 +155,25 @@ 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) } @@ -156,8 +196,6 @@ } 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 @@ -169,37 +207,33 @@ 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 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.95)" - ctx.fillRect(0, 0, canvas.width, canvas.height) - ctx.fillStyle = "rgba(0,0,0,1.0)" + mx.el.style.backgroundColor = backgroundColor // all walls get bottom lines - ctx.fillRect(0, 0, canvas.width, 1) + mx.el.style.borderBottom = "1px solid " + borderColor // all walls get top lines - ctx.fillRect(0, canvas.height-1, canvas.width, 1) + 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.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 + ")" }) } @@ -215,7 +249,7 @@ Wall.prototype.randomize_colors = function(){ var color = window.grayColors[ this.side | this.half_side ] - this.color(color) + // this.color(color) } Wall.prototype.stroke_colors = function(){ -- cgit v1.2.3-70-g09d2 From 978634c1c323240f36548468703a9512710c60e7 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 22 Aug 2014 12:11:40 -0400 Subject: wall placement --- .../rectangles/engine/scenery/_scenery.js | 14 +++--- .../javascripts/rectangles/engine/scenery/move.js | 14 +++--- .../rectangles/engine/scenery/types/_object.js | 58 +++++++++++----------- .../rectangles/engine/scenery/types/image.js | 5 +- .../rectangles/engine/scenery/types/video.js | 4 +- .../assets/javascripts/rectangles/models/wall.js | 40 +++++++++------ public/assets/javascripts/rectangles/util/mouse.js | 8 +-- 7 files changed, 76 insertions(+), 67 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js index 5d36b4f..58592d4 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js +++ b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js @@ -29,14 +29,14 @@ var Scenery = new function(){ return scene_media } - base.addNextToWall = function(wall, index){ - base.add({ - wall: wall, - media: base.nextMedia, - index: index || 0, - }) + base.addNextToWall = function(opt){ + opt.media = base.nextMedia + opt.index = opt.index || 0 + var scene_media = base.add(opt) + + // test if scenery was placed here base.nextMedia = null - return media + return scene_media } base.find = function(id){ diff --git a/public/assets/javascripts/rectangles/engine/scenery/move.js b/public/assets/javascripts/rectangles/engine/scenery/move.js index f2d37d8..47e155b 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/move.js +++ b/public/assets/javascripts/rectangles/engine/scenery/move.js @@ -91,21 +91,21 @@ Scenery.move = function(base){ oldState = null } - function switch_wall (e, new_wall, cursor){ + function switch_wall (e, target, cursor){ if (! dragging) return - if (new_wall.id == base.wall.id) return - if (! new_wall.fits(base.media, base.scale)) return + if (target.wall.id == base.wall.id) return + if (! target.wall.fits(base.media, base.scale)) return var old_wall_side = base.wall.side - var wall_group = old_wall_side | new_wall.side + var wall_group = old_wall_side | target.wall.side - base.set_wall(new_wall) + base.set_wall(target) bounds = base.bounds x = base.center.a z = base.center.b - if (old_wall_side !== new_wall.side && wall_group !== FRONT_BACK && wall_group !== LEFT_RIGHT) { + if (old_wall_side !== target.wall.side && wall_group !== FRONT_BACK && wall_group !== LEFT_RIGHT) { switch (old_wall_side) { case FRONT: z = bounds.x.a @@ -127,7 +127,7 @@ Scenery.move = function(base){ base.mx.move({ x: x, z: z, - rotationY: wall_rotation[ new_wall.side ] + rotationY: wall_rotation[ target.wall.side ] }) if (editor.permissions.resize) { diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js index 70d1d26..7b716f6 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js @@ -8,20 +8,36 @@ Scenery.types.base = Fiber.extend(function(base){ this.id = opt.id || Scenery.uid("scenery") this.move = new Scenery.move (this) this.media = opt.media - this.dimensions = new vec2(this.media.width, this.media.height) - this.scale = this.media.scale + this.naturalDimensions = new vec2(this.media.width, this.media.height) - this.scaleDimensions = this.dimensions.clone().mul(this.media.scale) + this.set_scale( opt.scale || this.media.scale || 1.0 ) this.position = new vec2(0,0) - if (opt.data && opt.data.position) { - // position is the coordinate of one of the corners with respect to a - // wall surface - // subtract x/z offset of wall - } + }, - if (opt.wall) { - this.set_wall(opt.wall, index) + set_wall: function(opt){ + this.wall = opt.wall || this.wall + this.bounds = this.wall.surface.bounds_at_index_with_dimensions(opt.index || 0, this.dimensions) + }, + + set_scale: function(scale){ + this.scale = scale || 1.0 + if (this.mx) { + this.mx.scale = this.mx.ops.scale = this.scale } + this.dimensions = this.naturalDimensions.clone().mul(this.scale) + }, + + recenter: function () { + var center = this.bounds.center() + center.a -= this.dimensions.a / 2 + center.b -= this.dimensions.b / 2 + console.log(center+"") + var mx_position = this.wall.positionToMx( center, this.dimensions ) + console.log(mx_position) + console.log(this.wall.surface.faces.join("\n")) + // also supply scale? + this.mx.move(mx_position) + this.position.assign(center) }, bind: function(){ @@ -49,6 +65,7 @@ Scenery.types.base = Fiber.extend(function(base){ this.move = null this.media = null this.dimensions = null + this.naturalDimensions = null this.wall = null this.bounds = null this.center = null @@ -68,26 +85,6 @@ Scenery.types.base = Fiber.extend(function(base){ } }, - set_wall: function(wall, index){ - this.wall = wall || this.wall - // this.bounds = this.wall.bounds_for(this.media, this.scale) - // this.center = this.wall.center() - }, - - set_scale: function(scale){ - this.scale = this.mx.scale = this.mx.ops.scale = scale || 1.0 - }, - - recenter: function(){ - this.mx.move({ - x: this.center.a, - y: Rooms.list[this.wall.room].height/2 - 20, - z: this.center.b, - scale: this.scale, - rotationY: wall_rotation[ this.wall.side ], - }) - }, - serialize: function(){ var data = { id: this.id, @@ -95,6 +92,7 @@ Scenery.types.base = Fiber.extend(function(base){ side: this.wall && this.wall.side, dimensions: this.dimensions.serialize(), position: app.position(this.mx), + scale: this.scale, media: this.media, } return data diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/image.js b/public/assets/javascripts/rectangles/engine/scenery/types/image.js index 576242e..1582e0f 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/image.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/image.js @@ -4,17 +4,18 @@ Scenery.types.image = Scenery.types.base.extend(function(base){ var exports = { init: function(opt){ + opt.scale = opt.scale || 300 / max(300, opt.media.width) + base.init.call(this, opt) - this.scale = 300 / max(300, this.media.width) this.build() this.bind() - this.set_wall() if (opt.data) { this.deserialize(opt.data) } else { + this.set_wall(opt) this.recenter() } }, diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/video.js b/public/assets/javascripts/rectangles/engine/scenery/types/video.js index 0bd5c06..e8bc7f7 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/video.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/video.js @@ -4,12 +4,12 @@ Scenery.types.video = Scenery.types.base.extend(function(base){ var exports = { init: function(opt){ + opt.scale = opt.scale || 300 / max(300, opt.media.width) + base.init.call(this, opt) - this.scale = this.media.scale = 300 / max(300, this.media.width) this.build() this.bind() - this.set_wall() if (opt.data) { this.deserialize(opt.data) diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index 3afe49a..a8bcbd8 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -44,7 +44,10 @@ mouseover: function(){ }, mouseenter: function(e){ - Scenery.mouse.mouseenter(e, base, mx) + Scenery.mouse.mouseenter(e, { + wall: base, + index: index, + }) }, mousemove: function(e){ }, @@ -52,7 +55,13 @@ // base.randomize_colors() // console.log(sidesToString(base.side)) if (Scenery.nextMedia) { - var scenery = Scenery.addNextToWall(base, index) + var scenery = Scenery.addNextToWall({ + wall: base, + index: index + }) + + // scenery was not placed + if (! scenery) return UndoStack.push({ type: 'create-scenery', @@ -80,53 +89,55 @@ // 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 & FRONT_BACK + // var useX = side & LEFT_RIGHT // var useA = side & (FRONT | LEFT) - // mx.rect[useX ? 'y': 'x'][ useA ? 'a': 'b'] - // surface: an ordered set of contiguous wall regions + // 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 = this.vec.a + position.a + dimension.a / 2 + x = position.a + dimension.a / 2 z = this.edge + painting_distance_from_wall break case BACK: - x = this.vec.a + position.a + dimension.a / 2 + x = position.a + dimension.a / 2 z = this.edge - painting_distance_from_wall break case LEFT: x = this.edge + painting_distance_from_wall - z = this.vec.a + position.a + dimension.a / 2 + z = position.a + dimension.a / 2 break case RIGHT: x = this.edge - painting_distance_from_wall - z = this.vec.a + position.a + dimension.a / 2 + z = position.a + dimension.a / 2 break } return { x: x, - y: position.b + dimension.b / 2 + y: position.b + dimension.b / 2, z: z, + rotationY: wall_rotation[ this.side ], } } Wall.prototype.mxToPosition = function(mx) { var position = new vec2(0,0) switch (this.side) { case FRONT: - position.a = mx.x - mx.width / 2 - this.vec.a + position.a = mx.x - mx.width / 2 position.b = mx.y - mx.height / 2 break case BACK: - position.a = mx.x - mx.width / 2 - this.vec.a + position.a = mx.x - mx.width / 2 position.b = mx.y - mx.height / 2 break case LEFT: - position.a = mx.z - mx.width / 2 - this.vec.a + position.a = mx.z - mx.width / 2 position.b = mx.y - mx.height / 2 break case RIGHT: - position.a = mx.z - mx.width / 2 - this.vec.a + position.a = mx.z - mx.width / 2 position.b = mx.y - mx.height / 2 break } @@ -200,7 +211,6 @@ var shouldFlip = this.side & (LEFT | BACK) var mx = this.mx - // console.log( sidesToString(this.side), mx.length ) if (! shouldFlip) { mx = mx.reverse() } diff --git a/public/assets/javascripts/rectangles/util/mouse.js b/public/assets/javascripts/rectangles/util/mouse.js index fea3376..34d3f5e 100644 --- a/public/assets/javascripts/rectangles/util/mouse.js +++ b/public/assets/javascripts/rectangles/util/mouse.js @@ -138,15 +138,15 @@ function mouse (opt) { base.tube("move", e, base.cursor) } } - base.mouseenter = function(e, el){ + base.mouseenter = function(e, target, index){ if (! base.down) return if (opt.use_offset && ! offset) return - base.tube("enter", e, el, base.cursor) + base.tube("enter", e, target, base.cursor) } - base.mouseleave = function(e, el){ + base.mouseleave = function(e, target){ if (! base.down) return if (opt.use_offset && ! offset) return - base.tube("leave", e, el, base.cursor) + base.tube("leave", e, target, base.cursor) } base.mouseup = function(e){ var pos, new_cursor -- cgit v1.2.3-70-g09d2 From 4ec4b0e42c06b63962658ae8b886ee9466d09a21 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 22 Aug 2014 12:54:27 -0400 Subject: reject image if it doesnt fit --- .../assets/javascripts/mx/primitives/mx.image.js | 3 ++- .../rectangles/engine/scenery/_scenery.js | 14 +++++++---- .../rectangles/engine/scenery/types/_object.js | 5 +--- .../rectangles/engine/scenery/types/image.js | 11 ++++++++- .../javascripts/rectangles/models/surface.js | 3 --- .../assets/javascripts/rectangles/models/wall.js | 25 +++++++++----------- public/assets/javascripts/ui/editor/MediaViewer.js | 27 ++++++++++++++-------- 7 files changed, 51 insertions(+), 37 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/mx/primitives/mx.image.js b/public/assets/javascripts/mx/primitives/mx.image.js index a640620..575e9c0 100644 --- a/public/assets/javascripts/mx/primitives/mx.image.js +++ b/public/assets/javascripts/mx/primitives/mx.image.js @@ -10,7 +10,7 @@ MX.Image = MX.Object3D.extend({ this.z = ops.z || 0 this.scale = ops.scale || 1 this.backface = ops.backface || false - + ops.className && this.el.classList.add(ops.className) this.backface && this.el.classList.add("backface-visible") this.el.classList.add("image") @@ -27,6 +27,7 @@ MX.Image = MX.Object3D.extend({ var image = new Image() image.onload = function(){ + if (! layer.ops) return layer.scale = layer.ops.scale || 1 layer.width = layer.ops.width || image.naturalWidth layer.height = layer.ops.height || image.naturalHeight diff --git a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js index 58592d4..119391d 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js +++ b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js @@ -35,8 +35,14 @@ var Scenery = new function(){ var scene_media = base.add(opt) // test if scenery was placed here - base.nextMedia = null - return scene_media + if (! scene_media.bounds) { + base.remove( scene_media.id ) + return null + } + else { + base.nextMedia = null + return scene_media + } } base.find = function(id){ @@ -44,9 +50,9 @@ var Scenery = new function(){ } base.remove = function(id){ - var media = base.list[id] + var scene_media = base.list[id] delete base.list[id] - media && media.destroy() + scene_media && scene_media.destroy() } base.uid = new UidGenerator(base.list) diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js index 7b716f6..effee2a 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js @@ -28,14 +28,11 @@ Scenery.types.base = Fiber.extend(function(base){ }, recenter: function () { + if (! this.bounds) return var center = this.bounds.center() center.a -= this.dimensions.a / 2 center.b -= this.dimensions.b / 2 - console.log(center+"") var mx_position = this.wall.positionToMx( center, this.dimensions ) - console.log(mx_position) - console.log(this.wall.surface.faces.join("\n")) - // also supply scale? this.mx.move(mx_position) this.position.assign(center) }, diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/image.js b/public/assets/javascripts/rectangles/engine/scenery/types/image.js index 1582e0f..3bee827 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/image.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/image.js @@ -12,11 +12,20 @@ Scenery.types.image = Scenery.types.base.extend(function(base){ this.bind() if (opt.data) { + console.log(opt.wall) + // console.log(opt.data.position) + if (opt.wall) { + var position = opt.wall.mxToPosition(opt.data.position) + console.log(position.a) + opt.index = opt.wall.surface.index_for_x( position.a, 0 ) + console.log(opt.index) + } + this.set_wall(opt) this.deserialize(opt.data) } else { this.set_wall(opt) - this.recenter() + this.bounds && this.recenter() } }, diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index a700fd4..9c45eaf 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -150,9 +150,6 @@ Surface.prototype.index_for_x = function(x, min_i){ min_i = min_i || 0 - if (x < 0 || x > this.width) { - return -1 - } for (var i = min_i; i < this.faces.length; i++) { if (this.faces[i].x.contains(x)) { return i diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index a8bcbd8..f6742ad 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -51,9 +51,7 @@ }, mousemove: function(e){ }, - mousedown: function(){ - // base.randomize_colors() - // console.log(sidesToString(base.side)) + mousedown: function(e){ if (Scenery.nextMedia) { var scenery = Scenery.addNextToWall({ wall: base, @@ -61,7 +59,10 @@ }) // scenery was not placed - if (! scenery) return + if (! scenery) { + e.stopPropagation() + return + } UndoStack.push({ type: 'create-scenery', @@ -125,22 +126,18 @@ var position = new vec2(0,0) switch (this.side) { case FRONT: - position.a = mx.x - mx.width / 2 - position.b = mx.y - mx.height / 2 - break case BACK: - position.a = mx.x - mx.width / 2 - position.b = mx.y - mx.height / 2 + position.a = mx.x + position.b = mx.y break case LEFT: - position.a = mx.z - mx.width / 2 - position.b = mx.y - mx.height / 2 - break case RIGHT: - position.a = mx.z - mx.width / 2 - position.b = mx.y - mx.height / 2 + position.a = mx.z + position.b = mx.y break } +// if (mx.width) { position.a -= mx.width / 2 } +// if (mx.height) { position.b -= mx.height / 2 } return position } diff --git a/public/assets/javascripts/ui/editor/MediaViewer.js b/public/assets/javascripts/ui/editor/MediaViewer.js index 5540023..40bfe80 100644 --- a/public/assets/javascripts/ui/editor/MediaViewer.js +++ b/public/assets/javascripts/ui/editor/MediaViewer.js @@ -133,7 +133,9 @@ var MediaViewer = ModalView.extend({ var $floatingImg = $('.floatingImg'); Scenery.nextMedia = media - console.log(media.type) + +// console.log(media.type) + switch (media.type) { case "video": $floatingImg.attr('src', '/assets/img/playbutton.png') @@ -150,18 +152,23 @@ var MediaViewer = ModalView.extend({ function _followCursor(e) { $floatingImg.parent().css({ top: (e.pageY - (height / 2)) + 'px', - left: (e.pageX - (width / 2)) + 'px' + left: (e.pageX - (width / 2)) + 'px', }); } - $(window).on('mousemove', _followCursor); - $(window).one('click', function () { + function _hideCursor (e) { + if (Scenery.nextMedia) { + return + } var $floatingImg = $('.floatingImg') - $floatingImg.attr('src', ''); - $(window).off('mousemove', _followCursor); - $floatingImg.parent().removeClass('edit'); - }); - $ants.addClass('edit'); - _followCursor(e); + $floatingImg.attr('src', '') + $(window).off('mousemove', _followCursor) + $(window).off('mousedown', _hideCursor) + $floatingImg.parent().removeClass('edit') + } + $(window).on('mousemove', _followCursor) + $(window).on('mousedown', _hideCursor) + $ants.addClass('edit') + _followCursor(e) }, }) -- cgit v1.2.3-70-g09d2 From 41349226e763751311d3b0135086505a77f5472a Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 22 Aug 2014 13:27:06 -0400 Subject: drag and clamp --- .../rectangles/engine/scenery/_scenery.js | 6 ++++++ .../javascripts/rectangles/engine/scenery/move.js | 24 +++++++++++++++++----- .../rectangles/engine/scenery/types/image.js | 6 +++--- .../javascripts/rectangles/models/surface.js | 2 -- .../assets/javascripts/rectangles/models/wall.js | 6 +++++- 5 files changed, 33 insertions(+), 11 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js index 119391d..1e2e83a 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js +++ b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js @@ -54,6 +54,12 @@ var Scenery = new function(){ delete base.list[id] scene_media && scene_media.destroy() } + + base.removeAll = function(){ + base.forEach(function(scene_media){ + base.remove(scene_media.id) + }) + } base.uid = new UidGenerator(base.list) diff --git a/public/assets/javascripts/rectangles/engine/scenery/move.js b/public/assets/javascripts/rectangles/engine/scenery/move.js index 47e155b..991d1d4 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/move.js +++ b/public/assets/javascripts/rectangles/engine/scenery/move.js @@ -1,7 +1,7 @@ Scenery.move = function(base){ - var x, y, z, bounds + var x, y, z, position, dimension, bounds var dragging = false var oldState @@ -48,7 +48,11 @@ Scenery.move = function(base){ x = base.mx.x y = base.mx.y z = base.mx.z + bounds = base.bounds + dimension = base.dimensions + position = base.wall.mxToPosition( base.mx, dimension ) + oldState = base.serialize() document.body.classList.add("dragging") } @@ -56,18 +60,28 @@ Scenery.move = function(base){ function drag (e, cursor){ if (! dragging) return - base.mx.y = bounds.y.clamp( y - cursor.y.magnitude()*cursor_amp ) + var flipX = base.wall.side & (FRONT | RIGHT) + + var delta = cursor.delta() + delta.mul( cursor_amp ) // this should be proportional to your distance from the wall + if (flipX) { delta.a *= -1 } + delta.b *= -1 + + var new_bounds = base.wall.surface.translate( bounds, dimension, position, delta ) + if (new_bounds) bounds = new_bounds + if (flipX) { delta.a *= -1 } + + base.mx.y = position.b + delta.b + dimension.b / 2 switch (base.wall.side) { case FRONT: case BACK: - base.mx.x = bounds.x.clamp( x + cos(wall_rotation[base.wall.side]) * cursor.x.magnitude()*cursor_amp ) + base.mx.x = position.a + delta.a * cos(wall_rotation[base.wall.side]) + dimension.a / 2 break case LEFT: case RIGHT: - base.mx.z = bounds.x.clamp( z + sin(wall_rotation[base.wall.side]) * cursor.x.magnitude()*cursor_amp ) + base.mx.z = position.a + delta.a * sin(wall_rotation[base.wall.side]) + dimension.a / 2 break } - if (editor.permissions.resize) { Scenery.resize.move_dots() } diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/image.js b/public/assets/javascripts/rectangles/engine/scenery/types/image.js index 3bee827..cab6a94 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/image.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/image.js @@ -12,13 +12,13 @@ Scenery.types.image = Scenery.types.base.extend(function(base){ this.bind() if (opt.data) { - console.log(opt.wall) + // console.log(opt.wall) // console.log(opt.data.position) if (opt.wall) { var position = opt.wall.mxToPosition(opt.data.position) - console.log(position.a) + // console.log(position.a) opt.index = opt.wall.surface.index_for_x( position.a, 0 ) - console.log(opt.index) + // console.log(opt.index) } this.set_wall(opt) this.deserialize(opt.data) diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 9c45eaf..5b411c0 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -97,8 +97,6 @@ } var top_edge = position.b + dimension.b + delta.b - bounds.y.b -// console.log(position.b, 0, delta.b, bounds.y.a, bottom_edge) -// console.log(position.b, dimension.b, delta.b, bounds.y.b, top_edge) if (top_edge > 0) { delta.b -= top_edge } diff --git a/public/assets/javascripts/rectangles/models/wall.js b/public/assets/javascripts/rectangles/models/wall.js index f6742ad..8723c3c 100644 --- a/public/assets/javascripts/rectangles/models/wall.js +++ b/public/assets/javascripts/rectangles/models/wall.js @@ -122,7 +122,7 @@ rotationY: wall_rotation[ this.side ], } } - Wall.prototype.mxToPosition = function(mx) { + Wall.prototype.mxToPosition = function(mx, dimension) { var position = new vec2(0,0) switch (this.side) { case FRONT: @@ -136,6 +136,10 @@ 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 -- cgit v1.2.3-70-g09d2 From 335232556d74ec37774b504ad8c8af29c34788bb Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 22 Aug 2014 13:57:23 -0400 Subject: switch wall --- .../javascripts/rectangles/engine/scenery/move.js | 41 +++++++++++----------- .../rectangles/engine/scenery/types/image.js | 4 --- .../javascripts/rectangles/models/surface.js | 3 +- 3 files changed, 22 insertions(+), 26 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/scenery/move.js b/public/assets/javascripts/rectangles/engine/scenery/move.js index 991d1d4..93bccb0 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/move.js +++ b/public/assets/javascripts/rectangles/engine/scenery/move.js @@ -63,7 +63,7 @@ Scenery.move = function(base){ var flipX = base.wall.side & (FRONT | RIGHT) var delta = cursor.delta() - delta.mul( cursor_amp ) // this should be proportional to your distance from the wall + delta.mul( cursor_amp ) // TODO: this should be proportional to your distance from the wall if (flipX) { delta.a *= -1 } delta.b *= -1 @@ -108,46 +108,47 @@ Scenery.move = function(base){ function switch_wall (e, target, cursor){ if (! dragging) return if (target.wall.id == base.wall.id) return - if (! target.wall.fits(base.media, base.scale)) return - + var old_wall_side = base.wall.side var wall_group = old_wall_side | target.wall.side + var new_bounds = target.wall.surface.bounds_at_index_with_dimensions(target.index || 0, dimension) - base.set_wall(target) + if (! new_bounds) return + + base.wall = target.wall + base.bounds = bounds = new_bounds + + position.a = bounds.x.midpoint() - dimension.a / 2 - bounds = base.bounds - x = base.center.a - z = base.center.b - if (old_wall_side !== target.wall.side && wall_group !== FRONT_BACK && wall_group !== LEFT_RIGHT) { switch (old_wall_side) { case FRONT: - z = bounds.x.a + position.a = bounds.x.a + dimension.a / 2 break case BACK: - z = bounds.x.b + position.a = bounds.x.b - dimension.a / 2 break case LEFT: - x = bounds.x.a + position.a = bounds.x.a + dimension.a / 2 break case RIGHT: - x = bounds.x.b + position.a = bounds.x.b - dimension.a / 2 break } } - cursor.x.a = cursor.x.b - - base.mx.move({ - x: x, - z: z, - rotationY: wall_rotation[ target.wall.side ] - }) + var mx_delta = base.wall.positionToMx( position, dimension ) + base.mx.move(mx_delta) if (editor.permissions.resize) { Scenery.resize.rotate_dots() - Scenery.resize.move_dots() +// Scenery.resize.move_dots() } + + cursor.x.a = cursor.x.b + //var delta = cursor.delta() + drag(e, cursor) + } return this diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/image.js b/public/assets/javascripts/rectangles/engine/scenery/types/image.js index cab6a94..2b50cbf 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/image.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/image.js @@ -12,13 +12,9 @@ Scenery.types.image = Scenery.types.base.extend(function(base){ this.bind() if (opt.data) { - // console.log(opt.wall) - // console.log(opt.data.position) if (opt.wall) { var position = opt.wall.mxToPosition(opt.data.position) - // console.log(position.a) opt.index = opt.wall.surface.index_for_x( position.a, 0 ) - // console.log(opt.index) } this.set_wall(opt) this.deserialize(opt.data) diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 5b411c0..53977c8 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -25,6 +25,7 @@ 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) } @@ -90,7 +91,6 @@ 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 @@ -105,7 +105,6 @@ } Surface.prototype.translate = function (old_bounds, dimension, position, delta) { - this.clamp_delta( this.bounds, dimension, position, delta ) var new_delta = delta.clone() -- cgit v1.2.3-70-g09d2 From bc3dc1faec775253eee016a786436d13ed18f8f4 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 22 Aug 2014 14:38:11 -0400 Subject: resizing --- .../javascripts/rectangles/engine/scenery/resize.js | 17 ++++++++++------- .../rectangles/engine/scenery/types/image.js | 3 ++- public/assets/javascripts/rectangles/models/rect.js | 1 + public/assets/javascripts/rectangles/models/vec2.js | 1 + 4 files changed, 14 insertions(+), 8 deletions(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/scenery/resize.js b/public/assets/javascripts/rectangles/engine/scenery/resize.js index 6b2e52c..d9cce18 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/resize.js +++ b/public/assets/javascripts/rectangles/engine/scenery/resize.js @@ -6,7 +6,7 @@ Scenery.resize = new function(){ var obj var x, y, z, bounds var dragging = false - var dimensions, position, scale + var naturalDimension, dimension, position, scale var oldState var dots = [], dot, selected_dot @@ -148,8 +148,9 @@ Scenery.resize = new function(){ selected_dot = selection[0] dragging = true - - dimensions = obj.dimensions + + naturalDimension = obj.naturalDimensions + dimension = obj.dimensions position = new vec3(obj.mx.x, obj.mx.y, obj.mx.z) scale = obj.mx.scale oldState = obj.serialize() @@ -165,7 +166,7 @@ Scenery.resize = new function(){ var width = cursor.x.magnitude() var height = cursor.y.magnitude() var mag = cursor.magnitude() - var old_width = dimensions.a * scale + var old_width = dimension.a * scale if (abs(width) > abs(height)) { mag = x_sign * mag * sign(width) @@ -173,8 +174,10 @@ Scenery.resize = new function(){ else { mag = y_sign * mag * sign(height) } - - obj.mx.scale = scale * (old_width + mag) / old_width + + obj.mx.scale = ( dimension.a + mag ) / naturalDimension.a // dimension.a // scale * (old_width + mag) / old_width + +// console.log(scale, obj.mx.scale, dimension.a + mag, naturalDimension.a) if (selected_dot.side & LEFT_RIGHT) { obj.mx.x = position.a + cos(rotationY) * mag/2 * (x_sign) @@ -192,7 +195,7 @@ Scenery.resize = new function(){ selected_dot = null if (! editor.permissions.resize) { return } obj.scale = obj.mx.ops.scale = obj.mx.scale - obj.set_wall() + obj.dimensions.assign(obj.naturalDimensions).mul(obj.scale) UndoStack.push({ type: 'update-scenery', diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/image.js b/public/assets/javascripts/rectangles/engine/scenery/types/image.js index 2b50cbf..d2fa3ab 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/image.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/image.js @@ -4,7 +4,8 @@ Scenery.types.image = Scenery.types.base.extend(function(base){ var exports = { init: function(opt){ - opt.scale = opt.scale || 300 / max(300, opt.media.width) + + opt.scale = opt.scale || (opt.data && opt.data.scale) || 300 / max(300, opt.media.width) base.init.call(this, opt) diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 67852b4..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()) diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index a36c614..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) { -- cgit v1.2.3-70-g09d2 From 4ef340497ef24bb2ecacb2c9c4106c24515c874f Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 22 Aug 2014 17:33:32 -0400 Subject: null fix --- public/assets/javascripts/rectangles/engine/scenery/_scenery.js | 2 +- public/assets/javascripts/rectangles/engine/scenery/types/_object.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'public/assets/javascripts/rectangles') diff --git a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js index 1e2e83a..2fd6122 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/_scenery.js +++ b/public/assets/javascripts/rectangles/engine/scenery/_scenery.js @@ -84,7 +84,7 @@ var Scenery = new function(){ base.deserialize = function(scenery_data){ scenery_data.forEach(function(data){ - var wall = Rooms.walls[data.wall_id] + var wall = Rooms.walls[data.wall_id] || Rooms.walls[0] var scene_media = base.add({ data: data, wall: wall, diff --git a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js index effee2a..2dbae48 100644 --- a/public/assets/javascripts/rectangles/engine/scenery/types/_object.js +++ b/public/assets/javascripts/rectangles/engine/scenery/types/_object.js @@ -16,6 +16,7 @@ Scenery.types.base = Fiber.extend(function(base){ set_wall: function(opt){ this.wall = opt.wall || this.wall + if (! this.wall) return this.bounds = this.wall.surface.bounds_at_index_with_dimensions(opt.index || 0, this.dimensions) }, -- cgit v1.2.3-70-g09d2