1 | var Frame = require('./frame')
|
2 | , Hand = require('./hand')
|
3 | , Pointable = require('./pointable')
|
4 | , CircularBuffer = require("./circular_buffer")
|
5 | , Pipeline = require("./pipeline")
|
6 | , EventEmitter = require('events').EventEmitter
|
7 | , gestureListener = require('./gesture').gestureListener
|
8 | , _ = require('underscore');
|
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 | var Controller = module.exports = function(opts) {
|
42 | var inNode = (typeof(process) !== 'undefined' && process.versions && process.versions.node),
|
43 | controller = this;
|
44 |
|
45 | opts = _.defaults(opts || {}, {
|
46 | inNode: inNode
|
47 | });
|
48 |
|
49 | this.inNode = opts.inNode;
|
50 |
|
51 | opts = _.defaults(opts || {}, {
|
52 | frameEventName: this.useAnimationLoop() ? 'animationFrame' : 'deviceFrame',
|
53 | suppressAnimationLoop: !this.useAnimationLoop(),
|
54 | loopWhileDisconnected: false,
|
55 | useAllPlugins: false
|
56 | });
|
57 |
|
58 | this.animationFrameRequested = false;
|
59 | this.onAnimationFrame = function() {
|
60 | controller.emit('animationFrame', controller.lastConnectionFrame);
|
61 | if (controller.loopWhileDisconnected && (controller.connection.focusedState || controller.connection.opts.background) ){
|
62 | window.requestAnimationFrame(controller.onAnimationFrame);
|
63 | }else{
|
64 | controller.animationFrameRequested = false;
|
65 | }
|
66 | }
|
67 | this.suppressAnimationLoop = opts.suppressAnimationLoop;
|
68 | this.loopWhileDisconnected = opts.loopWhileDisconnected;
|
69 | this.frameEventName = opts.frameEventName;
|
70 | this.useAllPlugins = opts.useAllPlugins;
|
71 | this.history = new CircularBuffer(200);
|
72 | this.lastFrame = Frame.Invalid;
|
73 | this.lastValidFrame = Frame.Invalid;
|
74 | this.lastConnectionFrame = Frame.Invalid;
|
75 | this.accumulatedGestures = [];
|
76 | if (opts.connectionType === undefined) {
|
77 | this.connectionType = (this.inBrowser() ? require('./connection/browser') : require('./connection/node'));
|
78 | } else {
|
79 | this.connectionType = opts.connectionType;
|
80 | }
|
81 | this.connection = new this.connectionType(opts);
|
82 | this.plugins = {};
|
83 | this._pluginPipelineSteps = {};
|
84 | this._pluginExtendedMethods = {};
|
85 | if (opts.useAllPlugins) this.useRegisteredPlugins();
|
86 | this.setupConnectionEvents();
|
87 | }
|
88 |
|
89 | Controller.prototype.gesture = function(type, cb) {
|
90 | var creator = gestureListener(this, type);
|
91 | if (cb !== undefined) {
|
92 | creator.stop(cb);
|
93 | }
|
94 | return creator;
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | Controller.prototype.setBackground = function(state) {
|
101 | this.connection.setBackground(state);
|
102 | return this;
|
103 | }
|
104 |
|
105 | Controller.prototype.inBrowser = function() {
|
106 | return !this.inNode;
|
107 | }
|
108 |
|
109 | Controller.prototype.useAnimationLoop = function() {
|
110 | return this.inBrowser() && !this.inBackgroundPage();
|
111 | }
|
112 |
|
113 | Controller.prototype.inBackgroundPage = function(){
|
114 |
|
115 | return (typeof(chrome) !== "undefined") &&
|
116 | chrome.extension &&
|
117 | chrome.extension.getBackgroundPage &&
|
118 | (chrome.extension.getBackgroundPage() === window)
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | Controller.prototype.connect = function() {
|
125 | this.connection.connect();
|
126 | return this;
|
127 | }
|
128 |
|
129 | Controller.prototype.runAnimationLoop = function(){
|
130 | if (!this.suppressAnimationLoop && !this.animationFrameRequested) {
|
131 | this.animationFrameRequested = true;
|
132 | window.requestAnimationFrame(this.onAnimationFrame);
|
133 | }
|
134 | }
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | Controller.prototype.disconnect = function() {
|
140 | this.connection.disconnect();
|
141 | return this;
|
142 | }
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | Controller.prototype.frame = function(num) {
|
161 | return this.history.get(num) || Frame.Invalid;
|
162 | }
|
163 |
|
164 | Controller.prototype.loop = function(callback) {
|
165 | switch (callback.length) {
|
166 | case 1:
|
167 | this.on(this.frameEventName, callback);
|
168 | break;
|
169 | case 2:
|
170 | var controller = this;
|
171 | var scheduler = null;
|
172 | var immediateRunnerCallback = function(frame) {
|
173 | callback(frame, function() {
|
174 | if (controller.lastFrame != frame) {
|
175 | immediateRunnerCallback(controller.lastFrame);
|
176 | } else {
|
177 | controller.once(controller.frameEventName, immediateRunnerCallback);
|
178 | }
|
179 | });
|
180 | }
|
181 | this.once(this.frameEventName, immediateRunnerCallback);
|
182 | break;
|
183 | }
|
184 | return this.connect();
|
185 | }
|
186 |
|
187 | Controller.prototype.addStep = function(step) {
|
188 | if (!this.pipeline) this.pipeline = new Pipeline(this);
|
189 | this.pipeline.addStep(step);
|
190 | }
|
191 |
|
192 |
|
193 | Controller.prototype.processFrame = function(frame) {
|
194 | if (frame.gestures) {
|
195 | this.accumulatedGestures = this.accumulatedGestures.concat(frame.gestures);
|
196 | }
|
197 |
|
198 | this.lastConnectionFrame = frame;
|
199 | this.runAnimationLoop();
|
200 | this.emit('deviceFrame', frame);
|
201 | }
|
202 |
|
203 |
|
204 | Controller.prototype.processFinishedFrame = function(frame) {
|
205 | this.lastFrame = frame;
|
206 | if (frame.valid) {
|
207 | this.lastValidFrame = frame;
|
208 | }
|
209 | frame.controller = this;
|
210 | frame.historyIdx = this.history.push(frame);
|
211 | if (frame.gestures) {
|
212 | frame.gestures = this.accumulatedGestures;
|
213 | this.accumulatedGestures = [];
|
214 | for (var gestureIdx = 0; gestureIdx != frame.gestures.length; gestureIdx++) {
|
215 | this.emit("gesture", frame.gestures[gestureIdx], frame);
|
216 | }
|
217 | }
|
218 | if (this.pipeline) {
|
219 | frame = this.pipeline.run(frame);
|
220 | if (!frame) frame = Frame.Invalid;
|
221 | }
|
222 | this.emit('frame', frame);
|
223 | }
|
224 |
|
225 | Controller.prototype.setupConnectionEvents = function() {
|
226 | var controller = this;
|
227 | this.connection.on('frame', function(frame) {
|
228 | controller.processFrame(frame);
|
229 | });
|
230 | this.on(this.frameEventName, function(frame) {
|
231 | controller.processFinishedFrame(frame);
|
232 | });
|
233 |
|
234 |
|
235 | this.connection.on('disconnect', function() { controller.emit('disconnect'); });
|
236 | this.connection.on('ready', function() { controller.emit('ready'); });
|
237 | this.connection.on('connect', function() { controller.emit('connect'); });
|
238 | this.connection.on('focus', function() { controller.emit('focus'); controller.runAnimationLoop(); });
|
239 | this.connection.on('blur', function() { controller.emit('blur') });
|
240 | this.connection.on('protocol', function(protocol) { controller.emit('protocol', protocol); });
|
241 | this.connection.on('deviceConnect', function(evt) { controller.emit(evt.state ? 'deviceConnected' : 'deviceDisconnected'); });
|
242 | }
|
243 |
|
244 |
|
245 | Controller._pluginFactories = {};
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 | Controller.plugin = function(pluginName, factory) {
|
328 | if (this._pluginFactories[pluginName]) {
|
329 | throw "Plugin \"" + pluginName + "\" already registered";
|
330 | }
|
331 | return this._pluginFactories[pluginName] = factory;
|
332 | };
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 | Controller.plugins = function() {
|
339 | return _.keys(this._pluginFactories);
|
340 | };
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 | Controller.prototype.use = function(pluginName, options) {
|
357 | var functionOrHash, pluginFactory, key, pluginInstance, klass;
|
358 |
|
359 | pluginFactory = (typeof pluginName == 'function') ? pluginName : Controller._pluginFactories[pluginName];
|
360 |
|
361 | if (!pluginFactory) {
|
362 | throw 'Leap Plugin ' + pluginName + ' not found.';
|
363 | }
|
364 |
|
365 | options || (options = {});
|
366 |
|
367 | if (this.plugins[pluginName]){
|
368 | _.extend(this.plugins[pluginName], options)
|
369 | return this;
|
370 | }
|
371 |
|
372 | this.plugins[pluginName] = options;
|
373 |
|
374 | pluginInstance = pluginFactory.call(this, options);
|
375 |
|
376 | for (key in pluginInstance) {
|
377 | functionOrHash = pluginInstance[key];
|
378 |
|
379 | if (typeof functionOrHash === 'function') {
|
380 | if (!this.pipeline) this.pipeline = new Pipeline(this);
|
381 | if (!this._pluginPipelineSteps[pluginName]) this._pluginPipelineSteps[pluginName] = [];
|
382 |
|
383 | this._pluginPipelineSteps[pluginName].push( this.pipeline.addWrappedStep(key, functionOrHash) );
|
384 | } else {
|
385 | if (!this._pluginExtendedMethods[pluginName]) this._pluginExtendedMethods[pluginName] = [];
|
386 |
|
387 | switch (key) {
|
388 | case 'frame':
|
389 | klass = Frame
|
390 | break;
|
391 | case 'hand':
|
392 | klass = Hand
|
393 | break;
|
394 | case 'pointable':
|
395 | klass = Pointable
|
396 | break;
|
397 | default:
|
398 | throw pluginName + ' specifies invalid object type "' + key + '" for prototypical extension'
|
399 | }
|
400 |
|
401 | _.extend(klass.prototype, functionOrHash);
|
402 | _.extend(klass.Invalid, functionOrHash);
|
403 | this._pluginExtendedMethods[pluginName].push([klass, functionOrHash])
|
404 | }
|
405 | }
|
406 | return this;
|
407 | };
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 | Controller.prototype.stopUsing = function (pluginName) {
|
419 | var steps = this._pluginPipelineSteps[pluginName],
|
420 | extMethodHashes = this._pluginExtendedMethods[pluginName],
|
421 | i = 0, klass, extMethodHash;
|
422 |
|
423 | if (!this.plugins[pluginName]) return;
|
424 |
|
425 | if (steps) {
|
426 | for (i = 0; i < steps.length; i++) {
|
427 | this.pipeline.removeStep(steps[i]);
|
428 | }
|
429 | }
|
430 |
|
431 | if (extMethodHashes){
|
432 | for (i = 0; i < extMethodHashes.length; i++){
|
433 | klass = extMethodHashes[i][0]
|
434 | extMethodHash = extMethodHashes[i][1]
|
435 | for (var methodName in extMethodHash) {
|
436 | delete klass.prototype[methodName]
|
437 | delete klass.Invalid[methodName]
|
438 | }
|
439 | }
|
440 | }
|
441 |
|
442 | delete this.plugins[pluginName]
|
443 |
|
444 | return this;
|
445 | }
|
446 |
|
447 | Controller.prototype.useRegisteredPlugins = function(){
|
448 | for (var plugin in Controller._pluginFactories){
|
449 | this.use(plugin);
|
450 | }
|
451 | }
|
452 |
|
453 |
|
454 | _.extend(Controller.prototype, EventEmitter.prototype);
|