So I am hoping to use a multi-touch table I built with a classmate in a show on Wednesday. I have the great examples from picturetunes as well as the Demo from the TUIO website. They both run perfectly. My question is: How can I access the array of x and y positions of all the cursors from the main draw() function? If i have to create a whole program inside of the draw function of the TuioCursor class I guess I will...but Id rather not =P. Thanks a lot! Any suggestions would be helpful as I am a beginner at dealing with reading TUIO in Processing.
import processing.opengl.*;
import tuio.*;
TuioClient client;
TuiCursorList cursorList;
void setup()
{
size(1024,768, OPENGL);
client = new TuioClient(this); // the TuioClient receives the information from Touchlib
cursorList = new TuiCursorList(); //this is your little helper who handles all the cursors for you
frameRate(30);
}
void draw()
{
cursorList.draw(); //draw the cursors
smooth();
noStroke();
}
/*
based on:::::::::::::::::::::::::::::::::::::::::::::::
TUIO processing demo - part of the reacTIVision project
http://mtg.upf.es/reactable
by Martin Kaltenbrunner
(see bottom of page for details)
modified by::::::::::::::::::::::::::::::::::::::::::::
picturetunes (http://christoph.picturetunes.at)
*/
//***************************//
// PREDEFINED TUIO FUNCTIONS //
//***************************//
////the redraw functions are no longer needed because we use a constant framerate
// called after each message bundle
void refresh() {
//redraw();
}
// called when a cursor appears the scene
void addTuiCursor(Integer s_id) {
System.out.println("add cursor “+s_id);
cursorList.add(s_id);
//redraw();
}
// called when a cursor is removed from the scene
void removeTuiCursor(Integer s_id) {
System.out.println("remove cursor “+s_id);
cursorList.remove(s_id);
//redraw();
}
// called when a cursor is moved
void updateTuiCursor (Integer s_id, Float xpos, Float ypos) {
//System.out.println("update cursor “+s_id+” “+xpos+” “+ypos);
float xsize =width;
float ysize =height;
//the following lines of code could be probably improved
// the position values are read and casted to float
// the values should (but strangely do not precicesly) start at 0 and end at 1 so you have to multiply them by the screensize to map them to the screen
// the calculated values are subtracted from the screensize to mirror the coordinates
cursorList.update(s_id,(float)xsize-((xsize*xpos.floatValue())),ysize-((float)(ysize*ypos.floatValue())));
//redraw();
}
//****************//
// LITTLE HELPERS //
//****************//
//this class stores all the cursors…
class TuiCursorList {
java.util.Hashtable cursorList;
TuiCursorList() {
//...in this java hastable
cursorList = new Hashtable();
}
//call the draw method of the cursors (just for testing to see what is happening)
void draw() {
Enumeration e = cursorList.elements();
while (e.hasMoreElements()) {
TuioCursor cursorPoint = (TuioCursor)e.nextElement();
cursorPoint.draw();
}
}
//create new cursor instance
void add(Integer s_id) {
//Vector pointList = new Vector();
TuioCursor cursorPoint = new TuioCursor();
cursorList.put(s_id,cursorPoint);
}
//remove cursor
void remove(Integer s_id) {
cursorList.remove(s_id);
}
//update cursor position
void update(Integer s_id, float xpos, float ypos) {
// println("number of cursors in cursor list:"+cursorList.size());
// that is a nasty little detail: for some reason updateTuiCursor() is called before add addTuiCursor()
// which causes the programm to behave strangly. to avoid that you want to check if the object you want ot update is already there
if(cursorList.containsKey(s_id)){
TuioCursor cursorPoint = (TuioCursor)cursorList.get(s_id);
cursorPoint.update(xpos,ypos);
}
}
}
// this is the class for the cursors. it doesn`t do pretty much yet but of course you can extend it!
class TuioCursor{
public float x,y;
TuioCursor(){
//println("hello i am a tuio cursor");
}
void update(float px, float py){
// println("hurray somebody updates my position x:"+px+" y"+y);
this.x=px;
this.y=py;
}
void draw(){
//this comes handy for testing - you get visual response from your cursors
//println("hurray somebody draws me");
fill(0,50);
ellipse(x, y, 16, 16);
fill(250,50);
rect(0,0,width, height);
}
}
