summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--public/assets/javascripts/rectangles/models/surface.js76
-rw-r--r--test/07-test-surface.js66
2 files changed, 133 insertions, 9 deletions
diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js
index 0fb90a8..8bce4eb 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++) {
@@ -103,7 +108,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
@@ -139,6 +202,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 48a2836..26a90e7 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)
@@ -38,7 +98,7 @@ describe('triple 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)
@@ -54,7 +114,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) ) )