summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models/surface.js
diff options
context:
space:
mode:
Diffstat (limited to 'public/assets/javascripts/rectangles/models/surface.js')
-rw-r--r--public/assets/javascripts/rectangles/models/surface.js76
1 files changed, 70 insertions, 6 deletions
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