|
28
|
1 |
/* |
|
|
2 |
TUIO Java backend - part of the reacTIVision project |
|
|
3 |
http://reactivision.sourceforge.net/ |
|
|
4 |
|
|
|
5 |
Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten@iua.upf.edu> |
|
|
6 |
|
|
|
7 |
This program is free software; you can redistribute it and/or modify |
|
|
8 |
it under the terms of the GNU General Public License as published by |
|
|
9 |
the Free Software Foundation; either version 2 of the License, or |
|
|
10 |
(at your option) any later version. |
|
|
11 |
|
|
|
12 |
This program is distributed in the hope that it will be useful, |
|
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
15 |
GNU General Public License for more details. |
|
|
16 |
|
|
|
17 |
You should have received a copy of the GNU General Public License |
|
|
18 |
along with this program; if not, write to the Free Software |
|
|
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
|
20 |
*/ |
|
|
21 |
package TUIO; |
|
|
22 |
|
|
|
23 |
import com.illposed.osc.*; |
|
|
24 |
import java.util.*; |
|
|
25 |
|
|
|
26 |
/** |
|
|
27 |
* The TuioClient class is the central TUIO protocol decoder component. It provides a simple callback infrastructure using the {@link TuioListener} interface. |
|
|
28 |
* In order to receive and decode TUIO messages an instance of TuioClient needs to be created. The TuioClient instance then generates TUIO events |
|
|
29 |
* which are broadcasted to all registered classes that implement the {@link TuioListener} interface.<P> |
|
|
30 |
* <code> |
|
|
31 |
* TuioClient client = new TuioClient();<br/> |
|
|
32 |
* client.addTuioListener(myTuioListener);<br/> |
|
|
33 |
* client.connect();<br/> |
|
|
34 |
* </code> |
|
|
35 |
* |
|
|
36 |
* @author Martin Kaltenbrunner |
|
|
37 |
* @version 1.4 |
|
|
38 |
*/ |
|
|
39 |
|
|
|
40 |
/* |
|
|
41 |
Modified by alexandre.bastien@iri.centrepompidou.fr to manage TUIO strings. |
|
|
42 |
*/ |
|
|
43 |
public class TuioClient implements OSCListener { |
|
|
44 |
|
|
|
45 |
public String comm; |
|
|
46 |
|
|
|
47 |
private int port = 3333; |
|
|
48 |
private OSCPortIn oscPort; |
|
|
49 |
private boolean connected = false; |
|
|
50 |
private Hashtable<Long,TuioObject> objectList = new Hashtable<Long,TuioObject>(); |
|
|
51 |
private Vector<Long> aliveObjectList = new Vector<Long>(); |
|
|
52 |
private Vector<Long> newObjectList = new Vector<Long>(); |
|
|
53 |
private Hashtable<Long,TuioCursor> cursorList = new Hashtable<Long,TuioCursor>(); |
|
|
54 |
private Vector<Long> aliveCursorList = new Vector<Long>(); |
|
|
55 |
private Vector<Long> newCursorList = new Vector<Long>(); |
|
|
56 |
private Hashtable<Long,TuioString> stringList = new Hashtable<Long,TuioString>(); |
|
|
57 |
private Vector<Long> aliveStringList = new Vector<Long>(); |
|
|
58 |
private Vector<Long> newStringList = new Vector<Long>(); |
|
|
59 |
|
|
|
60 |
private Vector<TuioObject> frameObjects = new Vector<TuioObject>(); |
|
|
61 |
private Vector<TuioCursor> frameCursors = new Vector<TuioCursor>(); |
|
|
62 |
private Vector<TuioString> frameStrings = new Vector<TuioString>(); |
|
|
63 |
|
|
|
64 |
private Vector<TuioCursor> freeCursorList = new Vector<TuioCursor>(); |
|
|
65 |
private int maxCursorID = -1; |
|
|
66 |
|
|
|
67 |
private Vector<TuioString> freeStringList = new Vector<TuioString>(); |
|
|
68 |
private int maxStringID = -1; |
|
|
69 |
|
|
|
70 |
private long currentFrame = 0; |
|
|
71 |
private TuioTime currentTime; |
|
|
72 |
|
|
|
73 |
private Vector<TuioListener> listenerList = new Vector<TuioListener>(); |
|
|
74 |
|
|
|
75 |
/** |
|
|
76 |
* The default constructor creates a client that listens to the default TUIO port 3333 |
|
|
77 |
*/ |
|
|
78 |
public TuioClient() {} |
|
|
79 |
|
|
|
80 |
/** |
|
|
81 |
* This constructor creates a client that listens to the provided port |
|
|
82 |
* |
|
|
83 |
* @param port the listening port number |
|
|
84 |
*/ |
|
|
85 |
public TuioClient(int port) { |
|
|
86 |
this.port = port; |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
/** |
|
|
90 |
* The TuioClient starts listening to TUIO messages on the configured UDP port |
|
|
91 |
* All reveived TUIO messages are decoded and the resulting TUIO events are broadcasted to all registered TuioListeners |
|
|
92 |
*/ |
|
|
93 |
public void connect() { |
|
|
94 |
|
|
|
95 |
TuioTime.initSession(); |
|
|
96 |
currentTime = new TuioTime(); |
|
|
97 |
currentTime.reset(); |
|
|
98 |
|
|
|
99 |
try { |
|
|
100 |
oscPort = new OSCPortIn(port); |
|
|
101 |
oscPort.addListener("/tuio/2Dobj",this); |
|
|
102 |
oscPort.addListener("/tuio/3Dcur",this); |
|
|
103 |
oscPort.addListener("/tuio/_siP",this); |
|
|
104 |
oscPort.startListening(); |
|
|
105 |
connected = true; |
|
|
106 |
} catch (Exception e) { |
|
|
107 |
System.out.println("TuioClient: failed to connect to port "+port); |
|
|
108 |
connected = false; |
|
|
109 |
} |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
/** |
|
|
113 |
* The TuioClient stops listening to TUIO messages on the configured UDP port |
|
|
114 |
*/ |
|
|
115 |
public void disconnect() { |
|
|
116 |
oscPort.stopListening(); |
|
|
117 |
try { Thread.sleep(100); } |
|
|
118 |
catch (Exception e) {}; |
|
|
119 |
oscPort.close(); |
|
|
120 |
connected = false; |
|
|
121 |
} |
|
|
122 |
|
|
|
123 |
/** |
|
|
124 |
* Returns true if this TuioClient is currently connected. |
|
|
125 |
* @return true if this TuioClient is currently connected |
|
|
126 |
*/ |
|
|
127 |
public boolean isConnected() { return connected; } |
|
|
128 |
|
|
|
129 |
/** |
|
|
130 |
* Adds the provided TuioListener to the list of registered TUIO event listeners |
|
|
131 |
* |
|
|
132 |
* @param listener the TuioListener to add |
|
|
133 |
*/ |
|
|
134 |
public void addTuioListener(TuioListener listener) { |
|
|
135 |
listenerList.addElement(listener); |
|
|
136 |
} |
|
|
137 |
|
|
|
138 |
/** |
|
|
139 |
* Removes the provided TuioListener from the list of registered TUIO event listeners |
|
|
140 |
* |
|
|
141 |
* @param listener the TuioListener to remove |
|
|
142 |
*/ |
|
|
143 |
public void removeTuioListener(TuioListener listener) { |
|
|
144 |
listenerList.removeElement(listener); |
|
|
145 |
} |
|
|
146 |
|
|
|
147 |
/** |
|
|
148 |
* Removes all TuioListener from the list of registered TUIO event listeners |
|
|
149 |
*/ |
|
|
150 |
public void removeAllTuioListeners() { |
|
|
151 |
listenerList.clear(); |
|
|
152 |
} |
|
|
153 |
|
|
|
154 |
/** |
|
|
155 |
* Returns a Vector of all currently active TuioObjects |
|
|
156 |
* |
|
|
157 |
* @return a Vector of all currently active TuioObjects |
|
|
158 |
*/ |
|
|
159 |
public Vector<TuioObject> getTuioObjects() { |
|
|
160 |
return new Vector<TuioObject>(objectList.values()); |
|
|
161 |
} |
|
|
162 |
|
|
|
163 |
/** |
|
|
164 |
* Returns a Vector of all currently active TuioCursors |
|
|
165 |
* |
|
|
166 |
* @return a Vector of all currently active TuioCursors |
|
|
167 |
*/ |
|
|
168 |
public Vector<TuioCursor> getTuioCursors() { |
|
|
169 |
return new Vector<TuioCursor>(cursorList.values()); |
|
|
170 |
} |
|
|
171 |
|
|
|
172 |
/** |
|
|
173 |
* Returns a Vector of all currently active TuioStrings |
|
|
174 |
* |
|
|
175 |
* @return a Vector of all currently active TuioStrings |
|
|
176 |
*/ |
|
|
177 |
public Vector<TuioString> getTuioStrings() { |
|
|
178 |
return new Vector<TuioString>(stringList.values()); |
|
|
179 |
} |
|
|
180 |
|
|
|
181 |
/** |
|
|
182 |
* Returns the TuioObject corresponding to the provided Session ID |
|
|
183 |
* or NULL if the Session ID does not refer to an active TuioObject |
|
|
184 |
* |
|
|
185 |
* @return an active TuioObject corresponding to the provided Session ID or NULL |
|
|
186 |
*/ |
|
|
187 |
public TuioObject getTuioObject(long s_id) { |
|
|
188 |
return objectList.get(s_id); |
|
|
189 |
} |
|
|
190 |
|
|
|
191 |
/** |
|
|
192 |
* Returns the TuioCursor corresponding to the provided Session ID |
|
|
193 |
* or NULL if the Session ID does not refer to an active TuioCursor |
|
|
194 |
* |
|
|
195 |
* @return an active TuioCursor corresponding to the provided Session ID or NULL |
|
|
196 |
*/ |
|
|
197 |
public TuioCursor getTuioCursor(long s_id) { |
|
|
198 |
return cursorList.get(s_id); |
|
|
199 |
} |
|
|
200 |
|
|
|
201 |
/** |
|
|
202 |
* Returns the TuioString corresponding to the provided Session ID |
|
|
203 |
* or NULL if the Session ID does not refer to an active TuioString |
|
|
204 |
* |
|
|
205 |
* @return an active TuioString corresponding to the provided Session ID or NULL |
|
|
206 |
*/ |
|
|
207 |
public TuioString getTuioString(long s_id) { |
|
|
208 |
return stringList.get(s_id); |
|
|
209 |
} |
|
|
210 |
|
|
|
211 |
/** |
|
|
212 |
* The OSC callback method where all TUIO messages are received and decoded |
|
|
213 |
* and where the TUIO event callbacks are dispatched |
|
|
214 |
* |
|
|
215 |
* @param date the time stamp of the OSC bundle |
|
|
216 |
* @param message the received OSC message |
|
|
217 |
*/ |
|
|
218 |
public void acceptMessage(Date date, OSCMessage message) { |
|
|
219 |
|
|
|
220 |
Object[] args = message.getArguments(); |
|
|
221 |
String command = (String)args[0]; |
|
|
222 |
String address = message.getAddress(); |
|
|
223 |
|
|
|
224 |
if (address.equals("/tuio/2Dobj")) { |
|
|
225 |
|
|
|
226 |
if (command.equals("set")) { |
|
|
227 |
|
|
|
228 |
long s_id = ((Integer)args[1]).longValue(); |
|
|
229 |
int c_id = ((Integer)args[2]).intValue(); |
|
|
230 |
float xpos = ((Float)args[3]).floatValue(); |
|
|
231 |
float ypos = ((Float)args[4]).floatValue(); |
|
|
232 |
float angle = ((Float)args[5]).floatValue(); |
|
|
233 |
float xspeed = ((Float)args[6]).floatValue(); |
|
|
234 |
float yspeed = ((Float)args[7]).floatValue(); |
|
|
235 |
float rspeed = ((Float)args[8]).floatValue(); |
|
|
236 |
float maccel = ((Float)args[9]).floatValue(); |
|
|
237 |
float raccel = ((Float)args[10]).floatValue(); |
|
|
238 |
|
|
|
239 |
if (objectList.get(s_id) == null) { |
|
|
240 |
|
|
|
241 |
TuioObject addObject = new TuioObject(s_id,c_id,xpos,ypos,angle); |
|
|
242 |
frameObjects.addElement(addObject); |
|
|
243 |
|
|
|
244 |
} else { |
|
|
245 |
|
|
|
246 |
TuioObject tobj = objectList.get(s_id); |
|
|
247 |
if (tobj==null) return; |
|
|
248 |
if ((tobj.xpos!=xpos) || (tobj.ypos!=ypos) || (tobj.angle!=angle) || (tobj.x_speed!=xspeed) || (tobj.y_speed!=yspeed) || (tobj.rotation_speed!=rspeed) || (tobj.motion_accel!=maccel) || (tobj.rotation_accel!=raccel)) { |
|
|
249 |
|
|
|
250 |
TuioObject updateObject = new TuioObject(s_id,c_id,xpos,ypos,angle); |
|
|
251 |
updateObject.update(xpos,ypos,angle,xspeed,yspeed,rspeed,maccel,raccel); |
|
|
252 |
frameObjects.addElement(updateObject); |
|
|
253 |
} |
|
|
254 |
|
|
|
255 |
} |
|
|
256 |
|
|
|
257 |
} else if (command.equals("alive")) { |
|
|
258 |
|
|
|
259 |
newObjectList.clear(); |
|
|
260 |
for (int i=1;i<args.length;i++) { |
|
|
261 |
// get the message content |
|
|
262 |
long s_id = ((Integer)args[i]).longValue(); |
|
|
263 |
newObjectList.addElement(s_id); |
|
|
264 |
// reduce the object list to the lost objects |
|
|
265 |
if (aliveObjectList.contains(s_id)) |
|
|
266 |
aliveObjectList.removeElement(s_id); |
|
|
267 |
} |
|
|
268 |
|
|
|
269 |
// remove the remaining objects |
|
|
270 |
for (int i=0;i<aliveObjectList.size();i++) { |
|
|
271 |
TuioObject removeObject = objectList.get(aliveObjectList.elementAt(i)); |
|
|
272 |
if (removeObject==null) continue; |
|
|
273 |
removeObject.remove(currentTime); |
|
|
274 |
frameObjects.addElement(removeObject); |
|
|
275 |
} |
|
|
276 |
|
|
|
277 |
} else if (command.equals("fseq")) { |
|
|
278 |
|
|
|
279 |
long fseq = ((Integer)args[1]).longValue(); |
|
|
280 |
boolean lateFrame = false; |
|
|
281 |
|
|
|
282 |
if (fseq>0) { |
|
|
283 |
if (fseq>currentFrame) currentTime = TuioTime.getSessionTime(); |
|
|
284 |
if ((fseq>=currentFrame) || ((currentFrame-fseq)>100)) currentFrame=fseq; |
|
|
285 |
else lateFrame = true; |
|
|
286 |
} else if (TuioTime.getSessionTime().subtract(currentTime).getTotalMilliseconds()>100) { |
|
|
287 |
currentTime = TuioTime.getSessionTime(); |
|
|
288 |
} |
|
|
289 |
|
|
|
290 |
if (!lateFrame) { |
|
|
291 |
Enumeration<TuioObject> frameEnum = frameObjects.elements(); |
|
|
292 |
while(frameEnum.hasMoreElements()) { |
|
|
293 |
TuioObject tobj = frameEnum.nextElement(); |
|
|
294 |
|
|
|
295 |
switch (tobj.getTuioState()) { |
|
|
296 |
case TuioObject.TUIO_REMOVED: |
|
|
297 |
TuioObject removeObject = tobj; |
|
|
298 |
removeObject.remove(currentTime); |
|
|
299 |
for (int i=0;i<listenerList.size();i++) { |
|
|
300 |
TuioListener listener = (TuioListener)listenerList.elementAt(i); |
|
|
301 |
if (listener!=null) listener.removeTuioObject(removeObject); |
|
|
302 |
} |
|
|
303 |
objectList.remove(removeObject.getSessionID()); |
|
|
304 |
break; |
|
|
305 |
|
|
|
306 |
case TuioObject.TUIO_ADDED: |
|
|
307 |
TuioObject addObject = new TuioObject(currentTime,tobj.getSessionID(),tobj.getSymbolID(),tobj.getX(),tobj.getY(),tobj.getAngle()); |
|
|
308 |
objectList.put(addObject.getSessionID(),addObject); |
|
|
309 |
for (int i=0;i<listenerList.size();i++) { |
|
|
310 |
TuioListener listener = (TuioListener)listenerList.elementAt(i); |
|
|
311 |
if (listener!=null) listener.addTuioObject(addObject); |
|
|
312 |
} |
|
|
313 |
break; |
|
|
314 |
|
|
|
315 |
default: |
|
|
316 |
TuioObject updateObject = objectList.get(tobj.getSessionID()); |
|
|
317 |
if ( (tobj.getX()!=updateObject.getX() && tobj.getXSpeed()==0) || (tobj.getY()!=updateObject.getY() && tobj.getYSpeed()==0) ) |
|
|
318 |
updateObject.update(currentTime,tobj.getX(),tobj.getY(),tobj.getAngle()); |
|
|
319 |
else |
|
|
320 |
updateObject.update(currentTime,tobj.getX(),tobj.getY(),tobj.getAngle(),tobj.getXSpeed(),tobj.getYSpeed(),tobj.getRotationSpeed(),tobj.getMotionAccel(),tobj.getRotationAccel()); |
|
|
321 |
|
|
|
322 |
for (int i=0;i<listenerList.size();i++) { |
|
|
323 |
TuioListener listener = (TuioListener)listenerList.elementAt(i); |
|
|
324 |
if (listener!=null) listener.updateTuioObject(updateObject); |
|
|
325 |
} |
|
|
326 |
} |
|
|
327 |
} |
|
|
328 |
|
|
|
329 |
for (int i=0;i<listenerList.size();i++) { |
|
|
330 |
TuioListener listener = (TuioListener)listenerList.elementAt(i); |
|
|
331 |
if (listener!=null) listener.refresh(new TuioTime(currentTime)); |
|
|
332 |
} |
|
|
333 |
|
|
|
334 |
Vector<Long> buffer = aliveObjectList; |
|
|
335 |
aliveObjectList = newObjectList; |
|
|
336 |
// recycling the vector |
|
|
337 |
newObjectList = buffer; |
|
|
338 |
} |
|
|
339 |
frameObjects.clear(); |
|
|
340 |
} |
|
|
341 |
} else if (address.equals("/tuio/3Dcur")) { |
|
|
342 |
|
|
|
343 |
if (command.equals("set")) { |
|
|
344 |
|
|
|
345 |
long s_id = ((Integer)args[1]).longValue(); |
|
|
346 |
float xpos = ((Float)args[2]).floatValue(); |
|
|
347 |
float ypos = ((Float)args[3]).floatValue(); |
|
|
348 |
float zpos = ((Float)args[4]).floatValue(); |
|
|
349 |
float xspeed = ((Float)args[5]).floatValue(); |
|
|
350 |
float yspeed = ((Float)args[6]).floatValue(); |
|
|
351 |
float maccel = ((Float)args[7]).floatValue(); |
|
|
352 |
|
|
|
353 |
if (cursorList.get(s_id) == null) { |
|
|
354 |
|
|
|
355 |
TuioCursor addCursor = new TuioCursor(s_id, -1 ,xpos,ypos,zpos); |
|
|
356 |
frameCursors.addElement(addCursor); |
|
|
357 |
|
|
|
358 |
} else { |
|
|
359 |
|
|
|
360 |
TuioCursor tcur = cursorList.get(s_id); |
|
|
361 |
if (tcur==null) return; |
|
|
362 |
if ((tcur.xpos!=xpos) || (tcur.ypos!=ypos) || (tcur.zpos!=zpos) || (tcur.x_speed!=xspeed) || (tcur.y_speed!=yspeed) || (tcur.motion_accel!=maccel)) { |
|
|
363 |
|
|
|
364 |
TuioCursor updateCursor = new TuioCursor(s_id,tcur.getCursorID(),xpos,ypos,zpos); |
|
|
365 |
updateCursor.update(xpos,ypos,zpos,xspeed,yspeed,maccel); |
|
|
366 |
frameCursors.addElement(updateCursor); |
|
|
367 |
} |
|
|
368 |
} |
|
|
369 |
|
|
|
370 |
//System.out.println("set cur " + s_id+" "+xpos+" "+ypos+" "+xspeed+" "+yspeed+" "+maccel); |
|
|
371 |
|
|
|
372 |
} else if (command.equals("alive")) { |
|
|
373 |
|
|
|
374 |
newCursorList.clear(); |
|
|
375 |
for (int i=1;i<args.length;i++) { |
|
|
376 |
// get the message content |
|
|
377 |
long s_id = ((Integer)args[i]).longValue(); |
|
|
378 |
newCursorList.addElement(s_id); |
|
|
379 |
// reduce the cursor list to the lost cursors |
|
|
380 |
if (aliveCursorList.contains(s_id)) |
|
|
381 |
aliveCursorList.removeElement(s_id); |
|
|
382 |
} |
|
|
383 |
|
|
|
384 |
// remove the remaining cursors |
|
|
385 |
for (int i=0;i<aliveCursorList.size();i++) { |
|
|
386 |
TuioCursor removeCursor = cursorList.get(aliveCursorList.elementAt(i)); |
|
|
387 |
if (removeCursor==null) continue; |
|
|
388 |
removeCursor.remove(currentTime); |
|
|
389 |
frameCursors.addElement(removeCursor); |
|
|
390 |
} |
|
|
391 |
|
|
|
392 |
} else if (command.equals("fseq")) { |
|
|
393 |
long fseq = ((Integer)args[1]).longValue(); |
|
|
394 |
boolean lateFrame = false; |
|
|
395 |
|
|
|
396 |
if (fseq>0) { |
|
|
397 |
if (fseq>currentFrame) currentTime = TuioTime.getSessionTime(); |
|
|
398 |
if ((fseq>=currentFrame) || ((currentFrame-fseq)>100)) currentFrame = fseq; |
|
|
399 |
else lateFrame = true; |
|
|
400 |
} else if (TuioTime.getSessionTime().subtract(currentTime).getTotalMilliseconds()>100) { |
|
|
401 |
currentTime = TuioTime.getSessionTime(); |
|
|
402 |
} |
|
|
403 |
if (!lateFrame) { |
|
|
404 |
|
|
|
405 |
Enumeration<TuioCursor> frameEnum = frameCursors.elements(); |
|
|
406 |
while(frameEnum.hasMoreElements()) { |
|
|
407 |
TuioCursor tcur = frameEnum.nextElement(); |
|
|
408 |
|
|
|
409 |
switch (tcur.getTuioState()) { |
|
|
410 |
case TuioCursor.TUIO_REMOVED: |
|
|
411 |
|
|
|
412 |
TuioCursor removeCursor = tcur; |
|
|
413 |
removeCursor.remove(currentTime); |
|
|
414 |
|
|
|
415 |
for (int i=0;i<listenerList.size();i++) { |
|
|
416 |
TuioListener listener = (TuioListener)listenerList.elementAt(i); |
|
|
417 |
if (listener!=null) listener.removeTuioCursor(removeCursor); |
|
|
418 |
} |
|
|
419 |
|
|
|
420 |
cursorList.remove(removeCursor.getSessionID()); |
|
|
421 |
|
|
|
422 |
if (removeCursor.getCursorID()==maxCursorID) { |
|
|
423 |
maxCursorID = -1; |
|
|
424 |
if (cursorList.size()>0) { |
|
|
425 |
Enumeration<TuioCursor> clist = cursorList.elements(); |
|
|
426 |
while (clist.hasMoreElements()) { |
|
|
427 |
int c_id = clist.nextElement().getCursorID(); |
|
|
428 |
if (c_id>maxCursorID) maxCursorID=c_id; |
|
|
429 |
} |
|
|
430 |
|
|
|
431 |
Enumeration<TuioCursor> flist = freeCursorList.elements(); |
|
|
432 |
while (flist.hasMoreElements()) { |
|
|
433 |
int c_id = flist.nextElement().getCursorID(); |
|
|
434 |
if (c_id>=maxCursorID) freeCursorList.removeElement(c_id); |
|
|
435 |
} |
|
|
436 |
} else freeCursorList.clear(); |
|
|
437 |
} else if (removeCursor.getCursorID()<maxCursorID) { |
|
|
438 |
freeCursorList.addElement(removeCursor); |
|
|
439 |
} |
|
|
440 |
|
|
|
441 |
break; |
|
|
442 |
|
|
|
443 |
case TuioCursor.TUIO_ADDED: |
|
|
444 |
|
|
|
445 |
int c_id = cursorList.size(); |
|
|
446 |
if ((cursorList.size()<=maxCursorID) && (freeCursorList.size()>0)) { |
|
|
447 |
TuioCursor closestCursor = freeCursorList.firstElement(); |
|
|
448 |
Enumeration<TuioCursor> testList = freeCursorList.elements(); |
|
|
449 |
while (testList.hasMoreElements()) { |
|
|
450 |
TuioCursor testCursor = testList.nextElement(); |
|
|
451 |
if (testCursor.getDistance(tcur)<closestCursor.getDistance(tcur)) closestCursor = testCursor; |
|
|
452 |
} |
|
|
453 |
c_id = closestCursor.getCursorID(); |
|
|
454 |
freeCursorList.removeElement(closestCursor); |
|
|
455 |
} else maxCursorID = c_id; |
|
|
456 |
|
|
|
457 |
TuioCursor addCursor = new TuioCursor(currentTime,tcur.getSessionID(),c_id,tcur.getX(),tcur.getY(),tcur.getZ()); |
|
|
458 |
cursorList.put(addCursor.getSessionID(),addCursor); |
|
|
459 |
|
|
|
460 |
for (int i=0;i<listenerList.size();i++) { |
|
|
461 |
TuioListener listener = (TuioListener)listenerList.elementAt(i); |
|
|
462 |
if (listener!=null) listener.addTuioCursor(addCursor); |
|
|
463 |
} |
|
|
464 |
break; |
|
|
465 |
|
|
|
466 |
default: |
|
|
467 |
|
|
|
468 |
TuioCursor updateCursor = cursorList.get(tcur.getSessionID()); |
|
|
469 |
if ( (tcur.getX()!=updateCursor.getX() && tcur.getXSpeed()==0) || (tcur.getY()!=updateCursor.getY() && tcur.getYSpeed()==0) || (tcur.getZ()!=updateCursor.getZ()) ) |
|
|
470 |
updateCursor.update(currentTime,tcur.getX(),tcur.getY(),tcur.getZ()); |
|
|
471 |
else |
|
|
472 |
updateCursor.update(currentTime,tcur.getX(),tcur.getY(),tcur.getZ(),tcur.getXSpeed(),tcur.getYSpeed(),tcur.getMotionAccel()); |
|
|
473 |
|
|
|
474 |
for (int i=0;i<listenerList.size();i++) { |
|
|
475 |
TuioListener listener = (TuioListener)listenerList.elementAt(i); |
|
|
476 |
if (listener!=null) listener.updateTuioCursor(updateCursor); |
|
|
477 |
} |
|
|
478 |
} |
|
|
479 |
} |
|
|
480 |
|
|
|
481 |
for (int i=0;i<listenerList.size();i++) { |
|
|
482 |
TuioListener listener = (TuioListener)listenerList.elementAt(i); |
|
|
483 |
if (listener!=null) listener.refresh(new TuioTime(currentTime)); |
|
|
484 |
} |
|
|
485 |
|
|
|
486 |
Vector<Long> buffer = aliveCursorList; |
|
|
487 |
aliveCursorList = newCursorList; |
|
|
488 |
// recycling the vector |
|
|
489 |
newCursorList = buffer; |
|
|
490 |
} |
|
|
491 |
|
|
|
492 |
frameCursors.clear(); |
|
|
493 |
} |
|
|
494 |
} else if (address.equals("/tuio/_siP")) { |
|
|
495 |
|
|
|
496 |
if (command.equals("set")) { |
|
|
497 |
|
|
|
498 |
long s_id = ((Integer)args[1]).longValue(); |
|
|
499 |
String msg = args[2].toString(); |
|
|
500 |
|
|
|
501 |
if (stringList.get(s_id) == null) { |
|
|
502 |
|
|
|
503 |
TuioString addString = new TuioString(s_id, -1, msg); |
|
|
504 |
frameStrings.addElement(addString); |
|
|
505 |
|
|
|
506 |
} else { |
|
|
507 |
|
|
|
508 |
TuioString tstr = stringList.get(s_id); |
|
|
509 |
if (tstr==null) return; |
|
|
510 |
if (!tstr.getMessage().equals(msg)) { |
|
|
511 |
|
|
|
512 |
TuioString updateString = new TuioString(s_id,tstr.getStringID(),msg); |
|
|
513 |
updateString.update(msg); |
|
|
514 |
frameStrings.addElement(updateString); |
|
|
515 |
} |
|
|
516 |
} |
|
|
517 |
|
|
|
518 |
//System.out.println("set cur " + s_id+" "+xpos+" "+ypos+" "+xspeed+" "+yspeed+" "+maccel); |
|
|
519 |
|
|
|
520 |
} else if (command.equals("alive")) { |
|
|
521 |
|
|
|
522 |
newStringList.clear(); |
|
|
523 |
for (int i=1;i<args.length;i++) { |
|
|
524 |
// get the message content |
|
|
525 |
long s_id = ((Integer)args[i]).longValue(); |
|
|
526 |
newStringList.addElement(s_id); |
|
|
527 |
// reduce the cursor list to the lost cursors |
|
|
528 |
if (aliveStringList.contains(s_id)) |
|
|
529 |
aliveStringList.removeElement(s_id); |
|
|
530 |
} |
|
|
531 |
|
|
|
532 |
// remove the remaining cursors |
|
|
533 |
for (int i=0;i<aliveStringList.size();i++) { |
|
|
534 |
TuioString removeString = stringList.get(aliveStringList.elementAt(i)); |
|
|
535 |
if (removeString==null) continue; |
|
|
536 |
removeString.remove(currentTime); |
|
|
537 |
frameStrings.addElement(removeString); |
|
|
538 |
} |
|
|
539 |
|
|
|
540 |
} else if (command.equals("fseq")) { |
|
|
541 |
long fseq = ((Integer)args[1]).longValue(); |
|
|
542 |
boolean lateFrame = false; |
|
|
543 |
|
|
|
544 |
if (fseq>0) { |
|
|
545 |
if (fseq>currentFrame) currentTime = TuioTime.getSessionTime(); |
|
|
546 |
if ((fseq>=currentFrame) || ((currentFrame-fseq)>100)) currentFrame = fseq; |
|
|
547 |
else lateFrame = true; |
|
|
548 |
} else if (TuioTime.getSessionTime().subtract(currentTime).getTotalMilliseconds()>100) { |
|
|
549 |
currentTime = TuioTime.getSessionTime(); |
|
|
550 |
} |
|
|
551 |
if (!lateFrame) { |
|
|
552 |
|
|
|
553 |
Enumeration<TuioString> frameEnum = frameStrings.elements(); |
|
|
554 |
while(frameEnum.hasMoreElements()) { |
|
|
555 |
TuioString tstr = frameEnum.nextElement(); |
|
|
556 |
|
|
|
557 |
switch (tstr.getTuioState()) { |
|
|
558 |
case TuioString.TUIO_REMOVED: |
|
|
559 |
|
|
|
560 |
TuioString removeString = tstr; |
|
|
561 |
removeString.remove(currentTime); |
|
|
562 |
|
|
|
563 |
for (int i=0;i<listenerList.size();i++) { |
|
|
564 |
TuioListener listener = (TuioListener)listenerList.elementAt(i); |
|
|
565 |
if (listener!=null) listener.removeTuioString(removeString); |
|
|
566 |
} |
|
|
567 |
|
|
|
568 |
stringList.remove(removeString.getSessionID()); |
|
|
569 |
|
|
|
570 |
if (removeString.getStringID()==maxStringID) { |
|
|
571 |
maxStringID = -1; |
|
|
572 |
if (stringList.size()>0) { |
|
|
573 |
Enumeration<TuioString> slist = stringList.elements(); |
|
|
574 |
while (slist.hasMoreElements()) { |
|
|
575 |
int sl_id = slist.nextElement().getStringID(); |
|
|
576 |
if (sl_id>maxStringID) maxStringID=sl_id; |
|
|
577 |
} |
|
|
578 |
|
|
|
579 |
Enumeration<TuioString> flist = freeStringList.elements(); |
|
|
580 |
while (flist.hasMoreElements()) { |
|
|
581 |
int sl_id = flist.nextElement().getStringID(); |
|
|
582 |
if (sl_id>=maxStringID) freeStringList.removeElement(sl_id); |
|
|
583 |
} |
|
|
584 |
} else freeStringList.clear(); |
|
|
585 |
} else if (removeString.getStringID()<maxStringID) { |
|
|
586 |
freeStringList.addElement(removeString); |
|
|
587 |
} |
|
|
588 |
|
|
|
589 |
break; |
|
|
590 |
|
|
|
591 |
case TuioString.TUIO_ADDED: |
|
|
592 |
|
|
|
593 |
int sl_id = stringList.size(); |
|
|
594 |
if ((stringList.size()<=maxStringID) && (freeStringList.size()>0)) { |
|
|
595 |
TuioString closestString = freeStringList.firstElement(); |
|
|
596 |
Enumeration<TuioString> testList = freeStringList.elements(); |
|
|
597 |
while (testList.hasMoreElements()) { |
|
|
598 |
TuioString testString = testList.nextElement(); |
|
|
599 |
//if (testString.getDistance(tstr)<closestString.getDistance(tstr)) |
|
|
600 |
closestString = testString; |
|
|
601 |
} |
|
|
602 |
sl_id = closestString.getStringID(); |
|
|
603 |
freeStringList.removeElement(closestString); |
|
|
604 |
} else maxStringID = sl_id; |
|
|
605 |
|
|
|
606 |
TuioString addString = new TuioString(currentTime,tstr.getSessionID(),sl_id,tstr.getMessage()); |
|
|
607 |
stringList.put(addString.getSessionID(),addString); |
|
|
608 |
|
|
|
609 |
for (int i=0;i<listenerList.size();i++) { |
|
|
610 |
TuioListener listener = (TuioListener)listenerList.elementAt(i); |
|
|
611 |
if (listener!=null) listener.addTuioString(addString); |
|
|
612 |
} |
|
|
613 |
break; |
|
|
614 |
|
|
|
615 |
default: |
|
|
616 |
|
|
|
617 |
TuioString updateString = stringList.get(tstr.getSessionID()); |
|
|
618 |
if ( tstr.getMessage()!=updateString.getMessage() ) |
|
|
619 |
updateString.update(currentTime,tstr.getMessage()); |
|
|
620 |
else |
|
|
621 |
updateString.update(currentTime,tstr.getMessage()); |
|
|
622 |
|
|
|
623 |
for (int i=0;i<listenerList.size();i++) { |
|
|
624 |
TuioListener listener = (TuioListener)listenerList.elementAt(i); |
|
|
625 |
if (listener!=null) listener.updateTuioString(updateString); |
|
|
626 |
} |
|
|
627 |
} |
|
|
628 |
} |
|
|
629 |
|
|
|
630 |
for (int i=0;i<listenerList.size();i++) { |
|
|
631 |
TuioListener listener = (TuioListener)listenerList.elementAt(i); |
|
|
632 |
if (listener!=null) listener.refresh(new TuioTime(currentTime)); |
|
|
633 |
} |
|
|
634 |
|
|
|
635 |
Vector<Long> buffer = aliveStringList; |
|
|
636 |
aliveStringList = newStringList; |
|
|
637 |
// recycling the vector |
|
|
638 |
newStringList = buffer; |
|
|
639 |
} |
|
|
640 |
|
|
|
641 |
frameStrings.clear(); |
|
|
642 |
} |
|
|
643 |
} |
|
|
644 |
|
|
|
645 |
|
|
|
646 |
} |
|
|
647 |
} |