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