summaryrefslogtreecommitdiff
path: root/site/public/assets/javascripts/mx/extensions/mx.scrubber.js
blob: 54612f2064920bb1179e89f59ad1c29bb9d0d39c (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
/*
	Use the scrollwheel to tween between different points and orientations

	scrubber = new MX.Scrubber(cam, [
		{
			position: [0, viewHeight, -1000],
			rotation: [0, 0]
		},
		{
			position: [0, 1000, 1000],
			rotation: [0, Math.PI]
		},
		{
			position: [0, viewHeight, -1000],
			rotation: [0, 2*Math.PI]
		},
		{
			position: [0, viewHeight, -2000],
			rotation: [0, 0]
		}
	])
	
	// in your animate function:
	scrubber.update()
	
*/

MX.Scrubber = function (obj, points) {

	obj = obj || {}
	points = points || {}

	var reversible = true, loop = false;
	
	var total = points.length * 100,
			distance = 0
			destination = 0,
			last_index = -1,
			last_name = null,
			locked = false,
			point_count = points.length + (loop+0)

	var avg_speed = scroll_avg_speed = 5,
			click_avg_speed = 20,
			webkit_ratio = 0.02
	
	if (points[0].position) {
		points.forEach(function(p){
			p.x = p.position[0]
			p.y = p.position[1]
			p.z = p.position[2]
			p.rotationX = p.rotation[0]
			p.rotationY = p.rotation[1]
		})
	}

	document.addEventListener('touchstart', next, false);
	document.addEventListener('mousedown', next, false);
	document.addEventListener('mousewheel', onDocumentMouseWheel, false);
  document.addEventListener('DOMMouseScroll', onDocumentMouseWheel, false);
	function onDocumentMouseWheel (e) {

		if (locked) return
		
		var delta = 0;
		
		// WebKit
		if ( event.wheelDeltaY ) {
			delta -= event.wheelDeltaY * webkit_ratio
		}
		// Opera / Explorer 9
		else if ( event.wheelDelta ) {
			delta -= event.wheelDelta * webkit_ratio
		}
		// Firefox
		else if ( event.detail ) {
			delta += event.detail * 2
		}
		if (! reversible && delta < 0) return;
		
		if (destination < total-100 || delta < 0) {
			e.preventDefault()
		}
		else {
			return
		}
		
		destination += delta
		
		avg_speed = scroll_avg_speed
	}
	
	function add_point(point){
		if (point.type == "Camera") {
			point = {
				position: [ point.x, point.y, point.z ],
				rotation: [ point.rotationX, point.rotationY ],
				callback: noop
			}
		}
		points.push(point)
		total = points.length * 100
	}
	
	function reset(){
		distance = destination = 0
		last_index = -1
		last_name = null
	}
	
	function next(){
		destination = ~~(destination/100) * 100
		destination += 100
		avg_speed = click_avg_speed
	}
	
	function update(){
		if (locked) return

		if (destination > total-100) destination = total-100

		distance = avg(distance, destination, avg_speed)
		var ratio = distance / total
		
		if (! loop) {
			if (ratio < 0)	{
				destination = 0
				ratio = 0
			}
			else if (ratio > 1) {
				destination = total
				ratio = 1
			}
		}
		
		var diff = ratio * point_count
		var step = (distance % 100) / 100
		var src = ~~clamp(diff, 0, point_count-1)
		var halfway = ~~clamp(diff + 0.5, 0, point_count-1)
		var dest = ~~clamp(diff + 1, 0, point_count-1)

		if (halfway != last_index) {
			last_index = halfway
			if (points[last_index].name != last_name) {
				last_name = points[last_index].name
			}
			$("#info .active").removeClass("active")
			$("#info div[data-name='" + last_name + "']").addClass("active")
			points[halfway].callback && points[halfway].callback()
		}
		
		var ry0 = points[src].rotationY
		var ry1 = points[dest].rotationY
		if (abs(ry0 - ry1) == TWO_PI) {
			ry0 = ry1
		}
		
		obj.x = lerp(step, points[src].x, points[dest].x)
		obj.y = lerp(step, points[src].y, points[dest].y)
		obj.z = lerp(step, points[src].z, points[dest].z)
		obj.rotationX = lerp(step, points[src].rotationX, points[dest].rotationX)
		obj.rotationY = lerp(step, ry0, ry1)
		if (obj.rotationY > PI) { obj.rotationY -= TWO_PI }
	}

	var scrubber = {
		init: function(){
			app && app.movements && app.movements.lock()
		},
		lock: function(){
			app && app.movements && app.movements.unlock()
			locked = true
		},
		unlock: function(){
			app && app.movements && app.movements.lock()
			locked = false
		},
		step: function(n){
			distance = destination = n * 100
		},
		add_point: add_point,
		reset: reset,
		next: next,
		update: update,
		
		obj: obj,
		points: points
	}
	
	return scrubber;
}