-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathindex.js
More file actions
207 lines (170 loc) · 7.13 KB
/
Copy pathindex.js
File metadata and controls
207 lines (170 loc) · 7.13 KB
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
module.exports = require('./build/Release/nuimotion.node');
module.exports.Joints = {};
module.exports.Joints.LEFT_HAND = "left_hand";
module.exports.Joints.RIGHT_HAND = "right_hand";
module.exports.Joints.LEFT_ELBOW = "left_elbow";
module.exports.Joints.RIGHT_ELBOW = "right_elbow";
module.exports.Joints.LEFT_SHOULDER = "left_shoulder";
module.exports.Joints.RIGHT_SHOULDER = "right_shoulder";
module.exports.Joints.LEFT_HIP = "left_hip";
module.exports.Joints.RIGHT_HIP = "right_hip";
module.exports.Joints.TORSO = "torso";
module.exports.Joints.HEAD = "head";
module.exports.Events = {};
module.exports.Events.Gestures = {};
module.exports.Events.Gestures.Progress = { start: "GESTURE_START", complete: "GESTURE_COMPLETE", cancelled: "GESTURE_CANCELLED" };
module.exports.Events.Gestures.Swipe = {};
/** gesture category */
module.exports.Events.Gestures.Swipe.SWIPE = "SWIPE_GESTURE";
/** gesture types */
module.exports.Events.Gestures.Swipe.types = {left: "SWIPE_LEFT", right: "SWIPE_RIGHT", up: "SWIPE_UP", down: "SWIPE_DOWN"};
module.exports.Events.Gestures.Wave = {};
/** gesture category */
module.exports.Events.Gestures.Wave.WAVE = "WAVE_GESTURE";
/** gesture types */
module.exports.Events.Gestures.Wave.types = {hand: "WAVE_HAND"};
module.exports.Events.GESTURE = "GESTURE";
module.exports.Events.DEVICE_INITIALIZED = "DEVICE_INITIALIZED";
module.exports.Events.DEVICE_ERROR = "DEVICE_ERROR";
module.exports.Events.NEW_USER = "NEW_USER";
module.exports.Events.USER_IS_VISIBLE = "USER_IS_VISIBLE";
module.exports.Events.USER_IS_OUT_OF_SCENE = "USER_IS_OUT_OF_SCENE";
module.exports.Events.USER_IS_LOST = "USER_IS_LOST";
module.exports.Events.SKELETON_STOPPED_TRACKING = "SKELETON_STOPPED_TRACKING";
module.exports.Events.SKELETON_TRACKING = "SKELETON_TRACKING";
module.exports.Events.SKELETON_CALIBRATING = "SKELETON_CALIBRATING";
module.exports.Events.SKELETON_CALIBRATION_FAILED = "SKELETON_CALIBRATION_FAILED";
/** what joints we are currently tracking in the NodeJS skeleton tracking loop */
module.exports.jointsTracking = [];
/** milliseconds between frame loop iterations, 50 is default */
module.exports.frameDelay = 50;
/** milliseconds between frame loop iterations, 50 is default */
module.exports.onSkeletonEventHandler = null;
/** is skeleton tracking */
module.exports.isSkeletonTracking = false;
/** frame loop timer for tracking skeleton */
module.exports._frameLoopTimer = null;
/** event callback dictionary */
module.exports._eventCallbackDict = {};
/** gesture callback dictionary */
module.exports._gestureCallbackDict = {};
/**
* convenience function to set joints
* @param joints
*/
module.exports.setJoints = function(joints) {
if (typeof joints == "string") {
module.exports.jointsTracking = [joints];
} else {
module.exports.jointsTracking = joints;
}
};
/**
* add a listener for specific events (device/user/etc)
* @param event name (string or array of events)
* @parm callback function
*/
module.exports.addListener = function(eventName, callback) {
if (typeof eventName == "string") {
module.exports._eventCallbackDict[eventName] = callback;
} else {
for (var evt in eventName) {
module.exports._eventCallbackDict[eventName[evt]] = callback;
}
}
};
/**
* remove a listener for specific events (device/user/etc)
* @param event name (string or array of events)
*/
module.exports.removeListener = function(eventName) {
if (typeof eventName == "string" && typeof module.exports._eventCallbackDict[eventName] !== "undefined") {
delete module.exports._eventCallbackDict[eventName];
} else {
for (var evt in eventName) {
if (typeof module.exports._eventCallbackDict[eventName[evt]] !== "undefined")
delete module.exports._eventCallbackDict[eventName[evt]];
}
}
};
/**
* add a listener for gestures
* @param gesture name (string or array of gestures)
* @parm callback function
*/
module.exports.addGesture = function(gestureName, callback) {
var gestureType;
if (typeof gestureName == "string") {
module.exports._gestureCallbackDict[gestureName] = callback;
//don't expose end user to system complexities of "gesture type"
//we'll get the type from the first part of the gesture name
gestureType = gestureName.split("_")[0] + "_GESTURE";
module.exports.addGestureListener(gestureType, gestureName);
} else {
for (var gst in gestureName) {
module.exports._gestureCallbackDict[gestureName[gst]] = callback;
//don't expose end user to system complexities of "gesture type"
//we'll get the type from the first part of the gesture name
gestureType = gestureName[gst].split("_")[0] + "_GESTURE";
module.exports.addGestureListener(gestureType, gestureName[gst]);
}
}
};
/**
* start skeleton listener loop when skeleton is in view
* @param callback for skeleton tracking
* @param rate (in milliseconds)
* @param joints array
*/
module.exports.startSkeletonListener = function(joints, callback, rate) {
module.exports.frameDelay = rate;
module.exports.onSkeletonEventHandler = callback;
if (joints) {
module.exports.jointsTracking = joints;
}
if (rate) {
module.exports.frameDelay = rate;
}
// if skeleton already tracking, immediately start the listener
if (module.exports.isSkeletonTracking === true && !module.exports._frameLoopTimer) {
module.exports._frameLoopTimer = setInterval(module.exports._onFrameLoopUpdate, module.exports.frameDelay);
}
};
/**
* timed frame loop update for issuing skeleton/joint tracking events
* @private
*/
module.exports._onFrameLoopUpdate = function() {
if (module.exports.jointsTracking.length > 0) {
var skeleton = module.exports.getJoints.apply(this, module.exports.jointsTracking);
module.exports.onSkeletonEventHandler.apply(this, [ {skeleton: skeleton} ]);
}
};
/**
* events listener
* also, listen for when skeleton is tracking to start frame loop
* @param name
*/
module.exports.context.on = function(event) {
// send on device/gesture event
if (module.exports._eventCallbackDict[event.eventType]) {
module.exports._eventCallbackDict[event.eventType].apply(this, [event]);
}
if (event.eventType == "GESTURE" && module.exports._gestureCallbackDict[event.gestureType]) {
module.exports._gestureCallbackDict[event.gestureType].apply(this, [event]);
}
switch (event.eventType) {
case module.exports.Events.SKELETON_TRACKING:
// skeleton has started tracking, start frame/update loop
if (!module.exports._frameLoopTimer && module.exports.onSkeletonEventHandler) {
module.exports._frameLoopTimer = setInterval(module.exports._onFrameLoopUpdate, module.exports.frameDelay);
}
break;
case module.exports.Events.SKELETON_STOPPED_TRACKING:
if (module.exports._frameLoopTimer) {
// skeleton is no longer tracking, shutdown frame/update loop
clearInterval(module.exports._frameLoopTimer);
module.exports._frameLoopTimer = null;
}
}
};