|
25
|
1 |
|
|
|
2 |
(function() { |
|
|
3 |
var TUIO = function() { |
|
|
4 |
// Listener class |
|
|
5 |
|
|
|
6 |
this.Listener = function(impl) { |
|
|
7 |
if (impl != undefined) { |
|
|
8 |
// override original method implementation |
|
|
9 |
for (var key in impl) { |
|
|
10 |
this[key] = impl[key]; |
|
|
11 |
} |
|
|
12 |
} |
|
|
13 |
} |
|
|
14 |
this.Listener.prototype = { |
|
|
15 |
object_add: function(data) { }, |
|
|
16 |
object_update: function(data) { }, |
|
|
17 |
object_remove: function(data) { }, |
|
|
18 |
cursor_add: function(data) { }, |
|
|
19 |
cursor_update: function(data) { }, |
|
|
20 |
cursor_remove: function(data) { } |
|
|
21 |
} |
|
|
22 |
|
|
|
23 |
// Instance variables |
|
|
24 |
|
|
|
25 |
this.objects = []; |
|
|
26 |
this.cursors = []; |
|
|
27 |
|
|
|
28 |
this._data = {}; |
|
|
29 |
|
|
|
30 |
this._default_listener = new this.Listener(); |
|
|
31 |
this._listeners = [this._default_listener]; |
|
|
32 |
|
|
|
33 |
this._connector = undefined; |
|
|
34 |
|
|
|
35 |
}; |
|
|
36 |
TUIO.prototype = { |
|
|
37 |
start: function(name) { |
|
|
38 |
var c = this._connector; |
|
|
39 |
if (c != undefined) { |
|
|
40 |
if (c.start != undefined) { |
|
|
41 |
c.start(); |
|
|
42 |
} |
|
|
43 |
} |
|
|
44 |
}, |
|
|
45 |
|
|
|
46 |
stop: function() { |
|
|
47 |
var c = this._connector; |
|
|
48 |
if (c != undefined) { |
|
|
49 |
if (c.stop != undefined) { |
|
|
50 |
c.stop(); |
|
|
51 |
} |
|
|
52 |
} |
|
|
53 |
}, |
|
|
54 |
|
|
|
55 |
setConnector: function(connector) { |
|
|
56 |
this._connector = connector; |
|
|
57 |
}, |
|
|
58 |
|
|
|
59 |
addListener: function(listener) { |
|
|
60 |
this._listeners.push(listener); |
|
|
61 |
}, |
|
|
62 |
removeListener: function(listener) { |
|
|
63 |
this._listeners.splice(this._listeners.indexOf(listener), 1); |
|
|
64 |
}, |
|
|
65 |
|
|
|
66 |
_invoke: function(method, data) { |
|
|
67 |
var i, len = this._listeners.length; |
|
|
68 |
for (i=0; i<len; i++) { |
|
|
69 |
var listener = this._listeners[i]; |
|
|
70 |
listener[method](data); |
|
|
71 |
} |
|
|
72 |
}, |
|
|
73 |
|
|
|
74 |
callback: function(type, sid, fid, x, y, angle) { |
|
|
75 |
var data; |
|
|
76 |
|
|
|
77 |
if ((type != 0) && (type != 3)) { |
|
|
78 |
data = this._data[sid]; |
|
|
79 |
} |
|
|
80 |
else { |
|
|
81 |
data = { |
|
|
82 |
sid: sid, |
|
|
83 |
fid: fid, |
|
|
84 |
path: [] |
|
|
85 |
} |
|
|
86 |
this._data[sid] = data; |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
data.path.push([x, y]); |
|
|
90 |
|
|
|
91 |
data.x = x; |
|
|
92 |
data.y = y; |
|
|
93 |
|
|
|
94 |
if (type < 3) { |
|
|
95 |
data.angle = angle; |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
switch (type) { |
|
|
99 |
case 0: |
|
|
100 |
this.objects.push(data); |
|
|
101 |
this._invoke('object_add', data); |
|
|
102 |
break; |
|
|
103 |
|
|
|
104 |
case 1: |
|
|
105 |
this._invoke('object_update', data); |
|
|
106 |
break; |
|
|
107 |
|
|
|
108 |
case 2: |
|
|
109 |
this.objects.splice(this.objects.indexOf(data), 1); |
|
|
110 |
this._invoke('object_remove', data); |
|
|
111 |
break; |
|
|
112 |
|
|
|
113 |
case 3: |
|
|
114 |
this.cursors.push(data); |
|
|
115 |
this._invoke('cursor_add', data); |
|
|
116 |
break; |
|
|
117 |
|
|
|
118 |
case 4: |
|
|
119 |
this._invoke('cursor_update', data); |
|
|
120 |
break; |
|
|
121 |
|
|
|
122 |
case 5: |
|
|
123 |
this.cursors.splice(this.cursors.indexOf(data), 1); |
|
|
124 |
this._invoke('cursor_remove', data); |
|
|
125 |
break; |
|
|
126 |
|
|
|
127 |
default: |
|
|
128 |
break; |
|
|
129 |
} |
|
|
130 |
|
|
|
131 |
if ((type == 2) || (type == 5)) { |
|
|
132 |
delete this._data[sid]; |
|
|
133 |
} |
|
|
134 |
}, |
|
|
135 |
|
|
|
136 |
// Convenient callbacks set |
|
|
137 |
|
|
|
138 |
object_add: function(f) { this._default_listener.object_add = f; }, |
|
|
139 |
object_update: function(f) { this._default_listener.object_update = f; }, |
|
|
140 |
object_remove: function(f) { this._default_listener.object_remove = f; }, |
|
|
141 |
cursor_add: function(f) { this._default_listener.cursor_add = f; }, |
|
|
142 |
cursor_update: function(f) { this._default_listener.cursor_update = f; }, |
|
|
143 |
cursor_remove: function(f) { this._default_listener.cursor_remove = f; } |
|
|
144 |
|
|
|
145 |
}; |
|
|
146 |
this.tuio = new TUIO(); |
|
|
147 |
})(); |
|
|
148 |
|