|
1 /* |
|
2 TUIO processing library - 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 |
|
22 package TUIO; |
|
23 |
|
24 import java.awt.event.*; |
|
25 import java.lang.reflect.*; |
|
26 import processing.core.*; |
|
27 import java.util.*; |
|
28 |
|
29 public class TuioProcessing implements TuioListener { |
|
30 |
|
31 private PApplet parent; |
|
32 private Method addTuioObject, removeTuioObject, updateTuioObject, addTuioCursor, removeTuioCursor, updateTuioCursor, addTuioString, removeTuioString, updateTuioString, refresh; |
|
33 private TuioClient client; |
|
34 |
|
35 public TuioProcessing(PApplet parent) { |
|
36 this(parent,3333); |
|
37 } |
|
38 |
|
39 public TuioProcessing(PApplet parent, int port) { |
|
40 this.parent = parent; |
|
41 parent.registerDispose(this); |
|
42 |
|
43 try { refresh = parent.getClass().getMethod("refresh",new Class[] { TuioTime.class } ); } |
|
44 catch (Exception e) { |
|
45 System.out.println("TUIO: missing or wrong 'refresh(TuioTime bundleTime)' method implementation"); |
|
46 refresh = null; |
|
47 } |
|
48 |
|
49 try { addTuioObject = parent.getClass().getMethod("addTuioObject", new Class[] { TuioObject.class }); } |
|
50 catch (Exception e) { |
|
51 System.out.println("TUIO: missing or wrong 'addTuioObject(TuioObject tobj)' method implementation"); |
|
52 addTuioObject = null; |
|
53 } |
|
54 |
|
55 try { removeTuioObject = parent.getClass().getMethod("removeTuioObject", new Class[] { TuioObject.class }); } |
|
56 catch (Exception e) { |
|
57 System.out.println("TUIO: missing or wrong 'removeTuioObject(TuioObject tobj)' method implementation"); |
|
58 removeTuioObject = null; |
|
59 } |
|
60 |
|
61 try { updateTuioObject = parent.getClass().getMethod("updateTuioObject", new Class[] { TuioObject.class }); } |
|
62 catch (Exception e) { |
|
63 System.out.println("TUIO: missing or wrong 'updateTuioObject(TuioObject tobj)' method implementation"); |
|
64 updateTuioObject = null; |
|
65 } |
|
66 |
|
67 try { addTuioCursor = parent.getClass().getMethod("addTuioCursor", new Class[] { TuioCursor.class }); } |
|
68 catch (Exception e) { |
|
69 System.out.println("TUIO: missing or wrong 'addTuioCursor(TuioCursor tcur)' method implementation"); |
|
70 addTuioCursor = null; |
|
71 } |
|
72 |
|
73 try { removeTuioCursor = parent.getClass().getMethod("removeTuioCursor", new Class[] { TuioCursor.class }); } |
|
74 catch (Exception e) { |
|
75 System.out.println("TUIO:missing or wrong 'removeTuioCursor(TuioCursor tcur)' method implementation"); |
|
76 removeTuioCursor = null; |
|
77 } |
|
78 |
|
79 try { updateTuioCursor = parent.getClass().getMethod("updateTuioCursor", new Class[] { TuioCursor.class }); } |
|
80 catch (Exception e) { |
|
81 System.out.println("TUIO: missing or wrong 'updateTuioCursor(TuioCursor tcur)' method implementation"); |
|
82 updateTuioCursor = null; |
|
83 } |
|
84 |
|
85 try { addTuioString = parent.getClass().getMethod("addTuioString", new Class[] { TuioString.class }); } |
|
86 catch (Exception e) { |
|
87 System.out.println("TUIO: missing or wrong 'addTuioString(TuioString tstr)' method implementation"); |
|
88 addTuioString = null; |
|
89 } |
|
90 |
|
91 try { removeTuioString = parent.getClass().getMethod("removeTuioString", new Class[] { TuioString.class }); } |
|
92 catch (Exception e) { |
|
93 System.out.println("TUIO:missing or wrong 'removeTuioString(TuioString tstr)' method implementation"); |
|
94 removeTuioString = null; |
|
95 } |
|
96 |
|
97 try { updateTuioString = parent.getClass().getMethod("updateTuioString", new Class[] { TuioString.class }); } |
|
98 catch (Exception e) { |
|
99 System.out.println("TUIO: missing or wrong 'updateTuioString(TuioString tstr)' method implementation"); |
|
100 updateTuioString = null; |
|
101 } |
|
102 |
|
103 client = new TuioClient(port); |
|
104 client.addTuioListener(this); |
|
105 client.connect(); |
|
106 } |
|
107 |
|
108 public void addTuioObject(TuioObject tobj) { |
|
109 if (addTuioObject!=null) { |
|
110 try { |
|
111 addTuioObject.invoke(parent, new Object[] { tobj }); |
|
112 } |
|
113 catch (IllegalAccessException e) {} |
|
114 catch (IllegalArgumentException e) {} |
|
115 catch (InvocationTargetException e) {} |
|
116 } |
|
117 } |
|
118 |
|
119 public void updateTuioObject(TuioObject tobj) { |
|
120 |
|
121 if (updateTuioObject!=null) { |
|
122 try { |
|
123 updateTuioObject.invoke(parent, new Object[] { tobj }); |
|
124 } |
|
125 catch (IllegalAccessException e) {} |
|
126 catch (IllegalArgumentException e) {} |
|
127 catch (InvocationTargetException e) {} |
|
128 } |
|
129 } |
|
130 |
|
131 public void removeTuioObject(TuioObject tobj) { |
|
132 if (removeTuioObject!=null) { |
|
133 try { |
|
134 removeTuioObject.invoke(parent, new Object[] { tobj }); |
|
135 } |
|
136 catch (IllegalAccessException e) {} |
|
137 catch (IllegalArgumentException e) {} |
|
138 catch (InvocationTargetException e) {} |
|
139 } |
|
140 } |
|
141 |
|
142 public void addTuioCursor(TuioCursor tcur) { |
|
143 if (addTuioCursor!=null) { |
|
144 try { |
|
145 addTuioCursor.invoke(parent, new Object[] { tcur }); |
|
146 } |
|
147 catch (IllegalAccessException e) {} |
|
148 catch (IllegalArgumentException e) {} |
|
149 catch (InvocationTargetException e) {} |
|
150 } |
|
151 } |
|
152 |
|
153 public void updateTuioCursor(TuioCursor tcur) { |
|
154 if (updateTuioCursor!=null) { |
|
155 try { |
|
156 updateTuioCursor.invoke(parent, new Object[] { tcur }); |
|
157 } |
|
158 catch (IllegalAccessException e) {} |
|
159 catch (IllegalArgumentException e) {} |
|
160 catch (InvocationTargetException e) {} |
|
161 } |
|
162 } |
|
163 |
|
164 public void removeTuioCursor(TuioCursor tcur) { |
|
165 if (removeTuioCursor!=null) { |
|
166 try { |
|
167 removeTuioCursor.invoke(parent, new Object[] { tcur }); |
|
168 } |
|
169 catch (IllegalAccessException e) {} |
|
170 catch (IllegalArgumentException e) {} |
|
171 catch (InvocationTargetException e) {} |
|
172 } |
|
173 } |
|
174 |
|
175 public void addTuioString(TuioString tstr) { |
|
176 if (addTuioString!=null) { |
|
177 try { |
|
178 addTuioString.invoke(parent, new Object[] { tstr }); |
|
179 } |
|
180 catch (IllegalAccessException e) {} |
|
181 catch (IllegalArgumentException e) {} |
|
182 catch (InvocationTargetException e) {} |
|
183 } |
|
184 } |
|
185 |
|
186 public void updateTuioString(TuioString tstr) { |
|
187 if (updateTuioString!=null) { |
|
188 try { |
|
189 updateTuioString.invoke(parent, new Object[] { tstr }); |
|
190 } |
|
191 catch (IllegalAccessException e) {} |
|
192 catch (IllegalArgumentException e) {} |
|
193 catch (InvocationTargetException e) {} |
|
194 } |
|
195 } |
|
196 |
|
197 public void removeTuioString(TuioString tstr) { |
|
198 if (removeTuioString!=null) { |
|
199 try { |
|
200 removeTuioString.invoke(parent, new Object[] { tstr }); |
|
201 } |
|
202 catch (IllegalAccessException e) {} |
|
203 catch (IllegalArgumentException e) {} |
|
204 catch (InvocationTargetException e) {} |
|
205 } |
|
206 } |
|
207 |
|
208 public void refresh(TuioTime bundleTime) { |
|
209 if (refresh!=null) { |
|
210 try { |
|
211 refresh.invoke(parent,new Object[] { bundleTime }); |
|
212 } |
|
213 catch (IllegalAccessException e) {} |
|
214 catch (IllegalArgumentException e) {} |
|
215 catch (InvocationTargetException e) {} |
|
216 } |
|
217 } |
|
218 |
|
219 public Vector getTuioObjects() { |
|
220 return client.getTuioObjects(); |
|
221 } |
|
222 |
|
223 public Vector getTuioCursors() { |
|
224 return client.getTuioCursors(); |
|
225 } |
|
226 |
|
227 public Vector getTuioStrings() { |
|
228 return client.getTuioStrings(); |
|
229 } |
|
230 |
|
231 public TuioObject getTuioObject(long s_id) { |
|
232 return client.getTuioObject(s_id); |
|
233 } |
|
234 |
|
235 public TuioCursor getTuioCursor(long s_id) { |
|
236 return client.getTuioCursor(s_id); |
|
237 } |
|
238 |
|
239 public TuioString getTuioString(long s_id) { |
|
240 return client.getTuioString(s_id); |
|
241 } |
|
242 |
|
243 public void pre() { |
|
244 //method that's called just after beginFrame(), meaning that it |
|
245 //can affect drawing. |
|
246 } |
|
247 |
|
248 public void draw() { |
|
249 //method that's called at the end of draw(), but before endFrame(). |
|
250 } |
|
251 |
|
252 public void mouseEvent(MouseEvent e) { |
|
253 //called when a mouse event occurs in the parent applet |
|
254 } |
|
255 |
|
256 public void keyEvent(KeyEvent e) { |
|
257 //called when a key event occurs in the parent applet |
|
258 } |
|
259 |
|
260 public void post() { |
|
261 //method called after draw has completed and the frame is done. |
|
262 //no drawing allowed. |
|
263 } |
|
264 |
|
265 public void size(int width, int height) { |
|
266 //this will be called the first time an applet sets its size, but |
|
267 //also any time that it's called while the PApplet is running. |
|
268 } |
|
269 |
|
270 public void stop() { |
|
271 //can be called by users, for instance movie.stop() will shut down |
|
272 //a movie that's being played, or camera.stop() stops capturing |
|
273 //video. server.stop() will shut down the server and shut it down |
|
274 //completely, which is identical to its "dispose" function. |
|
275 } |
|
276 |
|
277 public void dispose() { |
|
278 |
|
279 if (client.isConnected()) client.disconnect(); |
|
280 |
|
281 //this should only be called by PApplet. dispose() is what gets |
|
282 //called when the host applet is stopped, so this should shut down |
|
283 //any threads, disconnect from the net, unload memory, etc. |
|
284 } |
|
285 } |