summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models/surface.js
blob: 8330d4129636fa795031cbc2af73ac724140f3f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
(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.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)
	}
	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.bounds.x.b < v.a || this.bounds.y.b < 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
	}

	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

		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
			bounds.first = i
		}
		
		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
			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
	}
	else {
		module.exports = Surface
	}
})()