diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-08-06 18:49:58 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-08-06 18:49:58 -0400 |
| commit | 466ccfdccd2d761f31ba78a74a40544b77b358e5 (patch) | |
| tree | b7fdef29410851b7e89a3c038c9c12f2c2f6faf7 /public/assets/javascripts/rectangles/models/surface.js | |
| parent | 50ad04ca01da10f87bf907fdf3c185a1d68be506 (diff) | |
clamping
Diffstat (limited to 'public/assets/javascripts/rectangles/models/surface.js')
| -rw-r--r-- | public/assets/javascripts/rectangles/models/surface.js | 70 |
1 files changed, 69 insertions, 1 deletions
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 |
