diff options
| author | Julie Lala <jules@okfoc.us> | 2014-08-18 23:57:34 -0400 |
|---|---|---|
| committer | Julie Lala <jules@okfoc.us> | 2014-08-18 23:57:34 -0400 |
| commit | ca54810c810e8dd08b20dce5e901630d0d36fe25 (patch) | |
| tree | ab5a1dfbd60363d44ed4a75c735b99c1fdaf5cc7 /public/assets/javascripts/rectangles/models/surface.js | |
| parent | 056d78d21aa3ae3985f5711fbfe0c14d3e3d4791 (diff) | |
| parent | 559799f707a8c7fbd0d6296dfbce81ac6f9b376a (diff) | |
ok
Diffstat (limited to 'public/assets/javascripts/rectangles/models/surface.js')
| -rw-r--r-- | public/assets/javascripts/rectangles/models/surface.js | 292 |
1 files changed, 146 insertions, 146 deletions
diff --git a/public/assets/javascripts/rectangles/models/surface.js b/public/assets/javascripts/rectangles/models/surface.js index 8bce4eb..7001ee3 100644 --- a/public/assets/javascripts/rectangles/models/surface.js +++ b/public/assets/javascripts/rectangles/models/surface.js @@ -70,51 +70,14 @@ } 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 - } - - 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) { @@ -137,28 +100,57 @@ if (top_edge > 0) { delta.b -= top_edge } + + return delta } - 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 ) + + 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) - // 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 - // + 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() + + 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){ - if (x < 0 || x > width) { + 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 = 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 } @@ -166,113 +158,121 @@ 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) { + // not totally sure if we can clobber the bounds here since this would prevent + // us from looking rightwise later + break + } + else { + bounds.y.a = intersection.a + bounds.y.b = intersection.b + bounds.x.a = face.x.a + bounds.first = i + continue + } + } + else { + if (face.y.a > bounds.y.a) break + if (face.y.b < bounds.y.b) break } - bounds.x.a = 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) { + // here, since the element is tall enough, we can clobber the bounds completely + bounds.y.a = face.y.a + bounds.y.b = face.y.b + bounds.x.a = face.x.a + bounds.x.b = face.x.b + bounds.first = bounds.last = i + continue + } + else { + bounds.y.a = intersection.a + bounds.y.b = intersection.b + bounds.x.b = face.x.b + bounds.last = i + continue + } + } + if (face.y.a > bounds.y.a) break + if (face.y.b < bounds.y.b) break + bounds.x.b = face.x.b bounds.last = i } + + if (! bounds) { + // console.log('no bounds') + return false + } + else if (bounds.width() < width) { + // console.log('too narrow') + return false + } return bounds } - Surface.prototype.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 + Surface.prototype.bounds_at_index = function(index){ + var faces = this.faces + if (index == -1) index = this.faces.length-1 - // left edge - if (p.a < bounds.x.a) { - if (bounds.first == 0) { - p.a = bounds.x.a - } - else { - nextIndex = bounds.first - 1 - 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 - } - } - } - // 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 - } - } + 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) { |
